Solved [WorldGuard] Push players back from region

Discussion in 'Plugin Development' started by Luke_Lax, Dec 10, 2014.

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

    Luke_Lax

    Hey everyone,

    I'm currently working on a custom plugin, I thought it'd be simple and sweet however it's turning out to be a little bit troublesome.

    The issue I'm having is when a player enters a WorldGuard region, it should knock them back so they can't enter. They're blocked by a specific check thus I'm not using the EntryDeny flag from WG.

    Everything is fine except pushing the player away of the region, I originally had this:
    Show Spoiler

    Code:
            Vector dir = loc.getDirection();
            dir.normalize();
            dir.multiply(-2); //2 blocks back
            loc.add(dir);
            return loc;


    Although this works, it only works if you're looking into the region, if you're not it'll actually force you into the region.

    So I looked at the source for WorldGuard to see how they did the Entry Deny flag but when I try the way they did it, I am able to run into the region by one block and then I'm stuck in that block so I don't understand how exactly the event stops entry entirely when it doesn't for me.

    Here is the source from WorldGuard relating to Entry Deny: Click Here

    I'd very much appreciate if someone could help me out here, I do not know why it (the WG code for entry deny) allows me to enter one block in?

    Thank you!

    Regards,

    Luke
     
  2. Offline

    Tehmaker

    @Luke_Lax
    Not 100% sure how you could go about this, but you could possibly check what direction the center of the region is from where the player is facing, and adapt the code you had to account for what direction they are facing.
     
    KingFaris11 likes this.
  3. What @Tehmaker would be good. I think you can get that vector by subtracting the center of the region by the player's velocity.
     
  4. Offline

    Luke_Lax

    But my issue with that is... as far as I'm aware, they're don't do that in the WorldGuard code so it must be possible without it. They definitely block it from the PlayMoveEvent however I cannot figure out why it lets me go in one block but it's the same code
     
  5. Offline

    yewtree8

    how about waiting a tick before setting the velocity?
     
  6. Offline

    mythbusterma

    @Luke_Lax

    But my issue is, doesn't that solve your problem? It's a very simple solution and it would work quite well.


    @yewtree8

    How does that help....at all?
     
  7. Offline

    Luke_Lax

    Well no, why do that when it seems there's a way to do it without doing that
     
  8. Offline

    mythbusterma

    @Luke_Lax

    Is there something inherently wrong with this solution? It's quite straight-forward and it's not very computationally expensive. Who cares how WorldGuard itself does it.
     
    KingFaris11 likes this.
  9. Offline

    ChipDev

    I've done this just today, use the player move event and get the velocity between the old location and the new location of the player. Then you will send him back; Had this same idea! Good job on making it shorter!
     
  10. Offline

    Luke_Lax

    I've just woke up but if you're talking about the code in the first post, try looking away from the area you're being blocked from and walk backwards :p
     
  11. Offline

    ChipDev

    I know, but look at this:
    *You must walk INTO the area, meaning that you could have a .getFrom() and that would be BEHIND you, no matter where you are looking. Then with some cool vectors (as seen here) you can convert them into vectors, use Sethblings filter --> java, and then use the Player's gravity! (0.667) ≈
    Edit: I've done this on my server trying to do the thing that blocks you from portals on mineplex ;)
     
  12. Offline

    MisterErwin

    @ChipDev But if the player lags he might get pulled into the region, like Luke_Lax said.

    Thats why pushing away from the center/origin works better.
     
  13. Offline

    ChipDev

    Still, if the player lags, he will be coming IN from the outside.
     
  14. Offline

    MisterErwin

    @ChipDev Maybe true: But let's slow the scenario down:
    The player walks in
    =>The PlayerMoveEvent is fired => The player is shot out
    He is moving, so another PlayerMoveEvent is fired
    The velocity will get overwritten => Pinball

    If minecraft would simulate physics more realistic: Position<-Differentation-Velocity <-- Diff. - Acceleration this might work, but as minecraft's/bukkit's Entity#setVelocity(...) sets the Velocity, this depends on the connection & the TPS of the server.

    ^Don't quote me on the physics :p
     
  15. Offline

    Avygeil

    @MisterErwin Then teleport him instead, and launch him from e.getFrom(). I don't see what's wrong with @ChipDev 's reasoning on vectors. The only way it can pull a player in is if that vector looks out of the region, which is the case if you use the player's eye direction like in the OP.
    Both solutions work, since the vector looks out of the region. If you use the center you would only need to calculate/store it which isn't a big deal with cuboids. Another advantage of the center is that you can do it outside the event if needed. I don't see why the center would counter lag and not the other one, though?
    You are right on the physics, but probably not on the lag part. I don't see why lag would even be a problem here. Suppose connection stops for a bit, the event has still fired server side and the player has been pushed server side. Your position will be updated when connection comes back. The velocity "pinball" problem will never happen if you push the player as soon as he enters. The only impact lag could have is being able to see inside the region server side, but it's something you really can't counter. I'm aware you can bypass lots of things by pulling your cable, but I haven't seen a WG region bypass yet.

    @Luke_Lax You can use both solutions, any will work as long as you don't use the eye direction vector.
    Mind sending the code? This is exactly the way I do it in my plugins and it has always worked so far.
    I know I had a similar glitch if I just teleported myself inside a denied region in WG though.
     
    ChipDev likes this.
  16. Offline

    ChipDev

    Thats why I used a HashMap<Player, Boolean> to check if the player is NEAR the zone, when it pushes out it REMOVES him from the zone, But it needs to check if the player is Outside of the zone (Just a little)... don't get to complicated on me, it can be glitchy, but a very low chance.
     
  17. Offline

    MisterErwin

    @ChipDev I'm not saying anything about the Player Reference,
    but I hope you are also listening to a PlayerQuitEvent and using:
    PHP:
    Map<Player,Booleanname = new HashMap<Player,Boolean>();
    :p
     
    ChipDev likes this.
  18. Offline

    Luke_Lax

    I decided to take another approach now that I have more time and I solved it myself; maybe it's not the most efficient way but for anyone wondering:

    Rather than checking the location the player has moved to (still checking it's a whole block), I now check the block they're about to go to and cancel the event if the conditions are right, that way they don't get near the region.

    Thanks to all who helped me in this thread!
     
Thread Status:
Not open for further replies.

Share This Page