Solved (Easy) getServer().getPlayer(targetname) not working

Discussion in 'Spigot Plugin Development' started by mkcoldwolf, May 7, 2017.

  1. Code (Text):
     @EventHandler
        public void onArmorseeClose(InventoryCloseEvent e) {
            if(e.getInventory().getName().contains("的裝備欄")){
                e.getPlayer().sendMessage(e.getInventory().getName().substring(0,e.getInventory().getName().indexOf("的")));
                String targetname = e.getInventory().getName().substring(0,e.getInventory().getName().indexOf("的"));
                Player targetPlayer = e.getPlayer().getServer().getPlayer(targetname);
             
             
                e.getPlayer().sendMessage("" + targetPlayer.getName());
                e.getPlayer().sendMessage("" + e.getInventory().getItem(0));
             
                targetPlayer.getInventory().setHelmet(e.getInventory().getItem(0));
                targetPlayer.getInventory().setChestplate(e.getInventory().getItem(1));
                targetPlayer.getInventory().setLeggings(e.getInventory().getItem(2));
                targetPlayer.getInventory().setBoots(e.getInventory().getItem(3));
            }
        }
    that is my code , some for testing it, Sorry about those chinese lol

    Error Message:
    Code (Text):
    [21:46:57 ERROR]: Could not pass event InventoryCloseEvent to KITPVP v0.0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleInventoryCloseEvent(CraftEventFactory.java:852) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1411) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:18) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:1) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_92]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_92]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_92]
    Caused by: java.lang.NullPointerException
            at kitpvp.main.ArmorseeSave.onArmorseeClose(ArmorseeSave.java:27) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_92]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_92]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 16 more
    i don't know why it's not working lol plz help, P.S This is in other class, in main class
    ,This work fine, line ArmorseeSave.java:27 is
    Code (Text):
    e.getPlayer().sendMessage("" + targetPlayer.getName());
    and if i change this
    Code (Text):
    Player targetPlayer = e.getPlayer().getServer().getPlayer(targetname);
    to
    Code (Text):
    Player targetPlayer = e.getPlayer().getServer().getPlayer("mkcoldwolf");
    it work fine lol WTF? PLZ HELPPPPPPPPPP

    EDIT
    e.getInventory().getName() == "mkcoldwolf的裝備欄"
     
    #1 mkcoldwolf, May 7, 2017
    Last edited: May 7, 2017
  2. Try to debug the "targetname" string

    Also you can do something like Bukkit.getPlayer(String);
     
  3. It cannot find the target player. Try printing out the targetName and look if it is the name you are looking for!
     
  4. yes there is
    Code (Text):
     e.getPlayer().sendMessage(e.getInventory().getName().substring(0,e.getInventory().getName().indexOf("的")));
    it print "mkcoldwolf" for me idk why he can not find the string
     
  5. after that i added this :
    Code (Text):
    e.getPlayer().sendMessage(targetname);
    it print "mkcoldwolf"
    but still not work
    And e.getInventory().getName() should be:
    "mkcoldwolf的裝備欄"
     
  6. Change this "e.getPlayer().getServer().getPlayer(targetname);" by this: "Bukkit.getPlayer(targetname);", and then add a "if(targetPlayer != null) {" condition
     
  7. not work :( same error
     
  8. I'm not sure this "targetname = e.getInventory().getName().substring(0,e.getInventory().getName().indexOf("的"));" is a player name ahah, that's maybe why when you put an username, it work, but when you put this, it don't
     
  9. told you e.getInventory().getName() == "mkcoldwolf的裝備欄"
    and i f i use this :
    Code (Text):
    e.getPlayer().sendMessage(targetname);
    it print "mkcoldwolf" too
     
  10. In the message to player use the substring that you get instead of getting a player
     
  11. what means??
     
  12. Use the string instead of the player object
     
  13. it already is...
    Code (Text):
    String targetname = e.getInventory().getName().substring(0,e.getInventory().getName().indexOf("的"));
     
  14. Are you passing the chinese characters to #getPlayer? Print the string before you pass it.
     
  15. RIP TOLD MANY TIMES
    e.getInventory().getName() should be:
    "mkcoldwolf的裝備欄"
    If i print this it send me my name "mkcoldwolf"
    Code (Text):
    e.getPlayer().sendMessage(targetname);
     
  16. Here:
    e.getPlayer().sendMessage("" + targetPlayer.getName());
    use: e.getPlayer().sendMessage("" + targename);
     
  17. Well, that is indeed odd. Because your NPE is because targetPlayer is null. But if targetname is what you claim it to be, then it shouldnt be null...

    I think you should clean up your code a bit to make things easier to follow and debug. Start with storing things in variables so you're not doing things like e.getInventory().getName() 15 times. And for the love of all that is good, null check everything before you use it.

    There are 3 variables you need to make and debug the output of. First is the name of the inventory. Store it in a String and output the value. Next is the target's name you extract from that. Store it in a String and output its value. Next is the target player object. Store it in a Player or OfflinePlayer object and output its value. Once you debug like this you will find the problem.
     
  18. There is no matches of that String with that Chinese letter/word (idk dont blame me). Could you should us line 27 (from the actual whole class)?
     
  19. Print the char array:

    Code (Java):
    targetName.chars().mapToObj(i -> (char) i).collect(Collectors.toList()) //print this
     
  20. actually targetname print right, it give me "mkcoldwolf"
    but this cannot work
    Code (Text):
    e.getPlayer().sendMessage("" + targetPlayer.getName());
    idk why this its't work:
    Code (Text):
    Player targetPlayer = e.getPlayer().getServer().getPlayer(targetname);
     
    I confirmed targetname is "mkcoldwolf"