Solved HashMap not saving the value

Discussion in 'Plugin Development' started by Demyxa, Jul 11, 2018.

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

    Demyxa

    I have created a hashmap thats supposed to save a value "geraucht" per player.

    Code:
    if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
                    int geraucht = rauch.get(playername); //gets the value from the hashmap (for first right click = 0)
                    Block ploc = player.getLocation().getBlock();
                    Block bloc = event.getClickedBlock();
                    if (player.getItemInHand().getType().equals(Material.STICK)) {
                        if (player.getItemInHand().getItemMeta().hasLore()) {
                            if (ploc.getX() == bloc.getX() && ploc.getZ() == bloc.getZ()) {
                                player.sendMessage("Du hast deine Zigarette ausgetreten!");
                                player.getInventory().remove(player.getItemInHand());
                                geraucht = 0;
                                rauch.put(playername, geraucht); //sets the value to 0 then saves it
                            } else {
                                switch (geraucht) {
                                case 0:
                                    player.sendMessage(ChatColor.AQUA + "Du fühlst dich entspannt...");
                                    break;
                                case 50:
                                    player.sendMessage(ChatColor.AQUA + "Du spürst wie der Qualm deine Lunge füllt...");
                                    break;
                                case 100:
                                    player.sendMessage(ChatColor.AQUA + "Du spürst ein Stechen in deiner Brust...");
                                    break;
                                case 150:
                                    player.sendMessage(ChatColor.RED
                                            + "Du musstest vom vielen Rauchen husten und hast deine Zigarre fallen gelassen!");
                                    player.damage(6);
                                    player.getInventory().remove(player.getItemInHand());
                                    break;
                                }
                                geraucht++; //adds 1 to the value
                                rauch.put(playername, geraucht);
                                if (geraucht > 150) {
                                    geraucht = 0;
                                }
                                sendParticle(type, player.getLocation(), player.getEyeLocation(), (float) 0.1, (float) 0,
                                        (float) 0.1, (float) 0.01, 5);
                                
    what is supposed to happen, is that it saves after every increase, eventually leading up to 150 which sets it back down to zero. But by how the plugin reacts, I know that on every right click, the value is still 0, not increasing at all.

    What'd I do wrong?

    EDIT: I just noticed that the if-statement after the switch is super redundant.
     
  2. Online

    timtower Ninja on the waves Moderator

    @Demyxa Do you get any of the messages?
     
  3. Offline

    Demyxa

    Yeah, I get the one at case 0, which is why I can tell that it isn't wortking.
     
  4. Online

    timtower Ninja on the waves Moderator

    @Demyxa Add print statements to see it increasing. And look at your location check, can be the same.
     
  5. Offline

    Demyxa

    blegh.PNG


    Staying at 1 :/
    EDIT: I added a print for what the HashMap gives the value for whenever I use .get
    Everytime its Zero.
     
    Last edited: Jul 11, 2018
  6. Online

    timtower Ninja on the waves Moderator

    @Demyxa Please post the full class.
     
  7. Offline

    Demyxa

    I use two classes, one for right clicking on air and one for right clicking on Blocks (seems redundant I know, but I feel like it's easier to navigate through.)

    OnBlockClass:

    Code:
    package me.demyxa.cigars.EventsClass;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import net.md_5.bungee.api.ChatColor;
    import net.minecraft.server.v1_12_R1.EnumParticle;
    import net.minecraft.server.v1_12_R1.PacketPlayOutWorldParticles;
    
    @SuppressWarnings("unused")
    public class OnBlockClass implements Listener {
    
        public static HashMap<String, Integer> rauch = new HashMap<String, Integer>();
    
        public void sendParticle(EnumParticle type, Location sender, Location loc, float xOffset, float yOffset,
                float zOffset, float speed, int count) {
            float x = (float) loc.getX();
            float y = (float) loc.getY();
            float z = (float) loc.getZ();
    
            PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(type, true, x, y, z, xOffset, yOffset,
                    zOffset, speed, count);
            for (Entity entity : sender.getWorld().getNearbyEntities(sender, 50, 50, 50)) {
                if (entity instanceof Player) {
                    ((CraftPlayer) entity).getHandle().playerConnection.sendPacket(packet);
                }
            }
        }
    
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onRightClick(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
            String playername = event.getPlayer().getName();
    
            EnumParticle type = EnumParticle.SMOKE_NORMAL;
    
            try {
                if (action.equals(Action.RIGHT_CLICK_BLOCK)) {
                    int geraucht = rauch.get(playername);
                 
                    Block ploc = player.getLocation().getBlock();
                    Block bloc = event.getClickedBlock();
                    if (player.getItemInHand().getType().equals(Material.STICK)) {
                        if (player.getItemInHand().getItemMeta().hasLore()) {
                            if (ploc.getX() == bloc.getX() && ploc.getZ() == bloc.getZ()) {
                                player.sendMessage("Du hast deine Zigarette ausgetreten!");
                                player.getInventory().remove(player.getItemInHand());
                                geraucht = 0;
                                rauch.put(playername, geraucht);
                            } else {
                                switch (geraucht) {
                                case 0:
                                    player.sendMessage(ChatColor.AQUA + "Du fühlst dich entspannt...");
                                    break;
                                case 50:
                                    player.sendMessage(ChatColor.AQUA + "Du spürst wie der Qualm deine Lunge füllt...");
                                    break;
                                case 100:
                                    player.sendMessage(ChatColor.AQUA + "Du spürst ein Stechen in deiner Brust...");
                                    break;
                                case 150:
                                    player.sendMessage(ChatColor.RED
                                            + "Du musstest vom vielen Rauchen husten und hast deine Zigarre fallen gelassen!");
                                    player.damage(6);
                                    player.getInventory().remove(player.getItemInHand());
                                    geraucht = 0;
                                    break;
                                }
                                geraucht++;
                                rauch.put(playername, geraucht);
                                sendParticle(type, player.getLocation(), player.getEyeLocation(), (float) 0.1, (float) 0,
                                        (float) 0.1, (float) 0.01, 5);
                                }
                            }
                        }
                    }
             
    
            } catch (Exception ex) {
    
            }
        }
    }
    OnAirClass:
    Code:
    package me.demyxa.cigars.EventsClass;
    
    import java.util.HashMap;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import net.md_5.bungee.api.ChatColor;
    import net.minecraft.server.v1_12_R1.EnumParticle;
    import net.minecraft.server.v1_12_R1.PacketPlayOutWorldParticles;
    
    public class OnAirClass implements Listener {
     
        public HashMap<String, Integer> rauch = OnBlockClass.rauch;
    
    
        public void sendParticle(EnumParticle type, Location sender, Location loc, float xOffset, float yOffset,
                float zOffset, float speed, int count) {
            float x = (float) loc.getX();
            float y = (float) loc.getY();
            float z = (float) loc.getZ();
    
            PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(type, true, x, y, z, xOffset, yOffset,
                    zOffset, speed, count);
            for (Entity entity : sender.getWorld().getNearbyEntities(sender, 50, 50, 50)) {
                if (entity instanceof Player) {
                    ((CraftPlayer) entity).getHandle().playerConnection.sendPacket(packet);
                }
            }
        }
    
        @SuppressWarnings({ "deprecation" })
        @EventHandler
        public void onRightClick(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
            String playername = event.getPlayer().getName();
            rauch.put(playername, 0);
         
            EnumParticle type = EnumParticle.SMOKE_NORMAL;
            try {
                if (action.equals(Action.RIGHT_CLICK_AIR)) {
                    int geraucht = rauch.get(playername);
                    System.out.println("HashMap gave: " + rauch.get(playername));
                    if (player.getItemInHand().getType().equals(Material.STICK)) {
                        if (player.getItemInHand().getItemMeta().hasLore()) {
                            switch (geraucht) {
                            case 0:
                                player.sendMessage(ChatColor.AQUA + "Du fühlst dich entspannt...");
                                break;
                            case 50:
                                player.sendMessage(ChatColor.AQUA + "Du spürst wie der Qualm deine Lunge füllt...");
                                break;
                            case 100:
                                player.sendMessage(ChatColor.AQUA + "Du spürst ein Stechen in deiner Brust...");
                                break;
                            case 150:
                                player.sendMessage(ChatColor.RED
                                        + "Du musstest vom vielen Rauchen husten und hast deine Zigarre fallen gelassen!");
                                player.damage(6);
                                player.getInventory().remove(player.getItemInHand());
                                geraucht = 0;
                                break;
                            }
                            geraucht++;
                            System.out.println("Set to: " + geraucht);
                            if (geraucht > 150) {
                                geraucht = 0;
                            }
                            sendParticle(type, player.getLocation(), player.getEyeLocation(), (float) 0.1, (float) 0,
                                    (float) 0.1, (float) 0.01, 5);
                         
                            rauch.put(playername, geraucht);
                            System.out.println("HashMap has: " + rauch.get(playername));
                            }
                         
                        }
                    }
             
            } catch (Exception ex) {
            }
    
        }
    }
    
     
  8. Online

    timtower Ninja on the waves Moderator

    @Demyxa You do realize that this is being called every time right?
    rauch.put(playername, 0);
     
  9. Offline

    Demyxa

    Oh. You know, now I feel really dumb.
    I reaaally suck at noticing such things.
     
  10. Offline

    MightyOne

    @Demyxa gets better with the time. you are working on a smoking plugin? xD seems funny
     
  11. Offline

    Demyxa

    Yeah, I am. I kinda gave up on the food plugin since that was my first "yeah this is a plugin. it does things" kinda plugin, to get me acquainted with bukkit and a good share of basics.

    I asked a friend if he could give me a plugin that he may need for his server that I could practice on, and then this abomination of code came together.
     
Thread Status:
Not open for further replies.

Share This Page