Detecting if a player has been hit by a snowball?

Discussion in 'Spigot Plugin Help' started by MrMaster_, Jun 19, 2015.

  1. Hello! I'm new to the spigot forums and i am currently working on my second plugin! Its a snowball arena plugin and i need the players to be able to throw snowballs at each other, and if a player is hit, remove them from the arraylist. So, i have one variable in place : public boolean LobbyFull = false;

    Is there a way i can incorporate this variable into the snowball code to make sure it only detects the snowball hitting if the lobbyfull = true? I already have a load of code that does change lobbyfull, but i need help on the snowball detection! :)

    P.S I accidentally put this in the wrong forum before. :oops: That would be a tad bit awkward if its still in the wrong forum.
     
  2. Here is what I would do (I'm no expert though):

    Code (Java):

        List<UUID> playersInLobby = new ArrayList<UUID>();
        List<UUID> validSnowballs = new ArrayList<UUID>();
        boolean lobbyFull = false;

        @EventHandler
        public void onPlayerThrowSnowball(ProjectileLaunchEvent e){
            if (e.getEntity().getShooter() instanceof Player){        //If entity shooting projectile is a Player
                if (e.getEntity() instanceof Snowball){        //If projectile shot by player is a Snowball
                    if (lobbyFull){        //If the lobbyFull boolean is True
                        Player shooter = (Player) e.getEntity().getShooter();        //Get the Player object
                        if (playersInLobby.contains(shooter.getUniqueId())){        //If the playersInLobby list contains the UUID of the Player shooting this snowball
                            validSnowballs.add(e.getEntity().getUniqueId());        //Add this Snowball being launched to the validSnowballs list
                        }
                    }
                }
            }
        }
       
        @EventHandler
        public void onPlayerHitBySnowball(EntityDamageByEntityEvent e){
            if (e.getDamager() instanceof Snowball){        //If entity was hit by a Snowball
                if (e.getEntity() instanceof Player){        //If the entity hit is a Player
                    if (validSnowballs.contains(e.getDamager().getUniqueId())){        //If the Snowball is valid
                        Player hitPlayer = (Player) e.getEntity();        //Get the Player object
                        if (playersInLobby.contains(hitPlayer.getUniqueId())){        //If the Player hit by a valid Snowball is actually in the lobby
                            playersInLobby.remove(hitPlayer.getUniqueId());        //Remove this Player from the lobby
                            hitPlayer.teleport(arenaSpectateLocation);        //Teleport this Player out of the lobby
                        }
                    }
                }
            }
        }
     
    I tried to make it as understandable as possible.
    The basic concept is that Players that are in the lobby can only be removed from the lobby if they are hit by a Snowball that is in the validSnowballs list.
    A Snowball can only get itself onto the validSnowballs list if the lobbyFull boolean is true, the Snowball is throw by a player, and said player is actually within the lobby himself/herself.

    You could also assign Metadata to the Snowballs instead of adding them to a list, but I believe that this method should more the suffice. If you would want to use Metadata for whatever reason, I've made up code that uses that below:

    Code (Java):

        List<UUID> playersInLobby = new ArrayList<UUID>();
        boolean lobbyFull = false;

        @EventHandler
        public void onPlayerThrowSnowball(ProjectileLaunchEvent e){
            if (e.getEntity().getShooter() instanceof Player){        //If entity shooting projectile is a Player
                if (e.getEntity() instanceof Snowball){        //If projectile shot by player is a Snowball
                    if (lobbyFull){        //If the lobbyFull boolean is True
                        Player shooter = (Player) e.getEntity().getShooter();        //Get the Player object
                        if (playersInLobby.contains(shooter.getUniqueId())){        //If the playersInLobby list contains the UUID of the Player shooting this snowball
                            Snowball snowball = (Snowball) e.getEntity();        //Get the Snowball object
                            snowball.setMetadata("isValid", new FixedMetadataValue(plugin, true));        //Set the "isValid" Metadata of the Snowball to True
                        }
                    }
                }
            }
        }
       
        @EventHandler
        public void onPlayerHitBySnowball(EntityDamageByEntityEvent e){
            if (e.getDamager() instanceof Snowball){        //If entity was hit by a Snowball
                if (e.getEntity() instanceof Player){        //If the entity hit is a Player
                    Snowball snowball = (Snowball) e.getEntity();        //Get the Snowball object
                    if (snowball.hasMetadata("isValid")){        //If Snowball has the "isValid" Metadata
                        if(snowball.getMetadata("isValid").get(0).asBoolean()){        //If the "isValid" Metadata that the Snowball has is True
                            Player hitPlayer = (Player) e.getEntity();        //Get the Player object
                            if (playersInLobby.contains(hitPlayer.getUniqueId())){        //If the Player hit by a valid Snowball is actually in the lobby
                                playersInLobby.remove(hitPlayer.getUniqueId());        //Remove this Player from the lobby
                                hitPlayer.teleport(arenaSpectateLocation);        //Teleport this Player out of the lobby
                            }
                        }
                    }
                }
            }
        }