66import me .theminecoder .minecraft .nmsproxy .NMSProxy ;
77import me .theminecoder .minecraft .nmsproxy .annotations .NMSClass ;
88import me .theminecoder .minecraft .nmsproxy .util .NullReference ;
9+ import net .md_5 .bungee .api .plugin .Plugin ;
910import org .bukkit .Bukkit ;
1011import org .bukkit .plugin .java .JavaPlugin ;
1112
1617/**
1718 * @author theminecoder
1819 */
19- @ SuppressWarnings ({"rawtypes" , "unchecked" })
20+ @ SuppressWarnings ({"rawtypes" , "unchecked" , "JavaDoc" })
2021public final class NMSProxyProvider {
2122
2223 public static final String NMS_VERSION ;
@@ -33,21 +34,29 @@ public final class NMSProxyProvider {
3334 NMS_VERSION = LOADED_VERSION ;
3435 }
3536
36- private static final Map <JavaPlugin , NMSProxyProvider > PLUGIN_INSTANCES = Maps .newHashMap ();
37+ @ Deprecated
38+ private static final Map <Object , NMSProxyProvider > PLUGIN_INSTANCES = Maps .newHashMap ();
3739
38- private BiMap <Class , Class > proxyToNMSClassMap = HashBiMap .create ();
39- private NMSProxyInvocationMapper invocationMapper = new NMSProxyInvocationMapper (proxyToNMSClassMap );
40+ private final BiMap <Class , Class > proxyToNMSClassMap = HashBiMap .create ();
41+ private final NMSProxyInvocationMapper invocationMapper = new NMSProxyInvocationMapper (proxyToNMSClassMap );
4042
41- private NMSProxyProvider () {
43+ public NMSProxyProvider () {
4244 }
4345
46+ /**
47+ * @deprecated Just make your own instance now.
48+ */
49+ @ Deprecated
4450 public static NMSProxyProvider get (JavaPlugin plugin ) {
45- NMSProxyProvider instance = PLUGIN_INSTANCES .get (plugin );
46- if (instance == null ) {
47- instance = new NMSProxyProvider ();
48- PLUGIN_INSTANCES .put (plugin , instance );
49- }
50- return instance ;
51+ return PLUGIN_INSTANCES .computeIfAbsent (plugin , __ -> new NMSProxyProvider ());
52+ }
53+
54+ /**
55+ * @deprecated Just make your own instance now.
56+ */
57+ @ Deprecated // Only making this to allow for copy/paste into bungee plugins
58+ public static NMSProxyProvider get (Plugin plugin ) {
59+ return PLUGIN_INSTANCES .computeIfAbsent (plugin , __ -> new NMSProxyProvider ());
5160 }
5261
5362 private void registerNMSClasses (Class <? extends NMSProxy > clazz ) {
@@ -133,30 +142,36 @@ public <T extends NMSProxy> T getNMSObject(Class<T> clazz, Object object) {
133142 * Constructs and returns a NMS object wrapped in a proxy.
134143 *
135144 * @param clazz {@link NMSClass} annotated {@link NMSProxy} interface class
136- * @param params Objects to pass to the constructor (NMSProxy instances will be converted to their actual objects for you)
145+ * @param params Objects to pass to the constructor (NMSProxy instances will be converted to their actual objects for you).
146+ * Use of null must be modified to use a {@link NullReference} object instead, so the type of null is known.
137147 * @return The constructed NMS object wrapped in a proxy.
138148 * @throws ReflectiveOperationException
139149 */
140150 public <T extends NMSProxy > T constructNMSObject (Class <T > clazz , Object ... params ) throws ReflectiveOperationException {
141151 registerNMSClasses (clazz );
142152
143153 NullReference [] nullReferences = new NullReference [params .length ];
144- Object [] fixedArgs = unwrapArguments (params );
145154
146155 //pull out null references and swap them to null
147- for (int i = 0 ; i < fixedArgs .length ; i ++) {
148- if (fixedArgs [i ] instanceof NullReference ) {
149- nullReferences [i ] = (NullReference ) fixedArgs [i ];
150- fixedArgs [i ] = null ;
156+ for (int i = 0 ; i < params .length ; i ++) {
157+ if (params [i ] == null ) throw new IllegalArgumentException ("null argument is not supported directly. Use a NullReference instead." );
158+ if (params [i ] instanceof NullReference ) {
159+ nullReferences [i ] = (NullReference ) params [i ];
160+ params [i ] = null ;
151161 }
152162 }
153163
154- Class [] fixedArgTypes = Arrays .stream (fixedArgs ).map (Object ::getClass ).toArray (Class []::new );
164+ Object [] fixedArgs = unwrapArguments (params );
165+ Class [] fixedArgTypes = Arrays .stream (fixedArgs ).map (arg -> arg != null ? arg .getClass () : null ).toArray (Class []::new );
155166
156- //swap type search with null reference types
157167 for (int i = 0 ; i < nullReferences .length ; i ++) {
158168 if (nullReferences [i ] != null ) {
159- fixedArgTypes [i ] = nullReferences [i ].getType ();
169+ Class type = nullReferences [i ].getType ();
170+ if (NMSProxy .class .isAssignableFrom (type )) {
171+ this .registerNMSClasses (type );
172+ type = proxyToNMSClassMap .get (type );
173+ }
174+ fixedArgTypes [i ] = type ;
160175 }
161176 }
162177
0 commit comments