Event for checking when a player enters an area?

Discussion in 'Plugin Development' started by civ77, May 2, 2012.

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


    How would I use an event to check when a player enters an area? I want to stop players from being able to enter an area under certain circumstances and I can't think of any way of doing this, do I need to create a custom event (I have no idea how to do this)?
  2. Offline


    Before you use this, note that this code may be heavy in resource usage on larger servers.
    public void onMove(PlayerMoveEvent event) {
    Location loc = event.getPlayer().getLocation;
    int x = loc.getBlockX();
    int y = loc.getBlockY();
    int z = loc.getBlockZ();
    if (region.getX() > x > region2.getX() && region.getY()> y > region2.getY() && region.getZ() > z > region2.getZ()) {
    //teleport the player out or set event to cancelled.
  3. Offline


    Yeah I was worrying about the resource usage of onPlayerMove because this is intended for use in a plugin to interface with factions, so it is designed for large servers.
  4. Offline


    If you know that regions have integer coordinates, you can first check whether the player moved to another block before checking whether he's in a (new) region:
    if (e.getFrom().getBlockX() != e.getTo().getBlockX() || e.getFrom().getBlockY() != e.getTo().getBlockY() || e.getFrom().getBlockZ() != e.getTo().getBlockZ()) {
        // moved to another block
    You can now loop through all regions and check whether e.getFrom() and/or e.getTo() are in the region. If both are within the region or outside, do nothing, but if one is outside and one inside you know that the player entered/left this region.

    PS: If the z-coordinate doesn't matter for the regions you can remove the check for z.
  5. Offline


    Njol I know how to use the event, I was looking for an event or some way of checking when players enter an area which doesn't use loads of rescources.
  6. Offline


    Make it only do the check every x server ticks?
  7. Offline


    how would I do that? AFAIK you can't use sleep() to delay an event.
  8. Offline


    Use a delayed repeating task.

    I would provide a link but i am on my phone atm.

    Try looking through the tutorials in the dev area.
    civ77 likes this.
Thread Status:
Not open for further replies.

Share This Page