Death event and freezing a player

Discussion in 'Plugin Development' started by fourthbrook, Oct 23, 2012.

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

    fourthbrook

    ok so I want to freeze a player with out using Player move event. Also I want to stop the death event on the killing blow only by a player. also I get an error when a player is struck by an entity not a player.

    Code:
    package andrew.Knockout;
     
    import java.util.HashSet;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    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.BlockBreakEvent;
    import org.bukkit.event.block.BlockIgniteEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerTeleportEvent;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Knockout extends JavaPlugin implements Listener {
        public static Knockout plugin;
     
        public final Logger logger = Logger.getLogger("Minecraft");
        public final BukkitLogger blo = new BukkitLogger(this);
     
        private HashSet<String> knockout = new HashSet<String>();
     
       
        @Override
        public void onEnable(){
            blo.enabled(true);
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(this, this);
        }   
     
        @Override
        public void onDisable() {
            blo.enabled(false);
        }
     
        @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            Entity e = event.getEntity();
            Entity damager = event.getDamager();
           
            if (e instanceof Player && damager instanceof Player) {
                Player player = (Player) e;
                int health = player.getHealth();
               
                if (health <= 7 ) {
                    knockout.add(player.getName());
                    event.setCancelled(true);
                                   
                    if(knockout.contains(player.getName())) {
                        event.setCancelled(true);
                        player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 400, 4));
                        player.sendMessage(ChatColor.DARK_RED + "You were hit over the head and knocked out!");
                        player.setHealth(20);
                        startTimer(player);
                    }
     
                }
            }
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
           
            if(knockout.contains(player.getName())) {
                event.setCancelled(true);
            }
        }
     
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
            if(knockout.contains(player.getName())) {
                event.setCancelled(true);
            }
        }
     
        @EventHandler
        public void onBlockIgnite(BlockIgniteEvent event)
        {
            Player player = event.getPlayer();
            if(knockout.contains(player.getName())) {
                event.setCancelled(true);
            }
        }
     
        @EventHandler   
        public void onBlockPlace(BlockPlaceEvent event)
        {
            Player player = event.getPlayer();
            if(knockout.contains(player.getName())) {
                event.setCancelled(true);
            }
        }
     
        @EventHandler
        public void onPlayerTeleport(PlayerTeleportEvent event)
        {
            Player player = event.getPlayer();
            if(knockout.contains(player.getName())) {
                event.setCancelled(true);
            }
        }
     
        @EventHandler
        public void onEntityDamageEvent(EntityDamageEvent event) {
            Entity e = event.getEntity();
            Player player = (Player) e;
           
            if(knockout.contains(player.getName())) {
                event.setCancelled(true);
            }
        }
     
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
           
            if (knockout.contains(player.getName())){
                  event.setCancelled(true);
     
            }
        }
       
        public void startTimer(Player player) {
            final String name = player.getName();
            final Player player1 = (Player) player;
     
                   
                getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
                    public void run() {
                        if (knockout.contains(name)) {
                            knockout.remove(name);
                           
                            player1.sendMessage(ChatColor.DARK_RED + "You awake feeling dizzy and confused!");   
                        }
                    }
                }, 400);
        }
    }

    that was the source

    here is the error

    Code:
    18:38:41 [SEVERE] Could not pass event EntityDamageEvent to Knockout v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:341)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at net.minecraft.server.EntityLiving.z(EntityLiving.java:301)
            at net.minecraft.server.Entity.h_(Entity.java:216)
            at net.minecraft.server.EntityLiving.h_(EntityLiving.java:435)
            at net.minecraft.server.EntitySlime.h_(EntitySlime.java:72)
            at net.minecraft.server.World.entityJoinedWorld(World.java:1245)
            at net.minecraft.server.WorldServer.entityJoinedWorld(WorldServer.java:5
    11)
            at net.minecraft.server.World.playerJoinedWorld(World.java:1227)
            at net.minecraft.server.World.tickEntities(World.java:1125)
            at net.minecraft.server.WorldServer.tickEntities(WorldServer.java:428)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:547)
            at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
            at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.entity.CraftSlim
    e cannot be cast to org.bukkit.entity.Player
            at andrew.Knockout.Knockout.onEntityDamageEvent(Knockout.java:118)
            at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:339)
            ... 17 more
    18:39:11 [INFO] Read timed out
     
  2. Offline

    gomeow

    Code:
    if (e instanceof Player && damager instanceof Player) 
    To
    Code:
    if ((e instanceof Player) && (damager instanceof Player)) 
    Nvm mind about that: on your entity damage event, check if its a player

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  3. Offline

    fourthbrook

    I thought thats what I was doing? how do i freeze a player with out PlayerMoveEvent
     
  4. Offline

    gomeow

    On your EntityDamageEvent
     
  5. Offline

    fourthbrook

    Yea I need them to freeze but PlayerMoveEvent cant be used.
     
  6. Offline

    Tirelessly

    You need to use playermoveevent.
     
  7. Offline

    chaseoes

    Wrong!

    Create a repeating task that checks their location every couple ticks and teleports them back?
     
  8. Offline

    Tirelessly

    That's an absolutely terrible idea.
     
  9. Offline

    fourthbrook

    PlayerMoveEvent is way to often it becomes unreliable with hundreds of players using the plugin 24/7.
     
  10. Offline

    chaseoes

    You got any other ideas without using the move event, then?
     
  11. Offline

    Codex Arcanum

    Then a repeating synchronous task is the way to go. Look up scheduler programming on the bukkit wiki, but be warned it may produce odd results with freezing players as ticks per second varies. I honestly think that you may be better off with using the move event and just keeping your code as efficient as possible. If you give us more details about what you want to accomplish we may be able to give you more specific advice.
     
  12. Offline

    Tirelessly

    Why would you not use the move event? There's literally NO need for an extra thread here.
     
  13. Offline

    Codex Arcanum

    Argue with the OP, not the helpful person. OP says:
     
  14. Offline

    fourthbrook

    Ok well when a player get struck by another player I want them to be frozen for 20 seconds. also can someone help with the error
     
  15. Offline

    Tirelessly

    Use the move event, it's more reliable than a thread that's going to be iterating through your so called "hundreds of players" every few ticks.

    if(hashset.contains(e.getPlayer().getName())) e.getPlayer().teleport(e.getFrom());
     
  16. Offline

    fourthbrook

    ok im gonna stick with player move event. now what about the error
     
  17. Offline

    gomeow

    fourthbrook
    I told you what to change!
    EnitityDamageEvent
    not EntityDamageByEntityEvent!
     
  18. Offline

    fourthbrook

    ah sorry will do thanks. also my cancel event for damage by player only works if the most damage possible is 7 but lets say my server has a plugin that can theoretically do 500 damage. how would that work? cause the player would die. how can I make it so that it doesn't mater how much damage is done but if it is possible the blow could kill them it cancels it. any suggestions

    *edit* also I changed to EntityDamageEvent but Eclipse throws an error.
     
  19. Offline

    gomeow

    I do not think this is possible. However, I put a link here earlier about what you CAN do
    What is the error?
     
  20. Offline

    Tirelessly

    The EntityDamageByEntity event was right, because EntityDamage doesn't have a .getDamager() that returns an entity which you're using.
     
  21. Offline

    fourthbrook

    right but the server throws an error when none player entities hit a player.

    what link?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  22. Offline

    gomeow

  23. Offline

    fourthbrook

    I might have to do that.

    any advice on the server error at the top of the post.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  24. Offline

    gomeow

    It is simply a non player entity cannot be cast to a player.
    It's in EntityDamageEvent
     
  25. Offline

    fourthbrook

    yes but Entity damage event doesn't have getDamager.
     
  26. Offline

    gomeow

    All I did was tell you exactly what your error said
     
  27. Offline

    fourthbrook

    oh ok sorry. do you know how to fix it?
     
  28. Offline

    gomeow

    You did this:
    Code:java
    1. Entity e = event.getEntity();
    2. Player player = (Player) event.getEntity();

    Check for instanceof player, or something along those lines
     
  29. Offline

    fourthbrook

  30. Offline

    gomeow

    Please be more specific
     
Thread Status:
Not open for further replies.

Share This Page