Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit b36cd8d

Browse files
committed
add instanceOf check for NMS proxies
1 parent cdfe508 commit b36cd8d

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/main/java/me/theminecoder/minecraft/nmsproxy/proxy/NMSProxyProvider.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ private void registerNMSClasses(Class<? extends NMSProxy> clazz) {
6868
proxyToNMSClassMap.put(clazz, nmsClass);
6969
}
7070

71+
/**
72+
* Checks if the passed NMS object is an instance of the passed class
73+
*
74+
* @param object Object to check
75+
* @param clazz Class to check
76+
*/
77+
public boolean isInstanceOf(Object object, Class<? extends NMSProxy> clazz) {
78+
registerNMSClasses(clazz);
79+
80+
if (object instanceof NMSProxy) {
81+
object = ((NMSProxy) object).getProxyHandle();
82+
}
83+
84+
return proxyToNMSClassMap.get(clazz).isAssignableFrom(object.getClass());
85+
}
86+
7187
/**
7288
* Generates a static only proxy to an NMS class
7389
*
@@ -83,7 +99,7 @@ public <T extends NMSProxy> T getStaticNMSObject(Class<T> clazz) {
8399
/**
84100
* Generates a proxy to an NMS class instance
85101
*
86-
* @param clazz {@link NMSClass} annotated {@link NMSProxy} interface.
102+
* @param clazz {@link NMSClass} annotated {@link NMSProxy} interface.
87103
* @param object Object to proxy
88104
* @return Generated Proxy
89105
*/
@@ -99,7 +115,8 @@ public <T extends NMSProxy> T getNMSObject(Class<T> clazz, Object object) {
99115

100116
/**
101117
* Constructs and returns a NMS object wrapped in a proxy.
102-
* @param clazz {@link NMSClass} annotated {@link NMSProxy} interface class
118+
*
119+
* @param clazz {@link NMSClass} annotated {@link NMSProxy} interface class
103120
* @param params Objects to pass to the constructor (NMSProxy instances will be converted to their actual objects for you)
104121
* @return The constructed NMS object wrapped in a proxy.
105122
* @throws ReflectiveOperationException
@@ -117,11 +134,11 @@ public <T extends NMSProxy> T constructNMSObject(Class<T> clazz, Object... param
117134

118135
/**
119136
* Constructs and returns an object that is a subclass of another NMS class.
120-
*
137+
* <p>
121138
* Something of note here is that the class you use to define the subclass is not the same class you get back.
122139
* This is due to the use of a dynamic class generator to do the backend work with subclassing.
123140
*
124-
* @param clazz Class implementing another {@link NMSProxy} and {@link NMSSubclass}
141+
* @param clazz Class implementing another {@link NMSProxy} and {@link NMSSubclass}
125142
* @param params Objects to pass to the constructor of the final subclass
126143
* @return The constructed object (Note that this is not wrapped in a proxy)
127144
* @throws ReflectiveOperationException
@@ -171,8 +188,7 @@ public <T extends NMSSubclass> T constructNMSSubclass(Class<T> clazz, Object...
171188
proxyToNMSSubclassMap.put(clazz, dynamicType);
172189
}
173190

174-
return (T) dynamicType.getLoaded().getConstructor(Arrays.stream(params).map(Object::getClass).toArray(Class[]::new))
175-
.newInstance(params);
191+
return (T) dynamicType.getLoaded().getConstructor(Arrays.stream(params).map(Object::getClass).toArray(Class[]::new)).newInstance(params);
176192
}
177193

178194
Object[] unwrapArguments(Object[] args) {

0 commit comments

Comments
 (0)