Read desc

Added method getBukkitEntity to reflectionmanager
Added a way for itemframe values to exist.
This commit is contained in:
libraryaddict 2013-11-27 16:38:51 +13:00
parent ca913347ce
commit 473769eb26
6 changed files with 51 additions and 13 deletions

View File

@ -24,6 +24,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Ageable; import org.bukkit.entity.Ageable;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -96,6 +97,15 @@ public class LibsDisguises extends JavaPlugin {
* for mob noises. As well as setting their watcher class and entity size. * for mob noises. As well as setting their watcher class and entity size.
*/ */
private void registerValues() { private void registerValues() {
try {
DisguiseValues disguiseValues = new DisguiseValues((Enum) Class.forName("org.bukkit.entity.EntityType")
.getField("ITEM_FRAME").get(null), null, 0);
for (WrappedWatchableObject watch : WrappedDataWatcher.getEntityWatcher(
ReflectionManager.getBukkitEntity(ReflectionManager.createEntityInstance("ItemFrame"))).getWatchableObjects())
disguiseValues.setMetaValue(watch.getIndex(), watch.getValue());
} catch (Exception ex) {
ex.printStackTrace();
}
for (DisguiseType disguiseType : DisguiseType.values()) { for (DisguiseType disguiseType : DisguiseType.values()) {
if (disguiseType.getEntityType() == null) { if (disguiseType.getEntityType() == null) {
continue; continue;
@ -179,10 +189,12 @@ public class LibsDisguises extends JavaPlugin {
default: default:
break; break;
} }
if (DisguiseValues.getDisguiseValues(disguiseType) != null) {
continue;
}
try { try {
Object nmsEntity = ReflectionManager.createEntityInstance(nmsEntityName); Object nmsEntity = ReflectionManager.createEntityInstance(nmsEntityName);
Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity") Entity bukkitEntity = ReflectionManager.getBukkitEntity(nmsEntity);
.invoke(nmsEntity);
int entitySize = 0; int entitySize = 0;
for (Field field : ReflectionManager.getNmsClass("Entity").getFields()) { for (Field field : ReflectionManager.getNmsClass("Entity").getFields()) {
if (field.getType().getName().equals("EnumEntitySize")) { if (field.getType().getName().equals("EnumEntitySize")) {

View File

@ -139,7 +139,7 @@ public abstract class Disguise {
case ENDER_CRYSTAL: case ENDER_CRYSTAL:
case ENDER_DRAGON: case ENDER_DRAGON:
case GHAST: case GHAST:
// case ITEM_FRAME: // case ITEM_FRAME:
case MINECART: case MINECART:
case MINECART_CHEST: case MINECART_CHEST:
case MINECART_FURNACE: case MINECART_FURNACE:
@ -213,7 +213,7 @@ public abstract class Disguise {
Location loc = getEntity().getLocation(); Location loc = getEntity().getLocation();
mods.write( mods.write(
4, 4,
PacketsManager.getYaw(getType(), DisguiseType.getType(getEntity().getType()), PacketsManager.getYaw(getType(), getEntity().getType(),
(byte) Math.floor(loc.getYaw() * 256.0F / 360.0F))); (byte) Math.floor(loc.getYaw() * 256.0F / 360.0F)));
mods.write(5, (byte) Math.floor(loc.getPitch() * 256.0F / 360.0F)); mods.write(5, (byte) Math.floor(loc.getPitch() * 256.0F / 360.0F));
if (isSelfDisguiseVisible() && getEntity() instanceof Player) { if (isSelfDisguiseVisible() && getEntity() instanceof Player) {
@ -295,11 +295,10 @@ public abstract class Disguise {
Object entityTrackerEntry = trackedEntities.getClass().getMethod("get", int.class) Object entityTrackerEntry = trackedEntities.getClass().getMethod("get", int.class)
.invoke(trackedEntities, getEntity().getEntityId()); .invoke(trackedEntities, getEntity().getEntityId());
if (entityTrackerEntry != null) { if (entityTrackerEntry != null) {
Method method = ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity");
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers")
.get(entityTrackerEntry); .get(entityTrackerEntry);
for (Object p : trackedPlayers) { for (Object p : trackedPlayers) {
players.add((Player) method.invoke(p)); players.add((Player) ReflectionManager.getBukkitEntity(p));
} }
} }
} catch (Exception ex) { } catch (Exception ex) {
@ -445,7 +444,15 @@ public abstract class Disguise {
*/ */
private void setupWatcher() { private void setupWatcher() {
HashMap<Integer, Object> disguiseValues = DisguiseValues.getMetaValues(getType()); HashMap<Integer, Object> disguiseValues = DisguiseValues.getMetaValues(getType());
HashMap<Integer, Object> entityValues = DisguiseValues.getMetaValues(DisguiseType.getType(getEntity().getType())); HashMap<Integer, Object> entityValues = null;
if (DisguiseType.getType(getEntity().getType()) != null)
entityValues = DisguiseValues.getMetaValues(DisguiseType.getType(getEntity().getType()));
else {
DisguiseValues.getEntityValues(getEntity().getType()).getMetaValues();
}
if (entityValues == null) {
entityValues = new HashMap();
}
// Start from 2 as they ALL share 0 and 1 // Start from 2 as they ALL share 0 and 1
for (int dataNo = 2; dataNo <= 31; dataNo++) { for (int dataNo = 2; dataNo <= 31; dataNo++) {
// STEP 1. Find out if the watcher has set data on it. // STEP 1. Find out if the watcher has set data on it.

View File

@ -167,7 +167,11 @@ public enum DisguiseType {
} }
public static DisguiseType getType(org.bukkit.entity.EntityType entityType) { public static DisguiseType getType(org.bukkit.entity.EntityType entityType) {
return DisguiseType.valueOf(entityType.name()); try {
return DisguiseType.valueOf(entityType.name());
} catch (Throwable ex) {
return null;
}
} }
private int defaultData; private int defaultData;

View File

@ -58,13 +58,12 @@ public class DisguiseUtilities {
if (entityTrackerEntry != null) { if (entityTrackerEntry != null) {
HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers") HashSet trackedPlayers = (HashSet) entityTrackerEntry.getClass().getField("trackedPlayers")
.get(entityTrackerEntry); .get(entityTrackerEntry);
Method getBukkitEntity = ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity");
Method clear = entityTrackerEntry.getClass().getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer")); Method clear = entityTrackerEntry.getClass().getMethod("clear", ReflectionManager.getNmsClass("EntityPlayer"));
Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer", Method updatePlayer = entityTrackerEntry.getClass().getMethod("updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer")); ReflectionManager.getNmsClass("EntityPlayer"));
HashSet cloned = (HashSet) trackedPlayers.clone(); HashSet cloned = (HashSet) trackedPlayers.clone();
for (Object player : cloned) { for (Object player : cloned) {
if (entity instanceof Player && !((Player) getBukkitEntity.invoke(player)).canSee((Player) entity)) if (entity instanceof Player && !((Player) ReflectionManager.getBukkitEntity(player)).canSee((Player) entity))
continue; continue;
clear.invoke(entityTrackerEntry, player); clear.invoke(entityTrackerEntry, player);
updatePlayer.invoke(entityTrackerEntry, player); updatePlayer.invoke(entityTrackerEntry, player);

View File

@ -2,11 +2,13 @@ package me.libraryaddict.disguise.utilities;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.entity.EntityType;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
public class DisguiseValues { public class DisguiseValues {
private static HashMap<DisguiseType, DisguiseValues> values = new HashMap<DisguiseType, DisguiseValues>(); private static HashMap<Enum, DisguiseValues> values = new HashMap<Enum, DisguiseValues>();
public static DisguiseValues getDisguiseValues(DisguiseType type) { public static DisguiseValues getDisguiseValues(DisguiseType type) {
switch (type) { switch (type) {
@ -35,6 +37,10 @@ public class DisguiseValues {
return values.get(type); return values.get(type);
} }
public static DisguiseValues getEntityValues(EntityType type) {
return values.get(type);
}
public static HashMap<Integer, Object> getMetaValues(DisguiseType type) { public static HashMap<Integer, Object> getMetaValues(DisguiseType type) {
return getDisguiseValues(type).getMetaValues(); return getDisguiseValues(type).getMetaValues();
} }
@ -47,7 +53,7 @@ public class DisguiseValues {
private HashMap<Integer, Object> metaValues = new HashMap<Integer, Object>(); private HashMap<Integer, Object> metaValues = new HashMap<Integer, Object>();
private Class nmsEntityClass; private Class nmsEntityClass;
public DisguiseValues(DisguiseType type, Class classType, int entitySize) { public DisguiseValues(Enum type, Class classType, int entitySize) {
values.put(type, this); values.put(type, this);
enumEntitySize = entitySize; enumEntitySize = entitySize;
nmsEntityClass = classType; nmsEntityClass = classType;

View File

@ -114,7 +114,17 @@ public class ReflectionManager {
try { try {
return Class.forName("net.minecraft.server." + bukkitVersion + "." + className); return Class.forName("net.minecraft.server." + bukkitVersion + "." + className);
} catch (Exception e) { } catch (Exception e) {
// e.printStackTrace(); // e.printStackTrace();
}
return null;
}
public static Entity getBukkitEntity(Object nmsEntity) {
try {
Entity bukkitEntity = (Entity) ReflectionManager.getNmsClass("Entity").getMethod("getBukkitEntity").invoke(nmsEntity);
return bukkitEntity;
} catch (Exception ex) {
ex.printStackTrace();
} }
return null; return null;
} }