Initial commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* This file is for tutorial purposes made by ImproperIssues. Distribute if you want :)
|
||||
*/
|
||||
|
||||
package io.github.itzispyder.exampleplugin;
|
||||
|
||||
import io.github.itzispyder.exampleplugin.commands.CommandExample;
|
||||
import io.github.itzispyder.exampleplugin.data.Config;
|
||||
import io.github.itzispyder.exampleplugin.events.ExampleEvent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This is your main class, you register everything important here.
|
||||
*
|
||||
* To build the jar, go to terminal and run "./gradlew build"
|
||||
*/
|
||||
public final class ExamplePlugin extends JavaPlugin {
|
||||
|
||||
public static final PluginManager manager = Bukkit.getPluginManager();
|
||||
public static String starter = "";
|
||||
public static final Logger log = Bukkit.getLogger();
|
||||
|
||||
/**
|
||||
* Plugin startup logic
|
||||
*/
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
// Files
|
||||
getConfig().options().copyDefaults();
|
||||
saveDefaultConfig();
|
||||
|
||||
// Plugin startup logic
|
||||
log.info("Example plugin has loaded! (" + getDescription().getVersion() + ")");
|
||||
starter = Config.Plugin.getPrefix() + " ";
|
||||
|
||||
// Commands -> BE SURE TO REGISTER ANY NEW COMMANDS IN PLUGIN.YML (src/main/java/resources/plugin.yml)!
|
||||
getCommand("example").setExecutor(new CommandExample());
|
||||
getCommand("example").setTabCompleter(new CommandExample.Tabs());
|
||||
|
||||
// Events
|
||||
manager.registerEvents(new ExampleEvent(),this);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin shutdown logic
|
||||
*/
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
log.info("Example plugin has disabled! (" + getDescription().getVersion() + ")");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of this plugin
|
||||
* @return an instance of this plugin
|
||||
*/
|
||||
public static Plugin getInstance() {
|
||||
return manager.getPlugin("ExamplePlugin");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* This file is for tutorial purposes made by ImproperIssues. Distribute if you want :)
|
||||
*/
|
||||
|
||||
package io.github.itzispyder.exampleplugin.commands;
|
||||
|
||||
import io.github.itzispyder.exampleplugin.exceptions.CmdExHandler;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Example command
|
||||
*/
|
||||
public class CommandExample implements CommandExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
try {
|
||||
sender.sendMessage("You've executed an example command!");
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
CmdExHandler handler = new CmdExHandler(ex,command);
|
||||
sender.sendMessage(handler.getErrorMessage());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example command's tab completer
|
||||
*/
|
||||
public static class Tabs implements TabCompleter {
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
||||
List<String> list = new ArrayList<>();
|
||||
switch (args.length) {
|
||||
case 1 -> list.add("test");
|
||||
}
|
||||
list.removeIf(s -> !s.toLowerCase().contains(args[args.length - 1].toLowerCase()));
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* This file is for tutorial purposes made by ImproperIssues. Distribute if you want :)
|
||||
*/
|
||||
|
||||
package io.github.itzispyder.exampleplugin.data;
|
||||
|
||||
import io.github.itzispyder.exampleplugin.ExamplePlugin;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
/**
|
||||
* Config loader
|
||||
*/
|
||||
public abstract class Config {
|
||||
|
||||
private static final FileConfiguration config = ExamplePlugin.getInstance().getConfig();
|
||||
|
||||
/**
|
||||
* Config plugin section
|
||||
*/
|
||||
public class Plugin {
|
||||
public static String getPrefix() {
|
||||
return config.getString("config.plugin.prefix");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* This file is for tutorial purposes made by ImproperIssues. Distribute if you want :)
|
||||
*/
|
||||
|
||||
package io.github.itzispyder.exampleplugin.events;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
/**
|
||||
* An example event listener
|
||||
*/
|
||||
public class ExampleEvent implements Listener {
|
||||
|
||||
/**
|
||||
* Listens for join event, set any event you want in the parameter type!
|
||||
* Provided by Bukkit API
|
||||
* @param e the event to listen for
|
||||
*/
|
||||
@EventHandler
|
||||
public static void onJoin(PlayerJoinEvent e) {
|
||||
Player p = e.getPlayer();
|
||||
p.sendMessage("Hello, welcome!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* This file is for tutorial purposes made by ImproperIssues. Distribute if you want :)
|
||||
*/
|
||||
|
||||
package io.github.itzispyder.exampleplugin.exceptions;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
|
||||
/**
|
||||
* Handles a command exception
|
||||
*/
|
||||
public class CmdExHandler {
|
||||
|
||||
private Exception exception;
|
||||
private Command command;
|
||||
|
||||
/**
|
||||
* Constructs the command exception
|
||||
* @param exception the exception caught
|
||||
* @param command the command run
|
||||
*/
|
||||
public CmdExHandler(Exception exception, Command command) {
|
||||
this.exception = exception;
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error message
|
||||
* @return the error message
|
||||
*/
|
||||
public String getErrorMessage() {
|
||||
String msg = "§cCommand Error: ";
|
||||
if (exception instanceof NullPointerException) msg += "Command contains a null value!";
|
||||
else if (exception instanceof IndexOutOfBoundsException) msg += "Unknown or incomplete command!";
|
||||
else msg += exception.getMessage();
|
||||
return msg + "\n§cCorrect usage: §7" + command.getUsage();
|
||||
}
|
||||
|
||||
public Exception getException() {
|
||||
return exception;
|
||||
}
|
||||
|
||||
public Command getCommand() {
|
||||
return command;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
/**
|
||||
* This file is for tutorial purposes made by ImproperIssues. Distribute if you want :)
|
||||
*
|
||||
* I made this cuz Bukkit API sounds management is trash.
|
||||
* by ImproperIssues
|
||||
*/
|
||||
|
||||
|
||||
package io.github.itzispyder.exampleplugin.server.sound;
|
||||
|
||||
import io.github.itzispyder.exampleplugin.ExamplePlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class SoundPlayer {
|
||||
|
||||
private Location location;
|
||||
private Sound sound;
|
||||
private float volume;
|
||||
private float pitch;
|
||||
|
||||
/**
|
||||
* Constructs a new sound, this aims to add more methods to
|
||||
* the Bukkit APIs Sound class, as they don't have many
|
||||
* methods to use.
|
||||
*
|
||||
* @param location Location
|
||||
* @param sound Sound
|
||||
* @param volume float
|
||||
* @param pitch float
|
||||
*/
|
||||
public SoundPlayer(Location location, Sound sound, float volume, float pitch) {
|
||||
this.location = location;
|
||||
this.sound = sound;
|
||||
this.pitch = pitch;
|
||||
this.volume = volume;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Plays a sound to a player but at the store location
|
||||
*
|
||||
* @param player Player
|
||||
*/
|
||||
public void play(Player player) {
|
||||
player.playSound(this.location,this.sound,this.volume,this.pitch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a sound to a player but at the player's location
|
||||
*
|
||||
* @param player Player
|
||||
*/
|
||||
public void playAt(Player player) {
|
||||
player.playSound(player.getLocation(),this.sound,this.volume,this.pitch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays the sound to all players within a distance, but at the stored location.
|
||||
*
|
||||
* @param distance double
|
||||
*/
|
||||
public void playWithin(double distance) {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distanceSquared(this.location) < distance) {
|
||||
p.playSound(this.location,this.sound,this.volume,this.pitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays the sound to all players within a distance, but at the players' location.
|
||||
*
|
||||
* @param distance double
|
||||
*/
|
||||
public void playWithinAt(double distance) {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distanceSquared(this.location) < distance) {
|
||||
p.playSound(p.getLocation(),this.sound,this.volume,this.pitch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Plays the sound to all players on the server, but at the stored location.
|
||||
*/
|
||||
public void playAll() {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) p.playSound(this.location,this.sound,this.volume,this.pitch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays the sound to all players on the server, but at the players' location.
|
||||
*/
|
||||
public void playAllAt() {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) p.playSound(p.getLocation(),this.sound,this.volume,this.pitch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeats a sound to a player, but at the stored location.
|
||||
*
|
||||
* @param player Player
|
||||
* @param times int
|
||||
* @param tickDelay int
|
||||
*/
|
||||
public void repeat(Player player, int times, int tickDelay) {
|
||||
new BukkitRunnable() {
|
||||
int i = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
if (i < times) {
|
||||
play(player);
|
||||
i ++;
|
||||
} else {
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimer(ExamplePlugin.getInstance(),0,tickDelay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeats a sound to a player, but at the player's location.
|
||||
*
|
||||
* @param player Player
|
||||
* @param times int
|
||||
* @param tickDelay int
|
||||
*/
|
||||
public void repeatAt(Player player, int times, int tickDelay) {
|
||||
new BukkitRunnable() {
|
||||
int i = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
if (i < times) {
|
||||
playAt(player);
|
||||
i ++;
|
||||
} else {
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimer(ExamplePlugin.getInstance(),0,tickDelay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeats a sound to all players on the server, but at the stored location.
|
||||
*
|
||||
* @param times int
|
||||
* @param tickDelay int
|
||||
*/
|
||||
public void repeatAll(int times, int tickDelay) {
|
||||
new BukkitRunnable() {
|
||||
int i = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
if (i < times) {
|
||||
playAll();
|
||||
i ++;
|
||||
} else {
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimer(ExamplePlugin.getInstance(),0,tickDelay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeats a sound to all players on the server, but at the players' location.
|
||||
*
|
||||
* @param times int
|
||||
* @param tickDelay int
|
||||
*/
|
||||
public void repeatAllAt(int times, int tickDelay) {
|
||||
new BukkitRunnable() {
|
||||
int i = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
if (i < times) {
|
||||
playAllAt();
|
||||
i ++;
|
||||
} else {
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimer(ExamplePlugin.getInstance(),0,tickDelay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeats a sound to all players within a radius, but at the stored location.
|
||||
*
|
||||
* @param radius double
|
||||
* @param times int
|
||||
* @param tickDelay int
|
||||
*/
|
||||
public void repeatAll(double radius,int times, int tickDelay) {
|
||||
new BukkitRunnable() {
|
||||
int i = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
if (i < times) {
|
||||
playWithin(radius);
|
||||
i ++;
|
||||
} else {
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimer(ExamplePlugin.getInstance(),0,tickDelay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repeats a sound to all players within a radius, but at the players' location.
|
||||
*
|
||||
* @param distance double
|
||||
* @param times int
|
||||
* @param tickDelay int
|
||||
*/
|
||||
public void repeatAllAt(double distance, int times, int tickDelay) {
|
||||
new BukkitRunnable() {
|
||||
int i = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
if (i < times) {
|
||||
playWithinAt(distance);
|
||||
i ++;
|
||||
} else {
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimer(ExamplePlugin.getInstance(),0,tickDelay);
|
||||
}
|
||||
|
||||
public Sound getSound() {
|
||||
return sound;
|
||||
}
|
||||
|
||||
public float getPitch() {
|
||||
return pitch;
|
||||
}
|
||||
|
||||
public float getVolume() {
|
||||
return volume;
|
||||
}
|
||||
|
||||
public Location getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setPitch(float pitch) {
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public void setVolume(float volume) {
|
||||
this.volume = volume;
|
||||
}
|
||||
|
||||
public void setSound(Sound sound) {
|
||||
this.sound = sound;
|
||||
}
|
||||
|
||||
public void setLocation(Location location) {
|
||||
this.location = location;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* This file is for tutorial purposes made by ImproperIssues. Distribute if you want :)
|
||||
*/
|
||||
|
||||
package io.github.itzispyder.exampleplugin.server.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Represents Argument builder
|
||||
*/
|
||||
public class ArgBuilder {
|
||||
|
||||
private String result;
|
||||
|
||||
/**
|
||||
* Constructs an argument builder.
|
||||
*/
|
||||
public ArgBuilder() {
|
||||
this.result = " ";
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an argument builder with a string.
|
||||
* @param begin the beginner string
|
||||
*/
|
||||
public ArgBuilder(String begin) {
|
||||
this.result = begin + " ";
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string
|
||||
* @param string string
|
||||
* @return this class
|
||||
*/
|
||||
public ArgBuilder append(String string) {
|
||||
this.result += string + " ";
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string array
|
||||
* @param args string array
|
||||
* @return this class
|
||||
*/
|
||||
public ArgBuilder append(String[] args) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (String arg : args) builder.append(arg).append(" ");
|
||||
this.result += builder.toString();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string list
|
||||
* @param args string list
|
||||
* @return this class
|
||||
*/
|
||||
public ArgBuilder append(List<String> args) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (String arg : args) builder.append(arg).append(" ");
|
||||
this.result += builder.toString();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a string set
|
||||
* @param args string set
|
||||
* @return this class
|
||||
*/
|
||||
public ArgBuilder append(Set<String> args) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (String arg : args) builder.append(arg).append(" ");
|
||||
this.result += builder.toString();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this class as a string
|
||||
* @return this class as a string
|
||||
*/
|
||||
public String build() {
|
||||
return this.toString().trim();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.github.itzispyder.exampleplugin.server.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Randomize items from a list
|
||||
* @param <T> list of?
|
||||
*/
|
||||
public class Randomizer<T> {
|
||||
|
||||
private final List<T> array;
|
||||
|
||||
/**
|
||||
* From array list
|
||||
* @param array list
|
||||
*/
|
||||
public Randomizer(List<T> array) {
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
/**
|
||||
* From set
|
||||
* @param array set
|
||||
*/
|
||||
public Randomizer(Set<T> array) {
|
||||
this.array = new ArrayList<>(array);
|
||||
}
|
||||
|
||||
/**
|
||||
* From array
|
||||
* @param array array
|
||||
*/
|
||||
public Randomizer(T[] array) {
|
||||
this.array = List.of(array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick random from the array
|
||||
* @return random of list of?
|
||||
*/
|
||||
public T pickRand() {
|
||||
return array.get(rand(array.size() - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random integer from 1 to (max)
|
||||
* @param max max value
|
||||
* @return random
|
||||
*/
|
||||
public static int rand(int max) {
|
||||
if (max <= 0) throw new IllegalArgumentException("max cannot be less than 1!");
|
||||
return (int) Math.ceil(Math.random() * max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random integer from (min) to (max)
|
||||
* @param min min value
|
||||
* @param max max value
|
||||
* @return random
|
||||
*/
|
||||
public static int rand(int min, int max) {
|
||||
if (max <= 0 || min <= 0) throw new IllegalArgumentException("max or min cannot be less than 1!");
|
||||
if (max <= min) throw new IllegalArgumentException("max cannot be less than or equal to min!");
|
||||
return min + (int) Math.floor(Math.random() * (max - min + 1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* This file is for tutorial purposes made by ImproperIssues. Distribute if you want :)
|
||||
*/
|
||||
|
||||
package io.github.itzispyder.exampleplugin.server.util;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Server utils
|
||||
*/
|
||||
public abstract class ServerUtils {
|
||||
|
||||
/**
|
||||
* List of names of online players
|
||||
* @return list of names
|
||||
*/
|
||||
public static List<String> listPlayers() {
|
||||
List<String> list =new ArrayList<>();
|
||||
Bukkit.getOnlinePlayers().forEach(p -> list.add(p.getName()));
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* List of names of online staff
|
||||
* @return list of names
|
||||
*/
|
||||
public static List<String> listStaff() {
|
||||
List<String> list =new ArrayList<>();
|
||||
Bukkit.getOnlinePlayers().forEach(p -> {
|
||||
if (p.isOp()) list.add(p.getName());
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* List of names of online staff
|
||||
* @return list of staff
|
||||
*/
|
||||
public static Set<Player> getStaff() {
|
||||
Set<Player> list = new HashSet<>();
|
||||
Bukkit.getOnlinePlayers().forEach(p -> {
|
||||
if (p.isOp()) list.add(p);
|
||||
});
|
||||
return list;
|
||||
}
|
||||
}
|
||||
8
src/main/resources/config.yml
Normal file
8
src/main/resources/config.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
#
|
||||
# This will be your plugin config!
|
||||
#
|
||||
#
|
||||
config:
|
||||
plugin:
|
||||
prefix: "§7[§aExamplePlugin§7]"
|
||||
17
src/main/resources/plugin.yml
Normal file
17
src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,17 @@
|
||||
name: ExamplePlugin
|
||||
version: '${version}'
|
||||
main: io.github.itzispyder.exampleplugin.ExamplePlugin
|
||||
api-version: 1.17
|
||||
authors: [ YourNameHere ]
|
||||
description: An example plugin template for Spigot/Paper.
|
||||
website: https://ItziSpyder.github.io/
|
||||
permissions:
|
||||
example.commands:
|
||||
description: An example permission
|
||||
default: op
|
||||
commands:
|
||||
example:
|
||||
description: An example command.
|
||||
usage: /example
|
||||
permission: example.commands
|
||||
permission-message: You do not have permission!
|
||||
Reference in New Issue
Block a user