Making a rectangular area which no one can leave

Discussion in 'Plugin Development' started by skipperguy12, May 8, 2013.

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

    skipperguy12

  2. Offline

    caseif

    Allow players to select the bounds with the use of a WorldEdit-esque wand and the PlayerInteractEvent, then when the player runs a certain command, store the min and max values of each axis one way or another. Then, load those into your plugin when it starts (to save on memory) and check the PlayerMoveEvent to make sure it's within the bounds or each axis, and cancel it if it's not.
     
  3. Offline

    skipperguy12

    AngryNerd

    Well, players are not the ones who set them. I do. There's only 1 boundary per world.

    This is what I have, but I can't test due to some NPEs in my code, someone please check?
    Creating the rectangle
    Code:
        public Rectangle borders;
        Point a;
        Point b;
    
    Setting and checking if a loc is outside:
    Code:
        public boolean isBorder(Location l) {
            Rectangle tempRect = new Rectangle(plugin.getConfig().getInt(worldFileName + ".border.x"), plugin.getConfig().getInt(worldFileName + ".border.y"), plugin.getConfig().getInt(worldFileName + ".border.width"), plugin.getConfig().getInt(worldFileName + ".border.height"));
            a = new Point();
            a.setLocation(tempRect.getX(), tempRect.getY());
     
            b = new Point();
            b.setLocation(tempRect.getX() - tempRect.getWidth(), tempRect.getY() - tempRect.getHeight());
            Bukkit.getLogger().info("POINT B X: " + b.getX());
            Bukkit.getLogger().info("POINT B Y: " + b.getY());
            Bukkit.getLogger().info("POINT A X: " + a.getX());
            Bukkit.getLogger().info("POINT A Y: " + a.getY());
            if (l.getX() >= borders.getX() && l.getX() <= b.getX()) {
                if (l.getY() >= borders.getY() && l.getY() <= b.getY()) {
                    // On the border! D:
                    return true;
     
                }
            }
     
           
            return false;
     
        }
    The the values print, but the last method I gave for some reason gives an NPE ;l

    At this line:
    if (l.getX() >= borders.getX() && l.getX() <= b.getX()) {
     
  4. Offline

    molenzwiebel

    Code:
    if (event.getblock().getX() > Math.min(p1.getX(), p2.getX()) & event.getblock().getX() < Math.max(p1.getX(), p2.getX()) ){
    if(if (event.getblock().getZ() > Math.min(p1.getZ(), p2.getZ()) & event.getblock().getZ() < Math.max(p1.getZ(), p2.getZ()) ){
    event.setcancelled(true);
    }
    }
    
    p1 and p2 are the corners
     
  5. Offline

    caseif

    Don't ask the forums to find your mistakes for you; debug first. If several days have gone by and you truly are incapable of finding the problem code, then you can ask for help.
     
  6. Offline

    skipperguy12

    AngryNerd
    You think i'd just ask you this?
    This is a problem that has bothered me for weeks...
    I keep rewriting the code (and I think it turns out to be the same) but this one looks IDENTICAL to what I tried a couple weeks ago.
     
  7. Offline

    caseif

    Check the stack traces and figure out first which lines are causing the exceptions, then which variables within the lines.
     
  8. Offline

    skipperguy12

    AngryNerd
    But I made it output all variables:
    Code:
            Bukkit.getLogger().info("POINT B X: " + b.getX());
            Bukkit.getLogger().info("POINT B Y: " + b.getY());
            Bukkit.getLogger().info("POINT A X: " + a.getX());
            Bukkit.getLogger().info("POINT A Y: " + a.getY());

    And i've run tests within the event:
    Code:
                if (plugin == null) {
                    Bukkit.getLogger().info("Plugin is null : 237");
                }
                if (plugin.MapHandler == null) {
                    Bukkit.getLogger().info("Plugin.MapHandler is null : 240");
                }
                if (plugin.MapHandler.isBorder(e.getBlock().getLocation()) == false) {
                    Bukkit.getLogger().info("isBorder returned false : 243");
                }
                if (e.getBlock().getLocation() == null) {
                    Bukkit.getLogger().info("BLOCK IS NULL : 246");
                }
    Nothing that prints tells me anything is null. All the points print correctly.
     
  9. Offline

    Tzeentchful

    skipperguy12
    Here is a border object I made for one of my plugins.
    Simply lissen to the player move event and call the event.getTo() though the 'contains(Location loc)' method in the object. And just TP the player out if it returns false.
    Code:java
    1. public class Border {
    2.  
    3. private Vector p1;
    4. private Vector p2;
    5.  
    6.  
    7. public Border(Vector p1, Vector p2) {
    8. int x1 = Math.min(p1.getBlockX(), p2.getBlockX());
    9. int y1 = Math.min(p1.getBlockY(), p2.getBlockY());
    10. int z1 = Math.min(p1.getBlockZ(), p2.getBlockZ());
    11. int x2 = Math.max(p1.getBlockX(), p2.getBlockX());
    12. int y2 = Math.max(p1.getBlockY(), p2.getBlockY());
    13. int z2 = Math.max(p1.getBlockZ(), p2.getBlockZ());
    14. this.p1 = new Vector( x1, y1, z1);
    15. this.p2 = new Vector( x2, y2, z2);
    16. }
    17.  
    18. public boolean contains(Location loc) {
    19. if(loc == null) {
    20. return false;
    21. }
    22. return loc.getBlockX() >= p1.getBlockX() && loc.getBlockX() <= p2.getBlockX()
    23. && loc.getBlockY() >= p1.getBlockY() && loc.getBlockY() <= p2.getBlockY()
    24. && loc.getBlockZ() >= p1.getBlockZ() && loc.getBlockZ() <= p2.getBlockZ();
    25. }
    26.  
    27. }
    28.  
     
  10. Offline

    Technius

  11. Offline

    skipperguy12

    Tzeentchful
    Hello, thanks for literally helping me on every question i've posted on Bukkit that's important (to me) :)

    I've never tinkered around with Vectors before, and I started searching bukkit forums, I can't find much. I saw one post saying it used Trigonometry, so I just rushed to learn that. I now have a very basic understanding of how to use Trig, so now how do I use Vectors? I read it's a change in locations, but what does that mean? Sorry if I sound stupid :(

    Technius

    How could a location be null in an event...

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

    Technius

    skipperguy12 Looking back at your code, your border could be null too. See if this tells you anything bad:
    Code:
    System.out.println(l == null ? "Location is null!" : "Location is fine");
    System.out.println(borders == null ? "Borders is null!" : "Borders is fine");
    
     
  13. Offline

    skipperguy12

    Technius
    That wasn't null, look at posts above. I made it print variables from the borders, nothing null over there.
     
  14. Offline

    Technius

    skipperguy12 You printed out methods from the "a" and "b" variables, but not from the "borders" variable.
     
  15. Offline

    skipperguy12

    Technius
    a and b are Points, which are set from methods from the borders. I wouldn't have gotten that far if borders was null.
     
  16. Offline

    Technius

    skipperguy12
    "a" and "b" use methods from "tempRect". "tempRect" is created with values from your configuration file. I cannot see how "a" and "b" reference "borders".

    If "l" and "b" are not null, then logic dictates that "borders" is null.
     
    skipperguy12 likes this.
  17. Offline

    skipperguy12

    Technius
    You. Are. Amazing.

    I cannot express how amazing you are, I wish there was a second like button. :)
     
  18. Offline

    Technius

    skipperguy12 No problem! Learning from mistakes always helps.
     
  19. Offline

    skipperguy12

    Technius
    Now i've got a problem where it's always false, when it should be true :(

    So I drew a grid, it looks like it should be working...

    POINT B X: -254.0
    POINT B Y: 267.0
    POINT A X: -220.0
    POINT A Y: 303.0

    when I broke a block on the border at -187, 292
     
  20. Offline

    Technius

    skipperguy12 It seems like the x value is from -220 to -254. -187 is not within this range.
     
  21. Offline

    skipperguy12

    Technius
    Okay, so I set them to numbers easier to work with.
    Point a: x: 0, y/z: 0
    Point b: x: 10, y/z: 10

    And i've used this code:
    Code:
            Bukkit.getLogger().info("POINT B X: " + b.getX());
            Bukkit.getLogger().info("POINT B Y: " + b.getY());
            Bukkit.getLogger().info("POINT A X: " + a.getX());
            Bukkit.getLogger().info("POINT A Y: " + a.getY());
     
            Bukkit.getLogger().info(l.getX() >= a.getX() ? "loc x " + l.getX() + "was bigger than borders x" : "loc x " + l.getX() + "was less than borders x");
            Bukkit.getLogger().info(l.getX() <= b.getX() ? "loc x " + l.getX() + "was bigger than borders x" : "loc x " + l.getX() + "was less than borders x");
     
            Bukkit.getLogger().info(l.getY() >= a.getY() ? "loc y " + l.getY() + "was bigger than borders y" : "loc y " + l.getX() + "was less than borders x");
            Bukkit.getLogger().info(l.getY() <= b.getY() ? "loc y " + l.getY() + "was bigger than borders y" : "loc y " + l.getX() + "was less than borders x");
            if (l.getX() >= a.getX() && l.getX() <= b.getX()) {
                if (l.getY() >= a.getY() && l.getY() <= b.getY()) {
                    // On the border! D:
                    return true;
     
                }
            }
    When I break a block on the border, at 0 , 0 to be precise, I get:
    POINT B X: 10.0
    POINT B Y: 10.0
    POINT A X: 0.0
    POINT A Y: 0.0
    loc x 0.0 was bigger than borders x
    loc x 0.0 was bigger than borders x
    loc y 86.0 was bigger than borders y
    loc y 0.0was less than borders x

    Which....isn't exactly what I want. What the heck? :(
     
  22. Offline

    Technius

Thread Status:
Not open for further replies.

Share This Page