how do you convert Player to String

Discussion in 'Plugin Development' started by jakepozaic, Jan 4, 2012.

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

    jakepozaic

    [SOLVED] I am relatively new to plugins and I am looking for a way to change Player to string for use with a hashmap I need to do this because 'event.getPlayer()' returns a Player while 'sender.getName()' returns a String heres my source.


    Code:
    public void onPlayerLogin(PlayerJoinEvent event)
        {
            if (XXX.debug = true)
            {
                System.out.println((event.getPlayer()) + "has logged checking if tables have been generated...");
            }
            //on login check if there is a key set for that player
            //if yes then close if no then create a new key for the player
            //get if there is a key set for player name if false then create one
            if( XXX.isXXX.containsKey(event.getPlayer()) )
            {
    
            Player playername = (event.getPlayer());
            String playername1 = playername;
    
                if (XXX.debug = true)
                {
                    System.out.println((event.getPlayer()) + "has tables generated.");
                }
            }
            else
            {
                if (XXX.debug = true)
                {
                    System.out.println((event.getPlayer()) + "doesnt have tables generated.");
                    System.out.println("generating tables for" + (event.getPlayer()));
                }
            XXX.bite.put(playername, true);
            XXX.bite.put(playername, true);
            }
        }
    }

    Code:
    static HashMap<String, Boolean> bite = new HashMap<String, Boolean>();
        static HashMap<String, Boolean> isXXX = new HashMap<String, Boolean>();
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if(cmd.getName().equalsIgnoreCase("bite"))
            {
                //if player is XXX
                //{
                    bite.put(sender.getName(), true);
                    sender.sendMessage("Your next attack will XXX someone with XXX!");
                    if (debug = true)
                    {
                        sender.sendMessage("" + bite);
                    }
                  //}else{
                    //end code
                //}
                return true;
            }
            return false;
        }
    also if anyone knows how to save/load hashmaps that would be great (I cant figure out the SLAPI on the wiki's tutorial...)

    thanks
     
  2. Offline

    Father Of Time

    We sure do see this comment a lot, it needs to be added to a "generic FAQ".

    The problem is that a player isn't just a string, its an instance of a class that stores a ton of information, one of which is a String named "Name". When you just print a player object you are actually calling a function that returns a pre defined string that contains a a few peices of information about the player object itself. Instead what you are wanting is to grab a single string variable from the player object named "Name". Lucky the player class has a built in function named "getName()" that returns the string from the player object.

    So to put it simple, all you need to do is change:

    Code:
    event.getPlayer()
    to this:

    Code:
    event.getPlayer().getName()
    I hope this help clear things up, take care!
     
    jakepozaic likes this.
  3. Offline

    jakepozaic

    lol thanks, it would have take me a week to figure that out XD
     
    Father Of Time likes this.
  4. Father Of Time is correct, just simply change event.getPlayer() to event.getPlayer().getName() ;)
     
  5. Offline

    Father Of Time

    It's my pleasure, I am happy to be of assistance. :D
     
    r3Fuze likes this.
Thread Status:
Not open for further replies.

Share This Page