/** * This creates a DataWatcherItem usable with WrappedWatchableObject * * @param id * @param value * @return */ public static Object createDataWatcherItem(int id, Object value) { WrappedDataWatcherObject watcherObject = createDataWatcherObject(id, value); Constructor construct = getNmsConstructor("DataWatcher$Item", getNmsClass("DataWatcherObject"), Object.class); try { return construct.newInstance(watcherObject.getHandle(), convertInvalidMeta(value)); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return null; }
private static Object newHandle(WrappedDataWatcherObject watcherObject, Object value) { if (constructor == null) { constructor = Accessors.getConstructorAccessor(HANDLE_TYPE.getConstructors()[0]); } if (MinecraftReflection.watcherObjectExists()) { return constructor.invoke(watcherObject.getHandle(), value); } else { // new WatchableObject(classId, index, value) return constructor.invoke(WrappedDataWatcher.getTypeID(value.getClass()), watcherObject.getIndex(), value); } }
/** * Retrieve a watchable object by watcher object. * * @param object The watcher object * @return The watched object or null if it doesn't exist. */ public Object getObject(WrappedDataWatcherObject object) { Validate.notNull(object, "Watcher object cannot be null!"); if (GETTER == null) { FuzzyReflection fuzzy = FuzzyReflection.fromClass(handleType, true); if (MinecraftReflection.watcherObjectExists()) { GETTER = Accessors.getMethodAccessor(fuzzy.getMethod(FuzzyMethodContract.newBuilder() .parameterExactType(object.getHandleType()) .returnTypeExact(Object.class) .build(), "get")); } else { GETTER = Accessors.getMethodAccessor(fuzzy.getMethod(FuzzyMethodContract.newBuilder() .parameterExactType(int.class) .returnTypeExact(MinecraftReflection.getDataWatcherItemClass()) .build())); } } try { Object value = GETTER.invoke(handle, object.getHandle()); return WrappedWatchableObject.getWrapped(value); } catch (Exception ex) { // Nothing exists at this index return null; } }