Minor cleanup and untested disguise modify commands

This commit is contained in:
libraryaddict 2017-02-20 03:53:03 +13:00
parent 7ab456c7e6
commit 77a7e91766
7 changed files with 284 additions and 170 deletions

@ -53,6 +53,22 @@ commands:
aliases: [dviewself, dvs, disguisevs, disvs, vsd, viewselfdisguise, viewselfd]
permission: libsdisguises.seecmd.viewself
description: Toggle seeing your own disguise on or off.
disguisemodify:
aliases: [dmodify]
permission: libsdisguises.seecmd.disguisemodify
description: Modify your own disguise
disguisemodifyplayer:
aliases: [dmodifyplayer]
permission: libsdisguises.seecmd.disguisemodifyplayer
description: Modify the disguise of a player
disguisemodifyradius:
aliases: [dmodifyradius]
permission: libsdisguises.seecmd.disguisemodifyradius
description: Modify disguises in a radius
disguisemodifyentity:
aliases: [dmodifyentity]
permission: libsdisguises.seecmd.disguisemodifyentity
description: Modify a disguise by right clicking them
permissions:
libsdisguises.reload:

@ -1,6 +1,7 @@
package me.libraryaddict.disguise;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
@ -41,6 +42,9 @@ import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseParser;
import me.libraryaddict.disguise.utilities.DisguiseParser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.DisguiseParser.DisguisePerm;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager;
import me.libraryaddict.disguise.utilities.UpdateChecker;
@ -50,6 +54,7 @@ public class DisguiseListener implements Listener {
private String currentVersion;
private HashMap<String, Boolean[]> disguiseClone = new HashMap<>();
private HashMap<String, Disguise> disguiseEntity = new HashMap<>();
private HashMap<String, String[]> disguiseModify = new HashMap<>();
private HashMap<String, BukkitRunnable> disguiseRunnable = new HashMap<>();
private String latestVersion;
private LibsDisguises plugin;
@ -310,7 +315,10 @@ public class DisguiseListener implements Listener {
@EventHandler
public void onRightClick(PlayerInteractEntityEvent event) {
if (disguiseEntity.containsKey(event.getPlayer().getName()) || disguiseClone.containsKey(event.getPlayer().getName())) {
if (!disguiseEntity.containsKey(event.getPlayer().getName()) && !disguiseClone.containsKey(event.getPlayer().getName())) {
return;
}
Player p = event.getPlayer();
event.setCancelled(true);
@ -383,6 +391,37 @@ public class DisguiseListener implements Listener {
}
}
}
else if (disguiseModify.containsKey(p.getName())) {
String[] options = disguiseModify.remove(p.getName());
Disguise disguise = DisguiseAPI.getDisguise(p, entity);
if (disguise == null) {
p.sendMessage(ChatColor.RED + entityName + " is not disguised!");
return;
}
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> perms = DisguiseParser.getPermissions(p,
"libsdisguises.disguiseentitymodify.");
if (!perms.containsKey(new DisguisePerm(disguise.getType()))) {
p.sendMessage(ChatColor.RED + "You do not have permission to modify this disguise");
return;
}
try {
DisguiseParser.callMethods(p, disguise, perms.get(new DisguisePerm(disguise.getType())), new ArrayList<String>(),
options);
p.sendMessage(ChatColor.RED + "Modified the disguise!");
}
catch (DisguiseParseException ex) {
if (ex.getMessage() != null) {
p.sendMessage(ex.getMessage());
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
@ -538,4 +577,25 @@ public class DisguiseListener implements Listener {
disguiseEntity.put(player, disguise);
}
public void setDisguiseModify(final String player, String[] args) {
if (disguiseRunnable.containsKey(player)) {
BukkitRunnable run = disguiseRunnable.remove(player);
run.cancel();
run.run();
}
BukkitRunnable runnable = new BukkitRunnable() {
@Override
public void run() {
disguiseModify.remove(player);
disguiseRunnable.remove(player);
}
};
runnable.runTaskLater(plugin, 20 * DisguiseConfig.getDisguiseEntityExpire());
disguiseRunnable.put(player, runnable);
disguiseModify.put(player, args);
}
}

@ -26,6 +26,10 @@ import me.libraryaddict.disguise.commands.DisguiseCloneCommand;
import me.libraryaddict.disguise.commands.DisguiseCommand;
import me.libraryaddict.disguise.commands.DisguiseEntityCommand;
import me.libraryaddict.disguise.commands.DisguiseHelpCommand;
import me.libraryaddict.disguise.commands.DisguiseModifyCommand;
import me.libraryaddict.disguise.commands.DisguiseModifyEntityCommand;
import me.libraryaddict.disguise.commands.DisguiseModifyPlayerCommand;
import me.libraryaddict.disguise.commands.DisguiseModifyRadiusCommand;
import me.libraryaddict.disguise.commands.DisguisePlayerCommand;
import me.libraryaddict.disguise.commands.DisguiseRadiusCommand;
import me.libraryaddict.disguise.commands.DisguiseViewSelfCommand;
@ -35,8 +39,8 @@ import me.libraryaddict.disguise.commands.UndisguiseEntityCommand;
import me.libraryaddict.disguise.commands.UndisguisePlayerCommand;
import me.libraryaddict.disguise.commands.UndisguiseRadiusCommand;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.MetaIndex;
import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ArrowWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.GuardianWatcher;
@ -116,6 +120,10 @@ public class LibsDisguises extends JavaPlugin {
registerCommand("disguiseclone", new DisguiseCloneCommand());
registerCommand("libsdisguises", new LibsDisguisesCommand());
registerCommand("disguiseviewself", new DisguiseViewSelfCommand());
registerCommand("disguisemodify", new DisguiseModifyCommand());
registerCommand("disguisemodifyentity", new DisguiseModifyEntityCommand());
registerCommand("disguisemodifyplayer", new DisguiseModifyPlayerCommand());
registerCommand("disguisemodifyradius", new DisguiseModifyRadiusCommand(getConfig().getInt("DisguiseRadiusMax")));
try {
Metrics metrics = new Metrics(this);

@ -66,7 +66,7 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
return newArgs.toArray(new String[0]);
}
public String getPermNode() {
public final String getPermNode() {
if (this instanceof DisguiseCommand) {
return "disguise";
}
@ -79,9 +79,22 @@ public abstract class DisguiseBaseCommand implements CommandExecutor {
else if (this instanceof DisguiseRadiusCommand) {
return "disguiseradius";
}
else
else if (this instanceof DisguiseModifyCommand) {
return "disguisemodify";
}
else if (this instanceof DisguiseModifyEntityCommand) {
return "disguisemodifyentity";
}
else if (this instanceof DisguiseModifyPlayerCommand) {
return "disguisemodifyplayer";
}
else if (this instanceof DisguiseModifyRadiusCommand) {
return "disguisemodifyradius";
}
else {
throw new UnsupportedOperationException("Unknown disguise command, perm node not found");
}
}
protected HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> getPermissions(CommandSender sender) {
return DisguiseParser.getPermissions(sender, "libsdisguises." + getPermNode() + ".");

@ -11,6 +11,7 @@ import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import me.libraryaddict.disguise.DisguiseAPI;
@ -27,7 +28,7 @@ import me.libraryaddict.disguise.utilities.ReflectionFlagWatchers.ParamInfo;
public class DisguiseCommand extends DisguiseBaseCommand implements TabCompleter {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender.getName().equals("CONSOLE")) {
if (!(sender instanceof Entity)) {
sender.sendMessage(ChatColor.RED + "You may not use this command from the console!");
return true;
}

@ -27,11 +27,16 @@ import me.libraryaddict.disguise.utilities.ReflectionFlagWatchers.ParamInfo;
public class DisguiseEntityCommand extends DisguiseBaseCommand implements TabCompleter {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender.getName().equals("CONSOLE")) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "You may not use this command from the console!");
return true;
}
if (getPermissions(sender).isEmpty()) {
sender.sendMessage(ChatColor.RED + "You are forbidden to use this command.");
return true;
}
if (args.length == 0) {
sendCommandUsage(sender, getPermissions(sender));
return true;
@ -56,7 +61,7 @@ public class DisguiseEntityCommand extends DisguiseBaseCommand implements TabCom
LibsDisguises.getInstance().getListener().setDisguiseEntity(sender.getName(), disguise);
sender.sendMessage(ChatColor.RED + "Right click a entity in the next " + DisguiseConfig.getDisguiseEntityExpire()
sender.sendMessage(ChatColor.RED + "Right click an entity in the next " + DisguiseConfig.getDisguiseEntityExpire()
+ " seconds to disguise it as a " + disguise.getType().toReadable() + "!");
return true;
}
@ -64,6 +69,11 @@ public class DisguiseEntityCommand extends DisguiseBaseCommand implements TabCom
@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] origArgs) {
ArrayList<String> tabs = new ArrayList<String>();
if (!(sender instanceof Player)) {
return tabs;
}
String[] args = getArgs(origArgs);
HashMap<DisguisePerm, HashMap<ArrayList<String>, Boolean>> perms = getPermissions(sender);

@ -690,8 +690,16 @@ public class DisguiseParser {
// Copy strings to their new range
String[] newArgs = new String[args.length - toSkip];
System.arraycopy(args, toSkip, newArgs, 0, args.length - toSkip);
args = newArgs;
callMethods(sender, disguise, optionPermissions, usedOptions, newArgs);
// Alright. We've constructed our disguise.
return disguise;
}
public static void callMethods(CommandSender sender, Disguise disguise, HashMap<ArrayList<String>, Boolean> optionPermissions,
ArrayList<String> usedOptions, String[] args)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, DisguiseParseException {
Method[] methods = ReflectionFlagWatchers.getDisguiseWatcherMethods(disguise.getWatcher().getClass());
for (int i = 0; i < args.length; i += 2) {
@ -970,8 +978,6 @@ public class DisguiseParser {
methodToUse.invoke(disguise, value);
}
}
// Alright. We've constructed our disguise.
return disguise;
}
private static DisguiseParseException parseToException(String expectedValue, String receivedInstead, String methodName) {