Solved Storing a counting integer per player

Discussion in 'Plugin Development' started by FirecatHD, Apr 29, 2015.

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

    FirecatHD

    Hi everyone!

    So usually i try my very best to figure out stuff on my own, but from time to time i just get completely stuck.
    What i am trying to do is making a player take more and more knockback the more they are hit, and this would reset upon death.

    I managed to do this to some extent but it didnt work as i intended.
    So could someone please help me here?

    I will post the code i have right now, but it probably wont make much sence, since i have been sitting and experimenting for about 6 hours trying to get this to work.

    Code:
    public class KnockBack implements Listener{
    
        HashMap<UUID, Integer> Percent = new HashMap<UUID, Integer>();
    
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
            final Entity hit = e.getEntity();
            Entity p = e.getDamager();
    
            if (p.getType().equals(EntityType.PLAYER) /*&& hit.getType().equals(EntityType.PLAYER)*/) {
    
                int KB = 1;
    
                Percent.put(hit.getUniqueId(), 1);
    
                int knock = Percent.get(hit.getUniqueId());
                hit.setVelocity(p.getLocation().getDirection().multiply(knock++));
                Percent.put(hit.getUniqueId(), knock++);
    
                new BukkitRunnable() {
    
                    @Override
                    public void run() {
    
                        Bukkit.broadcastMessage("" + Percent.get(hit.getUniqueId()) + "");
                    }
                }.runTaskTimer(MainClass.getInstance(), 0, 20);
            }
        }
    
    
        @EventHandler
        public void onPlayerDeathEvent(PlayerDeathEvent e) {
            Player p = e.getEntity();
            if (!p.getType().equals(EntityType.PLAYER)) return;
    
            if (Percent.containsKey(p.getUniqueId())); {
                Percent.put(p.getUniqueId(), 1);
            }
        }
    }
    Thanks.
     
    Last edited: Apr 29, 2015
  2. Offline

    I Al Istannen

    @FirecatHD In line 14 you are updating the HashMap. The value gets reset to 1. Just put in in there if there is none or some time went by. That's what i would think at least.
     
  3. Offline

    FirecatHD

    @I Al Istannen

    Not sure if you understood my problem. First of all, this code is non-functional at the moment because as i said, i have been experimenting with it. I just posted it as reference.

    This is my problem: Player two hit player one 1 time. Player one's knockback lvl is now 2. Player one hits player two. You would expect Player two to also have knockback lvl 2, but gets instead lvl 3.

    See where this is going?
    Below is the code i had when i got stuck, and has the problem i explained.

    Code:
    public class KnockBack implements Listener{
    
        HashMap<UUID, Integer> Percent = new HashMap<UUID, Integer>();
    
        int KB = 1;
    
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
            final Entity hit = e.getEntity();
            Entity p = e.getDamager();
    
            if (p.getType().equals(EntityType.PLAYER) /*&& hit.getType().equals(EntityType.PLAYER)*/) {
    
                Percent.put(hit.getUniqueId(), KB++);
                hit.setVelocity(p.getLocation().getDirection().multiply(Percent.get(hit.getUniqueId())).setY(1.6));
                Percent.put(hit.getUniqueId(), Percent.get(hit.getUniqueId()));
    
                new BukkitRunnable() {
    
                    @Override
                    public void run() {
    
                        Bukkit.broadcastMessage("" + Percent.get(hit.getUniqueId()) + "");
                    }
                }.runTaskTimer(MainClass.getInstance(), 0, 20);
            }
        }
    
    
        @EventHandler
        public void onPlayerDeathEvent(PlayerDeathEvent e) {
            Player p = e.getEntity();
            if (!p.getType().equals(EntityType.PLAYER)) return;
    
            if (Percent.containsKey(p.getUniqueId())); {
                Percent.put(p.getUniqueId(), 1);
            }
        }
    }

    So does anyone have a method of doing this the right way? Or atleast point me in the right direction.
     
    Last edited: Apr 29, 2015
  4. Offline

    I Al Istannen

    @FirecatHD Line 5. You increase KB (name should start with lowercase, same for Percent). This is a semi-final(?) variable and stays the same for every call of the Event. So you hit player one. It goes to 2. You hit player two. It goes to 3. And so on. And, more important you don't need this! Just set the "Percent" value to (the previous value + 1). You must check if the Map contains the UUID before, but the implementation is totally up to you. And you don't use a taskTimer there, but a runTask, or runTaskLater. The timer goes on forever and with the same UUID as key every time.
     
  5. Offline

    FirecatHD

    @I Al Istannen

    I acually figured it out just before you posted this :3
    Used the previous value +1 plus fixing in some other stuff, its working now c:

    Anyways, happy to see that someone cares enough to help out!
     
Thread Status:
Not open for further replies.

Share This Page