Minor code cleanup

This commit is contained in:
libraryaddict 2013-11-24 07:29:13 +13:00
parent d6fba4c402
commit 6dfeb5f10a
3 changed files with 21 additions and 20 deletions

@ -3,8 +3,6 @@ package me.libraryaddict.disguise;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List;
import me.libraryaddict.disguise.commands.*; import me.libraryaddict.disguise.commands.*;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
@ -38,19 +36,19 @@ public class LibsDisguises extends JavaPlugin {
public void onEnable() { public void onEnable() {
saveDefaultConfig(); saveDefaultConfig();
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml")); FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml"));
boolean modified = false; boolean needToSaveConfig = false;
try { try {
for (String option : YamlConfiguration for (String option : YamlConfiguration.loadConfiguration(getClassLoader().getResource("config.yml").openStream())
.loadConfiguration(this.getClassLoader().getResource("config.yml").openStream()).getKeys(false)) { .getKeys(false)) {
if (!config.contains(option)) { if (!config.contains(option)) {
config.set(option, getConfig().get(option)); config.set(option, getConfig().get(option));
modified = true; needToSaveConfig = true;
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (modified) { if (needToSaveConfig) {
try { try {
config.save(new File(getDataFolder(), "config.yml")); config.save(new File(getDataFolder(), "config.yml"));
} catch (IOException e) { } catch (IOException e) {
@ -193,9 +191,7 @@ public class LibsDisguises extends JavaPlugin {
} }
} }
DisguiseValues disguiseValues = new DisguiseValues(disguiseType, nmsEntity.getClass(), entitySize); DisguiseValues disguiseValues = new DisguiseValues(disguiseType, nmsEntity.getClass(), entitySize);
WrappedDataWatcher dataWatcher = WrappedDataWatcher.getEntityWatcher(bukkitEntity); for (WrappedWatchableObject watch : WrappedDataWatcher.getEntityWatcher(bukkitEntity).getWatchableObjects())
List<WrappedWatchableObject> watchers = dataWatcher.getWatchableObjects();
for (WrappedWatchableObject watch : watchers)
disguiseValues.setMetaValue(watch.getIndex(), watch.getValue()); disguiseValues.setMetaValue(watch.getIndex(), watch.getValue());
DisguiseSound sound = DisguiseSound.getType(disguiseType.name()); DisguiseSound sound = DisguiseSound.getType(disguiseType.name());
if (sound != null) { if (sound != null) {

@ -54,16 +54,16 @@ public abstract class Disguise {
return; return;
if (newType.getEntityType() == null) { if (newType.getEntityType() == null) {
throw new RuntimeException("DisguiseType " + newType throw new RuntimeException("DisguiseType " + newType
+ " was attempted to construct a disguise, but this version of craftbukkit does not have that entity"); + " was used to attempt to construct a disguise, but this version of craftbukkit does not have that entity");
} }
// Set the disguise type // Set the disguise type
disguiseType = newType; disguiseType = newType;
// Set the option to replace the sounds // Set the option to replace the sounds
setReplaceSounds(doSounds); setReplaceSounds(doSounds);
// Get if they are a adult now.. // Get if they are a adult now..
boolean isBaby = false; boolean isAdult = true;
if (this instanceof MobDisguise) { if (this instanceof MobDisguise) {
isBaby = !((MobDisguise) this).isAdult(); isAdult = ((MobDisguise) this).isAdult();
} }
try { try {
// Construct the FlagWatcher from the stored class // Construct the FlagWatcher from the stored class
@ -72,7 +72,7 @@ public abstract class Disguise {
e.printStackTrace(); e.printStackTrace();
} }
// Set the disguise if its a baby or not // Set the disguise if its a baby or not
if (isBaby) { if (!isAdult) {
if (getWatcher() instanceof AgeableWatcher) { if (getWatcher() instanceof AgeableWatcher) {
((AgeableWatcher) getWatcher()).setAdult(false); ((AgeableWatcher) getWatcher()).setAdult(false);
} else if (getWatcher() instanceof ZombieWatcher) { } else if (getWatcher() instanceof ZombieWatcher) {
@ -80,11 +80,13 @@ public abstract class Disguise {
} }
} }
// If the disguise type is a wither, set the flagwatcher value for the skeleton to a wither skeleton // If the disguise type is a wither, set the flagwatcher value for the skeleton to a wither skeleton
if (getType() == DisguiseType.WITHER_SKELETON) if (getType() == DisguiseType.WITHER_SKELETON) {
getWatcher().setValue(13, (byte) 1); getWatcher().setValue(13, (byte) 1);
}
// Else if its a zombie, but the disguise type is a zombie villager. Set the value. // Else if its a zombie, but the disguise type is a zombie villager. Set the value.
else if (getType() == DisguiseType.ZOMBIE_VILLAGER) else if (getType() == DisguiseType.ZOMBIE_VILLAGER) {
getWatcher().setValue(13, (byte) 1); getWatcher().setValue(13, (byte) 1);
}
// Else if its a horse. Set the horse watcher type // Else if its a horse. Set the horse watcher type
else if (getWatcher() instanceof HorseWatcher) { else if (getWatcher() instanceof HorseWatcher) {
try { try {
@ -401,8 +403,9 @@ public abstract class Disguise {
* Set the entity of the disguise. Only used for internal things. * Set the entity of the disguise. Only used for internal things.
*/ */
public void setEntity(org.bukkit.entity.Entity entity) { public void setEntity(org.bukkit.entity.Entity entity) {
if (this.entity != null) if (this.entity != null) {
throw new RuntimeException("This disguise is already in use! Try .clone()"); throw new RuntimeException("This disguise is already in use! Try .clone()");
}
this.entity = entity; this.entity = entity;
setupWatcher(); setupWatcher();
velocityRunnable.runTaskTimer(plugin, 1, 1); velocityRunnable.runTaskTimer(plugin, 1, 1);

@ -129,6 +129,7 @@ public enum DisguiseType {
ZOMBIE(), ZOMBIE(),
ZOMBIE_VILLAGER(); ZOMBIE_VILLAGER();
static { static {
for (DisguiseType type : values()) { for (DisguiseType type : values()) {
try { try {
@ -160,6 +161,7 @@ public enum DisguiseType {
type.setEntityType(entityType); type.setEntityType(entityType);
} }
} catch (Throwable ex) { } catch (Throwable ex) {
// This version of craftbukkit doesn't have the disguise.
} }
} }
} }
@ -214,15 +216,15 @@ public enum DisguiseType {
} }
public boolean isMisc() { public boolean isMisc() {
return !entityType.isAlive(); return !getEntityType().isAlive();
} }
public boolean isMob() { public boolean isMob() {
return entityType.isAlive() && entityType != EntityType.PLAYER; return getEntityType().isAlive() && this != DisguiseType.PLAYER;
} }
public boolean isPlayer() { public boolean isPlayer() {
return entityType == EntityType.PLAYER; return this == DisguiseType.PLAYER;
} }
private void setEntityType(EntityType entityType) { private void setEntityType(EntityType entityType) {