LibsDisguises/src/me/libraryaddict/disguise/LibsDisguises.java

284 lines
14 KiB
Java
Raw Normal View History

2013-05-17 23:05:19 +02:00
package me.libraryaddict.disguise;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import me.libraryaddict.disguise.commands.*;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.AgeableWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.MinecartWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.SlimeWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.ZombieWatcher;
import me.libraryaddict.disguise.utilities.DisguiseSound;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.PacketsManager;
import me.libraryaddict.disguise.utilities.ReflectionManager;
import me.libraryaddict.disguise.utilities.DisguiseValues;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Ageable;
2013-11-18 04:24:25 +01:00
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Zombie;
2013-05-17 23:05:19 +02:00
import org.bukkit.plugin.java.JavaPlugin;
2013-11-18 04:24:25 +01:00
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
2013-11-18 04:24:25 +01:00
public class LibsDisguises extends JavaPlugin {
2013-07-21 05:14:59 +02:00
@Override
public void onEnable() {
saveDefaultConfig();
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "config.yml"));
2013-11-23 19:29:13 +01:00
boolean needToSaveConfig = false;
2014-04-04 16:47:33 +02:00
InputStream stream = null;
try {
2014-04-04 16:47:33 +02:00
stream = getClassLoader().getResource("config.yml").openStream();
YamlConfiguration internalConfig = YamlConfiguration.loadConfiguration(stream);
for (String option : internalConfig.getKeys(false)) {
if (internalConfig.isConfigurationSection(option)) {
ConfigurationSection section = internalConfig.getConfigurationSection(option);
for (String secondOption : section.getKeys(false)) {
if (!config.contains(secondOption)) {
config.set(option + "." + secondOption, section.get(secondOption));
needToSaveConfig = true;
}
}
} else if (!config.contains(option)) {
config.set(option, getConfig().get(option));
2013-11-23 19:29:13 +01:00
needToSaveConfig = true;
}
}
} catch (IOException e) {
e.printStackTrace();
2014-04-04 16:47:33 +02:00
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
2013-11-23 19:29:13 +01:00
if (needToSaveConfig) {
2013-11-05 18:05:10 +01:00
try {
config.save(new File(getDataFolder(), "config.yml"));
} catch (IOException e) {
e.printStackTrace();
}
}
PacketsManager.init(this);
DisguiseUtilities.init(this);
DisguiseConfig.setSoundsEnabled(getConfig().getBoolean("DisguiseSounds"));
DisguiseConfig.setVelocitySent(getConfig().getBoolean("SendVelocity"));
DisguiseConfig.setViewDisguises(getConfig().getBoolean("ViewSelfDisguises"));
DisguiseConfig.setHearSelfDisguise(getConfig().getBoolean("HearSelfDisguise"));
DisguiseConfig.setHideArmorFromSelf(getConfig().getBoolean("RemoveArmor"));
DisguiseConfig.setHideHeldItemFromSelf(getConfig().getBoolean("RemoveHeldItem"));
DisguiseConfig.setAddEntityAnimations(getConfig().getBoolean("AddEntityAnimations"));
DisguiseConfig.setNameOfPlayerShownAboveDisguise(getConfig().getBoolean("ShowNamesAboveDisguises"));
DisguiseConfig.setNameAboveHeadAlwaysVisible(getConfig().getBoolean("NameAboveHeadAlwaysVisible"));
DisguiseConfig.setModifyBoundingBox(getConfig().getBoolean("ModifyBoundingBox"));
DisguiseConfig.setMonstersIgnoreDisguises(getConfig().getBoolean("MonstersIgnoreDisguises"));
DisguiseConfig.setDisguiseBlownOnAttack(getConfig().getBoolean("BlowDisguises"));
DisguiseConfig.setDisguiseBlownMessage(ChatColor.translateAlternateColorCodes('&',
getConfig().getString("BlownDisguiseMessage")));
DisguiseConfig.setKeepDisguiseOnPlayerDeath(getConfig().getBoolean("KeepDisguises.PlayerDeath"));
DisguiseConfig.setKeepDisguiseOnPlayerLogout(getConfig().getBoolean("KeepDisguises.PlayerLogout"));
DisguiseConfig.setKeepDisguiseOnEntityDespawn(getConfig().getBoolean("KeepDisguises.EntityDespawn"));
DisguiseConfig.setMiscDisguisesForLivingEnabled(getConfig().getBoolean("MiscDisguisesForLiving"));
DisguiseConfig.setMovementPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Movement"));
DisguiseConfig.setWitherSkullPacketsEnabled(getConfig().getBoolean("PacketsEnabled.WitherSkull"));
DisguiseConfig.setEnquipmentPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Enquipment"));
DisguiseConfig.setAnimationPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Animation"));
DisguiseConfig.setBedPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Bed"));
DisguiseConfig.setRidingPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Riding"));
DisguiseConfig.setEntityStatusPacketsEnabled(getConfig().getBoolean("PacketsEnabled.EntityStatus"));
DisguiseConfig.setCollectPacketsEnabled(getConfig().getBoolean("PacketsEnabled.Collect"));
try {
// Here I use reflection to set the plugin for Disguise..
// Kind of stupid but I don't want open API calls for a commonly used object.
Field field = Disguise.class.getDeclaredField("plugin");
field.setAccessible(true);
field.set(null, this);
} catch (Exception ex) {
ex.printStackTrace();
}
PacketsManager.addPacketListeners();
2013-07-16 07:23:11 +02:00
DisguiseListener listener = new DisguiseListener(this);
Bukkit.getPluginManager().registerEvents(listener, this);
getCommand("disguise").setExecutor(new DisguiseCommand());
getCommand("undisguise").setExecutor(new UndisguiseCommand());
getCommand("disguiseplayer").setExecutor(new DisguisePlayerCommand());
getCommand("undisguiseplayer").setExecutor(new UndisguisePlayerCommand());
2013-07-16 07:23:11 +02:00
getCommand("undisguiseentity").setExecutor(new UndisguiseEntityCommand(listener));
getCommand("disguiseentity").setExecutor(new DisguiseEntityCommand(listener));
getCommand("disguiseradius").setExecutor(new DisguiseRadiusCommand(getConfig().getInt("DisguiseRadiusMax")));
getCommand("undisguiseradius").setExecutor(new UndisguiseRadiusCommand(getConfig().getInt("UndisguiseRadiusMax")));
2013-11-05 16:16:46 +01:00
getCommand("disguisehelp").setExecutor(new DisguiseHelpCommand());
getCommand("libsdisguises").setExecutor(new LibsDisguisesCommand());
registerValues();
}
/**
* Here we create a nms entity for each disguise. Then grab their default values in their datawatcher. Then their sound volume
* for mob noises. As well as setting their watcher class and entity size.
*/
private void registerValues() {
for (DisguiseType disguiseType : DisguiseType.values()) {
Class watcherClass = null;
try {
switch (disguiseType) {
case MINECART_FURNACE:
case MINECART_HOPPER:
case MINECART_MOB_SPAWNER:
case MINECART_TNT:
case MINECART_CHEST:
watcherClass = MinecartWatcher.class;
break;
case DONKEY:
case MULE:
case UNDEAD_HORSE:
case SKELETON_HORSE:
watcherClass = HorseWatcher.class;
break;
case ZOMBIE_VILLAGER:
case PIG_ZOMBIE:
watcherClass = ZombieWatcher.class;
break;
case MAGMA_CUBE:
watcherClass = SlimeWatcher.class;
break;
default:
watcherClass = Class.forName("me.libraryaddict.disguise.disguisetypes.watchers."
+ toReadable(disguiseType.name()) + "Watcher");
break;
}
} catch (ClassNotFoundException ex) {
// There is no explicit watcher for this entity.
Class entityClass = disguiseType.getEntityType().getEntityClass();
if (Ageable.class.isAssignableFrom(entityClass)) {
watcherClass = AgeableWatcher.class;
} else if (LivingEntity.class.isAssignableFrom(entityClass)) {
watcherClass = LivingWatcher.class;
} else {
watcherClass = FlagWatcher.class;
}
}
disguiseType.setWatcherClass(watcherClass);
String nmsEntityName = toReadable(disguiseType.name());
switch (disguiseType) {
case WITHER_SKELETON:
case ZOMBIE_VILLAGER:
case DONKEY:
case MULE:
case UNDEAD_HORSE:
case SKELETON_HORSE:
continue;
case PRIMED_TNT:
nmsEntityName = "TNTPrimed";
break;
case MINECART_TNT:
nmsEntityName = "MinecartTNT";
break;
case MINECART:
nmsEntityName = "MinecartRideable";
break;
case FIREWORK:
nmsEntityName = "Fireworks";
break;
case SPLASH_POTION:
nmsEntityName = "Potion";
break;
case GIANT:
nmsEntityName = "GiantZombie";
break;
case DROPPED_ITEM:
nmsEntityName = "Item";
break;
case FIREBALL:
nmsEntityName = "LargeFireball";
break;
2013-11-07 21:29:32 +01:00
case LEASH_HITCH:
nmsEntityName = "Leash";
2013-11-07 21:29:32 +01:00
break;
default:
break;
}
if (DisguiseValues.getDisguiseValues(disguiseType) != null) {
continue;
}
try {
Object nmsEntity = ReflectionManager.createEntityInstance(nmsEntityName);
if (nmsEntity == null) {
continue;
}
Entity bukkitEntity = ReflectionManager.getBukkitEntity(nmsEntity);
int entitySize = 0;
2013-11-18 04:24:25 +01:00
for (Field field : ReflectionManager.getNmsClass("Entity").getFields()) {
if (field.getType().getName().equals("EnumEntitySize")) {
Enum enumEntitySize = (Enum) field.get(nmsEntity);
entitySize = enumEntitySize.ordinal();
2013-11-18 04:24:25 +01:00
break;
}
}
DisguiseValues disguiseValues = new DisguiseValues(disguiseType, nmsEntity.getClass(), entitySize);
for (WrappedWatchableObject watch : WrappedDataWatcher.getEntityWatcher(bukkitEntity).getWatchableObjects()) {
disguiseValues.setMetaValue(watch.getIndex(), watch.getValue());
// Uncomment when I need to find the new datawatcher values for a class..
// System.out.print("Disguise: " + disguiseType + ", ID: " + watch.getIndex() + ", Class: "
2014-04-26 05:42:35 +02:00
// + (watch.getValue() == null ? "null" : watch.getValue().getClass()) + ", Value: " + watch.getValue());
}
DisguiseSound sound = DisguiseSound.getType(disguiseType.name());
if (sound != null) {
Float soundStrength = ReflectionManager.getSoundModifier(nmsEntity);
2013-11-18 04:24:25 +01:00
if (soundStrength != null) {
sound.setDamageAndIdleSoundVolume((Float) soundStrength);
2013-11-18 04:24:25 +01:00
}
}
// Get the bounding box
disguiseValues.setAdultBox(ReflectionManager.getBoundingBox(bukkitEntity));
if (bukkitEntity instanceof Ageable) {
((Ageable) bukkitEntity).setBaby();
disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity));
} else if (bukkitEntity instanceof Zombie) {
((Zombie) bukkitEntity).setBaby(true);
disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity));
}
2013-12-22 05:35:57 +01:00
disguiseValues.setEntitySize(ReflectionManager.getSize(bukkitEntity));
} catch (Exception ex) {
System.out.print("[LibsDisguises] Uh oh! Trouble while making values for the disguise " + disguiseType.name()
+ "!");
System.out.print("[LibsDisguises] Before reporting this error, "
+ "please make sure you are using the latest version of LibsDisguises and ProtocolLib");
System.out
2013-12-17 10:23:57 +01:00
.print("[LibsDisguises] You can try the latest builds at (ProtocolLib) "
+ "http://assets.comphenix.net/job/ProtocolLib/ and (LibsDisguises) http://ci.md-5.net/job/LibsDisguises/");
ex.printStackTrace();
}
}
}
2013-07-11 20:38:38 +02:00
private String toReadable(String string) {
StringBuilder builder = new StringBuilder();
for (String s : string.split("_")) {
builder.append(s.substring(0, 1) + s.substring(1).toLowerCase());
}
return builder.toString();
2013-05-17 23:05:19 +02:00
}
2013-08-01 10:29:04 +02:00
2013-11-14 06:36:53 +01:00
}