Storing x amount of regions.

Discussion in 'Plugin Development' started by repsor, Dec 10, 2012.

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

    repsor

    Hello again!
    I have a question about storing a x amount of regions in a config file. With regions I mean regions declared with a x and a z value. I need this for a plugin for a friend that has a pvp server, but there are some safezones. He wants to set as many safezones as he wants, but I need ideas how to store them in a config file. As a list of x's and z's, a list of regions apart? And must I make lists of all blocks in every region in the onEnable method, or should I check if the player is in a region by the if statements I use (the : if the x of the location of the player is equal or smaller than x1 and equal or bigger than x2 etc.) on every movement?
     
  2. Offline

    tommycake50

    every time you add a region put either a unique region name or append an incrementing number that increments every time a region is set.
     
  3. Offline

    repsor

    Thanks!

    Question: how would you check if the playermovement is in the region? I mean you can get all the x's and z's from the config file and check if the player is in it, but I think this can be done faster: maybe just a list with an updater (sheduler) to keep it up to date and if the list contains player.getLocation, the player is safe. Do you have another idea and if not, do you know how to do this?

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

    skipperguy12

    You can store the x and z from the config as a location, and check the distance between them and if its 0, continue...
     
  5. Offline

    repsor

    Wait I don't get what you're trying to do there :)
     
  6. Offline

    Jnorr44

    I know this may not be relevant to the question, but may I recommend a new class I wrote that is a region class but requires storage of only 4 locations?

    Code:
    public class Region {
        private Location loc1, loc2, loc3, loc4;
        private World world;
        private int highX, highZ, lowX, lowZ;
       
        /**
        * Creates a new 2D region from 2 corner points.
        *
        * @param loc1 The first corner
        * @param loc2 The second corner
        */
        public Region(Location loc1, Location loc2) {
            world = loc1.getWorld();
            this.loc1 = loc1;
            this.loc2 = loc2;
            loc3 = world.getBlockAt(loc2.getBlockX(), 0, loc1.getBlockZ()).getLocation();
            loc4 = world.getBlockAt(loc1.getBlockX(), 0, loc2.getBlockZ()).getLocation();
            if (loc1.getBlockX() > loc2.getBlockX()) {
                highX = loc1.getBlockX();
                lowX = loc2.getBlockX();
            } else {
                highX = loc2.getBlockX();
                lowX = loc1.getBlockX();
            }
            if (loc1.getBlockZ() > loc2.getBlockZ()) {
                highZ = loc1.getBlockZ();
                lowZ = loc2.getBlockZ();
            } else {
                highZ = loc2.getBlockZ();
                lowZ = loc1.getBlockZ();
            }
        }
       
        /**
        * Gets the corner that matches the given number.
        *
        * @param corner The corner number from 1-4
        * @return The corner of thet number
        */
        public Location getCorner(int corner) {
            switch(corner) {
                case 1:
                    return loc1;
                case 2:
                    return loc2;
                case 3:
                    return loc3;
                case 4:
                    return loc4;
            }
            return null;
        }
       
        /**
        * Checks if the given region touches or overlaps this region.
        *
        * @param other The region to check for
        * @return Whether or not they touch or overlap
        */
        public boolean overlaps(Region other) {
            boolean lows = false, highs = false, lowZs = false, lowXs = false, highZs = false, highXs = false;
            if (lowZ <= other.getLowestZ() && highZ >= other.getLowestZ())
                lowZs = true;
            if (lowX <= other.getLowestX() && highX >= other.getLowestX())
                lowXs = true;
            if (lowZs && lowXs)
                lows = true;
            if (lowZ <= other.getHighestZ() && highZ >= other.getHighestZ())
                highZs = true;
            if (lowX <= other.getHighestX() && highX >= other.getHighestX())
                highXs = true;
            if (highZs && highXs)
                highs = true;
            return (lows && highs);       
        }
       
        /**
        * Checks if the location is contained inside the region.
        *
        * @param loc The location to check for
        * @return Whether or not the location is contained in the region
        */
        public boolean contains(Location loc) {
            boolean Xs = false, Zs = false;
            if (lowZ <= loc.getBlockZ() && highZ >= loc.getBlockZ())
                Zs = true;
            if (lowX <= loc.getBlockX() && highX >= loc.getBlockX())
                Xs = true;
            return (Xs && Zs);   
        }
       
        /**
        * Checks if the player is contained inside the region.
        *
        * @param p The player to check for
        * @return Whether or not the player is contained in the region
        */
        public boolean contains(Player p) {
            return contains(p.getLocation());
        }
       
        /**
        * Gets the highest X of all 4 locations in this region.
        * @return The highest X value for this region
        */
        public int getHighestX() {
            return highX;
        }
       
        /**
        * Gets the lowest X of all 4 locations in this region.
        * @return The lowest X value for this region
        */
        public int getLowestX() {
            return lowX;
        }
       
        /**
        * Gets the highest Z of all 4 locations in this region.
        * @return The highest Z value for this region
        */
        public int getHighestZ() {
            return highZ;
        }
       
        /**
        * Gets the lowest Z of all 4 locations in this region.
        * @return The lowest Z value for this region
        */
        public int getLowestZ() {
            return lowZ;
        }
    }
    Just want to spread that out there because I have found some people saving every location in the region just wanting to check if a player is in the region. Currently this is only 2D, but I may make a 3D version in the future.
     
  7. Offline

    repsor

    This the contains method is way more efficiƫnt than the one I wrote myself: may I use the idea/concept of it? (only the idea: not literal copying :) )
     
  8. Offline

    fireblast709

    Maybe not as flexible as the above methods, but hell of a lot easier: safe chunks. Then you can store it in a config like this:
    Code:
    safe-chunks:
      - 0,0
      - 0,1
      - x,z
    You can just use a Set or ArrayList to store them in, you can read/write the list in 1 line (except loading the config and saving it), and checking is easy as well (get the chunk x and z, append them with a , like "x,z", and check if it is in the list. As I said before: not as flexible, but hell of a lot easier :3
     
  9. Offline

    Jnorr44

    Yes, yes you can :D

    fireblast709
    That only put chunks in a region, this class can go from any location to any other location.
     
  10. Offline

    fireblast709

    Jnorr44 thats what I said twice in my post ;3
     
  11. Offline

    repsor

    Thanks! It maybe not so precise, but I'm gonna use it :)
     
Thread Status:
Not open for further replies.

Share This Page