diff --git a/src/me/libraryaddict/disguise/DisguiseConfig.java b/src/me/libraryaddict/disguise/DisguiseConfig.java index de821698..b301e73e 100644 --- a/src/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/me/libraryaddict/disguise/DisguiseConfig.java @@ -83,6 +83,7 @@ public class DisguiseConfig { setDisguiseEntityExpire(config.getInt("DisguiseEntityExpire")); setDisguiseCloneExpire(config.getInt("DisguiseCloneExpire")); setMaxClonedDisguises(config.getInt("DisguiseCloneSize")); + setUnusedDisguisesRemoved(config.getBoolean("RemoveUnusedDisguises")); } public static boolean isAnimationPacketsEnabled() { diff --git a/src/me/libraryaddict/disguise/DisguiseListener.java b/src/me/libraryaddict/disguise/DisguiseListener.java index 4cd5e728..00c9faa0 100644 --- a/src/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/me/libraryaddict/disguise/DisguiseListener.java @@ -105,7 +105,8 @@ public class DisguiseListener implements Listener { public void onQuit(PlayerQuitEvent event) { if (DisguiseConfig.isUnusedDisguisesRemoved()) { for (TargetedDisguise disguise : DisguiseUtilities.getSeenDisguises(event.getPlayer().getName())) { - disguise.removeDisguise(); + // TODO fix + // disguise.removeDisguise(); } } } diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index dad9c1e6..1ae3041f 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -68,20 +68,8 @@ public class LibsDisguises extends JavaPlugin { } catch (IOException e) { e.printStackTrace(); } finally { - try { - if (stream != null) { - stream.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - try { - if (reader != null) { - reader.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } + tryClose(stream); + tryClose(reader); } PacketsManager.init(this); @@ -149,20 +137,8 @@ public class LibsDisguises extends JavaPlugin { } catch (Exception ex) { ex.printStackTrace(); } finally { - try { - if (input != null) { - input.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - try { - if (reader != null) { - reader.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } + tryClose(input); + tryClose(reader); } return toWrite; } @@ -314,9 +290,19 @@ public class LibsDisguises extends JavaPlugin { private String toReadable(String string) { StringBuilder builder = new StringBuilder(); for (String s : string.split("_")) { - builder.append(s.substring(0, 1) + s.substring(1).toLowerCase()); + builder.append(s.substring(0, 1)).append(s.substring(1).toLowerCase()); } return builder.toString(); } + private void tryClose(Closeable input) { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } diff --git a/src/me/libraryaddict/disguise/commands/DisguiseCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseCommand.java index 0c13dda6..be583cfb 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseCommand.java @@ -22,7 +22,7 @@ public class DisguiseCommand extends BaseDisguiseCommand { sender.sendMessage(ChatColor.RED + "You may not use this command from the console!"); return true; } - Disguise disguise = null; + Disguise disguise; try { disguise = parseDisguise(sender, args); } catch (Exception ex) { diff --git a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java index 720fb188..998a5b5a 100644 --- a/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java @@ -38,9 +38,7 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand { return true; } String[] newArgs = new String[args.length - 1]; - for (int i = 0; i < newArgs.length; i++) { - newArgs[i] = args[i + 1]; - } + System.arraycopy(args, 1, newArgs, 0, newArgs.length); Disguise disguise; try { disguise = parseDisguise(sender, newArgs); @@ -59,7 +57,7 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand { } if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (disguise.getWatcher() instanceof LivingWatcher) { - ((LivingWatcher) disguise.getWatcher()).setCustomName(((Player) player).getDisplayName()); + ((LivingWatcher) disguise.getWatcher()).setCustomName(player.getDisplayName()); if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { ((LivingWatcher) disguise.getWatcher()).setCustomNameVisible(true); } diff --git a/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java b/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java index 54dc3fc6..52b8f44a 100644 --- a/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java +++ b/src/me/libraryaddict/disguise/commands/DisguiseRadiusCommand.java @@ -86,9 +86,7 @@ public class DisguiseRadiusCommand extends BaseDisguiseCommand { radius = maxRadius; } String[] newArgs = new String[args.length - (starting + 1)]; - for (int i = 0; i < newArgs.length; i++) { - newArgs[i] = args[i + (starting + 1)]; - } + System.arraycopy(args, starting + 1, newArgs, 0, newArgs.length); Disguise disguise; try { disguise = parseDisguise(sender, newArgs); diff --git a/src/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/me/libraryaddict/disguise/disguisetypes/Disguise.java index cf6b01aa..b4167941 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -181,6 +181,7 @@ public abstract class Disguise { private int deadTicks = 0; private int refreshDisguise = 0; + // TODO refactor public void run() { // If entity is no longer valid. Remove it. if (!getEntity().isValid()) { diff --git a/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index 52519e1b..352c96e1 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -70,6 +70,7 @@ public class FlagWatcher { try { cloned = getClass().getConstructor(Disguise.class).newInstance(owningDisguise); } catch (Exception e) { + // TODO: This will throw NPE e.printStackTrace(); } cloned.entityValues = (HashMap) entityValues.clone(); @@ -152,9 +153,7 @@ public class FlagWatcher { public ItemStack[] getArmor() { ItemStack[] armor = new ItemStack[4]; - for (int i = 0; i < 4; i++) { - armor[i] = items[i]; - } + System.arraycopy(items, 0, armor, 0, 4); return armor; } @@ -305,11 +304,11 @@ public class FlagWatcher { if (itemStack == null) { // Find the item to replace it with if (getDisguise().getEntity() instanceof LivingEntity) { - EntityEquipment enquipment = ((LivingEntity) getDisguise().getEntity()).getEquipment(); + EntityEquipment equipment = ((LivingEntity) getDisguise().getEntity()).getEquipment(); if (slot == 4) { - itemStack = enquipment.getItemInHand(); + itemStack = equipment.getItemInHand(); } else { - itemStack = enquipment.getArmorContents()[slot]; + itemStack = equipment.getArmorContents()[slot]; } if (itemStack != null && itemStack.getTypeId() == 0) { itemStack = null; diff --git a/src/me/libraryaddict/disguise/disguisetypes/MobDisguise.java b/src/me/libraryaddict/disguise/disguisetypes/MobDisguise.java index 9311e467..faa7d390 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/MobDisguise.java +++ b/src/me/libraryaddict/disguise/disguisetypes/MobDisguise.java @@ -62,11 +62,8 @@ public class MobDisguise extends TargetedDisguise { } public boolean doesDisguiseAge() { - if (getWatcher() != null) { - return getWatcher() instanceof AgeableWatcher || getWatcher() instanceof ZombieWatcher; - } - return false; - + return getWatcher() != null && + (getWatcher() instanceof AgeableWatcher || getWatcher() instanceof ZombieWatcher); } public boolean isAdult() { diff --git a/src/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java b/src/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java index 483e6bc5..6fb61f8e 100644 --- a/src/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java +++ b/src/me/libraryaddict/disguise/utilities/BaseDisguiseCommand.java @@ -265,9 +265,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { } // Copy strings to their new range String[] newArgs = new String[args.length - toSkip]; - for (int i = toSkip; i < args.length; i++) { - newArgs[i - toSkip] = args[i]; - } + System.arraycopy(args, toSkip, newArgs, 0, args.length - toSkip); args = newArgs; for (int i = 0; i < args.length; i += 2) { String methodName = args[i]; @@ -350,7 +348,6 @@ public abstract class BaseDisguiseCommand implements CommandExecutor { // Parse to horse color } else if (param.getSimpleName().equals("Color")) { try { - value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase()); } catch (Exception ex) { throw parseToException("a horse color", valueString, methodName); diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 970936dd..8997fc47 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -30,7 +30,6 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.Zombie; import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; import org.bukkit.util.Vector; import com.comphenix.protocol.PacketType; @@ -175,8 +174,7 @@ public class DisguiseUtilities { } /** - * @param Sends - * entity removal packets, as this disguise was removed + * Sends entity removal packets, as this disguise was removed */ public static void destroyEntity(TargetedDisguise disguise) { try { @@ -217,7 +215,7 @@ public class DisguiseUtilities { disguiseBox = disguiseValues.getBabyBox(); } } - ReflectionManager.setBoundingBox(entity, disguiseBox, disguiseValues.getEntitySize()); + ReflectionManager.setBoundingBox(entity, disguiseBox); } else { DisguiseValues entityValues = DisguiseValues.getDisguiseValues(DisguiseType.getType(entity.getType())); FakeBoundingBox entityBox = entityValues.getAdultBox(); @@ -227,7 +225,7 @@ public class DisguiseUtilities { entityBox = entityValues.getBabyBox(); } } - ReflectionManager.setBoundingBox(entity, entityBox, entityValues.getEntitySize()); + ReflectionManager.setBoundingBox(entity, entityBox); } } } @@ -327,7 +325,7 @@ public class DisguiseUtilities { public void onLookup(Object gameProfile) { getAddedByPlugins().remove(disguise.getName()); if (DisguiseAPI.isDisguiseInUse(disguise)) { - DisguiseUtilities.refreshTrackers((TargetedDisguise) disguise); + DisguiseUtilities.refreshTrackers(disguise); if (disguise.getEntity() instanceof Player && disguise.isSelfDisguiseVisible()) { DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise); } @@ -445,11 +443,8 @@ public class DisguiseUtilities { } public static boolean isDisguiseInUse(Disguise disguise) { - if (disguise.getEntity() != null && getDisguises().containsKey(disguise.getEntity().getUniqueId()) - && getDisguises().get(disguise.getEntity().getUniqueId()).contains(disguise)) { - return true; - } - return false; + return disguise.getEntity() != null && getDisguises().containsKey(disguise.getEntity().getUniqueId()) + && getDisguises().get(disguise.getEntity().getUniqueId()).contains(disguise); } /** @@ -461,8 +456,7 @@ public class DisguiseUtilities { } /** - * @param Resends - * the entity to this specific player + * Resends the entity to this specific player */ public static void refreshTracker(TargetedDisguise disguise, String player) { if (disguise.getEntity() != null && disguise.getEntity().isValid()) { @@ -495,8 +489,7 @@ public class DisguiseUtilities { } /** - * @param A - * convidence method for me to refresh trackers in other plugins + * A convenience method for me to refresh trackers in other plugins */ public static void refreshTrackers(Entity entity) { if (entity.isValid()) { @@ -532,8 +525,7 @@ public class DisguiseUtilities { } /** - * @param Resends - * the entity to all the watching players, which is where the magic begins + * Resends the entity to all the watching players, which is where the magic begins */ public static void refreshTrackers(TargetedDisguise disguise) { try { @@ -728,9 +720,7 @@ public class DisguiseUtilities { } // Resend any active potion effects - Iterator iterator = player.getActivePotionEffects().iterator(); - while (iterator.hasNext()) { - PotionEffect potionEffect = (PotionEffect) iterator.next(); + for (Object potionEffect : player.getActivePotionEffects()) { sendSelfPacket(player, manager.createPacketConstructor(PacketType.Play.Server.ENTITY_EFFECT, player.getEntityId(), potionEffect) .createPacket(player.getEntityId(), potionEffect), fakeId); diff --git a/src/me/libraryaddict/disguise/utilities/PacketsManager.java b/src/me/libraryaddict/disguise/utilities/PacketsManager.java index ff95181c..f5a71ddf 100644 --- a/src/me/libraryaddict/disguise/utilities/PacketsManager.java +++ b/src/me/libraryaddict/disguise/utilities/PacketsManager.java @@ -94,7 +94,7 @@ public class PacketsManager { /** * Construct the packets I need to spawn in the disguise */ - public static PacketContainer[] constructSpawnPackets(Disguise disguise, Entity disguisedEntity, Player observer) { + public static PacketContainer[] constructSpawnPackets(Disguise disguise, Entity disguisedEntity) { if (disguise.getEntity() == null) disguise.setEntity(disguisedEntity); Object nmsEntity = ReflectionManager.getNmsEntity(disguisedEntity); @@ -179,7 +179,7 @@ public class PacketsManager { stringMods.write(i, ((PlayerDisguise) disguise).getName()); } } else { - Object gameProfile = null; + Object gameProfile; String name = ((PlayerDisguise) disguise).getName(); boolean removeName = false; if (!DisguiseUtilities.hasGameProfile(name)) { @@ -529,8 +529,9 @@ public class PacketsManager { if (disguise.isSoundsReplaced()) { String sound = null; DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name()); - if (dSound != null && soundType != null) + if (dSound != null) sound = dSound.getSound(soundType); + if (sound == null) { event.setCancelled(true); } else { @@ -563,7 +564,7 @@ public class PacketsManager { if (soundType == SoundType.HURT || soundType == SoundType.DEATH || soundType == SoundType.IDLE) { // If the volume is the default - if (((Float) mods.read(4)).equals(entitySound.getDamageAndIdleSoundVolume())) { + if (mods.read(4).equals(entitySound.getDamageAndIdleSoundVolume())) { mods.write(4, dSound.getDamageAndIdleSoundVolume()); } // Here I assume its the default pitch as I can't calculate if its real. @@ -702,8 +703,7 @@ public class PacketsManager { if (packets == null) { packets = new PacketContainer[] { event.getPacket() }; } - for (int i = 0; i < packets.length; i++) { - PacketContainer packet = packets[i]; + for (PacketContainer packet : packets) { if (packet.equals(event.getPacket())) { packet = packet.deepClone(); } @@ -716,10 +716,7 @@ public class PacketsManager { } if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) { event.setPacket(event.getPacket().deepClone()); - Iterator itel = event.getPacket().getWatchableCollectionModifier().read(0) - .iterator(); - while (itel.hasNext()) { - WrappedWatchableObject watch = itel.next(); + for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) { if (watch.getIndex() == 0) { byte b = (Byte) watch.getValue(); byte a = (byte) (b | 1 << 5); @@ -1231,7 +1228,7 @@ public class PacketsManager { || sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY_EXPERIENCE_ORB || sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY || sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY_PAINTING) { - packets = constructSpawnPackets(disguise, entity, observer); + packets = constructSpawnPackets(disguise, entity); } // Else if the disguise is attempting to send players a forbidden packet diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index 2709db8b..b736ea05 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -151,8 +151,7 @@ public class ReflectionManager { public static Entity getBukkitEntity(Object nmsEntity) { try { - Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity); - return bukkitEntity; + return (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity); } catch (Exception ex) { ex.printStackTrace(); } @@ -385,7 +384,7 @@ public class ReflectionManager { } } - public static void setBoundingBox(Entity entity, FakeBoundingBox newBox, float[] entitySize) { + public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) { try { Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity)); int stage = 0;