Solved getting a playername from a hashmap

Discussion in 'Plugin Development' started by rhunter, Oct 29, 2016.

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

    rhunter

    the title of this should be "getting playername from different event" lol
    i made something where a player can shoot a snowball from a stick and if it hits someone, they get frozen and can't move. Now what im trying to do is get the playername of the player who released the snowball from the stick. I tried putting the playername in a hashmap but i couldnt get the correct playername. Any help?
    Code:
        /////////////////////////FREEZE SPELL/////////////////
    public HashMap<Player, Long> frozen = new HashMap<Player, Long>();
    @EventHandler
        public void snowballThrow(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if(getYourClass().get(player.getName()) == 2) {
                if (player.getInventory().getItemInMainHand().getType() == Material.BLAZE_ROD) {
                    if (event.getAction() == Action.LEFT_CLICK_AIR
                            || event.getAction() == Action.LEFT_CLICK_BLOCK) {
                        if(mageSpell.get(player.getName())==3){
                            player.launchProjectile(Snowball.class);
                    }
                }
            }
        }
    
    }
        @EventHandler
        public void entityDamaged(EntityDamageByEntityEvent event) {
            if(event.getEntity() instanceof Player && event.getDamager() instanceof Snowball) {
                Player player = (Player) event.getEntity();
                if (!frozen.containsKey(player));{
                    frozen.put(player, 20L);
                }
            }
        }
        @EventHandler
        public void playerMoved(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            if (frozen.containsKey(player)) {
                Location location = player.getLocation();
                player.teleport(location);
                player.sendMessage(ChatColor.BLUE+"You were frozen by ");
            }
        }
     
  2. Offline

    Zombie_Striker

    @rhunter
    1. Don't store the Player variable inside the hashmap. This a good way to create memory leaks.
    2. Use miliseconds instead of schedulers for cooldowns. Instead of putting "20" into the hashmap and counting downwards, put it "20*1000+ currentMilis" and check if the current time surpasses this amount.
    3. You not checking for the location when the player moves. Since the event is called whenever a player moves their head, this event can be called multiple times per second and the player may not actually have moved. Do and XYZ check before teleporting.
    Now, to solve your problem, do the following:
    1. Create a new hashmap, Where they keys are UUIDs and the values are Strings. The UUID is the UUID of the frozen player, and the String is the name of the other player.
    2. When a player gets hit by a snowball, add the player's UUID and the shooter's name to the Map.
    3. When a player moves, get the name from the map.
     
  3. Offline

    zioGiok

    Hope this can help you!
    Code:
    @EventHandler
        public void onFreeze(EntityDamageByEntityEvent e) {
            if(e.getDamager() instanceof Snowball) {
                if(e.getEntity() instanceof Player) {
                    Snowball sb = (Snowball) e.getDamager();
                    if(sb.getShooter() instanceof Player) {
                        Player damager = (Player) sb.getShooter();     // <---- Snowball shooter
                        Player damaged = (Player) e.getEntity();         // <---- Freezed Player
                        // DO SOMETHING
                    }                    
                }
            }
     
  4. Offline

    Zombie_Striker

    @zioGiok
    Considering this is a re-write of what he already has, and that this does not even have the code needed to achieve what he wants, I highly doubt this will help.
     
  5. Offline

    rhunter

    the problem is, how do i get the shooter if its not in the shooter event? its in the hit event.
     
  6. Offline

    Zombie_Striker

    @rhunter
    You store the get the shooter in the hit event, store it in the map, and then get the shooter inside the other event using the map.
     
  7. Offline

    PhantomUnicorns

    When you launch the projectile set the shooter to the person who right clicked, on EntityDamageEntityEvent check for the shooter like what @zioGiok did... If I'm wrong please tell me what you are trying to do in a little simpler terms
     
  8. Offline

    rhunter

    solved. this reply helped me the most because I didnt know i could get the shooter of the snowball. thanks!
     
Thread Status:
Not open for further replies.

Share This Page