Solved Problem

Discussion in 'Plugin Development' started by D4rkz, Sep 21, 2014.

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

    D4rkz

    Basicly, im trying to create a cookie clicker plugin, it returns the value of 1 each time you click it. But i'm trying to make it go up a int everytime you click it :/
    My Code:
    public static HashMap<Player, Integer> clicks = new HashMap<Player, Integer>();



    public void onEnable() {
    Bukkit.getPluginManager().registerEvents(this, this);
    }


    public void onDisable() {
    //Stuff here
    }


    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if(e.getItem().getType() == Material.COOKIE) {
    p.sendMessage(ChatColor.GREEN + "You have " + ChatColor.RED + clicks.size() + ChatColor.GREEN + " Cookie Clicks!");
    clicks.put(p, clicks.size()+1);




    }
    }



    }
     
  2. Offline

    Tecno_Wizard

    @D4rkzm, your code as is will create a memory leak. you are just putting another value over and over again into the hashmap.
    Actually, that entire thing doesn't make sense... Storing player objects won't work in the first place.

    Code:java
    1.  
    2. HashMap<UUID, Integer> clicks = new HashMap<UUID, Integer>();
    3.  
    4. @EventHandler
    5. public void onInteract(PlayerInteractEvent e)
    6. {
    7. int playerClicks;
    8. Player p = e.getPlayer();
    9. if(e.getItem().getType() == Material.COOKIE)
    10. {
    11. if(clicks.get(p.getUniqueId)!=null)
    12. {
    13. playerClicks=clicks.get(p.getUniqueId);
    14. }
    15. else
    16. {
    17. playerClicks=0;
    18. }
    19. clicks.remove(p.getUniqueId);
    20. clicks.add(p.getUniqueId(), (playerClicks+1));
    21. p.sendMessage(ChatColor.GREEN + "You have " + ChatColor.RED + (playerClicks+1) + ChatColor.GREEN + " Cookie Clicks!");
    22. }//end of if cookie
    23. }//end of onInteract


    fair warning, I did not test this.
     
  3. Offline

    D4rkz

    Thanks!, I was going to store the information when in the onDisable! :p
     
  4. Offline

    Tecno_Wizard

    D4rkz this will not save between restarts if that is what you are asking.
     
  5. Offline

    D4rkz

    @Techno_Wizard, i saved it in a Config :p
     
Thread Status:
Not open for further replies.

Share This Page