Fix missing method, use an automatically generated entityid instead of -10

This commit is contained in:
libraryaddict 2017-12-06 08:08:24 +13:00
parent 7e18b96186
commit 971c091962
2 changed files with 61 additions and 62 deletions

View File

@ -1,41 +1,30 @@
package me.libraryaddict.disguise; package me.libraryaddict.disguise;
import java.lang.reflect.Field; import me.libraryaddict.disguise.disguisetypes.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.HorseInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.MiscDisguise;
import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
import me.libraryaddict.disguise.disguisetypes.watchers.AbstractHorseWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.AbstractHorseWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.HorseWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.ReflectionManager;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.entity.*;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.HorseInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class DisguiseAPI { public class DisguiseAPI {
private static int selfDisguiseId = ReflectionManager.getNewEntityId(true);
public static Disguise getCustomDisguise(String disguiseName) { public static Disguise getCustomDisguise(String disguiseName) {
Map.Entry<String, Disguise> entry = DisguiseConfig.getCustomDisguise(disguiseName); Map.Entry<String, Disguise> entry = DisguiseConfig.getCustomDisguise(disguiseName);
@ -99,9 +88,9 @@ public class DisguiseAPI {
} }
} }
for (Method method : entity.getClass().getMethods()) { for (Method method : entity.getClass().getMethods()) {
if ((doSneak || !method.getName().equals("setSneaking")) && (doSprint || !method.getName() if ((doSneak || !method.getName().equals("setSneaking")) &&
.equals("setSprinting")) && method.getParameterTypes().length == 0 && method (doSprint || !method.getName().equals("setSprinting")) && method.getParameterTypes().length == 0 &&
.getReturnType() != void.class) { method.getReturnType() != void.class) {
Class methodReturn = method.getReturnType(); Class methodReturn = method.getReturnType();
if (methodReturn == float.class || methodReturn == Float.class || methodReturn == Double.class) { if (methodReturn == float.class || methodReturn == Float.class || methodReturn == Double.class) {
@ -112,15 +101,16 @@ public class DisguiseAPI {
if (firstCapitalMethod > 0) { if (firstCapitalMethod > 0) {
for (Method watcherMethod : watcher.getClass().getMethods()) { for (Method watcherMethod : watcher.getClass().getMethods()) {
if (!watcherMethod.getName().startsWith("get") && watcherMethod if (!watcherMethod.getName().startsWith("get") && watcherMethod.getReturnType() == void.class &&
.getReturnType() == void.class && watcherMethod.getParameterTypes().length == 1) { watcherMethod.getParameterTypes().length == 1) {
int firstCapitalWatcher = firstCapital(watcherMethod.getName()); int firstCapitalWatcher = firstCapital(watcherMethod.getName());
if (firstCapitalWatcher > 0 && method.getName().substring(firstCapitalMethod) if (firstCapitalWatcher > 0 && method.getName().substring(firstCapitalMethod)
.equalsIgnoreCase(watcherMethod.getName().substring(firstCapitalWatcher))) { .equalsIgnoreCase(watcherMethod.getName().substring(firstCapitalWatcher))) {
Class methodParam = watcherMethod.getParameterTypes()[0]; Class methodParam = watcherMethod.getParameterTypes()[0];
if (methodParam == float.class || methodParam == Float.class || methodParam == Double.class) { if (methodParam == float.class || methodParam == Float.class ||
methodParam == Double.class) {
methodParam = double.class; methodParam = double.class;
} else if (methodParam == AnimalColor.class) { } else if (methodParam == AnimalColor.class) {
methodParam = DyeColor.class; methodParam = DyeColor.class;
@ -145,8 +135,8 @@ public class DisguiseAPI {
value = AnimalColor.valueOf(((DyeColor) value).name()); value = AnimalColor.valueOf(((DyeColor) value).name());
} }
} }
if (value instanceof Boolean && !(Boolean) value && watcherMethod if (value instanceof Boolean && !(Boolean) value &&
.getDeclaringClass() == FlagWatcher.class) { watcherMethod.getDeclaringClass() == FlagWatcher.class) {
continue; continue;
} }
} }
@ -189,9 +179,8 @@ public class DisguiseAPI {
disguise.setEntity(entity); disguise.setEntity(entity);
} }
disguise.setViewSelfDisguise( disguise.setViewSelfDisguise(Disguise.getViewSelf().contains(disguise.getEntity().getUniqueId()) !=
Disguise.getViewSelf().contains(disguise.getEntity().getUniqueId()) != DisguiseConfig DisguiseConfig.isViewDisguises());
.isViewDisguises());
disguise.startDisguise(); disguise.startDisguise();
} }
@ -242,17 +231,10 @@ public class DisguiseAPI {
disguise = disguise.clone(); disguise = disguise.clone();
} }
try { int id = ReflectionManager.getNewEntityId(false);
int id = ReflectionManager.getNmsField("Entity", "entityCount").getInt(null); DisguiseUtilities.addFutureDisguise(id, (TargetedDisguise) disguise);
DisguiseUtilities.addFutureDisguise(id, (TargetedDisguise) disguise);
return id; return id;
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
return -1;
} }
/** /**
@ -361,7 +343,7 @@ public class DisguiseAPI {
} }
public static int getSelfDisguiseId() { public static int getSelfDisguiseId() {
return -10; return selfDisguiseId;
} }
/** /**
@ -405,7 +387,8 @@ public class DisguiseAPI {
} }
/** /**
* Undisguise the entity. This doesn't let you cancel the UndisguiseEvent if the entity is no longer valid. Aka removed from * Undisguise the entity. This doesn't let you cancel the UndisguiseEvent if the entity is no longer valid. Aka
* removed from
* the world. * the world.
* *
* @param entity * @param entity

View File

@ -8,8 +8,6 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObje
import com.comphenix.protocol.wrappers.nbt.NbtCompound; import com.comphenix.protocol.wrappers.nbt.NbtCompound;
import com.comphenix.protocol.wrappers.nbt.NbtWrapper; import com.comphenix.protocol.wrappers.nbt.NbtWrapper;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.gson.Gson;
import com.mojang.authlib.GameProfileRepository;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.ImmutablePair;
import org.bukkit.*; import org.bukkit.*;
@ -19,10 +17,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Pattern;
public class ReflectionManager { public class ReflectionManager {
private static final String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3]; private static final String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3];
@ -39,8 +34,8 @@ public class ReflectionManager {
static { static {
for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) { for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) {
try { try {
if (method.getReturnType() == float.class && Modifier.isProtected(method.getModifiers()) && method if (method.getReturnType() == float.class && Modifier.isProtected(method.getModifiers()) &&
.getParameterTypes().length == 0) { method.getParameterTypes().length == 0) {
Object entity = createEntityInstance("Cow"); Object entity = createEntityInstance("Cow");
method.setAccessible(true); method.setAccessible(true);
@ -77,6 +72,27 @@ public class ReflectionManager {
entityCountField.setAccessible(true); entityCountField.setAccessible(true);
} }
public static int getNewEntityId() {
return getNewEntityId(true);
}
public static int getNewEntityId(boolean increment) {
try {
int id = entityCountField.getInt(null);
if (increment) {
entityCountField.set(null, id + 1);
}
return id;
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
return -1;
}
public static Object createEntityInstance(String entityName) { public static Object createEntityInstance(String entityName) {
try { try {
Class<?> entityClass = getNmsClass("Entity" + entityName); Class<?> entityClass = getNmsClass("Entity" + entityName);
@ -865,11 +881,11 @@ public class ReflectionManager {
if (value.getClass().getSimpleName().equals("NBTTagCompound")) if (value.getClass().getSimpleName().equals("NBTTagCompound"))
return null; // Handle PaperSpigot's bad coding return null; // Handle PaperSpigot's bad coding
throw new IllegalArgumentException("Unable to find Serializer for " + value + ( throw new IllegalArgumentException("Unable to find Serializer for " + value +
value instanceof Optional && ((Optional) value).isPresent() ? (value instanceof Optional && ((Optional) value).isPresent() ?
" (" + ((Optional) value).get().getClass().getName() + ")" : " (" + ((Optional) value).get().getClass().getName() + ")" :
value instanceof Optional || value == null ? "" : " " + value.getClass() value instanceof Optional || value == null ? "" : " " + value.getClass().getName()) +
.getName()) + "! Are you running " + "the latest " + "version of " + "ProtocolLib?"); "! Are you running " + "the latest " + "version of " + "ProtocolLib?");
} }
WrappedDataWatcherObject watcherObject = new WrappedDataWatcherObject(id, serializer); WrappedDataWatcherObject watcherObject = new WrappedDataWatcherObject(id, serializer);