Initial commit
This commit is contained in:
commit
30c775d0e3
28 changed files with 1610 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
# Project exclude paths
|
||||
/target/
|
49
pom.xml
Normal file
49
pom.xml
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<groupId>nl.iobyte</groupId>
|
||||
<artifactId>commandapi</artifactId>
|
||||
<version>1.1</version>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>sk89q-repo</id>
|
||||
<url>https://maven.enginehub.org/repo/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
<artifactId>spigot-api</artifactId>
|
||||
<version>1.12.2-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sk89q.worldguard</groupId>
|
||||
<artifactId>worldguard-legacy</artifactId>
|
||||
<version>6.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sk89q.worldedit</groupId>
|
||||
<artifactId>worldedit-bukkit</artifactId>
|
||||
<version>6.1.5</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sk89q.worldedit</groupId>
|
||||
<artifactId>worldedit-core</artifactId>
|
||||
<version>6.1.4-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
178
src/main/java/nl/iobyte/commandapi/CommandFactory.java
Normal file
178
src/main/java/nl/iobyte/commandapi/CommandFactory.java
Normal file
|
@ -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<ICommandMiddleware> 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<SubCommand>
|
||||
*/
|
||||
public Collection<SubCommand> getSubCommands() {
|
||||
return commandMap.getSubCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SubCommand's available to CommandSender
|
||||
* @param sender CommandSender
|
||||
* @return List<SubCommand>
|
||||
*/
|
||||
public List<SubCommand> getApplicableSubCommands(CommandSender sender) {
|
||||
List<SubCommand> 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<Boolean, Integer> pair;
|
||||
List<ICommandArgument<?>> arguments;
|
||||
List<Object> parsedArguments;
|
||||
List<CommandSyntax> 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<Object> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Boolean> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
return "true".equals(args[0].toLowerCase());
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Enum<?>> {
|
||||
|
||||
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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
return Arrays.stream(validArgumentHolders).filter(enumVariable -> enumVariable.toString().equalsIgnoreCase(args[0])).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
return String.join(" ", args).replace("\"", "");
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Player> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
return Bukkit.getPlayer(args[0]);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<List<Player>> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> previousArguments) {
|
||||
List<Player> 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<Player> getArgument(CommandSender sender, String[] args, List<Object> previousArguments) {
|
||||
return new SpigotPlayerSelector(args[0]).getPlayers(sender);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String> {
|
||||
|
||||
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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
return args[0];
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer[]> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String[]> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
return args[0].replace("[", "").replace("]", "").replaceAll("\"", "").split(",");
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Double> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
try {
|
||||
return Double.parseDouble(args[0]);
|
||||
} catch (Exception e) {
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Float> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
try {
|
||||
return Float.parseFloat(args[0]);
|
||||
} catch (Exception e) {
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Integer> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
try {
|
||||
return Integer.parseInt(args[0]);
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Long> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
try {
|
||||
return Long.parseLong(args[0]);
|
||||
} catch (Exception e) {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Short> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments) {
|
||||
try {
|
||||
return Short.parseShort(args[0]);
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<T> {
|
||||
|
||||
/**
|
||||
* 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<Boolean, Integer>
|
||||
*/
|
||||
Pair<Boolean, Integer> checkArgument(CommandSender sender, String[] args, List<Object> 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<Object> previousArguments);
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package nl.iobyte.commandapi.interfaces;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public interface ICommandMiddleware {
|
||||
|
||||
boolean continueCommand(CommandSender sender, SubCommand command);
|
||||
|
||||
}
|
109
src/main/java/nl/iobyte/commandapi/interfaces/SubCommand.java
Normal file
109
src/main/java/nl/iobyte/commandapi/interfaces/SubCommand.java
Normal file
|
@ -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<CommandSyntax> 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<CommandSyntax>
|
||||
*/
|
||||
public List<CommandSyntax> getSyntaxList() {
|
||||
return syntaxList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Syntax's sender can use
|
||||
* @param sender CommandSender
|
||||
* @return List<CommandSyntax>
|
||||
*/
|
||||
public List<CommandSyntax> getApplicableSyntaxList(CommandSender sender) {
|
||||
List<CommandSyntax> 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<CommandSyntax>
|
||||
*/
|
||||
public void addSyntaxList(List<CommandSyntax> 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<Object> args, int syntaxUsed);
|
||||
|
||||
/**
|
||||
* When console executes command
|
||||
* @param sender CommandSender
|
||||
* @param args Object[]
|
||||
* @param syntaxUsed Integer
|
||||
*/
|
||||
public abstract void onConsoleCommand(CommandSender sender, List<Object> args, int syntaxUsed);
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
97
src/main/java/nl/iobyte/commandapi/objects/CommandMap.java
Normal file
97
src/main/java/nl/iobyte/commandapi/objects/CommandMap.java
Normal file
|
@ -0,0 +1,97 @@
|
|||
package nl.iobyte.commandapi.objects;
|
||||
|
||||
import nl.iobyte.commandapi.interfaces.SubCommand;
|
||||
import java.util.*;
|
||||
|
||||
public class CommandMap {
|
||||
|
||||
private final Map<String, Object> map = new HashMap<>();
|
||||
private final List<SubCommand> 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<SubCommand>
|
||||
*/
|
||||
public List<SubCommand> 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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<ICommandArgument<?>> 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<ICommandArgument<?>>
|
||||
*/
|
||||
public List<ICommandArgument<?>> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Player> getPlayers(CommandSender commandSender) {
|
||||
List<Player> 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package nl.iobyte.commandapi.worldguard;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Reflection {
|
||||
|
||||
private static final HashMap<String, Class<?>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
197
src/main/java/nl/iobyte/commandapi/worldguard/WGManager.java
Normal file
197
src/main/java/nl/iobyte/commandapi/worldguard/WGManager.java
Normal file
|
@ -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<ProtectedRegion> getRegions(Location loc) {
|
||||
ArrayList<ProtectedRegion> 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<ProtectedRegion>) 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<ProtectedRegion> {
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue