Merge pull request #28 from riking/refactoring

Fix IntelliJ warnings
This commit is contained in:
libraryaddict 2014-06-04 13:43:06 +12:00
commit cbbf5c8f2e
13 changed files with 51 additions and 87 deletions

@ -83,6 +83,7 @@ public class DisguiseConfig {
setDisguiseEntityExpire(config.getInt("DisguiseEntityExpire")); setDisguiseEntityExpire(config.getInt("DisguiseEntityExpire"));
setDisguiseCloneExpire(config.getInt("DisguiseCloneExpire")); setDisguiseCloneExpire(config.getInt("DisguiseCloneExpire"));
setMaxClonedDisguises(config.getInt("DisguiseCloneSize")); setMaxClonedDisguises(config.getInt("DisguiseCloneSize"));
setUnusedDisguisesRemoved(config.getBoolean("RemoveUnusedDisguises"));
} }
public static boolean isAnimationPacketsEnabled() { public static boolean isAnimationPacketsEnabled() {

@ -105,7 +105,8 @@ public class DisguiseListener implements Listener {
public void onQuit(PlayerQuitEvent event) { public void onQuit(PlayerQuitEvent event) {
if (DisguiseConfig.isUnusedDisguisesRemoved()) { if (DisguiseConfig.isUnusedDisguisesRemoved()) {
for (TargetedDisguise disguise : DisguiseUtilities.getSeenDisguises(event.getPlayer().getName())) { for (TargetedDisguise disguise : DisguiseUtilities.getSeenDisguises(event.getPlayer().getName())) {
disguise.removeDisguise(); // TODO fix
// disguise.removeDisguise();
} }
} }
} }

@ -68,20 +68,8 @@ public class LibsDisguises extends JavaPlugin {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { tryClose(stream);
if (stream != null) { tryClose(reader);
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
PacketsManager.init(this); PacketsManager.init(this);
@ -149,20 +137,8 @@ public class LibsDisguises extends JavaPlugin {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} finally { } finally {
try { tryClose(input);
if (input != null) { tryClose(reader);
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
return toWrite; return toWrite;
} }
@ -314,9 +290,19 @@ public class LibsDisguises extends JavaPlugin {
private String toReadable(String string) { private String toReadable(String string) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (String s : string.split("_")) { 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(); return builder.toString();
} }
private void tryClose(Closeable input) {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }

@ -22,7 +22,7 @@ public class DisguiseCommand extends BaseDisguiseCommand {
sender.sendMessage(ChatColor.RED + "You may not use this command from the console!"); sender.sendMessage(ChatColor.RED + "You may not use this command from the console!");
return true; return true;
} }
Disguise disguise = null; Disguise disguise;
try { try {
disguise = parseDisguise(sender, args); disguise = parseDisguise(sender, args);
} catch (Exception ex) { } catch (Exception ex) {

@ -38,9 +38,7 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand {
return true; return true;
} }
String[] newArgs = new String[args.length - 1]; String[] newArgs = new String[args.length - 1];
for (int i = 0; i < newArgs.length; i++) { System.arraycopy(args, 1, newArgs, 0, newArgs.length);
newArgs[i] = args[i + 1];
}
Disguise disguise; Disguise disguise;
try { try {
disguise = parseDisguise(sender, newArgs); disguise = parseDisguise(sender, newArgs);
@ -59,7 +57,7 @@ public class DisguisePlayerCommand extends BaseDisguiseCommand {
} }
if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) {
if (disguise.getWatcher() instanceof LivingWatcher) { if (disguise.getWatcher() instanceof LivingWatcher) {
((LivingWatcher) disguise.getWatcher()).setCustomName(((Player) player).getDisplayName()); ((LivingWatcher) disguise.getWatcher()).setCustomName(player.getDisplayName());
if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) {
((LivingWatcher) disguise.getWatcher()).setCustomNameVisible(true); ((LivingWatcher) disguise.getWatcher()).setCustomNameVisible(true);
} }

@ -86,9 +86,7 @@ public class DisguiseRadiusCommand extends BaseDisguiseCommand {
radius = maxRadius; radius = maxRadius;
} }
String[] newArgs = new String[args.length - (starting + 1)]; String[] newArgs = new String[args.length - (starting + 1)];
for (int i = 0; i < newArgs.length; i++) { System.arraycopy(args, starting + 1, newArgs, 0, newArgs.length);
newArgs[i] = args[i + (starting + 1)];
}
Disguise disguise; Disguise disguise;
try { try {
disguise = parseDisguise(sender, newArgs); disguise = parseDisguise(sender, newArgs);

@ -181,6 +181,7 @@ public abstract class Disguise {
private int deadTicks = 0; private int deadTicks = 0;
private int refreshDisguise = 0; private int refreshDisguise = 0;
// TODO refactor
public void run() { public void run() {
// If entity is no longer valid. Remove it. // If entity is no longer valid. Remove it.
if (!getEntity().isValid()) { if (!getEntity().isValid()) {

@ -70,6 +70,7 @@ public class FlagWatcher {
try { try {
cloned = getClass().getConstructor(Disguise.class).newInstance(owningDisguise); cloned = getClass().getConstructor(Disguise.class).newInstance(owningDisguise);
} catch (Exception e) { } catch (Exception e) {
// TODO: This will throw NPE
e.printStackTrace(); e.printStackTrace();
} }
cloned.entityValues = (HashMap<Integer, Object>) entityValues.clone(); cloned.entityValues = (HashMap<Integer, Object>) entityValues.clone();
@ -152,9 +153,7 @@ public class FlagWatcher {
public ItemStack[] getArmor() { public ItemStack[] getArmor() {
ItemStack[] armor = new ItemStack[4]; ItemStack[] armor = new ItemStack[4];
for (int i = 0; i < 4; i++) { System.arraycopy(items, 0, armor, 0, 4);
armor[i] = items[i];
}
return armor; return armor;
} }
@ -305,11 +304,11 @@ public class FlagWatcher {
if (itemStack == null) { if (itemStack == null) {
// Find the item to replace it with // Find the item to replace it with
if (getDisguise().getEntity() instanceof LivingEntity) { if (getDisguise().getEntity() instanceof LivingEntity) {
EntityEquipment enquipment = ((LivingEntity) getDisguise().getEntity()).getEquipment(); EntityEquipment equipment = ((LivingEntity) getDisguise().getEntity()).getEquipment();
if (slot == 4) { if (slot == 4) {
itemStack = enquipment.getItemInHand(); itemStack = equipment.getItemInHand();
} else { } else {
itemStack = enquipment.getArmorContents()[slot]; itemStack = equipment.getArmorContents()[slot];
} }
if (itemStack != null && itemStack.getTypeId() == 0) { if (itemStack != null && itemStack.getTypeId() == 0) {
itemStack = null; itemStack = null;

@ -62,11 +62,8 @@ public class MobDisguise extends TargetedDisguise {
} }
public boolean doesDisguiseAge() { public boolean doesDisguiseAge() {
if (getWatcher() != null) { return getWatcher() != null &&
return getWatcher() instanceof AgeableWatcher || getWatcher() instanceof ZombieWatcher; (getWatcher() instanceof AgeableWatcher || getWatcher() instanceof ZombieWatcher);
}
return false;
} }
public boolean isAdult() { public boolean isAdult() {

@ -265,9 +265,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
} }
// Copy strings to their new range // Copy strings to their new range
String[] newArgs = new String[args.length - toSkip]; String[] newArgs = new String[args.length - toSkip];
for (int i = toSkip; i < args.length; i++) { System.arraycopy(args, toSkip, newArgs, 0, args.length - toSkip);
newArgs[i - toSkip] = args[i];
}
args = newArgs; args = newArgs;
for (int i = 0; i < args.length; i += 2) { for (int i = 0; i < args.length; i += 2) {
String methodName = args[i]; String methodName = args[i];
@ -350,7 +348,6 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
// Parse to horse color // Parse to horse color
} else if (param.getSimpleName().equals("Color")) { } else if (param.getSimpleName().equals("Color")) {
try { try {
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase()); value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
} catch (Exception ex) { } catch (Exception ex) {
throw parseToException("a horse color", valueString, methodName); throw parseToException("a horse color", valueString, methodName);

@ -30,7 +30,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie; import org.bukkit.entity.Zombie;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
@ -175,8 +174,7 @@ public class DisguiseUtilities {
} }
/** /**
* @param Sends * Sends entity removal packets, as this disguise was removed
* entity removal packets, as this disguise was removed
*/ */
public static void destroyEntity(TargetedDisguise disguise) { public static void destroyEntity(TargetedDisguise disguise) {
try { try {
@ -217,7 +215,7 @@ public class DisguiseUtilities {
disguiseBox = disguiseValues.getBabyBox(); disguiseBox = disguiseValues.getBabyBox();
} }
} }
ReflectionManager.setBoundingBox(entity, disguiseBox, disguiseValues.getEntitySize()); ReflectionManager.setBoundingBox(entity, disguiseBox);
} else { } else {
DisguiseValues entityValues = DisguiseValues.getDisguiseValues(DisguiseType.getType(entity.getType())); DisguiseValues entityValues = DisguiseValues.getDisguiseValues(DisguiseType.getType(entity.getType()));
FakeBoundingBox entityBox = entityValues.getAdultBox(); FakeBoundingBox entityBox = entityValues.getAdultBox();
@ -227,7 +225,7 @@ public class DisguiseUtilities {
entityBox = entityValues.getBabyBox(); 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) { public void onLookup(Object gameProfile) {
getAddedByPlugins().remove(disguise.getName()); getAddedByPlugins().remove(disguise.getName());
if (DisguiseAPI.isDisguiseInUse(disguise)) { if (DisguiseAPI.isDisguiseInUse(disguise)) {
DisguiseUtilities.refreshTrackers((TargetedDisguise) disguise); DisguiseUtilities.refreshTrackers(disguise);
if (disguise.getEntity() instanceof Player && disguise.isSelfDisguiseVisible()) { if (disguise.getEntity() instanceof Player && disguise.isSelfDisguiseVisible()) {
DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise); DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise);
} }
@ -445,11 +443,8 @@ public class DisguiseUtilities {
} }
public static boolean isDisguiseInUse(Disguise disguise) { public static boolean isDisguiseInUse(Disguise disguise) {
if (disguise.getEntity() != null && getDisguises().containsKey(disguise.getEntity().getUniqueId()) return disguise.getEntity() != null && getDisguises().containsKey(disguise.getEntity().getUniqueId())
&& getDisguises().get(disguise.getEntity().getUniqueId()).contains(disguise)) { && getDisguises().get(disguise.getEntity().getUniqueId()).contains(disguise);
return true;
}
return false;
} }
/** /**
@ -461,8 +456,7 @@ public class DisguiseUtilities {
} }
/** /**
* @param Resends * Resends the entity to this specific player
* the entity to this specific player
*/ */
public static void refreshTracker(TargetedDisguise disguise, String player) { public static void refreshTracker(TargetedDisguise disguise, String player) {
if (disguise.getEntity() != null && disguise.getEntity().isValid()) { if (disguise.getEntity() != null && disguise.getEntity().isValid()) {
@ -495,8 +489,7 @@ public class DisguiseUtilities {
} }
/** /**
* @param A * A convenience method for me to refresh trackers in other plugins
* convidence method for me to refresh trackers in other plugins
*/ */
public static void refreshTrackers(Entity entity) { public static void refreshTrackers(Entity entity) {
if (entity.isValid()) { if (entity.isValid()) {
@ -532,8 +525,7 @@ public class DisguiseUtilities {
} }
/** /**
* @param Resends * Resends the entity to all the watching players, which is where the magic begins
* the entity to all the watching players, which is where the magic begins
*/ */
public static void refreshTrackers(TargetedDisguise disguise) { public static void refreshTrackers(TargetedDisguise disguise) {
try { try {
@ -728,9 +720,7 @@ public class DisguiseUtilities {
} }
// Resend any active potion effects // Resend any active potion effects
Iterator iterator = player.getActivePotionEffects().iterator(); for (Object potionEffect : player.getActivePotionEffects()) {
while (iterator.hasNext()) {
PotionEffect potionEffect = (PotionEffect) iterator.next();
sendSelfPacket(player, sendSelfPacket(player,
manager.createPacketConstructor(PacketType.Play.Server.ENTITY_EFFECT, player.getEntityId(), potionEffect) manager.createPacketConstructor(PacketType.Play.Server.ENTITY_EFFECT, player.getEntityId(), potionEffect)
.createPacket(player.getEntityId(), potionEffect), fakeId); .createPacket(player.getEntityId(), potionEffect), fakeId);

@ -94,7 +94,7 @@ public class PacketsManager {
/** /**
* Construct the packets I need to spawn in the disguise * 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) if (disguise.getEntity() == null)
disguise.setEntity(disguisedEntity); disguise.setEntity(disguisedEntity);
Object nmsEntity = ReflectionManager.getNmsEntity(disguisedEntity); Object nmsEntity = ReflectionManager.getNmsEntity(disguisedEntity);
@ -179,7 +179,7 @@ public class PacketsManager {
stringMods.write(i, ((PlayerDisguise) disguise).getName()); stringMods.write(i, ((PlayerDisguise) disguise).getName());
} }
} else { } else {
Object gameProfile = null; Object gameProfile;
String name = ((PlayerDisguise) disguise).getName(); String name = ((PlayerDisguise) disguise).getName();
boolean removeName = false; boolean removeName = false;
if (!DisguiseUtilities.hasGameProfile(name)) { if (!DisguiseUtilities.hasGameProfile(name)) {
@ -529,8 +529,9 @@ public class PacketsManager {
if (disguise.isSoundsReplaced()) { if (disguise.isSoundsReplaced()) {
String sound = null; String sound = null;
DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name()); DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name());
if (dSound != null && soundType != null) if (dSound != null)
sound = dSound.getSound(soundType); sound = dSound.getSound(soundType);
if (sound == null) { if (sound == null) {
event.setCancelled(true); event.setCancelled(true);
} else { } else {
@ -563,7 +564,7 @@ public class PacketsManager {
if (soundType == SoundType.HURT || soundType == SoundType.DEATH if (soundType == SoundType.HURT || soundType == SoundType.DEATH
|| soundType == SoundType.IDLE) { || soundType == SoundType.IDLE) {
// If the volume is the default // 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()); mods.write(4, dSound.getDamageAndIdleSoundVolume());
} }
// Here I assume its the default pitch as I can't calculate if its real. // 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) { if (packets == null) {
packets = new PacketContainer[] { event.getPacket() }; packets = new PacketContainer[] { event.getPacket() };
} }
for (int i = 0; i < packets.length; i++) { for (PacketContainer packet : packets) {
PacketContainer packet = packets[i];
if (packet.equals(event.getPacket())) { if (packet.equals(event.getPacket())) {
packet = packet.deepClone(); packet = packet.deepClone();
} }
@ -716,10 +716,7 @@ public class PacketsManager {
} }
if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) { if (event.getPacketType() == PacketType.Play.Server.ENTITY_METADATA) {
event.setPacket(event.getPacket().deepClone()); event.setPacket(event.getPacket().deepClone());
Iterator<WrappedWatchableObject> itel = event.getPacket().getWatchableCollectionModifier().read(0) for (WrappedWatchableObject watch : event.getPacket().getWatchableCollectionModifier().read(0)) {
.iterator();
while (itel.hasNext()) {
WrappedWatchableObject watch = itel.next();
if (watch.getIndex() == 0) { if (watch.getIndex() == 0) {
byte b = (Byte) watch.getValue(); byte b = (Byte) watch.getValue();
byte a = (byte) (b | 1 << 5); 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_EXPERIENCE_ORB
|| sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY || sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY
|| sentPacket.getType() == PacketType.Play.Server.SPAWN_ENTITY_PAINTING) { || 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 // Else if the disguise is attempting to send players a forbidden packet

@ -151,8 +151,7 @@ public class ReflectionManager {
public static Entity getBukkitEntity(Object nmsEntity) { public static Entity getBukkitEntity(Object nmsEntity) {
try { try {
Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity); return (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity);
return bukkitEntity;
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); 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 { try {
Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity)); Object boundingBox = getNmsClass("Entity").getField("boundingBox").get(getNmsEntity(entity));
int stage = 0; int stage = 0;