get location player, check locations in water

Discussion in 'Plugin Development' started by Veyn12, Mar 18, 2021.

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

    Veyn12

    Hello everyone. The other day I made a plugin that if a player died in water, he is given a compensation of 2 diamonds, if, on the contrary, in another place, but not in water, then nothing happens. In general, I created an event for the death of a player, check it for being in the water like this:
    p.getLocation (). getBlock (). getType (). equals (Material.WATER). I also tried it through comparison. Events work, I checked it through joinEvent. Tell me what my mistake is and how to correctly check whether a player has died in the water or not
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 Need the full method to say anything useful
     
  3. Offline

    Veyn12

    Code:
     
    @EventHandler
    public void onRespawn(PlayerRespawnEvent e){
    Player p = e.getPlayer();
    if(p.getLocation().getBlock().getType().equals(Material.WATER){
    p.sendMessage(“you last death in water”);
    }
    
    //TODO TWO:
    if(p.getLocation().getBlock().getType() == Material.WATER){
    p.sendMessage(“you last death in water”);
    }
    }
    
    
    
    
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 That is because the respawn is happening when they are alive again.
    Considered using the PlayerDeathEvent?
     
  5. Offline

    Veyn12


    Yes, I just used this event. And I wrote the respawn, because this is the main necessary event in which I want to use the very same check if the player was in the water. But you can also try adding a hashmap to someone who died in the water and getting it in a respawn event. The bottom line is, why doesn't it work? I checked just on PlayerDeathEvent
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 Again: need code to say that.
    Post the full class.
     
  7. Offline

    Veyn12


    okay, how will I be at home - I will throw off the whole class

    it's please
    Show Spoiler

    Code:
    package test.event;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin implements Listener {
    
     
     
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
        public void onDisble() {
         
        }
     
     
        @EventHandler
        public void onDeathEv(PlayerDeathEvent e) {
            Player p = e.getEntity();
            if(p.getLocation().getBlock().getType() == Material.WATER) {
                p.sendMessage("this is operator check");
            }
            if(p.getLocation().getBlock().getType().equals(Material.WATER)) {
                p.sendMessage("this is equals check");
            }
        }
    }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 19, 2021
  8. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 Considered printing the blocktype?
     
  9. Offline

    Veyn12

    Sorry, I am from the Russian community and could not understand this question = (
     
  10. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 System.out.println(p.getLocation().getBlock().getType().name())
     
  11. Offline

    Veyn12

    [​IMG]
    It brought out, not ordinary water, stationary, but the fact is that there is such, yes, it is marked like this:
    LEGACY_STATIONARY_WATER
    but when I change in my check, it warns of an error and strikes out the material, along with this the code still does not work

    And, sorry, everything is fine, the kernel version was 1.16.5, and I tested it on 1.11.2. Thank you very much, everything works, very grateful!

    If you can still please help me, I've been trying for about a week or two to do:

    If the player's nickname is in ArrayList <String> names
    then damage from snowballs does not pass through it, tried all the methods, from all rubakits, none of them helped = (


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 19, 2021
  12. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 Set the API-version in the plugin.yml for the LEGACY_STATIONARY_WATER

    Second issue: need code again.
     
  13. Offline

    Veyn12

    And there is no code = (
    I purely logically cannot figure out how to implement it.
     
  14. Offline

    timtower Administrator Administrator Moderator

  15. Offline

    Veyn12

    That’s not the point. I know how to check the contents of the sheet. It's about the implementation itself. How to remove the damage itself and get a player who was hit by a snowball.
     
  16. Offline

    timtower Administrator Administrator Moderator

  17. Offline

    Veyn12

    Okay, yes, I added .. but how to remove the damage?
    Show Spoiler

    Code:
    @EventHandler
        public void onHitEvent (ProjectileHitEvent e) {
            Player p = (Player) e.getEntity();
            if(Respawn.main.pd.contains(p.getName())) {
        
            }
            return;
        }
     
  18. Offline

    KarimAKL

    @Veyn12 I would use the EntityDamageByEntityEvent.
    Code:Java
    1. // Check if the victim is a player and the attacker is a snowball
    2. if (!(event.getEntity() instanceof Player && event.getDamager() instanceof Snowball)) return;
    3.  
    4. // Do your other checks here
    5.  
    6. // Cancel the damage event
    7. event.setCancelled(true);
     
  19. Offline

    Veyn12

    Thank you =)
     
Thread Status:
Not open for further replies.

Share This Page