HashMap<Player, Integer> issue

Discussion in 'Spigot Plugin Development' started by XZot1K, May 5, 2015.

  1. How do I get the player from the hashmap and add 1 to the integer <Player, Integer>?

    Code (Text):
    import java.util.HashMap;

    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.TNTPrimed;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;

    public class TnT implements Listener {

        Main plugin;

        public TnT(Main instance) {
            plugin = instance;
        }

        public HashMap<String, Long> tntcd = new HashMap<String, Long>();

        public HashMap<String, Integer> tnttag = new HashMap<String, Integer>();

        @EventHandler
        public void onPlace(BlockPlaceEvent e) {

            Player player = e.getPlayer();

            if (e.getBlock().getType() == Material.TNT) {

                if (tntcd.containsKey(player.getName())) {
                    long secondsLeft = ((Long) tntcd.get(player.getName()))
                            .longValue()
                            / 1000L
                            + plugin.tntct
                            - System.currentTimeMillis() / 1000L;
                    if (secondsLeft > 0L) {

                        e.setCancelled(true);

                        player.sendMessage(plugin.prefix
                                + "You Can't Use Another TnT For §a" + secondsLeft
                                + " §6Seconds!");

                        return;
                    }
                }

                e.getBlock().setType(Material.AIR);

                Entity primed = e
                        .getPlayer()
                        .getWorld()
                        .spawn(e.getBlock().getLocation().add(0.0D, 1.0D, 0.0D),
                                TNTPrimed.class);
                ((TNTPrimed) primed).setFuseTicks(40);

                if (tnttag.containsKey(player.getName())) {
                 
                    //How do i add 1 to the <Player, Integer> (Integer)

                } else {

                    tnttag.put(player.getName(), 1);
                    player.sendMessage("Added!");

                }

                if (tnttag.containsKey(player.getName().equals(plugin.usetnt))) {

                    tntcd.put(player.getName(),
                            Long.valueOf(System.currentTimeMillis()));

                    player.sendMessage("Cooldowned!");

                }

            }

        }

    }
     
     
  2. For get the long you can put Long long = tnttag.get(player); and then tnttag.put(player, long++)
     
  3. To answer your first question, it's as simple as
    Code (Text):
    tnttag.put(player.getName(), tnttag.get(player.getName()) + 1);
    To go further in depth, you don't in fact have a map of <Player, Integer>, you have a map of <String, Integer>. This is good, as keeping strong references to player objects is one of the ways plugins suffer from memory leaks.

    Before people wade in and complain that you should be using uuid's for this scenario, as long as this is transient; ie you aren't storing these values for (long term) future reference, nor are you going name -> player object, then continuing to use player names is acceptable (if not the best practice!)

    A bigger point of concern is tnttag.containsKey(player.getName().equals(plugin.usetnt)) - you are testing if a map of strings contains a boolean object!

    - flamin
     
  4. its giving errors cause its a integer
     
  5. C
    You can change Long to int
     
  6. plugin.usetnt is a int in the config how can I catch if the Integer of <String, Integer> is equal to plugin.usetnt
     
  7. Code (Text):
    if(getConfig.getInt("") == hash.get(p.getName)
     
  8. ty
     
  9. Just remove the player from the Map when they quit and you won't leak any memory. Using Players as keys is not a bad habit.
     
    • Like Like x 1