Solved Player Damage Event

Discussion in 'Plugin Development' started by RexTheMon, Dec 22, 2016.

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

    RexTheMon

    Hello, I am coding a plugin that lets you freeze a player. It works great! But, I cant seem to get it to where the frozen player cant hit other players. I made it where the frozen player can't get damaged and when you do /freeze <player> they cant move.

    Any help with letting the frozen player not damage other players?
     
  2. @RexTheMon
    I'd assume you have some sort of Collection, perhaps a List or a Set which you store all the frozen players in. Simply listen to the Damage Event and check if the player doing the damage is in the collection, and if so, cancel the event.
     
  3. Offline

    RexTheMon

    Yes I have an ArrayList for the frozen players. I also have a onPlayerDamage event.

    So....This is what I have so far.

    Code:
    @EventHandler
    public void onPlayerDamage(EntityDamageEvent e) {
    if (e.getEntity() instanceof  Player) [
    Player p = (Player) e.getEntity();
    if (frozenPlayers.contains(p.getName())){
    e.setCancelled(true);
    }
    }
    }
    It works perfectly fine for the frozen player to not get damaged.

    What I would need to add is ....

    Code:
    if(frozenPlayers.contains(e.getName())){
    e.setCancelled(true);
    }
     
  4. Offline

    kameronn

    Why doesn't
    Code:
    if(frozenPlayers.contains(e.getName()) ||frozenPlayers.contains(p.getName())  ){
    
    work
     
    Blooder likes this.
  5. Offline

    mine2012craft

    @RexTheMon

    You need to use a EntityDamageByEntityEvent instead. Then, get the damager, check if the damager is in the arraylist, and if so, cancel the event. Simple as that :)

    Make sure you also check if the damager is a instance of a player, unless you're freezing mobs too.
     
  6. Offline

    RexTheMon

    I will try that.

    I am trying that now.

    Edit: Both did not work.

    @mine2012craft This code did not work for what you said.

    Code:
    @EventHandler
    public void onEntityDamage(EntityDamageByEntityEvent e){
    if (e.getEntity() instanceof Player) {
    Player p = (Player) e.getEntity();
    if (frozenPlayers.contians(p.getName())){
    e.setCancelled(true);
    }
    }
    }
     
    Last edited: Dec 23, 2016
  7. Offline

    Zombie_Striker

    @RexTheMon
    Have you debugged? Which line does it stop working/ stop reading?
     
  8. Offline

    kameronn

    @RexTheMon
    Oh... Also do what @Zombie_Striker asks, check if the event is even passing the if statement. Are you sure you're adding them to the arraylist? Did you register events? Did you initialize your classes if you're using multiple classes? Double check for things like this

    Can you please show us your arraylist or maybe the whole class you're using?
     
  9. Offline

    mine2012craft

    @RexTheMon Are you sure? Did you register it in your main?

    I have something similar to that and it works.

    Like @kameronn and @Zombie_Striker said, debug it. Add check messages between lines and see where it stops, read errors, etc
     
  10. Offline

    HawkSoldier

    Maybe do a hashmap?
    Example:
    Code:
    Hashmap<UUID, Boolean> exampleMap = new Hashmap<UUID, Boolean>();
    
    and then to enable it:
    Code:
    UUID uuid = (e/(Player) sender/etc).getPlayer().getUniqueId()
    exampleMap.put(uuid, true)
    
    and then to disable hitting:
    Code:
    UUID uuid = e.getPlayer().getUniqueId();
    if (exampleMap.get(uuid) == true) {
         e.setCancelled(true);
    }
    
    If this works, no problem, haven't tested it though
    exampleMap is interchangeable by a name you prefer
     
  11. Offline

    mine2012craft

    Oh... Wait a sec....

    *Looks closer at code*

    Oh..

    *Double facepalm*

    "Get the damager"

    AHEM!

    I said get the damager! Not the player who got damaged lol
     
  12. Offline

    RexTheMon

    @mine2012craft

    I did this and it isn't working.

    Code:
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e) {
            if (e.getDamager() instanceof Player) {
                Player p = (Player) e.getEntity();
                if (frozenPlayers.contains(p.getName())) {
                    e.setCancelled(true);
                }
            }
        }
    @HawkSoldier

    No need for a hash map, already have an arraylist for the frozen Players.

    @kameronn

    Here is my whole class for the frozen command. Ignore the vanish command.

    Code:
    package com.rext.RTMEssentials;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.rext.RTMEssentials.commands.Fly;
    import com.rext.RTMEssentials.commands.Gm;
    import com.rext.RTMEssentials.commands.Heal;
    import com.rext.RTMEssentials.commands.Jarvis;
    import com.rext.RTMEssentials.commands.Rtm;
    
    public class Essentials extends JavaPlugin implements Listener {
    
        public ArrayList<String> frozenPlayers = new ArrayList<String>();
        public ArrayList<Player> vanished = new ArrayList<Player>();
    
        public void onEnable() {
    
            frozenPlayers.clear();
            Bukkit.getPluginManager().registerEvents(this, this);
    
            getCommand("rtm").setExecutor(new Rtm());
            getCommand("fly").setExecutor(new Fly());
            getCommand("heal").setExecutor(new Heal());
            getCommand("gm").setExecutor(new Gm());
            getCommand("jarvis").setExecutor(new Jarvis());
    
            System.out.println("------------");
            System.out.println("RTMEssentials has been enabled.");
            System.out.println("------------");
    
        }
    
        public void onDisable() {
            System.out.println("------------");
            System.out.println("RTMEssentials has been disabled.");
            System.out.println("------------");
        }
       
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    
            Player player = (Player) sender;
    
            if (commandLabel.equalsIgnoreCase("freeze") && player.hasPermission("rtm.freeze")) {
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.RED + "Usage: /freeze <Player>");
                } else {
                    switch (args.length) {
                    case 1:
                        Player p = Bukkit.getPlayer(args[0]);
                        if (p == null) {
                            sender.sendMessage(ChatColor.RED + "Player is not online.");
                        } else {
                            if (frozenPlayers.contains(p.getName())) {
                                frozenPlayers.remove(p.getName());
                                p.setGameMode(GameMode.SURVIVAL);
                                sender.sendMessage(ChatColor.GOLD + "You have unfrozen " + p.getName() + ".");
                                p.sendMessage(ChatColor.GOLD + "You have been unfrozen!");
                            } else {
                                frozenPlayers.add(p.getName());
                                p.sendMessage(ChatColor.RED + "You have been frozen by " + sender.getName() + ".");
                                sender.sendMessage(ChatColor.GOLD + "You have frozen " + p.getName() + ".");
                            }
                        }
    
                        break;
                    }
                }
            } else if (commandLabel.equalsIgnoreCase("vanish") && player.hasPermission("rtm.vanish")) {
    
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "You must be a player to use this command.");
                }
    
                Player p = (Player) sender;
    
                if (!vanished.contains(p)) {
                    for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                        pl.hidePlayer(p);
                    }
                    vanished.add(p);
                    p.sendMessage(ChatColor.GOLD + "You have vanished.");
                    return true;
                } else {
                        for (Player pl : Bukkit.getServer().getOnlinePlayers()) {
                            pl.showPlayer(p);
                        }
                        vanished.remove(p);
                        p.sendMessage(ChatColor.GOLD + "You have unvanished.");
                        return true;
                    }
    
                }
    
                return true;
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            for (Player p : vanished) {
                e.getPlayer().hidePlayer(p);
            }
        }
       
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent e) {
            vanished.remove(e.getPlayer());
        }
    
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            if (frozenPlayers.contains(e.getPlayer().getName())) {
                e.getPlayer().setGameMode(GameMode.ADVENTURE);
                e.getPlayer().teleport(e.getPlayer());
                e.getPlayer().sendMessage(ChatColor.RED + "You are frozen!");
            }
        }
       
        @EventHandler
        public void onPlayerDamage(EntityDamageEvent e) {
            if (e.getEntity() instanceof Player) {
                Player p = (Player) e.getEntity();
                if (frozenPlayers.contains(p.getName())) {
                    e.setCancelled(true);
                }
            }
        }
       
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e) {
            if (e.getDamager() instanceof Player) {
                Player p = (Player) e.getEntity();
                if (frozenPlayers.contains(p.getName())) {
                    e.setCancelled(true);
                }
            }
        }
    }
    
     
  13. Offline

    mine2012craft

    @RexTheMon *Facepalm* Not trying to be rude by doing that.

    Anyway, I meant this line. Change it from this:

    Code:
                Player p = (Player) e.getEntity();
    To this:

    Code:
                Player p = (Player) e.getDamager();
     
  14. Offline

    RexTheMon

    Hey, it worked!

    Makes more sense that way to.

    Thanks for the help everyone.
     
  15. Offline

    Zombie_Striker

    @RexTheMon
    If your problem has been solved, mark this thread as solved
     
Thread Status:
Not open for further replies.

Share This Page