Making stuff happen whenever a player is at a certain location?

Discussion in 'Plugin Development' started by techboy291, Oct 13, 2012.

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

    techboy291

    Hi,

    So on my server I want to make my plugin greet people when they enter the town hall. How would I go about doing this? Would a PlayerMoveEvent be best? Here's the code:

    Code:
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
            Player eventplayer = event.getPlayer();
            Location loc = eventplayer.getLocation();
            if((loc.getBlockY() == 83 && loc.getBlockZ() == -146) && (loc.getBlockX() == -205 && loc.getBlockX() == -206))
                eventplayer.sendMessage("Welcome to town hall!");
    }

    Thanks in advance :)
     
  2. Offline

    stantheman68

    You could just use World Guard for this. Just select you're region / block that you want it to send the player the message. Then use /region define <Name> <Owner Of Region> and after that use /region flag <Region Name> greeting <Message> .
     
  3. Offline

    techboy291

    I was hoping I would only have to rely on my plugin. Thanks though.

    Anyone else got an idea?
     
  4. Offline

    stantheman68

    I would, though you had never put out you're plugin code. Sorry
     
  5. Offline

    techboy291

    Oops, sorry. I edited the post and the code is included now.
     
  6. Offline

    Dave_

    here ya go!
    Code:
    public void onPlayerShopRegion(PlayerMoveEvent p){
        if((loc.getBlockY() <= 83 && loc.getBlockZ() >= -146) && (loc.getBlockX() <= -205 && loc.getBlockX() >= -206)){
            p.getPlayer().sendMessage("Welcome to town hall");
        }
    }
     
  7. Offline

    CevinWa

    You could just put a block out and therfor place it in an arraylist using serielizableLcation class and then just check for it when you move.
     
  8. Offline

    techboy291

    Thanks! I edited what you said a little bit and ended up doing this:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player eventplayer = event.getPlayer();
            Location loc = eventplayer.getLocation();
            if((loc.getBlockY() == 83.0 && loc.getBlockZ() == -146.0) && (loc.getBlockX() == -205.0 || loc.getBlockX() == -206.0))
                eventplayer.sendMessage("Welcome to town hall!");
    }
    The problem now is that the message shows around four times every time I walk on one of the two blocks. Ideas?
     
  9. Offline

    CorrieKay

    yeesh, this is a nightmare. i hate using the player move event, it fires so frequently, but i guess a little math wont hurt. anyways...

    Boolean. Create one when they log in, and check their location. if its within the location, flip it to true, and leave it alone. else, its false.

    on the player move event, if the boolean is false (last known outside of the area) and theyre inside the area, then you know theyve just walked into the area. display the text, and flip their boolean to true.

    ELSE (boolean is true, they were last known as being inside the area)

    if the boolean is true, and they are OUTSIDE the area, you know theyve just walked out of the area. flip their boolean to false (and optionally display a message that theyre leaving the area)

    Optionally, you can create an Area object that you can store in a set or list of sorts, iterate through them and call a method inside that asks if the player is within the area. Each area object can hold its own boolean list for players who are/aren't in the area, if you wish
     
  10. Offline

    ZerothAngel

    That's because a PlayerMoveEvent is fired every time a player moves -- including rotation/pitch changes as well as movements within the block.

    You should probably first check that the player actually moved 1 block before doing your usual checks. Something like:

    Code:
        @EventHandler(ignoreCancelled=true)
        public void onPlayerMove(PlayerMoveEvent event) {
            if (event.getFrom().getBlockX() != event.getTo().getBlockX() ||
                    event.getFrom().getBlockY() != event.getTo().getBlockY() ||
                    event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
    
                        Player eventplayer = event.getPlayer();
                        Location loc = event.getTo(); // check destination location
                        if((loc.getBlockY() == 83 && loc.getBlockZ() == -146) && (loc.getBlockX() == -205 || loc.getBlockX() == -206.0))
                            eventplayer.sendMessage("Welcome to town hall!");
            }
        }
    
    Note also that I believe .getBlockX() etc return integers.
     
  11. Offline

    techboy291

Thread Status:
Not open for further replies.

Share This Page