Switch to ProtocolLib MinecraftKeys wrapper

Add in helper method to quickly create DataWatcher.Item objects
Typo in last commit, meant to say, disguises will NOT show up. Sorry to those who got excited. :(
This commit is contained in:
NavidK0 2016-03-04 22:52:52 -05:00
parent 2cf48f7916
commit 1576c895b5
5 changed files with 42 additions and 65 deletions

View File

@ -109,15 +109,13 @@ public class FlagWatcher {
value = this.addEntityAnimations((byte) value, (byte) watch.getValue()); value = this.addEntityAnimations((byte) value, (byte) watch.getValue());
} }
boolean isDirty = watch.getDirtyState(); boolean isDirty = watch.getDirtyState();
watch = new WrappedWatchableObject(id); watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(id, value));
watch.setValue(value);
if (!isDirty) { if (!isDirty) {
watch.setDirtyState(false); watch.setDirtyState(false);
} }
} else { } else {
boolean isDirty = watch.getDirtyState(); boolean isDirty = watch.getDirtyState();
watch = new WrappedWatchableObject(id); watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(id, watch.getValue()));
watch.setValue(watch.getValue());
if (!isDirty) { if (!isDirty) {
watch.setDirtyState(false); watch.setDirtyState(false);
} }
@ -126,16 +124,15 @@ public class FlagWatcher {
} }
if (sendAllCustom) { if (sendAllCustom) {
// Its sending the entire meta data. Better add the custom meta // Its sending the entire meta data. Better add the custom meta
for (int value : entityValues.keySet()) { for (int id : entityValues.keySet()) {
if (sentValues.contains(value)) { if (sentValues.contains(id)) {
continue; continue;
} }
Object obj = entityValues.get(value); Object value = entityValues.get(id);
if (obj == null) { if (value == null) {
continue; continue;
} }
WrappedWatchableObject watch = new WrappedWatchableObject(value); WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(id, value));
watch.setValue(obj);
newList.add(watch); newList.add(watch);
} }
} }
@ -246,11 +243,9 @@ public class FlagWatcher {
for (int i = 0; i <= 31; i++) { for (int i = 0; i <= 31; i++) {
WrappedWatchableObject watchable = null; WrappedWatchableObject watchable = null;
if (this.entityValues.containsKey(i) && this.entityValues.get(i) != null) { if (this.entityValues.containsKey(i) && this.entityValues.get(i) != null) {
watchable = new WrappedWatchableObject(i); watchable = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(i, entityValues.get(i)));
watchable.setValue(entityValues.get(i));
} else if (this.backupEntityValues.containsKey(i) && this.backupEntityValues.get(i) != null) { } else if (this.backupEntityValues.containsKey(i) && this.backupEntityValues.get(i) != null) {
watchable = new WrappedWatchableObject(i); watchable = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(i, entityValues.get(i)));
watchable.setValue(backupEntityValues.get(i));
} }
if (watchable != null) { if (watchable != null) {
watchableObjects.add(watchable); watchableObjects.add(watchable);
@ -271,8 +266,7 @@ public class FlagWatcher {
if (isEntityAnimationsAdded() && DisguiseConfig.isMetadataPacketsEnabled() && data == 0) { if (isEntityAnimationsAdded() && DisguiseConfig.isMetadataPacketsEnabled() && data == 0) {
value = addEntityAnimations((byte) value, WrappedDataWatcher.getEntityWatcher(disguise.getEntity()).getByte(0)); value = addEntityAnimations((byte) value, WrappedDataWatcher.getEntityWatcher(disguise.getEntity()).getByte(0));
} }
WrappedWatchableObject watch = new WrappedWatchableObject(data); WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(data, value));
watch.setValue(value);
list.add(watch); list.add(watch);
} }
if (!list.isEmpty()) { if (!list.isEmpty()) {

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities;
import com.comphenix.protocol.wrappers.MinecraftKey;
import org.bukkit.Sound; import org.bukkit.Sound;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -132,10 +133,8 @@ public enum DisguiseSound {
try { try {
Field f_getMinecraftKey = ReflectionManager.getNmsField("SoundEffect", "b"); Field f_getMinecraftKey = ReflectionManager.getNmsField("SoundEffect", "b");
f_getMinecraftKey.setAccessible(true); f_getMinecraftKey.setAccessible(true);
Object minecraftKey = f_getMinecraftKey.get(soundEffect); MinecraftKey key = MinecraftKey.fromHandle(f_getMinecraftKey.get(soundEffect));
Field f_getValue = ReflectionManager.getNmsField("MinecraftKey", "a"); return key.getKey();
String sound = (String) f_getValue.get(soundEffect); //Our prize!
return sound;
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -7,13 +7,11 @@ import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedDataWatcher; import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType;
@ -593,40 +591,6 @@ public class DisguiseUtilities {
return ReflectionManager.getSkullBlob(ReflectionManager.grabProfileAddUUID(playerName)); return ReflectionManager.getSkullBlob(ReflectionManager.grabProfileAddUUID(playerName));
} }
/**
* Please note that in the future when 'DualInt' and the like are removed. This should break.. However, that should be negated in the future as I'd be able to set the watcher index's as per the spigot version. Instead of checking on the player's version every single packet..
*/
public static List<WrappedWatchableObject> rebuildForVersion(Player player, FlagWatcher watcher,
List<WrappedWatchableObject> list) {
if (true) // Use for future protocol compatibility
{
return list;
}
ArrayList<WrappedWatchableObject> rebuiltList = new ArrayList<>();
ArrayList<WrappedWatchableObject> backups = new ArrayList<>();
for (WrappedWatchableObject obj : list) {
if (obj.getValue().getClass().getName().startsWith("org.")) {
backups.add(obj);
continue;
}
switch (obj.getIndex()) {
// TODO: Future version support
}
}
Iterator<WrappedWatchableObject> itel = backups.iterator();
while (itel.hasNext()) {
int index = itel.next().getIndex();
for (WrappedWatchableObject obj2 : rebuiltList) {
if (index == obj2.getIndex()) {
itel.remove();
break;
}
}
}
rebuiltList.addAll(backups);
return rebuiltList;
}
/** /**
* Resends the entity to this specific player * Resends the entity to this specific player
*/ */

View File

@ -364,7 +364,6 @@ public class PacketsManager {
try { try {
List<WrappedWatchableObject> list = DisguiseConfig.isMetadataPacketsEnabled() ? flagWatcher.convert(watcher List<WrappedWatchableObject> list = DisguiseConfig.isMetadataPacketsEnabled() ? flagWatcher.convert(watcher
.getWatchableObjects()) : flagWatcher.getWatchableObjects(); .getWatchableObjects()) : flagWatcher.getWatchableObjects();
list = DisguiseUtilities.rebuildForVersion(player, flagWatcher, list);
for (WrappedWatchableObject watchableObject : list) { for (WrappedWatchableObject watchableObject : list) {
newWatcher.setObject(watchableObject.getIndex(), watchableObject.getValue()); newWatcher.setObject(watchableObject.getIndex(), watchableObject.getValue());
} }
@ -818,11 +817,10 @@ public class PacketsManager {
StructureModifier<Object> mods = packet.getModifier(); StructureModifier<Object> mods = packet.getModifier();
mods.write(0, observer.getEntityId()); mods.write(0, observer.getEntityId());
List<WrappedWatchableObject> watchableList = new ArrayList<>(); List<WrappedWatchableObject> watchableList = new ArrayList<>();
byte b = (byte) 1 << 5; Byte b = 1 << 5;
if (observer.isSprinting()) if (observer.isSprinting())
b = (byte) (b | 1 << 3); b = (byte) (b | 1 << 3);
WrappedWatchableObject watch = new WrappedWatchableObject(0); WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0, b));
watch.setValue(b);
watchableList.add(watch); watchableList.add(watch);
packet.getWatchableCollectionModifier().write(0, watchableList); packet.getWatchableCollectionModifier().write(0, watchableList);
try { try {
@ -1313,7 +1311,6 @@ public class PacketsManager {
if (DisguiseConfig.isMetadataPacketsEnabled()) { if (DisguiseConfig.isMetadataPacketsEnabled()) {
List<WrappedWatchableObject> watchableObjects = disguise.getWatcher().convert( List<WrappedWatchableObject> watchableObjects = disguise.getWatcher().convert(
packets[0].getWatchableCollectionModifier().read(0)); packets[0].getWatchableCollectionModifier().read(0));
watchableObjects = DisguiseUtilities.rebuildForVersion(observer, disguise.getWatcher(), watchableObjects);
packets[0] = new PacketContainer(sentPacket.getType()); packets[0] = new PacketContainer(sentPacket.getType());
StructureModifier<Object> newMods = packets[0].getModifier(); StructureModifier<Object> newMods = packets[0].getModifier();
newMods.write(0, entity.getEntityId()); newMods.write(0, entity.getEntityId());
@ -1422,8 +1419,8 @@ public class PacketsManager {
// Convert the datawatcher // Convert the datawatcher
List<WrappedWatchableObject> list = new ArrayList<>(); List<WrappedWatchableObject> list = new ArrayList<>();
if (DisguiseConfig.isMetadataPacketsEnabled()) { if (DisguiseConfig.isMetadataPacketsEnabled()) {
WrappedWatchableObject watch = new WrappedWatchableObject(0); WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0,
watch.setValue(WrappedDataWatcher.getEntityWatcher(entity).getByte(0)); WrappedDataWatcher.getEntityWatcher(entity).getByte(0)));
list.add(watch); list.add(watch);
list = disguise.getWatcher().convert(list); list = disguise.getWatcher().convert(list);
} else { } else {
@ -1434,7 +1431,6 @@ public class PacketsManager {
} }
} }
} }
list = DisguiseUtilities.rebuildForVersion(observer, disguise.getWatcher(), list);
// Construct the packets to return // Construct the packets to return
PacketContainer packetBlock = new PacketContainer(Server.ENTITY_METADATA); PacketContainer packetBlock = new PacketContainer(Server.ENTITY_METADATA);
packetBlock.getModifier().write(0, entity.getEntityId()); packetBlock.getModifier().write(0, entity.getEntityId());

View File

@ -1,5 +1,8 @@
package me.libraryaddict.disguise.utilities; package me.libraryaddict.disguise.utilities;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer;
import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject;
import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.bukkit.Art; import org.bukkit.Art;
@ -14,6 +17,7 @@ import org.bukkit.potion.PotionEffect;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Map; import java.util.Map;
@ -457,9 +461,10 @@ public class ReflectionManager {
} }
public static void removePlayer(Player player) { public static void removePlayer(Player player) {
//Some future remove code if needed
} }
public static void setAllowSleep(Player player) { public static void setAllowSleep(Player player) {
try { try {
//TODO: Fix this! //TODO: Fix this!
@ -471,6 +476,25 @@ public class ReflectionManager {
} }
} }
/**
* This creates a DataWatcherItem usable with WrappedWatchableObject
* @param id
* @param value
* @return
*/
public static Object createDataWatcherItem(int id, Object value) {
if (value == null) return null;
Serializer serializer = Registry.get(value.getClass());
WrappedDataWatcherObject watcherObject = new WrappedDataWatcherObject(id, serializer);
Constructor construct = ReflectionManager.getNmsConstructor("DataWatcher$Item", getNmsClass("DataWatcherObject"), Object.class);
try {
return construct.newInstance(watcherObject.getHandle(), value);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) { public static void setBoundingBox(Entity entity, FakeBoundingBox newBox) {
try { try {
Location loc = entity.getLocation(); Location loc = entity.getLocation();