From 4262221133190de0c4fcf9ba21b3995752dd7b42 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Mon, 2 Jun 2014 04:08:54 +1200 Subject: [PATCH] Add Disguise Clone command to clone a entity to look like them --- plugin.yml | 4 +- .../disguise/DisguiseListener.java | 68 +++++++++++++++---- .../libraryaddict/disguise/LibsDisguises.java | 1 + .../commands/DisguiseCloneCommand.java | 64 +++++++++++++++++ .../commands/DisguiseEntityCommand.java | 2 +- .../commands/UndisguiseEntityCommand.java | 4 +- 6 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 src/me/libraryaddict/disguise/commands/DisguiseCloneCommand.java diff --git a/plugin.yml b/plugin.yml index 7cb76fa8..e4a1b910 100644 --- a/plugin.yml +++ b/plugin.yml @@ -22,4 +22,6 @@ commands: undisguiseplayer: aliases: [undisplayer, undplayer] undisguiseradius: - aliases: [undisradius, undradius] \ No newline at end of file + aliases: [undisradius, undradius] + disguiseclone: + aliases: [disguisec, disc, disclone, dclone, clonedisguise, clonedis, cdisguise, cdis] \ No newline at end of file diff --git a/src/me/libraryaddict/disguise/DisguiseListener.java b/src/me/libraryaddict/disguise/DisguiseListener.java index 48375408..05a5a83a 100644 --- a/src/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/me/libraryaddict/disguise/DisguiseListener.java @@ -30,8 +30,9 @@ import org.bukkit.scheduler.BukkitRunnable; public class DisguiseListener implements Listener { private String currentVersion; + private HashMap disguiseClone = new HashMap(); + private HashMap disguiseEntity = new HashMap(); private HashMap disguiseRunnable = new HashMap(); - private HashMap disguiseSlap = new HashMap(); private String latestVersion; private LibsDisguises plugin; private String updateMessage = ChatColor.RED + "[LibsDisguises] " + ChatColor.DARK_RED @@ -120,10 +121,9 @@ public class DisguiseListener implements Listener { @EventHandler public void onRightClick(PlayerInteractEntityEvent event) { - if (disguiseSlap.containsKey(event.getPlayer().getName())) { + if (disguiseEntity.containsKey(event.getPlayer().getName()) || disguiseClone.containsKey(event.getPlayer().getName())) { Player p = event.getPlayer(); event.setCancelled(true); - Disguise disguise = disguiseSlap.remove(p.getName()); disguiseRunnable.remove(p.getName()).cancel(); Entity entity = event.getRightClicked(); String entityName = ""; @@ -138,21 +138,36 @@ public class DisguiseListener implements Listener { } } } + Disguise disguise = null; + Entity disguiseTarget = null; + if (disguiseClone.containsKey(p.getName())) { + Boolean[] options = disguiseClone.remove(p.getName()); + disguiseTarget = p; + disguise = DisguiseAPI.getDisguise(p, entity); + if (disguise == null) { + disguise = DisguiseAPI.constructDisguise(entity, options[0], options[1], options[2]); + } else { + disguise = disguise.clone(); + } + } else if (disguiseEntity.containsKey(p.getName())) { + disguiseTarget = entity; + disguise = disguiseEntity.remove(p.getName()); + } if (disguise != null) { if (disguise.isMiscDisguise() && !DisguiseConfig.isMiscDisguisesForLivingEnabled() - && entity instanceof LivingEntity) { + && disguiseTarget instanceof LivingEntity) { p.sendMessage(ChatColor.RED + "Can't disguise a living entity as a misc disguise. This has been disabled in the config!"); } else { - if (entity instanceof Player && DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { + if (disguiseTarget instanceof Player && DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (disguise.getWatcher() instanceof LivingWatcher) { - ((LivingWatcher) disguise.getWatcher()).setCustomName(((Player) entity).getDisplayName()); + ((LivingWatcher) disguise.getWatcher()).setCustomName(((Player) disguiseTarget).getDisplayName()); if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { ((LivingWatcher) disguise.getWatcher()).setCustomNameVisible(true); } } } - DisguiseAPI.disguiseToAll(entity, disguise); + DisguiseAPI.disguiseToAll(disguiseTarget, disguise); String disguiseName = "a "; if (disguise instanceof PlayerDisguise) { disguiseName = "the player " + ((PlayerDisguise) disguise).getName(); @@ -165,8 +180,13 @@ public class DisguiseListener implements Listener { } } } - p.sendMessage(ChatColor.RED + "Disguised " + (entity instanceof Player ? "" : "the ") + entityName + " as " - + disguiseName + "!"); + if (disguiseTarget == p) { + p.sendMessage(ChatColor.RED + "Disguised yourself" + " as " + (entity instanceof Player ? "" : "a ") + + entityName + "!"); + } else { + p.sendMessage(ChatColor.RED + "Disguised " + (entity instanceof Player ? "" : "the ") + entityName + + " as " + disguiseName + "!"); + } } } else { if (DisguiseAPI.isDisguised(entity)) { @@ -222,20 +242,38 @@ public class DisguiseListener implements Listener { } } - public void setSlap(final String player, Disguise disguise) { - if (disguiseSlap.containsKey(player)) { - disguiseSlap.remove(player); - disguiseRunnable.remove(player).cancel(); + public void setDisguiseClone(final String player, Boolean[] options) { + if (disguiseRunnable.containsKey(player)) { + BukkitRunnable run = disguiseRunnable.remove(player); + run.cancel(); + run.run(); } BukkitRunnable runnable = new BukkitRunnable() { public void run() { - disguiseSlap.remove(player); + disguiseClone.remove(player); disguiseRunnable.remove(player); } }; runnable.runTaskLater(plugin, 20 * 10); disguiseRunnable.put(player, runnable); - disguiseSlap.put(player, disguise); + disguiseClone.put(player, options); + } + + public void setDisguiseEntity(final String player, Disguise disguise) { + if (disguiseRunnable.containsKey(player)) { + BukkitRunnable run = disguiseRunnable.remove(player); + run.cancel(); + run.run(); + } + BukkitRunnable runnable = new BukkitRunnable() { + public void run() { + disguiseEntity.remove(player); + disguiseRunnable.remove(player); + } + }; + runnable.runTaskLater(plugin, 20 * 10); + disguiseRunnable.put(player, runnable); + disguiseEntity.put(player, disguise); } } diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index 8f1d94a3..80ad8e95 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -138,6 +138,7 @@ public class LibsDisguises extends JavaPlugin { getCommand("disguiseradius").setExecutor(new DisguiseRadiusCommand(getConfig().getInt("DisguiseRadiusMax"))); getCommand("undisguiseradius").setExecutor(new UndisguiseRadiusCommand(getConfig().getInt("UndisguiseRadiusMax"))); getCommand("disguisehelp").setExecutor(new DisguiseHelpCommand()); + getCommand("disguiseclone").setExecutor(new DisguiseCloneCommand(listener)); getCommand("libsdisguises").setExecutor(new LibsDisguisesCommand()); registerValues(); } diff --git a/src/me/libraryaddict/disguise/commands/DisguiseCloneCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseCloneCommand.java new file mode 100644 index 00000000..e1adeb36 --- /dev/null +++ b/src/me/libraryaddict/disguise/commands/DisguiseCloneCommand.java @@ -0,0 +1,64 @@ +package me.libraryaddict.disguise.commands; + +import me.libraryaddict.disguise.DisguiseListener; +import me.libraryaddict.disguise.utilities.BaseDisguiseCommand; + +import org.apache.commons.lang.StringUtils; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; + +public class DisguiseCloneCommand extends BaseDisguiseCommand { + + private DisguiseListener listener; + + public DisguiseCloneCommand(DisguiseListener listener) { + this.listener = listener; + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (sender.getName().equals("CONSOLE")) { + sender.sendMessage(ChatColor.RED + "You may not use this command from the console!"); + return true; + } + if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) { + boolean doEnquipment = true; + boolean doSneak = false; + boolean doSprint = false; + for (String option : args) { + if (StringUtils.startsWithIgnoreCase(option, "ignoreEnquip")) { + doEnquipment = false; + } else if (option.equalsIgnoreCase("doSneakSprint")) { + doSneak = true; + doSprint = true; + } else if (option.equalsIgnoreCase("doSneak")) { + doSneak = true; + } else if (option.equalsIgnoreCase("doSprint")) { + doSprint = true; + } else { + sender.sendMessage(ChatColor.DARK_RED + "Unknown option '" + option + + "' - Valid options are 'IgnoreEnquipment' 'DoSneakSprint' 'DoSneak' 'DoSprint'"); + return true; + } + } + listener.setDisguiseClone(sender.getName(), new Boolean[] { doEnquipment, doSneak, doSprint }); + sender.sendMessage(ChatColor.RED + "Right click a entity in the next 10 seconds to disguise as it!"); + } else { + sender.sendMessage(ChatColor.RED + "There was once a curious little boy," + + " he liked to try out these strange sounding commands." + + " One day, that curious little boy tried out the wrong command and was set on fire!" + " Aka."); + sender.sendMessage(ChatColor.DARK_RED + "You are forbidden to use this command."); + } + return true; + } + + /** + * Send the player the information + */ + protected void sendCommandUsage(CommandSender sender) { + sender.sendMessage(ChatColor.DARK_GREEN + "Disguise as the entity you right click! Or as their disguise!"); + sender.sendMessage(ChatColor.DARK_GREEN + "/disguiseclone IgnoreEnquipment" + ChatColor.DARK_GREEN + "(" + + ChatColor.GREEN + "Optional" + ChatColor.DARK_GREEN + ")"); + } +} diff --git a/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java index 04ed37fd..776fef1a 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseEntityCommand.java @@ -35,7 +35,7 @@ public class DisguiseEntityCommand extends BaseDisguiseCommand { } return true; } - listener.setSlap(sender.getName(), disguise); + listener.setDisguiseEntity(sender.getName(), disguise); sender.sendMessage(ChatColor.RED + "Right click a entity in the next 10 seconds to disguise it as a " + disguise.getType().toReadable() + "!"); return true; diff --git a/src/me/libraryaddict/disguise/commands/UndisguiseEntityCommand.java b/src/me/libraryaddict/disguise/commands/UndisguiseEntityCommand.java index ea4955f7..9807b7c7 100644 --- a/src/me/libraryaddict/disguise/commands/UndisguiseEntityCommand.java +++ b/src/me/libraryaddict/disguise/commands/UndisguiseEntityCommand.java @@ -21,10 +21,10 @@ public class UndisguiseEntityCommand implements CommandExecutor { return true; } if (sender.hasPermission("libsdisguises.undisguiseentity")) { - listener.setSlap(sender.getName(), null); + listener.setDisguiseEntity(sender.getName(), null); sender.sendMessage(ChatColor.RED + "Right click a disguised entity to undisguise them!"); } else - sender.sendMessage(ChatColor.RED + "You are forbidden to use this command!"); + sender.sendMessage(ChatColor.RED + "You are forbidden to use this command."); return true; } }