.

Discussion in 'Plugin Development' started by elementalgodz11, Nov 30, 2013.

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

    elementalgodz11

  2. Offline

    the_merciless

    Answer to A:

    Code:
        @EventHandler
        public void onPlayerDamage(EntityDamageEvent e) {
     
            if (e.getEntity() instanceof Player) {
     
                Player p = (Player) e.getEntity();
     
                if (e.getCause() == DamageCause.FALL) {
                 
                    Location center = new Location(Bukkit.getServer().getWorld("world"), 0, 64, 0); // set this to your center point
                    Entity orb = Bukkit.getWorld("world").spawnEntity(center, EntityType.EXPERIENCE_ORB);
                 
                    List<Entity> nearby = orb.getNearbyEntities(10, 10, 10); //will give you a list of all entities within a 10 block radius
                 
                    for (Entity ent : nearby){
                        if (ent instanceof Player){
                            if (ent.equals(p)){
                                p.setFallDistance(0);
                                e.setCancelled(true);
                            }
                        }
                    }
                 
                    orb.remove();
                 
                }
            }
        }
     
  3. Offline

    Codex Arcanum


    I believe this will, technically, get all of the entities in a 10 block long cube around the center point. If
    elementalgodz11 wants a 10 block long cube, then a more efficient way to do it would be like this, so you don't have to spawn an experience orb and iterate through several entities for no good reason.

    Code:
        @EventHandler
        public void onPlayerDamage(EntityDamageEvent e) {
     
            if (e.getEntity() instanceof Player) {
     
                Player p = (Player) e.getEntity();
                Location l = new Location(...); //set arguments here to be your center point.
                Location pl = p.getLocation();
                if (e.getCause() == DamageCause.FALL) {
                        if(Math.abs(pl.getX() - l.getX()) >= 10 && Math.abs(pl.getY() - l.getY()) >= 10 && Math.abs(pl.getY()  -l.getY()) >= 10){
                                e.setCancelled(true);
                                //setting fall distance to 0 here isn't necessary
                        }     
                }
            }
        }
    
    Alternately, if you want it to be a sphere centered on the center point, this would work fine.

    Code:
        @EventHandler
        public void onPlayerDamage(EntityDamageEvent e) {
     
            if (e.getEntity() instanceof Player) {
     
                Player p = (Player) e.getEntity();
                Location l = new Location(...); //set arguments here to be your center point.
                Location pl = p.getLocation();
                if (e.getCause() == DamageCause.FALL && l.distanceSquare(pl) >= 100) {
                        e.setCancelled(true)
                }
            }
        }
     
    
    No idea why this would be happening. While kind of circuitous, the_merciless 's code shouldn't be canceling any damage other than fall damage.
     
    elementalgodz11 and Plo124 like this.
  4. Offline

    the_merciless

    I believe you need <= not >=

    I don't see how this can be causing that problem. Let's see your class

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

    Codex Arcanum

    the_merciless You are quite right.

    Corrected version:

    Code:
        @EventHandler
        public void onPlayerDamage(EntityDamageEvent e) {
     
            if (e.getEntity() instanceof Player) {
     
                Player p = (Player) e.getEntity();
                Location l = new Location(...); //set arguments here to be your center point.
                Location pl = p.getLocation();
                if (e.getCause() == DamageCause.FALL) {
                        if(Math.abs(pl.getX() - l.getX()) <= 10 && Math.abs(pl.getY() - l.getY()) <= 10 && Math.abs(pl.getY()  -l.getY()) <= 10){
                                e.setCancelled(true);
                                //setting fall distance to 0 here isn't necessary
                        }   
                }
            }
        }
    
    I second mercilless's request for you to post the rest of your code.
     
  6. Offline

    the_merciless

    You have 3 of the same event. I told you before that that will cause problems. You need to keep all the code in 1 event.

    What is mlg

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  7. Offline

    Codex Arcanum

    You either need three different listeners, or, like merciless said, put the code for all three in one of your public void onDamage(...) methods.
     
  8. Offline

    amhokies

    It's not the fact that he has three different methods using the same Event, it's that in one of the methods, he is always cancelling the EntityDamageEvent without checking the DamageCause.
     
  9. Offline

    Noxyro

    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent e) {
    3. if (e.getEntity() instanceof Player) {
    4. Player player = (Player) e.getEntity();
    5. if (e.getCause().equals(DamageCause.FALL)) {
    6. Location l = new Location(Bukkit.getServer().getWorld("world"), 1006, 6, 36);
    7. if (Math.abs(player.getLocation().getX() - l.getX()) <= 10) {
    8. if (Math.abs(player.getLocation().getY() - l.getY()) <= 10) {
    9. if (Math.abs(player.getLocation().getZ() - l.getZ()) <= 10) {
    10. e.setCancelled(true);
    11. return;
    12. }
    13. }
    14. }
    15. } else {
    16. if (SpawnLocation.isCloseToSpawn(player.getLocation())) {
    17. e.setCancelled(true);
    18. player.setFireTicks(0);
    19. return;
    20. } else {
    21. Location mlg = new Location(Bukkit.getServer().getWorld("world"), 297, 4.5, 299);
    22.  
    23. if (mlg.distance(player.getLocation()) <= 120) {
    24. e.setCancelled(true);
    25. player.setFireTicks(0);
    26. return;
    27. }
    28. }
    29. }
    30. }
    31. }


    I cleared it up a bit. Test it and tell me what's not working... eventually put in debug messages.
     
  10. Offline

    Codex Arcanum

    You've put "e.setCancelled(true);" outside of the if statement that checks distance.
     
Thread Status:
Not open for further replies.

Share This Page