dimanche 31 juillet 2016

When trying to set a private static final field using reflections, I get an error saying I cannot set the object to another object

I am working on a plugin for a Minecraft server to change an item type to another item type (using reflections). I have most of it finished, but I have run into an issue setting a single field. code:

Field WHEAT = Items.class.getDeclaredField("WHEAT");
Method get = Items.class.getDeclaredMethod("get", String.class);
get.setAccessible(true);
WHEAT.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(WHEAT, WHEAT.getModifiers() & ~Modifier.FINAL);
WHEAT.set(null, (Item) get.invoke(null,"wheat"));

I have gotten this far via this thread:Change private static final field using Java reflection

but I cannot figure out where to go from here. Anyone have any suggestions?





Aucun commentaire:

Enregistrer un commentaire