Added rudimentary support for disguising Custom Entities and Modded Entities

Added the DisguiseType 'Unknown', obviously can't be used by players
Cleaned up code
This commit is contained in:
NavidK0 2015-07-07 11:30:12 -04:00
parent d30ffa1994
commit 27cc9cd70f
10 changed files with 53 additions and 22 deletions

View File

@ -13,7 +13,7 @@ println 'Compiling LibsDisguises via Gradle ver. ' + gradle.gradleVersion
sourceCompatibility = '1.7'
ext.spigotVersion = '1.8.7-R0.1-SNAPSHOT'
ext.disguisesVersion = '8.5.1'
ext.disguisesVersion = '8.5.2'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

View File

@ -189,6 +189,8 @@ public class DisguiseAPI {
/**
* Disguise the next entity to spawn with this disguise. This may not work however if the entity doesn't actually spawn.
* @param disguise
* @return
*/
public static int disguiseNextEntity(Disguise disguise) {
if (disguise == null)
@ -202,7 +204,7 @@ public class DisguiseAPI {
int id = field.getInt(null);
DisguiseUtilities.addFutureDisguise(id, (TargetedDisguise) disguise);
return id;
} catch (Exception ex) {
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
ex.printStackTrace(System.out);
}
return -1;
@ -210,6 +212,8 @@ public class DisguiseAPI {
/**
* Disguise this entity with this disguise
* @param entity
* @param disguise
*/
public static void disguiseToAll(Entity entity, Disguise disguise) {
if (disguise.getEntity() != null) {

View File

@ -197,7 +197,7 @@ public class DisguiseListener implements Listener {
event.setCancelled(true);
disguiseRunnable.remove(p.getName()).cancel();
Entity entity = event.getRightClicked();
String entityName = "";
String entityName;
if (entity instanceof Player && !disguiseClone.containsKey(p.getName())) {
entityName = ((Player) entity).getName();
} else {

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise;
import com.comphenix.protocol.reflect.FieldAccessException;
import java.io.IOException;
import java.lang.reflect.Field;
@ -31,6 +32,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.utilities.FakeBoundingBox;
import me.libraryaddict.disguise.utilities.Metrics;
import org.bukkit.event.HandlerList;
@ -87,7 +89,7 @@ public class LibsDisguises extends JavaPlugin {
if (disguiseType.getEntityType() == null) {
continue;
}
Class watcherClass = null;
Class watcherClass;
try {
switch (disguiseType) {
case MINECART_CHEST:
@ -125,6 +127,7 @@ public class LibsDisguises extends JavaPlugin {
} catch (ClassNotFoundException ex) {
// There is no explicit watcher for this entity.
Class entityClass = disguiseType.getEntityType().getEntityClass();
if (entityClass != null) {
if (Tameable.class.isAssignableFrom(entityClass)) {
watcherClass = TameableWatcher.class;
} else if (Ageable.class.isAssignableFrom(entityClass)) {
@ -134,6 +137,9 @@ public class LibsDisguises extends JavaPlugin {
} else {
watcherClass = FlagWatcher.class;
}
} else {
watcherClass = FlagWatcher.class; //Disguise is unknown type
}
}
disguiseType.setWatcherClass(watcherClass);
if (DisguiseValues.getDisguiseValues(disguiseType) != null) {
@ -182,6 +188,15 @@ public class LibsDisguises extends JavaPlugin {
break;
}
try {
if (nmsEntityName.equalsIgnoreCase("Unknown")) {
DisguiseValues disguiseValues = new DisguiseValues(disguiseType, null, 0, 0);
disguiseValues.setAdultBox(new FakeBoundingBox(0, 0, 0));
DisguiseSound sound = DisguiseSound.getType(disguiseType.name());
if (sound != null) {
sound.setDamageAndIdleSoundVolume(1f);
}
continue;
}
Object nmsEntity = ReflectionManager.createEntityInstance(nmsEntityName);
if (nmsEntity == null) {
continue;
@ -201,14 +216,14 @@ public class LibsDisguises extends JavaPlugin {
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: "
// + (watch.getValue() == null ? "null" : watch.getValue().getClass()) + ", Value: " + watch.getValue());
// System.out.print("Disguise: " + disguiseType + ", ID: " + watch.getIndex() + ", Class: "
// + (watch.getValue() == null ? "null" : watch.getValue().getClass()) + ", Value: " + watch.getValue());
}
DisguiseSound sound = DisguiseSound.getType(disguiseType.name());
if (sound != null) {
Float soundStrength = ReflectionManager.getSoundModifier(nmsEntity);
if (soundStrength != null) {
sound.setDamageAndIdleSoundVolume((Float) soundStrength);
sound.setDamageAndIdleSoundVolume(soundStrength);
}
}
@ -222,7 +237,7 @@ public class LibsDisguises extends JavaPlugin {
disguiseValues.setBabyBox(ReflectionManager.getBoundingBox(bukkitEntity));
}
disguiseValues.setEntitySize(ReflectionManager.getSize(bukkitEntity));
} catch (Exception ex) {
} catch (SecurityException | IllegalArgumentException | IllegalAccessException | FieldAccessException ex) {
System.out.print("[LibsDisguises] Uh oh! Trouble while making values for the disguise " + disguiseType.name()
+ "!");
System.out.print("[LibsDisguises] Before reporting this error, "

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.commands;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
@ -29,7 +30,7 @@ public class DisguiseEntityCommand extends BaseDisguiseCommand {
sender.sendMessage(ex.getMessage());
}
return true;
} catch (Exception ex) {
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace(System.out);
return true;
}

View File

@ -147,7 +147,9 @@ public enum DisguiseType {
ZOMBIE,
ZOMBIE_VILLAGER;
ZOMBIE_VILLAGER,
UNKNOWN;
private static Method isVillager, getVariant, getSkeletonType, isElder;
@ -253,7 +255,7 @@ public enum DisguiseType {
try {
return valueOf(entityType.name().toUpperCase());
} catch (Throwable ex) {
return null;
return DisguiseType.UNKNOWN;
}
}
@ -316,6 +318,10 @@ public enum DisguiseType {
return this == DisguiseType.PLAYER;
}
public boolean isUnknown() {
return this == DisguiseType.UNKNOWN;
}
private void setEntityType(EntityType entityType) {
this.entityType = entityType;
}

View File

@ -345,6 +345,9 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
throw new DisguiseParseException(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0]
+ ChatColor.RED + " doesn't exist!");
}
if (disguiseType.isUnknown()) {
throw new DisguiseParseException(ChatColor.RED + "Error! You cannot disguise as " + ChatColor.GREEN + "Unknown!");
}
if (disguiseType.getEntityType() == null) {
throw new DisguiseParseException(ChatColor.RED + "Error! This version of minecraft does not have that disguise!");
}

View File

@ -970,6 +970,7 @@ public class DisguiseUtilities {
/**
* Setup it so he can see himself when disguised
* @param disguise
*/
public static void setupFakeDisguise(final Disguise disguise) {
Entity e = disguise.getEntity();

View File

@ -52,6 +52,7 @@ public class DisguiseValues {
private HashMap<Integer, Object> metaValues = new HashMap<>();
private Class nmsEntityClass;
@SuppressWarnings("LeakingThisInConstructor")
public DisguiseValues(DisguiseType type, Class classType, int entitySize, double maxHealth) {
values.put(type, this);
enumEntitySize = entitySize;

View File

@ -1,6 +1,6 @@
name: LibsDisguises
main: me.libraryaddict.disguise.LibsDisguises
version: 8.5.1
version: 8.5.2
author: libraryaddict
authors: [Byteflux, Navid K.]
depend: [ProtocolLib]