Solved Help with simple area code

Discussion in 'Plugin Development' started by Yazan, Dec 29, 2013.

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

    Yazan

    Hello. I've been trying to send a message to the player in a certain zone, it worked but the problem is it spams it every time he moves in that zone because it's in the playerMoveEvent. Here's my code:
    Code:
    @EventHandler
        public void moveEvent(PlayerMoveEvent event)
        {
            Player player = event.getPlayer();
            int x = player.getLocation().getBlockX();
            int y = player.getLocation().getBlockY();
            int z = player.getLocation().getBlockZ();
           
            int bXstart = this.getConfig().getInt("blockX");
            int bYstart = this.getConfig().getInt("blockY");
            int bZstart = this.getConfig().getInt("blockZ");
     
            int bXend = this.getConfig().getInt("blockX2");
            int bYend = this.getConfig().getInt("blockY2");
            int bZend = this.getConfig().getInt("blockZ2");
           
            if(x >= bXstart && x <= bXend || bXstart >= x && bXend <= x)
            {
                if(y >= bYstart && y <= bYend || bYstart >= y && bYend <= y)
                {
                    if(z >= bZstart && z <= bZend || bZstart >= z && bZend <= z)
                    {
                        player.sendMessage("You're in the area");
                    }
                }
            }
        }
    And can you please tell me how to check when a player leaves the zone? Thanks!
     
  2. Offline

    sgavster

    Use else statements?....
     
  3. Offline

    Yazan

    For which part? Please be more specific.

    Anyone?

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

    thepaperboy99

    You could you a Boolean method to check if the player is in the area. Just use all of that code in the Boolean method so it would be easier to check.
     
  5. Offline

    Yazan

    No that's not the problem, anyone?

    :/

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

    metalhedd

    PlayerMoveEvent has a .getTo() and a .getFrom() method. perform your logic on EACH of those, then:
    - if TO is inside the region and FROM is not, then you're entering.
    - if FROM is inside the region and TO is not, then you're leaving
    - if they're both inside, don't spam, he already got the message.
    - if they're both outside, do nothing.
     
    NathanWolf and Yazan like this.
  7. Offline

    Yazan

    Finally! Thank you so much I'll try it out and check if there are any errors. If there are then I'll re-post here.

    Works and I don't have any errors! Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page