diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java index 83900523..9de076f5 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedDataWatcher.java @@ -25,9 +25,12 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.bukkit.entity.Entity; import org.bukkit.inventory.ItemStack; +import com.comphenix.protocol.injector.BukkitUnwrapper; import com.comphenix.protocol.reflect.FieldAccessException; +import com.comphenix.protocol.reflect.FieldUtils; import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.accessors.Accessors; import com.comphenix.protocol.reflect.accessors.ConstructorAccessor; @@ -39,11 +42,23 @@ public class WrappedDataWatcher extends AbstractWrapper implements Iterable + * Warning: This is only supported on 1.7.2 and above. + * + * @return The entity, or NULL. + */ + public Entity getEntity() { + if (!MinecraftReflection.isUsingNetty()) + throw new IllegalStateException("This method is only supported on 1.7.2 and above."); + + try { + return (Entity) MinecraftReflection.getBukkitEntity(ENTITY_FIELD.get(handle)); + } catch (Exception e) { + throw new RuntimeException("Unable to retrieve entity.", e); + } + } + + /** + * Set the entity associated with this data watcher. + *

+ * Warning: This is only supported on 1.7.2 and above. + * + * @param entity - the new entity. + */ + public void setEntity(Entity entity) { + if (!MinecraftReflection.isUsingNetty()) + throw new IllegalStateException("This method is only supported on 1.7.2 and above."); + + try { + ENTITY_FIELD.set(handle, BukkitUnwrapper.getInstance().unwrapItem(entity)); + } catch (Exception e) { + throw new RuntimeException("Unable to set entity.", e); + } + } + @Deprecated @SuppressWarnings("unused") public static Integer getTypeID(Class clazz) { diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java index 5a9fb97e..08d2aa2b 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/WrappedWatchableObject.java @@ -17,9 +17,12 @@ public class WrappedWatchableObject extends AbstractWrapper { private WrappedWatchableObject() { super(MinecraftReflection.getDataWatcherItemClass()); - } + /** + * Constructs a wrapped watchable object around an existing NMS data watcher item. + * @param handle Data watcher item + */ public WrappedWatchableObject(Object handle) { this(); setHandle(handle); @@ -29,10 +32,18 @@ public class WrappedWatchableObject extends AbstractWrapper { // ---- Getter methods + /** + * Gets the wrapped value of this data watcher item. + * @return The wrapped value + */ public Object getValue() { return getWrapped(getRawValue()); } + /** + * Gets the raw value of this data watcher item. + * @return Raw value + */ public Object getRawValue() { return modifier.readSafely(1); }