2017-02-19 16:56:14 +01:00
|
|
|
package me.libraryaddict.disguise.commands;
|
|
|
|
|
2017-06-08 17:06:58 +02:00
|
|
|
import me.libraryaddict.disguise.DisguiseAPI;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.Disguise;
|
|
|
|
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
|
2017-06-19 19:06:35 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.*;
|
2017-06-08 17:06:58 +02:00
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseParser.DisguiseParseException;
|
|
|
|
import me.libraryaddict.disguise.utilities.DisguiseParser.DisguisePerm;
|
|
|
|
import me.libraryaddict.disguise.utilities.ReflectionFlagWatchers.ParamInfo;
|
2017-02-19 16:56:14 +01:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Location;
|
2017-06-08 17:06:58 +02:00
|
|
|
import org.bukkit.command.BlockCommandSender;
|
2017-02-19 16:56:14 +01:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.command.TabCompleter;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2017-06-08 17:06:58 +02:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.*;
|
2017-02-19 16:56:14 +01:00
|
|
|
|
|
|
|
public class DisguiseModifyRadiusCommand extends DisguiseBaseCommand implements TabCompleter {
|
|
|
|
private int maxRadius = 30;
|
|
|
|
|
|
|
|
public DisguiseModifyRadiusCommand(int maxRadius) {
|
|
|
|
this.maxRadius = maxRadius;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Collection<Entity> getNearbyEntities(CommandSender sender, int radius) {
|
|
|
|
Location center;
|
|
|
|
|
|
|
|
if (sender instanceof Player) {
|
|
|
|
center = ((Player) sender).getLocation();
|
2017-06-19 11:23:02 +02:00
|
|
|
} else {
|
2017-06-08 17:06:58 +02:00
|
|
|
center = ((BlockCommandSender) sender).getBlock().getLocation().add(0.5, 0, 0.5);
|
2017-02-19 16:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return center.getWorld().getNearbyEntities(center, radius, radius, radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
|
|
|
if (sender.getName().equals("CONSOLE")) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.NO_CONSOLE.get());
|
2017-02-19 16:56:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> map = getPermissions(sender);
|
|
|
|
|
|
|
|
if (map.isEmpty()) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.NO_PERM.get());
|
2017-02-19 16:56:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length == 0) {
|
|
|
|
sendCommandUsage(sender, map);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
if (args[0].equalsIgnoreCase(TranslateType.DISGUISES.get("DisguiseType")) || args[0]
|
|
|
|
.equalsIgnoreCase(TranslateType.DISGUISES.get("DisguiseType") + "s")) {
|
2017-02-19 16:56:14 +01:00
|
|
|
ArrayList<String> classes = new ArrayList<>();
|
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
|
|
|
classes.add(type.toReadable());
|
2017-02-19 16:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(classes);
|
|
|
|
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS_USABLE
|
|
|
|
.get(ChatColor.GREEN + StringUtils.join(classes, ChatColor.DARK_GREEN + ", " + ChatColor.GREEN)));
|
2017-02-19 16:56:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
DisguiseType baseType = null;
|
2017-02-19 16:56:14 +01:00
|
|
|
int starting = 0;
|
|
|
|
|
|
|
|
if (!isNumeric(args[0])) {
|
2017-06-26 01:55:45 +02:00
|
|
|
for (DisguiseType t : DisguiseType.values()) {
|
|
|
|
if (t.toReadable().replaceAll(" ", "").equalsIgnoreCase(args[0].replaceAll("_", ""))) {
|
|
|
|
baseType = t;
|
2017-02-19 16:56:14 +01:00
|
|
|
starting = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
if (baseType == null) {
|
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS_UNRECOGNIZED.get(args[0]));
|
|
|
|
return true;
|
2017-02-19 16:56:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length == starting + 1) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(
|
|
|
|
(starting == 0 ? LibsMsg.DMODRADIUS_NEEDOPTIONS : LibsMsg.DMODRADIUS_NEEDOPTIONS_ENTITY).get());
|
2017-02-19 16:56:14 +01:00
|
|
|
return true;
|
2017-06-19 11:23:02 +02:00
|
|
|
} else if (args.length < 2) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS_NEEDOPTIONS.get());
|
2017-02-19 16:56:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNumeric(args[starting])) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.NOT_NUMBER.get(args[starting]));
|
2017-02-19 16:56:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int radius = Integer.parseInt(args[starting]);
|
|
|
|
|
|
|
|
if (radius > maxRadius) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.LIMITED_RADIUS.get(maxRadius));
|
2017-02-19 16:56:14 +01:00
|
|
|
radius = maxRadius;
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] newArgs = new String[args.length - (starting + 1)];
|
|
|
|
System.arraycopy(args, starting + 1, newArgs, 0, newArgs.length);
|
|
|
|
|
|
|
|
if (newArgs.length == 0) {
|
|
|
|
sendCommandUsage(sender, map);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Time to use it!
|
|
|
|
int modifiedDisguises = 0;
|
|
|
|
int noPermission = 0;
|
|
|
|
|
|
|
|
for (Entity entity : getNearbyEntities(sender, radius)) {
|
|
|
|
if (entity == sender) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
if (baseType != null && !baseType.name().equalsIgnoreCase(entity.getType().name())) {
|
2017-02-19 16:56:14 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Disguise disguise;
|
|
|
|
|
|
|
|
if (sender instanceof Player)
|
|
|
|
disguise = DisguiseAPI.getDisguise((Player) sender, entity);
|
|
|
|
else
|
|
|
|
disguise = DisguiseAPI.getDisguise(entity);
|
|
|
|
|
|
|
|
if (!map.containsKey(new DisguisePerm(disguise.getType()))) {
|
|
|
|
noPermission++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
DisguiseParser.callMethods(sender, disguise, map.get(new DisguisePerm(disguise.getType())),
|
2017-12-11 08:43:25 +01:00
|
|
|
new ArrayList<String>(), DisguiseParser.split(StringUtils.join(newArgs, " ")));
|
2017-02-19 16:56:14 +01:00
|
|
|
modifiedDisguises++;
|
|
|
|
}
|
|
|
|
catch (DisguiseParseException ex) {
|
|
|
|
if (ex.getMessage() != null) {
|
|
|
|
sender.sendMessage(ex.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noPermission > 0) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS_NOPERM.get(noPermission));
|
2017-02-19 16:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modifiedDisguises > 0) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS.get(modifiedDisguises));
|
2017-06-19 11:23:02 +02:00
|
|
|
} else {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS_NOENTS.get());
|
2017-02-19 16:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
|
2017-06-19 11:23:02 +02:00
|
|
|
ArrayList<String> tabs = new ArrayList<>();
|
2017-02-19 16:56:14 +01:00
|
|
|
String[] args = getArgs(origArgs);
|
|
|
|
|
|
|
|
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> perms = getPermissions(sender);
|
|
|
|
|
|
|
|
if (args.length == 0) {
|
2017-06-26 01:55:45 +02:00
|
|
|
for (DisguiseType type : DisguiseType.values()) {
|
|
|
|
tabs.add(type.toReadable().replaceAll(" ", "_"));
|
2017-02-19 16:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return filterTabs(tabs, origArgs);
|
|
|
|
}
|
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
int starting = 0;
|
2017-02-19 16:56:14 +01:00
|
|
|
|
|
|
|
if (!isNumeric(args[0])) {
|
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
for (DisguiseType t : DisguiseType.values()) {
|
|
|
|
if (t.toReadable().replaceAll(" ", "").equalsIgnoreCase(args[0].replaceAll("_", ""))) {
|
|
|
|
starting = 2;
|
|
|
|
break;
|
|
|
|
}
|
2017-02-19 16:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not a valid radius
|
|
|
|
if (starting == 1 || args.length == 1 || !isNumeric(args[1]))
|
|
|
|
return filterTabs(tabs, origArgs);
|
|
|
|
}
|
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
if (!isNumeric(args[starting])) {
|
|
|
|
return filterTabs(tabs, origArgs);
|
|
|
|
}
|
|
|
|
|
2017-02-19 16:56:14 +01:00
|
|
|
int radius = Integer.parseInt(args[starting]);
|
|
|
|
|
|
|
|
if (radius > maxRadius) {
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.LIMITED_RADIUS.get(maxRadius));
|
2017-02-19 16:56:14 +01:00
|
|
|
radius = maxRadius;
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:23:02 +02:00
|
|
|
ArrayList<String> usedOptions = new ArrayList<>();
|
2017-02-19 16:56:14 +01:00
|
|
|
|
|
|
|
for (Entity entity : getNearbyEntities(sender, radius)) {
|
|
|
|
Disguise disguise = DisguiseAPI.getDisguise(entity);
|
|
|
|
|
|
|
|
if (disguise == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DisguiseType disguiseType = disguise.getType();
|
|
|
|
|
|
|
|
for (Method method : ReflectionFlagWatchers.getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
|
2017-06-19 11:23:02 +02:00
|
|
|
for (String arg : args) {
|
2017-06-26 01:55:45 +02:00
|
|
|
if (!method.getName().equalsIgnoreCase(arg) || usedOptions.contains(arg))
|
2017-02-19 16:56:14 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
usedOptions.add(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 11:23:02 +02:00
|
|
|
if (passesCheck(sender, perms.get(new DisguisePerm(disguiseType)), usedOptions)) {
|
2017-02-19 16:56:14 +01:00
|
|
|
boolean addMethods = true;
|
|
|
|
|
|
|
|
if (args.length > 1 + starting) {
|
|
|
|
String prevArg = args[args.length - 1];
|
|
|
|
|
|
|
|
ParamInfo info = ReflectionFlagWatchers.getParamInfo(disguiseType, prevArg);
|
|
|
|
|
|
|
|
if (info != null) {
|
|
|
|
if (info.getParamClass() != boolean.class)
|
|
|
|
addMethods = false;
|
|
|
|
|
|
|
|
if (info.isEnums()) {
|
2017-06-26 01:55:45 +02:00
|
|
|
tabs.addAll(Arrays.asList(info.getEnums(origArgs[origArgs.length - 1])));
|
2017-06-19 11:23:02 +02:00
|
|
|
} else {
|
2017-02-19 16:56:14 +01:00
|
|
|
if (info.getParamClass() == String.class) {
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
tabs.add(player.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addMethods) {
|
|
|
|
// If this is a method, add. Else if it can be a param of the previous argument, add.
|
2017-06-19 19:06:35 +02:00
|
|
|
for (Method method : ReflectionFlagWatchers
|
|
|
|
.getDisguiseWatcherMethods(disguiseType.getWatcherClass())) {
|
2017-02-19 16:56:14 +01:00
|
|
|
tabs.add(method.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filterTabs(tabs, origArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the player the information
|
|
|
|
*/
|
|
|
|
@Override
|
2017-06-19 11:23:02 +02:00
|
|
|
protected void sendCommandUsage(CommandSender sender,
|
|
|
|
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> map) {
|
2017-02-19 16:56:14 +01:00
|
|
|
ArrayList<String> allowedDisguises = getAllowedDisguises(map);
|
|
|
|
|
2017-06-19 19:06:35 +02:00
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS_HELP1.get(maxRadius));
|
2017-06-19 19:19:46 +02:00
|
|
|
sender.sendMessage(LibsMsg.DMODIFY_HELP3
|
2017-06-19 19:06:35 +02:00
|
|
|
.get(ChatColor.GREEN + StringUtils.join(allowedDisguises, ChatColor.RED + ", " + ChatColor.GREEN)));
|
2017-02-19 16:56:14 +01:00
|
|
|
|
2017-06-26 01:55:45 +02:00
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS_HELP2.get());
|
|
|
|
sender.sendMessage(LibsMsg.DMODRADIUS_HELP3.get());
|
2017-02-19 16:56:14 +01:00
|
|
|
}
|
|
|
|
}
|