Single Integer in HashMap ?

Discussion in 'Plugin Development' started by recon88, May 30, 2012.

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

    recon88

    Is it possible to put just a single Integer into a hashmap?
    I want to set a number (as "security" key) for something.

    e.g.:
    Code:
    Integer number = 1234;
     
    // Missing HashMap stuff here
     
    if (commandLabel.equalsIgnoreCase("newkey")
    {
    singleinteger.put(number);
    }
     
     
    if (message.equals(number))
    {
    singleinteger.remove(number);
    }
     
  2. Offline

    Njol

  3. Offline

    recon88

    Do I need a Hashmap? Not sure about that. That's why I'm asking.

    I just want to store a given number.
    If I type the stored number in the chat it will trigger something.
    Also it will remove the stored number.
     
  4. most people want an hashmap whit key the player or mostly its name, see my sign, and as value what they want , so:
    Map<String,Integer>
     
  5. Offline

    Njol

    If you intend to have more than one number stored at a time you should use a Set<Integer> to store them, and can use .contains(int) to find whether a number is stored and .remove(int) to remove a number from the set.
    If you however only have one number i suggest a simple int which you set to -1 when it is used.
     
  6. Offline

    Wundark

    Create a HashMap
    Code:
    Map<String, Integer> security = new HashMap<String, Integer>();
    Add a entry to the HashMap
    Code:
    security.put("Player Name", Securiry Key);
    Remove an entry from the HashMap
    Code:
    security.remove("Player Name");
     
  7. Offline

    recon88


    It's a single number which gets randomly generated and I want to store it.
    Code:
    Integer random = 1+object.nextInt(100);
    Now if I type exactly this message it gets removed and can't be used a second time.
    Code:
    if (message.equals(storednumber)
     
  8. Offline

    HON95

    Use a set:
    Code:
    Set<Integer> a = new HashSet<Integer>();
    Code:
    Integer b = Integer.parseInt(message); // Put in try/catch
     
    if (a.contains(b)) {
        a.remove(b);
        // stuff
    }
     
  9. Offline

    ItsHarry

    As suggested, use a Set or an alternative would be ArrayList.
     
  10. Offline

    recon88

    HON95
    Thanks for the example.
    Got it now.
     
  11. ItsHarry A ArrayList is bad for storing Integers, cause it has the function remove(int index) which will conflict with remove(Object o).
     
    HON95 likes this.
  12. Offline

    recon88

    Another Problem (related to Set):

    I want to parse an Integer from Command (That works).

    Code:
    Set<Integer> rewards = new HashSet<Integer>();
    Code:
            if (args.length == 1) {
                try {
                    Integer reward = Integer.parseInt(args[0]);
                    rewards.add(reward);
                } catch (NumberFormatException nfe) {
                }
            }
    Now how can I access the Integer from other methods?

    Code:
    public void onPlayerMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        if (player.hasPermission("run.reward")) {
            run.economy.depositPlayer(player.getName(), HOW TO GET THE INT FROM THE SET "rewards" ?);
    Or is there something which works without Set for this?
     
  13. recon88
    So you want each player to have a reward value or something ? Because then a hashmap would be useful with String (playername) and Integer.

    Also, why are you using PlayerMoveEvent for rewards ??? :confused: Just make a task if you want to delay it...
     
  14. Offline

    recon88

    That was just a fast written example =)

    For some reasons it doesn't work:
    Code:
    HashMap<Player, Integer> rewards = new HashMap<Player, Integer>();
    On Command
    Code:
    rewards.put(player.getPlayer(), reward);
    On Event
    Code:
    run.economy.depositPlayer(player.getName(), rewards.get(player));
    rewards.remove(player);
    - I want to execute a command. Like "/run 120".
    - Now I want to store the 120.
    - After someone triggers the event he/she will get the 120 rewarded.
     
  15. First, use player names.

    Then, what you're showing should work tough, there could be another problem there... try some debug messages, print player name, value, retrieved value from hashmap, etc.
     
  16. Offline

    recon88

    What's wrong with that? If I trigger the event I'll get the reward.
    If someone else triggers it the console throws an exception:

    HashMap
    Code:
    HashMap<Player, Integer> rewards = new HashMap<Player, Integer>();
    On Command
    Code:
    rewards.put(player.getPlayer(), reward);
    On Event
    Code:
    Player player = event.getPlayer();
    Integer reward = rewards.get(player);
    run.economy.depositPlayer(player.getName(), reward);  (LINE 104)
    rewards.remove(player);
    Exception
    Code:
    [SEVERE] Could not pass event PlayerMoveEvent to Run
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:786)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at com.recon.Run.Run.onPlayerMove(Run.java:104)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
    
    Btw, if I use player names it also throws exceptions.

    nvm... I used a static string instead of player now.

    HashMap
    Code:
    HashMap<String, Integer> rewards = new HashMap<String, Integer>();
    On Command
    Code:
    String thereward = "rew";
     
      Integer reward = Integer.parseInt(args[0]);
      rewards.put(thereward, reward);
    On Event
    Code:
    Player player = event.getPlayer();
    String thereward = "rew";
    Integer reward = rewards.get(thereward);
     
    run.economy.depositPlayer(player.getName(), reward);
    rewards.remove(thereward);
    Works now with that code.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  17. what is: com.recon.Run.Run.onPlayerMove(Run.java:104)
    give us that row
     
  18. Offline

    recon88

    See "OnEvent" i wrote down "(LINE 104)" there.
    But as I said i got that fixed with a static String. So that's OK now.
     
  19. that line is the ccause of your lastest exception, so theres something wrong there
     
  20. Offline

    recon88

    That makes no sense what you are saying.

    As I said I marked Line 104. But there was no mistake in it.
    Also I fixed it with a static string (as I already said).

    So: Already solved
     
  21. recon88 your fail was here:
    Integer reward = rewards.get(player);
    That's the only thing that could give a NPE at line 104. And that means the list rewards didn't have the entry player. Why not? Cause the list stores the object type String, not Player... so the fix would have been to relpace it with:
    Integer reward = rewards.get(player.getName());
     
  22. Offline

    recon88

    V10lator
    Oh ok. Thanks for that.
    Well. It also works with that string now.. But I'll use this one if I need it again =)
     
Thread Status:
Not open for further replies.

Share This Page