Solved Suffocation event cancel

Discussion in 'Spigot Plugin Development' started by SoyDrex, Aug 18, 2018.

  1. Hello, lately I have been looking for a method to cancel the damage by suffocation, but I want to cancel it when the player is suffocating in bedrock since I have a server where I use bedrock as a wall and the players to be fighting get stuck in the bedrock and as the damage of the players + that of the suffocation the the bedrock increases the damage..

    Current code:
    Code (Java):
        @EventHandler(priority = EventPriority.MONITOR)
        public void onEntityDamage(EntityDamageEvent ede){
            if (ede.getCause().equals(EntityDamageEvent.DamageCause.SUFFOCATION)){
                ede.setCancelled(true);
            }
        }
     
  2. sorry for my very bad english :cry:
     
  3. so you want to only cancel it if they are in bedrock?

    just get the block their head is in (#getBlockAtEyeLevel() or something like that) and check if its bedrock
     
  4. Thanks, I'll test it

    EDIT:
    I could not find a similar method i think
     
    #4 SoyDrex, Aug 18, 2018
    Last edited: Aug 18, 2018
  5. I think you need use #getEyeLocation method and then get block and check if type of block equals bedrock.
     
  6. Ok, testing now, thanks

    EDIT:
    I do not know if my knowledge about spigot does not give for this or I'm just an idiot lol
    any example pls?
     
    #6 SoyDrex, Aug 18, 2018
    Last edited: Aug 18, 2018
  7. Check if player location || player eye location block Is bedrock and cancel the event.

    You need both since player is suffocating.
     
  8. This works for me:
    Code (Text):
    @EventHandler
        public void onEntityDamage(EntityDamageEvent event){
            Entity ent = event.getEntity();
            if(ent instanceof Player) {
                Player player = (Player) ent;
                if (event.getCause().equals(EntityDamageEvent.DamageCause.SUFFOCATION)){
                    if(player.getEyeLocation().getBlock().getType() == Material.BEDROCK) {
                        event.setCancelled(true);
                    }
                }
            }      
        }
     
  9. Code (Text):
        @EventHandler
        public void onSuffocation(EntityDamageEvent event) {
            if (!(event.getEntity() instanceof Player) || !event.getCause().equals(EntityDamageEvent.DamageCause.SUFFOCATION)) {
                return;
            }
            Player player = (Player) event.getEntity();
            if (player.getEyeLocation().getBlock().getType() != Material.BEDROCK && player.getLocation().getBlock().getType() != Material.BEDROCK) {
                return;
            }
            event.setCancelled(true);
        }
    that seems kinda better.
     
  10. Great, thanks :D
     
    • Friendly Friendly x 1