Solved Player object will cause troubles in a map?

Discussion in 'Plugin Development' started by Gonmarte, Mar 25, 2016.

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

    Gonmarte

    If i store a Player object in a map and if he changes the name, it wont cause memory leaks? It wont be any problem with the map? I know that UUID would be a solution, but i cannot in my code.
     
    Last edited: Mar 25, 2016
  2. Offline

    WolfMage1

    Why can;t you store UUID's in your map? Please be a bit more specific why it wont work?
     
    Zombie_Striker likes this.
  3. Offline

    Zombie_Striker

    Why? There should be no reason why you cannot store UUID instead of player objects. Using UUIDs are the recommend way to save player objects, since they are both smaller objects and should not cause memory leaks.
     
  4. Offline

    Gonmarte

    @Zombie_Striker @WolfMage1
    Its hard to explain.
    I have 2 hashmaps:
    Code:
         public static HashMap<UUID, String> players = new HashMap<>();
                        //Cannot use UUID
    public static HashMap<UUID, Player> clanRequests = new HashMap<>();
    So im going to try to explain to you in a fast way. When i do /clan invite Zombie_Striker it will add Zombie_Striker to the clan requests map, benig the key, and it will the player who run the command (the owner of the clan) to the map as well, being the value. So, when zombie_striker do /clan accept , it will have some checks to see if he got any request bla bla but if everything goes well i need to add him to the players map, the keys are uuid of players and the string is the clan name. So i can put the Zombie_Striker in the map as a key, thats wont be a problem, but then i need to get the clan of the player who invited zombie_striker, and i have a method for getting the clan of a player but that needs a player object as paramter, so thats way i cannot use UUID in the map, and i have to use Player.
    If i was confuse tell - me i will try to explaim again.
     
  5. Offline

    mcdorli

    Why do it needs the player object?
    Also, why are those 2 variables static?
     
    Zombie_Striker likes this.
  6. Offline

    Zombie_Striker

    This is all you have to say.

    Use this to turn UUIDs to player objects
    Code:
    //if the player is online
    Bukkit.getPlayer(UUID);
    
    //if the player is not online
    Bukkit.getOfflinePlayer(UUID);
     
    WolfMage1 likes this.
  7. Offline

    Gonmarte

    Im to lazy for getters and setters methods :p
    I will try it.
    EDIT: @Zombie_Striker If i do Bukkit.getPlayer(player.getUniqueId()) it will give me a red line... im not sure but maybe its because im using 1.7.2 craftbukkit version.
     
  8. Offline

    mcdorli

    Then don't code, if you're too lazy for a getter, then how will you make bigger and more complicated things?

    Isn't it a yellow line?
     
  9. Offline

    Gonmarte

    Ik... I will work on a getter right now.
    No, its a red line.
     
  10. Offline

    mcdorli

    Are you sure you supply the UUID object, and not a tring? Sbow us your code
     
  11. Offline

    Zombie_Striker

    @Gonmarte
    What does the error message for the read line say?
     
  12. Offline

    Gonmarte

    @mcdorli @Zombie_Striker
    Error message:
    The method getPlayer(String) in the type Bukkit is not applicable for the arguments (UUID)
    Code:
    Code:
     ClanManager.players.put(player.getUniqueId(), ClanManager.getClan(clanRequests.get(Bukkit.getPlayer(player.getUniqueId()))));
     
  13. Offline

    Zombie_Striker

    @Gonmarte
    It may be that some newer versions of bukkit have this method. Versions 1.7.9 should have the UUID feature.

    Since you should use UUIDs, you can loop through all the player s online and test if their UUID is equal to the one you're testing for. This is essentially what the ".getPlayer" method does.
     
  14. Offline

    Gonmarte

    Code:
       
    public Player getPlayer(Player player){
            for(Player players : Bukkit.getOnlinePlayers()){
                if(players.getUniqueId()==player.getUniqueId()){
                    return player;
                }
            }
            return null;
        }
    
    Not working..
    Im downloading the crafbukkit 1.7.9 btw
     
  15. Offline

    mcdorli

    DON'T COMPARE OBJECTS WITH ==
     
  16. Offline

    Zombie_Striker

    @Gonmarte
    Use .equals instead of ==. '==' compares where in the memory the objects are located. '.equals' compares the values of the objects.
     
  17. Offline

    Gonmarte

    @mcdorli @Zombie_Striker
    Sorry.
    But it wont work anyway, i just downloaded the craftbukkit 1.7.9 now it underlines the method getClan..
    The error is, as i told you, The method getClan(Player) in the type ClanManager is not applicable for the arguments (UUID)

    Code:
    Code:
     
                        ClanManager.players.put(player.getUniqueId(), ClanManager.getClan(clanRequests.get(Bukkit.getPlayer(player.getUniqueId()))));
    
    Code:
    public static String getClan(Player player) {
        if (players.containsKey(player.getUniqueId())){
            return players.get(player.getUniqueId());
        }
        return null;
    }
    
     
  18. Offline

    mcdorli

    Set the argument to a UUID, and get the player in the method
     
  19. Offline

    Zombie_Striker

    This most likely does not have anything to do with the version of craftbukkit, but instead something you did with that line.

    First, change the '==' to '.equals' inside the 'getPlayer' method. After that ,post what the error message says.
     
  20. Offline

    Gonmarte

    What method? getClan or getPlayer?
    I did.
    Error:
    The method getPlayer(Player) in the type Commands is not applicable for the arguments (UUID)
     
  21. Offline

    Zombie_Striker

    @Gonmarte
    That is because you have this:
    What you meant to put in was "UUID" uuid, as you do not have the player and you are instead looking for the player. Change "Player player" to "UUID uuid", and change "player.getUnqueID" to "uuid"
     
  22. Offline

    Gonmarte

    How would i loop through all the uuids?
     
  23. Create a for loop that looks through all the UUIDs.

    _zircon_.
     
  24. Offline

    Gonmarte

    Dont say that sherlock -.-
    Its not that my problem.
    @Zombie_Striker Its impossible to use UUID in the map once my getClan() function needs needs a Player at param.. unless i can change that param to a UUID it wont be possible.
     
  25. Offline

    mcdorli

    Change it to UUID, then inside the getClans method, get the player.
     
  26. Offline

    Gonmarte

    Thank you :D
    Its not giving an error in the code, although i havent tested it yet.
    Im going to test it now!
    Code:
    public static String getClan(UUID id) {
        if (players.containsKey(Bukkit.getPlayer(id))){
            return players.get(Bukkit.getPlayer(id));
        }
        return null;
    }
    
    @mcdorli Didnt work :/
    Error:
    Code:
       
    issued server command: /clan accept
    2016-03-25 23:14:46 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'clan' in plugin Clans v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:965)
        at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:883)
        at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:840)
        at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
        at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:109)
        at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NoSuchMethodError: org.bukkit.Bukkit.getPlayer(Ljava/util/UUID;)Lorg/bukkit/entity/Player;
        at Clans.ClanManager.getClan(ClanManager.java:116)
        at ClanCommands.Commands.onCommand(Commands.java:177)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
     
    Last edited: Mar 25, 2016
  27. Offline

    Irantwomiles

    I'm going to tell you this now. I tried exactly what you are doing now and let me tell you, being lazy is the least of your problems. Somewhere down the line this WILL break. Take it from someone who has done it and failed. From what I can tell you are making some sort of team/clan plugin. Make a Team/Clan managers class that adds/removes, invites and uninvite's players to teams. It will make your life easier.
     
  28. Offline

    mcdorli

    Your version doesn't contain the getPlayer(UUID uuid) method, update it.
     
  29. Offline

    Gonmarte

    I have already failed to and im now folowing the @mcdorli tutorial I have already a ClanManager class and a Clan class that represents each clan, dont worry.

    You mean my minecraft version? I was testing on 1.5.2 , do i need to uptade it?
     
    Last edited: Mar 26, 2016
  30. Offline

    mcdorli

    Your bukkit version, you should update to at least 1.7.9
     
Thread Status:
Not open for further replies.

Share This Page