commit 30c775d0e3ab2ecfe1e1e46f16605b856280341a Author: BuildTools Date: Thu Jan 21 22:08:36 2021 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..744289d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Project exclude paths +/target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f17e0a7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + + 1.8 + 1.8 + + + nl.iobyte + commandapi + 1.1 + + + + sk89q-repo + https://maven.enginehub.org/repo/ + + + + + + org.spigotmc + spigot-api + 1.12.2-R0.1-SNAPSHOT + provided + + + com.sk89q.worldguard + worldguard-legacy + 6.2 + provided + + + com.sk89q.worldedit + worldedit-bukkit + 6.1.5 + provided + + + com.sk89q.worldedit + worldedit-core + 6.1.4-SNAPSHOT + provided + + + \ No newline at end of file diff --git a/src/main/java/nl/iobyte/commandapi/CommandFactory.java b/src/main/java/nl/iobyte/commandapi/CommandFactory.java new file mode 100644 index 0000000..7fc6715 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/CommandFactory.java @@ -0,0 +1,178 @@ +package nl.iobyte.commandapi; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import nl.iobyte.commandapi.interfaces.ICommandMiddleware; +import nl.iobyte.commandapi.objects.CommandMap; +import nl.iobyte.commandapi.objects.CommandSyntax; +import nl.iobyte.commandapi.interfaces.SubCommand; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.CommandExecutor; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; +import java.util.*; + +public class CommandFactory implements CommandExecutor { + + private final String name; + private final List middlewares = new ArrayList<>(); + private final CommandMap commandMap = new CommandMap(); + private Plugin plugin; + + public CommandFactory(String name) { + this.name = name; + } + + /** + * Get name of command + * @return String + */ + public String getName() { + return name; + } + + public CommandFactory addMiddleware(ICommandMiddleware middleware) { + if(middleware != null) + middlewares.add(middleware); + + return this; + } + + /** + * Add sub-command to CommandFactory + * @param subCommand SubCommand + * @return CommandFactory + */ + public CommandFactory addSubCommand(SubCommand subCommand) { + if(subCommand == null) + return this; + + commandMap.addSubCommand(subCommand); + return this; + } + + /** + * Get collection of all sub-commands + * @return Collection + */ + public Collection getSubCommands() { + return commandMap.getSubCommands(); + } + + /** + * Get SubCommand's available to CommandSender + * @param sender CommandSender + * @return List + */ + public List getApplicableSubCommands(CommandSender sender) { + List list = new ArrayList<>(); + for(SubCommand subCommand : commandMap.getSubCommands()) { + if (subCommand.hasPermission() && !sender.hasPermission(subCommand.getPermission())) + continue; + + if(subCommand.getApplicableSyntaxList(sender).size() == 0) + continue; + + list.add(subCommand); + } + + return list; + } + + /** + * When a command gets fired + * @param sender CommandSender + * @param cmd Command + * @param s String + * @param args []String + * @return Boolean + */ + public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) { + if(args.length == 0) + args = new String[]{"help"}; + + //Get SubCommand if exists + SubCommand subCommand = commandMap.getSubCommand(args); + if(subCommand == null || subCommand.getSyntaxList().isEmpty()) + return false; + + //Remove sub-command name from arguments + args = Arrays.copyOfRange(args, subCommand.getName().length, args.length); + + //Handle Middleware + for(ICommandMiddleware middleware : middlewares) + if(!middleware.continueCommand(sender, subCommand)) + return true; + + //Pre assign variables + boolean b; + String str = null; + Pair pair; + List> arguments; + List parsedArguments; + List syntaxList = subCommand.getApplicableSyntaxList(sender); + + //Try possible syntax's + for(CommandSyntax syntax : syntaxList) { + arguments = syntax.getArguments(); + if(arguments.size() > args.length) + continue; + + b = true; + int j = 0; + parsedArguments = new ArrayList<>(); + for (ICommandArgument argument : arguments) { + String[] array = Arrays.copyOfRange(args, j, args.length); + pair = argument.checkArgument(sender, array, parsedArguments); + if (!pair.getKey()) { + str = argument.getMessage(array); + b = false; + break; + } + + parsedArguments.add(argument.getArgument(sender, Arrays.copyOfRange(array, 0, pair.getValue()), parsedArguments)); + j += pair.getValue(); + } + + if(j != args.length) + continue; + + if(!b) + continue; + + int i = subCommand.getSyntaxList().indexOf(syntax); + List finalParsedArguments = parsedArguments; + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + if (sender instanceof Player) { + subCommand.onPlayerCommand((Player) sender, finalParsedArguments, i); + } else { + subCommand.onConsoleCommand(sender, finalParsedArguments, i); + } + }); + + return true; + } + + if(str != null) { + sender.sendMessage(ChatColor.RED +str); + return true; + } + + sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.WHITE + syntaxList.get(0).getUsage()); + return true; + } + + /** + * Register command + * @param plugin JavaPlugin + */ + public void registerCommand(JavaPlugin plugin) { + this.plugin = plugin; + plugin.getCommand(name).setExecutor(this); + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/BooleanArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/BooleanArgument.java new file mode 100644 index 0000000..3dfad77 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/BooleanArgument.java @@ -0,0 +1,47 @@ +package nl.iobyte.commandapi.arguments; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.List; + +public class BooleanArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid boolean "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid Boolean + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + switch (args[0].toLowerCase()) { + case "true": + case "false": + return new Pair<>(true, 1); + default: + return new Pair<>(false, 0); + } + } + + /** + * Get Boolean passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Boolean getArgument(CommandSender sender, String[] args, List previousArguments) { + return "true".equals(args[0].toLowerCase()); + } + +} \ No newline at end of file diff --git a/src/main/java/nl/iobyte/commandapi/arguments/EnumArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/EnumArgument.java new file mode 100644 index 0000000..6d31a37 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/EnumArgument.java @@ -0,0 +1,48 @@ +package nl.iobyte.commandapi.arguments; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.Arrays; +import java.util.List; + +public class EnumArgument implements ICommandArgument> { + + private final Enum[] validArgumentHolders; + + public EnumArgument(Enum... validArgumentHolders) { + this.validArgumentHolders = validArgumentHolders; + } + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid value "+ChatColor.WHITE+args[0]+ChatColor.RED+" for "+validArgumentHolders[0].name(); + } + + /** + * Check if argument is valid Enum + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + return new Pair<>(Arrays.stream(validArgumentHolders).map(Enum::toString).anyMatch(args[0]::equalsIgnoreCase), 1); + } + + /** + * Get Enum passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Enum + */ + public Enum getArgument(CommandSender sender, String[] args, List previousArguments) { + return Arrays.stream(validArgumentHolders).filter(enumVariable -> enumVariable.toString().equalsIgnoreCase(args[0])).findFirst().orElse(null); + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/MessageArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/MessageArgument.java new file mode 100644 index 0000000..6ec73bd --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/MessageArgument.java @@ -0,0 +1,48 @@ +package nl.iobyte.commandapi.arguments; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.List; + +public class MessageArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return ""; + } + + /** + * Check if argument is valid String + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + if(!args[0].startsWith("\"")) + return new Pair<>(true, 1); + + for(int i = 1; i < args.length; i++) + if(args[i].endsWith("\"")) + return new Pair<>(true, i + 1); + + return new Pair<>(true, args.length); + } + + /** + * Get String passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return String + */ + public String getArgument(CommandSender sender, String[] args, List previousArguments) { + return String.join(" ", args).replace("\"", ""); + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/PlayerArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/PlayerArgument.java new file mode 100644 index 0000000..24d1bf8 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/PlayerArgument.java @@ -0,0 +1,43 @@ +package nl.iobyte.commandapi.arguments; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import java.util.List; + +public class PlayerArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "No player found with name: "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid Player + * @param sender CommandSender + * @param args Argument passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + return new Pair<>(Bukkit.getPlayer(args[0]) != null, 1); + } + + /** + * Get Player passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Player + */ + public Player getArgument(CommandSender sender, String[] args, List previousArguments) { + return Bukkit.getPlayer(args[0]); + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/PlayersArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/PlayersArgument.java new file mode 100644 index 0000000..57df97e --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/PlayersArgument.java @@ -0,0 +1,44 @@ +package nl.iobyte.commandapi.arguments; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import nl.iobyte.commandapi.objects.SpigotPlayerSelector; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import java.util.List; + +public class PlayersArgument implements ICommandArgument> { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "No player(s) found for: "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid Player + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + List players = new SpigotPlayerSelector(args[0]).getPlayers(sender); + return new Pair<>(players != null && !players.isEmpty(), 1); + } + + /** + * Get Player passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Player + */ + public List getArgument(CommandSender sender, String[] args, List previousArguments) { + return new SpigotPlayerSelector(args[0]).getPlayers(sender); + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/StringArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/StringArgument.java new file mode 100644 index 0000000..b1eeb0a --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/StringArgument.java @@ -0,0 +1,51 @@ +package nl.iobyte.commandapi.arguments; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.Arrays; +import java.util.List; + +public class StringArgument implements ICommandArgument { + + private final String[] validArguments; + + public StringArgument(String... validArguments) { + this.validArguments = validArguments; + } + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid string: "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid String + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + if(validArguments == null || validArguments.length == 0) + return new Pair<>(true, 1); + + return new Pair<>(Arrays.stream(validArguments).anyMatch(args[0]::equalsIgnoreCase), 1); + } + + /** + * Get String passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return String + */ + public String getArgument(CommandSender sender, String[] args, List previousArguments) { + return args[0]; + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/array/IntegerArrayArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/array/IntegerArrayArgument.java new file mode 100644 index 0000000..7079098 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/array/IntegerArrayArgument.java @@ -0,0 +1,42 @@ +package nl.iobyte.commandapi.arguments.array; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.List; + +public class IntegerArrayArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid integer array: "+ ChatColor.WHITE+args[0]; + } + + @Override + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + String[] items = args[0].replace("[", "").replace("]", "").split(","); + try { + for(String item : items) + Integer.parseInt(item); + + return new Pair<>(true, 1); + } catch (Exception e) { + return new Pair<>(false, 0); + } + } + + @Override + public Integer[] getArgument(CommandSender sender, String[] args, List previousArguments) { + String[] items = args[0].replace("[", "").replace("]", "").split(","); + Integer[] results = new Integer[items.length]; + for(int i = 0; i < items.length; i++) + results[i] = Integer.parseInt(items[i]); + + return results; + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/array/StringArrayArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/array/StringArrayArgument.java new file mode 100644 index 0000000..042a61a --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/array/StringArrayArgument.java @@ -0,0 +1,32 @@ +package nl.iobyte.commandapi.arguments.array; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.List; + +public class StringArrayArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid string array: "+ ChatColor.WHITE+args[0]; + } + + @Override + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + if(!args[0].startsWith("[\"") || !args[0].endsWith("\"]")) + return new Pair<>(false, 0); + + return new Pair<>(true, 1); + } + + @Override + public String[] getArgument(CommandSender sender, String[] args, List previousArguments) { + return args[0].replace("[", "").replace("]", "").replaceAll("\"", "").split(","); + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/number/DoubleArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/number/DoubleArgument.java new file mode 100644 index 0000000..cae705a --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/number/DoubleArgument.java @@ -0,0 +1,51 @@ +package nl.iobyte.commandapi.arguments.number; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +import java.util.List; + +public class DoubleArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid double: "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid Double + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + try { + Double.parseDouble(args[0]); + return new Pair<>(true, 1); + } catch (Exception e) { + return new Pair<>(false, 0); + } + } + + /** + * Get Double passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Double + */ + public Double getArgument(CommandSender sender, String[] args, List previousArguments) { + try { + return Double.parseDouble(args[0]); + } catch (Exception e) { + return 0.0; + } + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/number/FloatArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/number/FloatArgument.java new file mode 100644 index 0000000..601a22b --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/number/FloatArgument.java @@ -0,0 +1,50 @@ +package nl.iobyte.commandapi.arguments.number; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.List; + +public class FloatArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid float: "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid Float + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + try { + Float.parseFloat(args[0]); + return new Pair<>(true, 1); + } catch (Exception e) { + return new Pair<>(false, 0); + } + } + + /** + * Get Float passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Float + */ + public Float getArgument(CommandSender sender, String[] args, List previousArguments) { + try { + return Float.parseFloat(args[0]); + } catch (Exception e) { + return 0f; + } + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/number/IntegerArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/number/IntegerArgument.java new file mode 100644 index 0000000..181be6d --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/number/IntegerArgument.java @@ -0,0 +1,50 @@ +package nl.iobyte.commandapi.arguments.number; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.List; + +public class IntegerArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid integer: "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid Integer + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + try { + Integer.parseInt(args[0]); + return new Pair<>(true, 1); + } catch (Exception e) { + return new Pair<>(false, 0); + } + } + + /** + * Get Integer passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Integer + */ + public Integer getArgument(CommandSender sender, String[] args, List previousArguments) { + try { + return Integer.parseInt(args[0]); + } catch (Exception e) { + return 0; + } + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/number/LongArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/number/LongArgument.java new file mode 100644 index 0000000..b252952 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/number/LongArgument.java @@ -0,0 +1,50 @@ +package nl.iobyte.commandapi.arguments.number; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.List; + +public class LongArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid long: "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid Long + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + try { + Long.parseLong(args[0]); + return new Pair<>(true, 1); + } catch (Exception e) { + return new Pair<>(false, 0); + } + } + + /** + * Get Long passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Long + */ + public Long getArgument(CommandSender sender, String[] args, List previousArguments) { + try { + return Long.parseLong(args[0]); + } catch (Exception e) { + return 0L; + } + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/arguments/number/ShortArgument.java b/src/main/java/nl/iobyte/commandapi/arguments/number/ShortArgument.java new file mode 100644 index 0000000..7323231 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/arguments/number/ShortArgument.java @@ -0,0 +1,50 @@ +package nl.iobyte.commandapi.arguments.number; + +import javafx.util.Pair; +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import java.util.List; + +public class ShortArgument implements ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + public String getMessage(String[] args) { + return "Invalid short: "+ ChatColor.WHITE+args[0]; + } + + /** + * Check if argument is valid Short + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Boolean + */ + public Pair checkArgument(CommandSender sender, String[] args, List previousArguments) { + try { + Short.parseShort(args[0]); + return new Pair<>(true, 1); + } catch (Exception e) { + return new Pair<>(false, 0); + } + } + + /** + * Get Short passed by command + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Short + */ + public Short getArgument(CommandSender sender, String[] args, List previousArguments) { + try { + return Short.parseShort(args[0]); + } catch (Exception e) { + return 0; + } + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/interfaces/ICommandArgument.java b/src/main/java/nl/iobyte/commandapi/interfaces/ICommandArgument.java new file mode 100644 index 0000000..5e2f11f --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/interfaces/ICommandArgument.java @@ -0,0 +1,33 @@ +package nl.iobyte.commandapi.interfaces; + +import javafx.util.Pair; +import org.bukkit.command.CommandSender; +import java.util.List; + +public interface ICommandArgument { + + /** + * Message to display when giving an error + * @return String + */ + String getMessage(String[] args); + + /** + * Check if valid argument + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Pair + */ + Pair checkArgument(CommandSender sender, String[] args, List previousArguments); + + /** + * Get object of argument + * @param sender CommandSender + * @param args Arguments passed by Command + * @param previousArguments Previous arguments + * @return Object of type T + */ + T getArgument(CommandSender sender, String[] args, List previousArguments); + +} diff --git a/src/main/java/nl/iobyte/commandapi/interfaces/ICommandMiddleware.java b/src/main/java/nl/iobyte/commandapi/interfaces/ICommandMiddleware.java new file mode 100644 index 0000000..9edfc90 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/interfaces/ICommandMiddleware.java @@ -0,0 +1,9 @@ +package nl.iobyte.commandapi.interfaces; + +import org.bukkit.command.CommandSender; + +public interface ICommandMiddleware { + + boolean continueCommand(CommandSender sender, SubCommand command); + +} diff --git a/src/main/java/nl/iobyte/commandapi/interfaces/SubCommand.java b/src/main/java/nl/iobyte/commandapi/interfaces/SubCommand.java new file mode 100644 index 0000000..76b833e --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/interfaces/SubCommand.java @@ -0,0 +1,109 @@ +package nl.iobyte.commandapi.interfaces; + +import nl.iobyte.commandapi.objects.CommandSyntax; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.List; + +public abstract class SubCommand { + + private final String[] name; + private final String permission; + private final List syntaxList = new ArrayList<>(); + + public SubCommand(String[] name) { + this.name = name; + this.permission = ""; + } + + public SubCommand(String permission, String... name) { + this.name = name; + this.permission = permission; + } + + /** + * Get name of sub-command + * @return String + */ + public String[] getName() { + return name; + } + + /** + * Get permission of sub-command + * @return String + */ + public String getPermission() { + return permission; + } + + /** + * Check if sub-command has permission + * @return Boolean + */ + public boolean hasPermission() { + return permission != null && !permission.isEmpty(); + } + + /** + * Get list with syntax's + * @return List + */ + public List getSyntaxList() { + return syntaxList; + } + + /** + * Get all Syntax's sender can use + * @param sender CommandSender + * @return List + */ + public List getApplicableSyntaxList(CommandSender sender) { + List list = new ArrayList<>(); + for(CommandSyntax syntax : syntaxList) { + if(!syntax.isConsoleAllowed() && !(sender instanceof Player)) + continue; + + list.add(syntax); + } + + return list; + } + + /** + * Add list of syntax's to sub-command + * @param syntaxList List + */ + public void addSyntaxList(List syntaxList) { + this.syntaxList.addAll(syntaxList); + } + + /** + * Add syntax to sub-command + * @param usage String + * @return CommandSyntax + */ + public CommandSyntax addSyntax(String usage) { + CommandSyntax syntax = new CommandSyntax(usage); + syntaxList.add(syntax); + return syntax; + } + + /** + * When player executes command + * @param player Player + * @param args Object[] + * @param syntaxUsed Integer + */ + public abstract void onPlayerCommand(Player player, List args, int syntaxUsed); + + /** + * When console executes command + * @param sender CommandSender + * @param args Object[] + * @param syntaxUsed Integer + */ + public abstract void onConsoleCommand(CommandSender sender, List args, int syntaxUsed); + +} diff --git a/src/main/java/nl/iobyte/commandapi/middlewares/NoPlayerMiddleware.java b/src/main/java/nl/iobyte/commandapi/middlewares/NoPlayerMiddleware.java new file mode 100644 index 0000000..27ec69f --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/middlewares/NoPlayerMiddleware.java @@ -0,0 +1,15 @@ +package nl.iobyte.commandapi.middlewares; + +import nl.iobyte.commandapi.interfaces.ICommandMiddleware; +import nl.iobyte.commandapi.interfaces.SubCommand; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class NoPlayerMiddleware implements ICommandMiddleware { + + @Override + public boolean continueCommand(CommandSender sender, SubCommand command) { + return !(sender instanceof Player); + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/middlewares/PermissionMiddleware.java b/src/main/java/nl/iobyte/commandapi/middlewares/PermissionMiddleware.java new file mode 100644 index 0000000..21ead8e --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/middlewares/PermissionMiddleware.java @@ -0,0 +1,38 @@ +package nl.iobyte.commandapi.middlewares; + +import nl.iobyte.commandapi.interfaces.ICommandMiddleware; +import nl.iobyte.commandapi.interfaces.SubCommand; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +public class PermissionMiddleware implements ICommandMiddleware { + + private final String[] permissions; + + public PermissionMiddleware() { + permissions = null; + } + + public PermissionMiddleware(String... permissions) { + this.permissions = permissions; + } + + @Override + public boolean continueCommand(CommandSender sender, SubCommand command) { + if(command.hasPermission() && !sender.hasPermission(command.getPermission())) { + sender.sendMessage(ChatColor.RED + "You don't have permission to use this command"); + return false; + } + + if(permissions != null && permissions.length != 0) { + for (String permission : permissions) { + if (!sender.hasPermission(permission)) { + sender.sendMessage(ChatColor.RED + "You don't have permission to use this command"); + return false; + } + } + } + + return true; + } +} diff --git a/src/main/java/nl/iobyte/commandapi/middlewares/PlayerMiddleware.java b/src/main/java/nl/iobyte/commandapi/middlewares/PlayerMiddleware.java new file mode 100644 index 0000000..5cd1f01 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/middlewares/PlayerMiddleware.java @@ -0,0 +1,15 @@ +package nl.iobyte.commandapi.middlewares; + +import nl.iobyte.commandapi.interfaces.ICommandMiddleware; +import nl.iobyte.commandapi.interfaces.SubCommand; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class PlayerMiddleware implements ICommandMiddleware { + + @Override + public boolean continueCommand(CommandSender sender, SubCommand command) { + return sender instanceof Player; + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/objects/CommandMap.java b/src/main/java/nl/iobyte/commandapi/objects/CommandMap.java new file mode 100644 index 0000000..3230231 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/objects/CommandMap.java @@ -0,0 +1,97 @@ +package nl.iobyte.commandapi.objects; + +import nl.iobyte.commandapi.interfaces.SubCommand; +import java.util.*; + +public class CommandMap { + + private final Map map = new HashMap<>(); + private final List subCommands = new ArrayList<>(); + + /** + * Get SubCommand from Args + * @param args String[] + * @return SubCommand + */ + public SubCommand getSubCommand(String... args) { + return getSubCommand(0, args); + } + + /** + * Get SubCommand from Args + * @param i Integer + * @param args String[] + * @return SubCommand + */ + private SubCommand getSubCommand(int i, String... args) { + if(args.length <= i) + return getSubCommand(0, ""); + + Object obj = map.get(args[i]); + if(obj == null) + return (SubCommand) map.get(""); + + if(args.length == 1) { + if(obj instanceof CommandMap) + return ((CommandMap) obj).getSubCommand(0,""); + + return (SubCommand) obj; + } + + if(obj instanceof CommandMap) + return ((CommandMap) obj).getSubCommand(i + 1, args); + + return null; + } + + /** + * Get List of all SubCommands + * @return List + */ + public List getSubCommands() { + return subCommands; + } + + /** + * Add SubCommand + * @param command SubCommand + */ + public void addSubCommand(SubCommand command) { + addSubCommand(command.getName(), 0, command); + for(SubCommand subCommand : subCommands) + if(Arrays.equals(subCommand.getName(), command.getName())) + return; + + subCommands.add(command); + } + + /** + * Add SubCommand + * @param name String[] + * @param i Integer + * @param command SubCommand + */ + private void addSubCommand(String[] name, int i, SubCommand command) { + if(name.length <= i) { + if(map.containsKey("")) { + ((SubCommand) map.get("")).addSyntaxList(command.getSyntaxList()); + return; + } + + map.put("", command); + return; + } + + CommandMap cm; + if(map.containsKey(name[i])) { + cm = (CommandMap) map.get(name[i]); + } else { + cm = new CommandMap(); + map.put(name[i], cm); + } + + cm.addSubCommand(name, i + 1, command); + + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/objects/CommandSyntax.java b/src/main/java/nl/iobyte/commandapi/objects/CommandSyntax.java new file mode 100644 index 0000000..2ced687 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/objects/CommandSyntax.java @@ -0,0 +1,55 @@ +package nl.iobyte.commandapi.objects; + +import nl.iobyte.commandapi.interfaces.ICommandArgument; +import java.util.ArrayList; +import java.util.List; + +public class CommandSyntax { + + private final String usage; + private final List> arguments = new ArrayList<>(); + private boolean allowConsole = true; + + public CommandSyntax(String usage) { + this.usage = usage; + } + + /** + * Get usage of syntax + * @return String + */ + public String getUsage() { + return usage; + } + + /** + * Get list of arguments + * @return List> + */ + public List> getArguments() { + return arguments; + } + + public CommandSyntax addArgument(ICommandArgument argument) { + arguments.add(argument); + return this; + } + + /** + * Check if console is allowed + * @return Boolean + */ + public boolean isConsoleAllowed() { + return allowConsole; + } + + /** + * Set if console is allowed + * @param allowConsole Boolean + */ + public CommandSyntax setAllowConsole(boolean allowConsole) { + this.allowConsole = allowConsole; + return this; + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/objects/SpigotPlayerSelector.java b/src/main/java/nl/iobyte/commandapi/objects/SpigotPlayerSelector.java new file mode 100644 index 0000000..bf462f9 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/objects/SpigotPlayerSelector.java @@ -0,0 +1,153 @@ +package nl.iobyte.commandapi.objects; + +import nl.iobyte.commandapi.worldguard.WGManager; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.command.BlockCommandSender; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Use selectors in commands! + * + * @author Mindgamesnl + * + * Code from + * https://github.com/Mindgamesnl/OpenAudioMc/blob/master/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/players/objects/SpigotPlayerSelector.java + */ +public class SpigotPlayerSelector { + + private final String selector; + + public SpigotPlayerSelector(String selector) { + this.selector = selector; + } + + /** + * this turns selectors like @a[r=5] into a usable list, since + * 1.13 spigot removed this feature, FOR SOME REASON.. thanks guys.. + * + * @param commandSender the sender + * @return players following the selector + */ + public List getPlayers(CommandSender commandSender) { + List players = new ArrayList<>(); + + if (selector.startsWith("@p")) { + //get Location + Location standPoint = getLocation(commandSender); + + if (getArgument("r").length() != 0) { + int radius = Integer.parseInt(getArgument("r")); + Player nearest = Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .min(Comparator.comparing(player -> player.getLocation().distance(standPoint))) + .filter(player -> radius > player.getLocation().distance(standPoint)) + .get(); + players.add(nearest); + } + + if (getArgument("distance").length() != 0) { + int distance = Integer.parseInt(getArgument("distance")); + Player nearest = Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .min(Comparator.comparing(player -> player.getLocation().distance(standPoint))) + .filter(player -> distance > player.getLocation().distance(standPoint)) + .get(); + players.add(nearest); + } else { + Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .min(Comparator.comparing(player -> player.getLocation().distance(standPoint))) + .ifPresent(players::add); + } + } else if (selector.startsWith("@a")) { + //everyone + Location standPoint = getLocation(commandSender); + + if(getArgument("region").length() != 0) { + WGManager manager = WGManager.getInstance(); + String targetRegion = getArgument("region"); + for (Player player : Bukkit.getOnlinePlayers()) { + manager.getApplicableRegionSet(player.getLocation(standPoint)).forEach(region -> { + if (region.getId().equalsIgnoreCase(targetRegion)) + players.add(player); + }); + } + } else if (getArgument("r").length() != 0) { + int radius = Integer.parseInt(getArgument("r")); + players.addAll(Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .filter(player -> radius > player.getLocation().distance(standPoint)) + .collect(Collectors.toList())); + } else if (getArgument("distance").length() != 0) { + int distance = Integer.parseInt(getArgument("distance")); + players.addAll(Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .filter(player -> distance > player.getLocation().distance(standPoint)) + .collect(Collectors.toList())); + } + + else { + players.addAll(Bukkit.getOnlinePlayers().stream() + .filter(player -> player.getLocation().getWorld().getName().equals(standPoint.getWorld().getName())) + .collect(Collectors.toList())); + } + } else if (selector.length() <= 16) { + //player + Player player = Bukkit.getPlayer(selector); + if (player != null) + players.add(player); + } + + return players; + } + + /** + * attempt to parse the location + * + * @param commandSender the sender + * @return the location or null + */ + private Location getLocation(CommandSender commandSender) { + Location initialLocation = new Location(Bukkit.getWorlds().get(0), 0, 0, 0); + if (commandSender instanceof Player) { + initialLocation = ((Player) commandSender).getLocation(); + } else if (commandSender instanceof BlockCommandSender) { + initialLocation = ((BlockCommandSender) commandSender).getBlock().getLocation(); + } + + if (!getArgument("x").equals("") && !getArgument("y").equals("") && !getArgument("z").equals("")) { + try { + int x = Integer.parseInt(getArgument("x")); + int y = Integer.parseInt(getArgument("y")); + int z = Integer.parseInt(getArgument("z")); + return new Location(initialLocation.getWorld(), x, y, z); + } catch (Exception e) { + return initialLocation; + } + } + + return initialLocation; + } + + private String getArgument(String key) { + StringBuilder result = new StringBuilder(); + String[] arguments = selector.split(key + "="); + if (arguments.length == 1) return ""; + for (byte type : arguments[1].getBytes()) { + char element = (char) type; + if (element == ',' || element == ']') { + return result.toString(); + } else { + result.append(element); + } + } + return result.toString(); + } + +} diff --git a/src/main/java/nl/iobyte/commandapi/worldguard/Reflection.java b/src/main/java/nl/iobyte/commandapi/worldguard/Reflection.java new file mode 100644 index 0000000..3dbd878 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/worldguard/Reflection.java @@ -0,0 +1,35 @@ +package nl.iobyte.commandapi.worldguard; + +import org.bukkit.Bukkit; +import java.util.HashMap; + +public class Reflection { + + private static final HashMap> classes = new HashMap<>(); + private static final String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; + + public static Class getCraftBukkitClass(String name) { + return getClass("org.bukkit.craftbukkit." + version + "." + name); + } + + public static Class getNMSClass(String name) { + return getClass("net.minecraft.server." + version + "." + name); + } + + public static Class getClass(String path) { + if(classes.containsKey(path)) + return classes.get(path); + + try { + Class clazz = Class.forName(path); + classes.put(path, clazz); + return clazz; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + classes.put(path, null); + return null; + } + } + +} + diff --git a/src/main/java/nl/iobyte/commandapi/worldguard/WGManager.java b/src/main/java/nl/iobyte/commandapi/worldguard/WGManager.java new file mode 100644 index 0000000..3773d37 --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/worldguard/WGManager.java @@ -0,0 +1,197 @@ +package nl.iobyte.commandapi.worldguard; + +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.bukkit.BukkitWorld; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.flags.registry.FlagRegistry; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.plugin.Plugin; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +public class WGManager { + + private static String wgVerStr = null; + private static WGManager instance = null; + + public static WGManager getInstance() { + if (instance == null) { + instance = new WGManager(); + } + + return instance; + } + + public WorldGuardPlugin getWorldGuard() { + Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard"); + if ((!(plugin instanceof WorldGuardPlugin))) + return null; + + return (WorldGuardPlugin) plugin; + } + + public WorldEditPlugin getWorldEdit() { + Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit"); + if ((!(plugin instanceof WorldEditPlugin))) + return null; + + return (WorldEditPlugin) plugin; + } + + public ProtectedRegion getLowerCasePlot(World w, String regionname) { + for (ProtectedRegion pr : getRegionManager(w).getRegions().values()) + if (pr.getId().toLowerCase().equalsIgnoreCase(regionname)) + return pr; + + return null; + } + + public RegionManager getRegionManager(World w) { + if (getWgVer().contains("7.")) { + try { + Class wgClass = Reflection.getClass("com.sk89q.worldguard.WorldGuard"); + + Object instance = wgClass.getDeclaredMethod("getInstance").invoke(null); + Class wgInstanceClass = instance.getClass(); + + Object platform = wgInstanceClass.getDeclaredMethod("getPlatform").invoke(instance); + Class wgPlatformClass = platform.getClass(); + + Object regionContainer = wgPlatformClass.getDeclaredMethod("getRegionContainer").invoke(platform); + Class wgRegionContainer = regionContainer.getClass(); + + return (RegionManager) wgRegionContainer.getSuperclass() + .getMethod("get", com.sk89q.worldedit.world.World.class) + .invoke(regionContainer, new BukkitWorld(w)); + } catch (Exception ex) { + ex.printStackTrace(); + } + } else { + return getWorldGuard().getRegionManager(w); + } + + return null; + } + + @SuppressWarnings("unchecked") + public List getRegions(Location loc) { + ArrayList regions = new ArrayList<>(); + if (getWgVer().contains("7.")) { + try { + RegionManager mngr = getRegionManager(loc.getWorld()); + + Class blockVector3 = Reflection.getClass("com.sk89q.worldedit.math.BlockVector3"); + + Method applicableRegions = mngr.getClass().getDeclaredMethod("getApplicableRegions", blockVector3); + + Method blockVectorAt = blockVector3.getDeclaredMethod("at", double.class, double.class, double.class); + Object blockVector = blockVectorAt.invoke(null, loc.getX(), loc.getY(), loc.getZ()); + + Object regionSet = applicableRegions.invoke(mngr, blockVector); + + Method getregions = regionSet.getClass().getMethod("getRegions"); + + regions = new ArrayList<>(((HashSet) getregions.invoke(regionSet))); + + } catch (Exception ex) { + ex.printStackTrace(); + } + } else { + regions = new ArrayList<>(getRegionManager(loc.getWorld()) + .getApplicableRegions(new Vector(loc.getX(), loc.getY(), loc.getZ())).getRegions()); + } + + regions.sort(new WGRegionCompare()); + return regions; + } + + public ProtectedCuboidRegion getProtectedCubiodRegion(String regionname, Location loc1, Location loc2) { + if (getWgVer().contains("7.")) { + try { + + Object bvloc1 = getBlockVectorV3(loc1); + Object bvloc2 = getBlockVectorV3(loc2); + + Class prCbRg = Reflection.getClass("com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion"); + + return (ProtectedCuboidRegion) prCbRg.getConstructor(String.class, bvloc1.getClass(), bvloc2.getClass()) + .newInstance(regionname, bvloc1, bvloc2); + } catch (Exception ex) { + ex.printStackTrace(); + } + } else { + return new ProtectedCuboidRegion(regionname, + new BlockVector(loc1.getX(), loc1.getY(), loc1.getZ()), + new BlockVector(loc2.getX(), loc2.getY(), loc2.getZ())); + } + return null; + } + + public Object getBlockVectorV3(Location loc) throws Exception { + Class blockVector3 = Reflection.getClass("com.sk89q.worldedit.math.BlockVector3"); + + Method blockVectorAt = blockVector3.getDeclaredMethod("at", double.class, double.class, double.class); + return blockVectorAt.invoke(null, loc.getX(), loc.getY(), loc.getZ()); + } + + public FlagRegistry getFlagRegistry() { + if (getWgVer().contains("7.")) { + try { + Class wgClass = Reflection.getClass("com.sk89q.worldguard.WorldGuard"); + + Object instance = wgClass.getDeclaredMethod("getInstance").invoke(null); + Class wgInstanceClass = instance.getClass(); + Method declaredMethod = wgInstanceClass.getDeclaredMethod("getFlagRegistry"); + return (FlagRegistry) declaredMethod.invoke(instance); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } else { + return getWorldGuard().getFlagRegistry(); + } + } + + public ApplicableRegionSet getApplicableRegionSet(Location loc) { + if (getWgVer().contains("7.")) { + try { + RegionManager mngr = getRegionManager(loc.getWorld()); + + Class blockVector3 = Reflection.getClass("com.sk89q.worldedit.math.BlockVector3"); + + Method applicableRegions = mngr.getClass().getDeclaredMethod("getApplicableRegions", blockVector3); + + Method blockVectorAt = blockVector3.getDeclaredMethod("at", double.class, double.class, double.class); + Object blockVector = blockVectorAt.invoke(null, loc.getX(), loc.getY(), loc.getZ()); + + Object regionSet = applicableRegions.invoke(mngr, blockVector); + + return (ApplicableRegionSet) regionSet; + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } else { + return getRegionManager(loc.getWorld()).getApplicableRegions(new Vector(loc.getX(), loc.getY(), loc.getZ())); + } + } + + + public String getWgVer() { + if (wgVerStr == null) + wgVerStr = Bukkit.getPluginManager().getPlugin("WorldGuard").getDescription().getVersion(); + + return wgVerStr; + } + +} \ No newline at end of file diff --git a/src/main/java/nl/iobyte/commandapi/worldguard/WGRegionCompare.java b/src/main/java/nl/iobyte/commandapi/worldguard/WGRegionCompare.java new file mode 100644 index 0000000..865082d --- /dev/null +++ b/src/main/java/nl/iobyte/commandapi/worldguard/WGRegionCompare.java @@ -0,0 +1,19 @@ +package nl.iobyte.commandapi.worldguard; + +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import java.util.Comparator; + +public class WGRegionCompare implements Comparator { + + @Override + public int compare(ProtectedRegion o1, ProtectedRegion o2) { + if (o1.getPriority() == o2.getPriority()) { + return 0; + } else if (o1.getPriority() < o2.getPriority()) { + return 1; + } + + return -1; + } + +} \ No newline at end of file