/** * Remove a entry in the IntHashMap using a fallback method. * @param key - the key of the mapping to remove. * @return The removed element. */ private Object removeFallback(int key) { Object old = get(key); invokeMethod(PUT_METHOD, key, null); return old; }
@Override public void setLookup(int packetID, Class<?> clazz) { intHashMap.put(packetID, clazz); }
/** * Construct a wrapper around a given NMS IntHashMap. * @param handle - the NMS IntHashMap. * @return The created wrapped. * @throws IllegalArgumentException If the handle is not an IntHasMap. */ public static WrappedIntHashMap fromHandle(@Nonnull Object handle) { return new WrappedIntHashMap(handle); }
/** * Remove a mapping of a key to a value if it is present. * @param key - the key of the mapping to remove. * @return The object that was removed, or NULL if the key is not present. */ public Object remove(int key) { initializeGetMethod(); if (REMOVE_METHOD == null) return removeFallback(key); return invokeMethod(REMOVE_METHOD, key); }
/** * Retrieve the value associated with a specific key, or NULL if not found. * @param key - the integer key. * @return The associated value, or NULL. */ public Object get(int key) { initializeGetMethod(); return invokeMethod(GET_METHOD, key); }
private void initializeGetMethod() { if (GET_METHOD == null) { WrappedIntHashMap temp = WrappedIntHashMap.newMap(); String expected = "hello"; temp.put(1, expected); boolean first = expected.equals(method.invoke(temp.getHandle(), 1)); boolean second = expected.equals(method.invoke(temp.getHandle(), 1));
private static Object getEntityTrackerEntry(World world, int entityID) throws FieldAccessException, IllegalArgumentException { BukkitUnwrapper unwrapper = new BukkitUnwrapper(); Object worldServer = unwrapper.unwrapItem(world); if (entityTrackerField == null) entityTrackerField = FuzzyReflection.fromObject(worldServer). getFieldByType("tracker", MinecraftReflection.getEntityTrackerClass()); // Get the tracker Object tracker = null; try { tracker = FieldUtils.readField(entityTrackerField, worldServer, false); } catch (IllegalAccessException e) { throw new FieldAccessException("Cannot access 'tracker' field due to security limitations.", e); } // Looking for an IntHashMap in the tracker entry if (trackedEntitiesField == null) { trackedEntitiesField = FuzzyReflection.fromObject(tracker, false) .getFieldByType("trackedEntities", MinecraftReflection.getIntHashMapClass()); } // Read the map Object trackedEntities = null; try { trackedEntities = FieldUtils.readField(trackedEntitiesField, tracker, false); } catch (IllegalAccessException e) { throw new FieldAccessException("Cannot access 'trackedEntities' field due to security limitations.", e); } return WrappedIntHashMap.fromHandle(trackedEntities).get(entityID); }
/** * Associates a specified key with the given value in the integer map. * <p> * If the key has already been associated with a value, then it will be replaced by the new value. * @param key - the key to insert. * @param value - the value to insert. Cannot be NULL. * @throws RuntimeException If the reflection machinery failed. */ public void put(int key, Object value) { Preconditions.checkNotNull(value, "value cannot be NULL."); initializePutMethod(); putInternal(key, value); }
/** * Invoked when a value must be inserted into the underlying map, regardless of preconditions. * @param key - the key. * @param value - the value to insert. */ private void putInternal(int key, Object value) { invokeMethod(PUT_METHOD, key, value); }
private void initialize() throws IllegalAccessException { if (intHashMap == null) { // We're looking for the first static field with a Minecraft-object. This should be a IntHashMap. Field intHashMapField = FuzzyReflection.fromClass(MinecraftReflection.getPacketClass(), true). getFieldByType("packetIdMap", MinecraftReflection.getIntHashMapClass()); try { intHashMap = WrappedIntHashMap.fromHandle( FieldUtils.readField(intHashMapField, (Object) null, true)); } catch (IllegalArgumentException e) { throw new RuntimeException("Minecraft is incompatible.", e); } } } }
/** * Construct a new IntHashMap. * @return A new IntHashMap. */ public static WrappedIntHashMap newMap() { try { return new WrappedIntHashMap(MinecraftReflection.getIntHashMapClass().newInstance()); } catch (Exception e) { throw new RuntimeException("Unable to construct IntHashMap.", e); } }