Solved Closing a nether portal with a bucket of lava

Discussion in 'Plugin Development' started by Maxx_Qc, Jan 7, 2016.

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

    Maxx_Qc

    Hi guys, I have a little problem.
    Code:
    if (e.getPlayer().getItemInHand().getType() == Material.LAVA_BUCKET
                        && Util.isLocationNearPortal(e.getClickedBlock().getLocation(), 3)) {
                    e.setCancelled(true);
                    Util.sendMsg(e.getPlayer(), "Vous ne pouvez pas faire ceci aussi près d'un portail");
                }
    This code actually works, it stops the player from placing lava near a nether portal.
    The problem is if the player place the block inside the portal (to close it), the event is still cancelled and this is not what I want. I want the player to be able to close it with a lava bucket but not able to place lava near it.
    isLocationNearPortal method:
    Code:
    public static boolean isLocationNearPortal(Location loc, int radius) {
            for (int x = (radius * -1); x <= radius; x++) {
                for (int y = (radius * -1); y <= radius; y++) {
                    for (int z = (radius * -1); z <= radius; z++) {
                        Block b = loc.getWorld().getBlockAt(loc.getBlockX() + x, loc.getBlockY() + y, loc.getBlockZ() + z);
                        if (b.getType() == Material.PORTAL)
                            return true;
                    }
                }
            }
            return false;
        }
    Thank ya!
     
  2. Offline

    teej107

    Get the distance squared between the portal block and the lava placement. Cancel the event if it is under 9 and above 1.
     
  3. Offline

    Maxx_Qc

    @teej107
    Nice, I never though about this.
    I'll try tomorrow
     
  4. Offline

    Zombie_Striker

    @Maxx_Qc
    This is to make the radius negitive, right? Then why not just have
    Code:
    (-radius)
    Also, make sure "b" is not null (if it's air, it will be null), and why do you do the math for each and every single x,y,z instance (The X + 'x') instead of just doing the math once when creating the for loop (int x = loc.getBlockX() -radius; x < loc.getBlockX()+radius.....)
     
  5. Offline

    teej107

    It doesn't make a difference.
     
  6. Offline

    Maxx_Qc

    @Zombie_Striker if b is null no one cares because we want the type, I never got an error.

    This method is not working like you said
    @teej107
    Code:
    public static boolean isLocationNearPortal(Location loc, int radius) {
            for (int x = (radius * -1); x <= radius; x++) {
                for (int y = (radius * -1); y <= radius; y++) {
                    for (int z = (radius * -1); z <= radius; z++) {
                        if (x == 0 && y == 0 && z == 0)
                            return false;
                        Block b = loc.getWorld().getBlockAt(loc.getBlockX() + x, loc.getBlockY() + y, loc.getBlockZ() + z);
                        if (b.getType() == Material.PORTAL)
                            return true;
                    }
                }
            }
            return false;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 8, 2016
  7. Offline

    Maxx_Qc

  8. Offline

    teej107

  9. Offline

    Maxx_Qc

Thread Status:
Not open for further replies.

Share This Page