HashMaps Help.

Discussion in 'Plugin Development' started by EnderLance, Apr 26, 2014.

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

    EnderLance

    So I'm making a plugin that involves a command that requires a confirmation from another player.

    Kinda like the /tpa command in essentials, though when I looked at their source code I found nothing useful.

    Basically I want to have a command where a person executes /rdna share EnderLance and I have to do /rdna accept for the command to go through.

    It was suggested that I use a HashMap storing both players' names and a boolean value. Here's what I ended up doing:

    Code:java
    1. private HashMap<String, String> map = new HashMap<String, String>();


    In the onCommand I have the initial command:

    Code:java
    1. map.put("offerer", sender.getName());
    2. map.put("offeree", target.getName());


    Then in the confirm command is this:

    Code:java
    1. if(map.get("offeree") == sender.getName())
    2. {
    3. //Do code involving map.get("offeree") and map.get("offerer");
    4. }


    This didn't work out as I expected it would.

    What can I do? I really need a confirm command for my plugin.

    Any suggested code or ideas greatly appreciated.

    Thanks!
     
  2. Offline

    DxDy

    Make sure you get the Idea behind maps ;) A map contains mappings between a key and a value, not a storage list of a pair of values.

    In your case you'd propably need a Map<String, String> where the keys would be the players who need to accept and the values the players who initated the request.

    May I also suggest using Map<UUID, UUID> with regards to the UUID system ;)
     
  3. Offline

    EnderLance

    So how would I use a Map? Is it just,
    Code:java
    1. Map<String, String> map = new Map<String, String>();
    ?

    P.S. Thanks for helping me! :)
     
  4. Offline

    DxDy

    Map is the interface. There are several types of Maps in the Java Libaries which implement that interface and fullfill the contracts laid out in its definition. You use Map<String, String> in your declaration to be able to switch the underlying actual implementation without having to change all the code that uses it. In your case you could use a HashMap for example. That would then be:

    Code:java
    1. Map<String, String> requests = new HashMap<String, String>();
     
Thread Status:
Not open for further replies.

Share This Page