From a7908e57386ff39d017bac654942011eb3339296 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 29 May 2013 10:44:55 +1200 Subject: [PATCH] Added disguising other players commands --- plugin.yml | 6 +- .../{ => Commands}/DisguiseCommand.java | 3 +- .../Commands/DisguisePlayerCommand.java | 159 ++++++++++++++++++ .../{ => Commands}/UndisguiseCommand.java | 4 +- .../Commands/UndisguisePlayerCommand.java | 32 ++++ .../libraryaddict/disguise/LibsDisguises.java | 6 + 6 files changed, 207 insertions(+), 3 deletions(-) rename src/me/libraryaddict/disguise/{ => Commands}/DisguiseCommand.java (98%) create mode 100644 src/me/libraryaddict/disguise/Commands/DisguisePlayerCommand.java rename src/me/libraryaddict/disguise/{ => Commands}/UndisguiseCommand.java (90%) create mode 100644 src/me/libraryaddict/disguise/Commands/UndisguisePlayerCommand.java diff --git a/plugin.yml b/plugin.yml index 9131892f..e1ed28f5 100644 --- a/plugin.yml +++ b/plugin.yml @@ -7,4 +7,8 @@ commands: disguise: aliases: [d, dis] undisguise: - aliases: [undis] \ No newline at end of file + aliases: [undis] + disguiseplayer: + aliases: [dplayer, displayer] + undisguiseplayer: + aliases: [undisplayer] \ No newline at end of file diff --git a/src/me/libraryaddict/disguise/DisguiseCommand.java b/src/me/libraryaddict/disguise/Commands/DisguiseCommand.java similarity index 98% rename from src/me/libraryaddict/disguise/DisguiseCommand.java rename to src/me/libraryaddict/disguise/Commands/DisguiseCommand.java index 4478a96f..a54a8c79 100644 --- a/src/me/libraryaddict/disguise/DisguiseCommand.java +++ b/src/me/libraryaddict/disguise/Commands/DisguiseCommand.java @@ -1,8 +1,9 @@ -package me.libraryaddict.disguise; +package me.libraryaddict.disguise.Commands; import java.util.ArrayList; import java.util.Collections; +import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseTypes.Disguise; import me.libraryaddict.disguise.DisguiseTypes.DisguiseType; import me.libraryaddict.disguise.DisguiseTypes.MiscDisguise; diff --git a/src/me/libraryaddict/disguise/Commands/DisguisePlayerCommand.java b/src/me/libraryaddict/disguise/Commands/DisguisePlayerCommand.java new file mode 100644 index 00000000..558b0d8e --- /dev/null +++ b/src/me/libraryaddict/disguise/Commands/DisguisePlayerCommand.java @@ -0,0 +1,159 @@ +package me.libraryaddict.disguise.Commands; + +import java.util.ArrayList; +import java.util.Collections; + +import me.libraryaddict.disguise.DisguiseAPI; +import me.libraryaddict.disguise.DisguiseTypes.Disguise; +import me.libraryaddict.disguise.DisguiseTypes.DisguiseType; +import me.libraryaddict.disguise.DisguiseTypes.MiscDisguise; +import me.libraryaddict.disguise.DisguiseTypes.MobDisguise; +import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise; + +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class DisguisePlayerCommand implements CommandExecutor { + + private ArrayList allowedDisguises(CommandSender sender) { + ArrayList names = new ArrayList(); + for (DisguiseType type : DisguiseType.values()) { + String name = type.name().toLowerCase(); + if (sender.hasPermission("libsdisguises.disguise." + name)) + names.add(name); + } + Collections.sort(names); + return names; + } + + private ArrayList forbiddenDisguises(CommandSender sender) { + ArrayList names = new ArrayList(); + for (DisguiseType type : DisguiseType.values()) { + String name = type.name().toLowerCase(); + if (!sender.hasPermission("libsdisguises.disguise." + name)) + names.add(name); + } + Collections.sort(names); + return names; + } + + private boolean isNumeric(String string) { + try { + Integer.parseInt(string); + return true; + } catch (Exception ex) { + return false; + } + } + + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender.hasPermission("libsdisguises.disguiseothers")) { + if (args.length == 0) { + ArrayList names = allowedDisguises(sender); + ArrayList otherNames = forbiddenDisguises(sender); + if (names.size() > 0) { + sender.sendMessage(ChatColor.RED + "You can use the disguises: " + ChatColor.GREEN + + StringUtils.join(names, ChatColor.RED + ", " + ChatColor.GREEN)); + if (otherNames.size() > 0) { + sender.sendMessage(ChatColor.RED + "Other disguises: " + ChatColor.GREEN + + StringUtils.join(names, ChatColor.RED + ", " + ChatColor.GREEN)); + } + } else + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + } else { + if (args.length == 1) { + Player p = Bukkit.getPlayer(args[0]); + if (p != null) { + if (args[1].equalsIgnoreCase("undiguise") || args[1].equalsIgnoreCase("undis") + || args[1].equalsIgnoreCase("un")) { + if (sender.hasPermission("libsdisguises.undisguise")) { + if (DisguiseAPI.isDisguised(p.getName())) { + DisguiseAPI.undisguiseToAll(p); + sender.sendMessage(ChatColor.RED + "You are no longer disguised"); + } else + sender.sendMessage(ChatColor.RED + "You are not disguised!"); + } else + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + } else if (args[1].equalsIgnoreCase("player")) { + if (sender.hasPermission("libsdisguises.disguise.player")) { + if (args.length > 2) { + String name = ChatColor.translateAlternateColorCodes('&', StringUtils.join(args, " ") + .substring(args[0].length() + args[1].length() + 2)); + PlayerDisguise disguise = new PlayerDisguise(name); + DisguiseAPI.disguiseToAll(p, disguise); + sender.sendMessage(ChatColor.RED + "Disguised as the player '" + ChatColor.GREEN + name + + ChatColor.RESET + ChatColor.RED + "'"); + } else + sender.sendMessage(ChatColor.RED + "You need to provide a player name"); + } else { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command"); + } + + } else { + if (allowedDisguises(sender).size() == 0) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + DisguiseType type; + try { + type = DisguiseType.valueOf(args[1].toUpperCase()); + } catch (Exception ex) { + sender.sendMessage(ChatColor.RED + "Failed to find disguise: " + ChatColor.GREEN + args[1] + + "\n/disguise player \n/disguise \n/disguise undisguise/un/undis"); + return true; + } + if (sender.hasPermission("libsdisguises.disguise." + type.name().toLowerCase())) { + Object args1 = true; + if (type.isMisc()) + args1 = -1; + int args2 = -1; + if (args.length > 2) { + if (type.isMob()) { + if (args[2].equalsIgnoreCase("true")) { + args1 = false; + } else if (!args[2].equalsIgnoreCase("false")) { + sender.sendMessage(ChatColor.RED + "Set baby: " + ChatColor.GREEN + args[2] + + ChatColor.RED + " - Thats not true or false.."); + return true; + } + } else if (type.isMisc()) { + if (isNumeric(args[2])) { + args1 = Integer.parseInt(args[2]); + } else { + sender.sendMessage(ChatColor.RED + args[2] + " is not a number"); + return true; + } + if (args.length > 3) + if (isNumeric(args[3])) { + args2 = Integer.parseInt(args[3]); + } else { + sender.sendMessage(ChatColor.RED + args[3] + " is not a number"); + return true; + } + } + } + Disguise disguise; + if (type.isMob()) + disguise = new MobDisguise(type, (Boolean) args1); + else + disguise = new MiscDisguise(type, (Integer) args1, args2); + DisguiseAPI.disguiseToAll(p, disguise); + sender.sendMessage(ChatColor.RED + "Now disguised as a " + type.name().toLowerCase() + "!"); + } else + sender.sendMessage(ChatColor.RED + "You do not have permission to use this disguise."); + } + } else + sender.sendMessage(ChatColor.RED + "Player not found"); + } else + sender.sendMessage(ChatColor.RED + "/disguiseplayer "); + } + } else + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command"); + return true; + } +} diff --git a/src/me/libraryaddict/disguise/UndisguiseCommand.java b/src/me/libraryaddict/disguise/Commands/UndisguiseCommand.java similarity index 90% rename from src/me/libraryaddict/disguise/UndisguiseCommand.java rename to src/me/libraryaddict/disguise/Commands/UndisguiseCommand.java index b143cb62..eebcc831 100644 --- a/src/me/libraryaddict/disguise/UndisguiseCommand.java +++ b/src/me/libraryaddict/disguise/Commands/UndisguiseCommand.java @@ -1,4 +1,6 @@ -package me.libraryaddict.disguise; +package me.libraryaddict.disguise.Commands; + +import me.libraryaddict.disguise.DisguiseAPI; import org.bukkit.ChatColor; import org.bukkit.command.Command; diff --git a/src/me/libraryaddict/disguise/Commands/UndisguisePlayerCommand.java b/src/me/libraryaddict/disguise/Commands/UndisguisePlayerCommand.java new file mode 100644 index 00000000..fd775dea --- /dev/null +++ b/src/me/libraryaddict/disguise/Commands/UndisguisePlayerCommand.java @@ -0,0 +1,32 @@ +package me.libraryaddict.disguise.Commands; + +import me.libraryaddict.disguise.DisguiseAPI; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class UndisguisePlayerCommand implements CommandExecutor { + + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender.hasPermission("libsdisguises.undisguiseothers")) { + if (args.length > 0) { + Player p = Bukkit.getPlayer(args[0]); + if (p != null) { + if (DisguiseAPI.isDisguised(p.getName())) { + DisguiseAPI.undisguiseToAll(p); + sender.sendMessage(ChatColor.RED + "He is no longer disguised"); + } else + sender.sendMessage(ChatColor.RED + "He is not disguised!"); + } + sender.sendMessage(ChatColor.RED + "Player not found"); + } else + sender.sendMessage(ChatColor.RED + "/undisguiseplayer "); + } else + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } +} diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index 19d7f7dd..6207e06d 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -2,6 +2,10 @@ package me.libraryaddict.disguise; import java.util.List; +import me.libraryaddict.disguise.Commands.DisguiseCommand; +import me.libraryaddict.disguise.Commands.DisguisePlayerCommand; +import me.libraryaddict.disguise.Commands.UndisguiseCommand; +import me.libraryaddict.disguise.Commands.UndisguisePlayerCommand; import me.libraryaddict.disguise.DisguiseTypes.Disguise; import me.libraryaddict.disguise.DisguiseTypes.DisguiseType; import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise; @@ -107,5 +111,7 @@ public class LibsDisguises extends JavaPlugin { }); getCommand("disguise").setExecutor(new DisguiseCommand()); getCommand("undisguise").setExecutor(new UndisguiseCommand()); + getCommand("disguiseplayer").setExecutor(new DisguisePlayerCommand()); + getCommand("undisguiseplayer").setExecutor(new UndisguisePlayerCommand()); } } \ No newline at end of file