|
17 | 17 | package com.comphenix.protocol.wrappers;
|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Field;
|
20 |
| -import java.lang.reflect.Method; |
21 | 20 | import java.lang.reflect.Modifier;
|
22 | 21 | import java.lang.reflect.ParameterizedType;
|
23 | 22 | import java.lang.reflect.Type;
|
|
39 | 38 | import com.comphenix.protocol.reflect.accessors.ConstructorAccessor;
|
40 | 39 | import com.comphenix.protocol.reflect.accessors.FieldAccessor;
|
41 | 40 | import com.comphenix.protocol.reflect.accessors.MethodAccessor;
|
| 41 | +import com.comphenix.protocol.reflect.fuzzy.FuzzyFieldContract; |
42 | 42 | import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
|
43 | 43 | import com.comphenix.protocol.utility.MinecraftReflection;
|
44 | 44 | import com.comphenix.protocol.wrappers.collection.ConvertedMap;
|
@@ -99,6 +99,20 @@ public WrappedDataWatcher(Entity entity) {
|
99 | 99 | this(newHandle(BukkitUnwrapper.getInstance().unwrapItem(entity)));
|
100 | 100 | }
|
101 | 101 |
|
| 102 | + /** |
| 103 | + * Constructs a new DataWatcher using a fake lightning entity and a given |
| 104 | + * list of watchable objects. |
| 105 | + * |
| 106 | + * @param objects The list of objects |
| 107 | + */ |
| 108 | + public WrappedDataWatcher(List<WrappedWatchableObject> objects) { |
| 109 | + this(); |
| 110 | + |
| 111 | + for (WrappedWatchableObject object : objects) { |
| 112 | + setObject(object.getWatcherObject(), object); |
| 113 | + } |
| 114 | + } |
| 115 | + |
102 | 116 | private static Object newHandle(Object entity) {
|
103 | 117 | if (constructor == null) {
|
104 | 118 | constructor = Accessors.getConstructorAccessor(HANDLE_TYPE, MinecraftReflection.getEntityClass());
|
@@ -127,16 +141,10 @@ private static Object fakeEntity() {
|
127 | 141 | private Map<Integer, Object> getMap() {
|
128 | 142 | if (MAP_FIELD == null) {
|
129 | 143 | FuzzyReflection fuzzy = FuzzyReflection.fromClass(handleType, true);
|
130 |
| - List<Field> candidates = fuzzy.getFieldListByType(Map.class); |
131 |
| - |
132 |
| - for (Field candidate : candidates) { |
133 |
| - if (Modifier.isStatic(candidate.getModifiers())) { |
134 |
| - // This is the entity class to current index map, which we really don't have a use for |
135 |
| - } else { |
136 |
| - // This is the map we're looking for |
137 |
| - MAP_FIELD = Accessors.getFieldAccessor(candidate); |
138 |
| - } |
139 |
| - } |
| 144 | + MAP_FIELD = Accessors.getFieldAccessor(fuzzy.getField(FuzzyFieldContract.newBuilder() |
| 145 | + .banModifier(Modifier.STATIC) |
| 146 | + .typeDerivedOf(Map.class) |
| 147 | + .build())); |
140 | 148 | }
|
141 | 149 |
|
142 | 150 | if (MAP_FIELD == null) {
|
@@ -329,7 +337,11 @@ public Object getObject(WrappedDataWatcherObject object) {
|
329 | 337 | Validate.notNull(object, "Watcher object cannot be null!");
|
330 | 338 |
|
331 | 339 | if (GETTER == null) {
|
332 |
| - GETTER = Accessors.getMethodAccessor(handleType, "get", object.getHandleType()); |
| 340 | + FuzzyReflection fuzzy = FuzzyReflection.fromClass(handleType); |
| 341 | + GETTER = Accessors.getMethodAccessor(fuzzy.getMethod(FuzzyMethodContract.newBuilder() |
| 342 | + .parameterExactType(object.getHandleType()) |
| 343 | + .returnTypeExact(Object.class) |
| 344 | + .build(), "get")); |
333 | 345 | }
|
334 | 346 |
|
335 | 347 | Object value = GETTER.invoke(handle, object.getHandle());
|
@@ -392,18 +404,13 @@ public void setObject(WrappedDataWatcherObject object, Object value) {
|
392 | 404 |
|
393 | 405 | if (SETTER == null || REGISTER == null) {
|
394 | 406 | FuzzyReflection fuzzy = FuzzyReflection.fromClass(handleType, true);
|
395 |
| - List<Method> methods = fuzzy.getMethodList(FuzzyMethodContract.newBuilder() |
| 407 | + FuzzyMethodContract contract = FuzzyMethodContract.newBuilder() |
396 | 408 | .banModifier(Modifier.STATIC)
|
397 | 409 | .requireModifier(Modifier.PUBLIC)
|
398 | 410 | .parameterExactArray(object.getHandleType(), Object.class)
|
399 |
| - .build()); |
400 |
| - for (Method method : methods) { |
401 |
| - if (method.getName().equals("set")) { |
402 |
| - SETTER = Accessors.getMethodAccessor(method); |
403 |
| - } else if (method.getName().equals("register")) { |
404 |
| - REGISTER = Accessors.getMethodAccessor(method); |
405 |
| - } |
406 |
| - } |
| 411 | + .build(); |
| 412 | + SETTER = Accessors.getMethodAccessor(fuzzy.getMethod(contract, "set")); |
| 413 | + REGISTER = Accessors.getMethodAccessor(fuzzy.getMethod(contract, "register")); |
407 | 414 | }
|
408 | 415 |
|
409 | 416 | if (hasIndex(object.getIndex())) {
|
@@ -613,6 +620,19 @@ public Serializer getSerializer() {
|
613 | 620 | public String toString() {
|
614 | 621 | return "DataWatcherObject[index=" + getIndex() + ", serializer=" + getSerializer() + "]";
|
615 | 622 | }
|
| 623 | + |
| 624 | + @Override |
| 625 | + public boolean equals(Object obj) { |
| 626 | + if (obj == this) return true; |
| 627 | + if (obj == null) return false; |
| 628 | + |
| 629 | + if (obj instanceof WrappedDataWatcherObject) { |
| 630 | + WrappedDataWatcherObject other = (WrappedDataWatcherObject) obj; |
| 631 | + return handle.equals(other.handle); |
| 632 | + } |
| 633 | + |
| 634 | + return false; |
| 635 | + } |
616 | 636 | }
|
617 | 637 |
|
618 | 638 | /**
|
|
0 commit comments