EntityHuman and NPCLib problem [Read first]

Discussion in 'Plugin Development' started by kreashenz, Nov 22, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    kreashenz

    So I'm trying to fix something in NPCLib which is the setName(String str) method in the HumanNPC class. I can't seem to return the EntityHuman class so I can set a custom name through that.
    I have this code
    Code:java
    1. public void setName(String name) {
    2. try {
    3. Object ob = getEntity().getBukkitEntity().getClass().getMethod("getHandle").invoke(getEntity().getBukkitEntity());
    4.  
    5. System.out.print(ob.getClass().getName());
    6. // prints out 'com.sharesc.caliog.npclib.NPCEntity'
    7.  
    8. Field f = ob.getClass().getField("name");
    9. f.setAccessible(true);
    10. f.set(ob, name);
    11. } catch(Throwable t){
    12. t.printStackTrace();
    13. }
    14. }


    My question is: How to retrieve the EntityHuman (NMS version) class via reflection?
     
  2. Offline

    xTrollxDudex

    kreashenz
    You mean EntityHuman?
    PHP:
    Class<?clazz = Class.forName("net.minecraft.server." Bukkit.getServer().getClass().getPackage().getName().replace("."",").split(",")[3] + "EntityHuman");
     
  3. Offline

    kreashenz

  4. Offline

    xTrollxDudex

    kreashenz
     
  5. Offline

    kreashenz

    xTrollxDudex Ok, I've got this now
    Code:java
    1. public void setName(String name) {
    2. try {
    3. Object ob = Class.forName("net.minecraft.server." + Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3] + ".EntityHuman");
    4. Field f = ob.getClass().getField("name");
    5. f.setAccessible(true);
    6. f.set(ob, name);
    7. } catch(Throwable t){
    8. t.printStackTrace();
    9. }
    10. }

    But I get this error..
    Code:
    2013-11-23 13:36:18 [SEVERE] java.lang.NoSuchFieldException: name
    2013-11-23 13:36:18 [SEVERE]    at java.lang.Class.getField(Unknown Source)
    2013-11-23 13:36:18 [SEVERE]    at com.sharesc.caliog.npclib.HumanNPC.setName(HumanNPC.java:58)
    2013-11-23 13:36:18 [SEVERE]    at me.kreashenz.Test.onCommand(Test.java:34)
    2013-11-23 13:36:18 [SEVERE]    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    2013-11-23 13:36:18 [SEVERE]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    2013-11-23 13:36:18 [SEVERE]    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:968)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:886)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:837)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    2013-11-23 13:36:18 [SEVERE]    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Even though the field is there it's still giving that error.
     
  6. Offline

    LazyLemons

    Use
    Code:
    Field f = ob.getClass().getDeclaredField("name");
    , the field isn't public.
     
  7. Offline

    kreashenz

    LazyLemons xTrollxDudex Ok, no more errors but apparently there NPC couldn't be found using this code
    Code:java
    1. if(s instanceof Player){
    2. Player p = (Player)s;
    3. if(args.length == 0){
    4. p.sendMessage("use /test <name> or /test setname <npcId> <name>");
    5. } else {
    6. if(!args[0].equalsIgnoreCase("setname")){
    7. HumanNPC npc = (HumanNPC)manager.spawnHumanNPC(args[0], p.getLocation());
    8. p.sendMessage("NPC id: " + manager.getNPCIdFromEntity(npc.getBukkitEntity()));
    9. } else {
    10. HumanNPC npc = (HumanNPC)manager.getNPC(args[1]);
    11. if(npc != null){
    12. npc.setName(ChatColor.translateAlternateColorCodes('&', args[2].replace("_", " ")));
    13. } else p.sendMessage("no such NPC");
    14. }
    15. }
    16. }

    When I spawn the NPC using /test <name> it tells me the ID is the args[0] and that seems to work but it doesn't set the name... Any help with that?
     
  8. Offline

    xTrollxDudex

    kreashenz
    Wait... I don't get how you use just that one argument...
     
  9. Offline

    kreashenz

    xTrollxDudex It's only a test command, it doesn't really matter, does it?
     
  10. Offline

    xTrollxDudex

    kreashenz
    I can't really see what you're trying to do with THAT
     
Thread Status:
Not open for further replies.

Share This Page