简体   繁体   中英

Java reflection - Get values from class instance field

I am trying to create an some cheat for Minecraft. For this I decided to use reflection and agents. At the first stage, a question arose. I have Minecraft.class class. This class has getMinecraft() method that returns the instance of Minecraft. And this instance has player field. From this field, i need to get posX variable. I think I did not explain very clearly, so here is an example code of what I want to do:

final net.minecraft.client.Minecraft mc = net.minecraft.client.Minecraft.getMinecraft();
System.out.println(mc.player.posX);

And now, is it possible to somehow implement this through reflection?

You use the following reflection methods:

Class<?> minecraftClass = Class.forName("net.minecraft.client.Minecraft");
Object mc = minecraftClass.getMethod("getMinecraft")
                          .invoke(null);
Object player = mc.getClass().getField("player")
                  .get(mc);
Object posX = player.getClass().getField("posX")
                    .get(player);
System.out.println(posX);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM