Getting if a player is in a location in a config

Discussion in 'Plugin Development' started by WingedMLGPro, Jun 6, 2015.

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

    WingedMLGPro

    Hey Guys!
    I am creating a Region Plugin and i am having trouble with getting if a Player inside a Location!
    There are 2 Locations and each of them are in a config so how would i get if a player is between the 2 locations?
    If you need any of my code Please tell me!

    -WingedMLGPro
     
  2. Offline

    mythbusterma

    @WingedMLGPro

    Find the two points as Vectors, then do something along the lines of:

    player.getLocation().toVector().isInAABB(vector1, vector2)

    This will check if the Player is in between the points represented by vector1 and vector2.
     
  3. Offline

    WingedMLGPro

    @mythbusterma How would i get All The Regions Locations?
    My Config Looks like this:
    SwiftyGuard:
    (RegionName):
    Location 1:
    Location 2:
    //Other Things

    But how would i get the vector for all the Regions?
    Because there will be more that 1 (RegionName):
     
  4. Offline

    WingedMLGPro

    @mythbusterma Ok i tried to do it by myself but it didnt work...
    Here is what i did:

    Code:
    public ConfigurationSection getRegions(){
    
            ConfigurationSection rg = (ConfigurationSection) Regions.getInstance().getConfig().getConfigurationSection("SwiftyGuard").getKeys(false);
            if (rg!=null){
                return rg;
            }
                return null;
        }
    Code:
    @EventHandler
        public void onBlock(BlockBreakEvent e){
            Player p = e.getPlayer();
            //SwiftyPlayer user = new SwiftyPlayer(p);
            Vector p1 = (Vector)Regions.getInstance().getConfig().get("SwiftyGuard"+getRegions()+".Location 1");
            Vector p2 = (Vector)Regions.getInstance().getConfig().get("SwiftyGuard"+getRegions()+".Location 2");
            if (p.getLocation().toVector().isInAABB(p1, p2);
                    e.setCancelled(true);
                    p.sendMessage(Regions.Error+"You cannot break blocks in this area!");
            }
        }
     
  5. You're doing quite a lot wrong, such as casting a Set to a ConfigurationSection, making a List a String, casting a String to a Vector, etc.

    I don't really have much time, but I can leave my Cuboid class here (modification of desht's):

    https://gist.github.com/KingFaris10/4527fbaf8caa9fd7b800

    With this, you can use:

    Cuboid#contains(location); - Check if a cuboid contains a location.
    Cuboid#serialize(); - Save a cuboid the config.
    Cuboid#deserialize(Map); - Deserialize a cuboid from a Map from the configuration.

    Example usage:
    Code:
    public List<Cuboid> getRegions() {
        List<Cuboid> regions = new ArrayList<Cuboid>();
        if (Regions.getInstance().getConfig().isConfigurationSection("SwiftyGuard")) { // Check if the "SwiftyGuard" key exists in the config and is a config section.
            for (Map.Entry<String, Object> configEntry : Regions.getInstance().getConfig().getConfigurationSection("SwiftyGuard").getValues(false).entrySet()) { // Loop through all the keys and values in the config section.
                if (configEntry.getValue() instanceof ConfigurationSection) regions.add(Cuboid.deserialize(((ConfigurationSection) configEntry.getValue()).getValues(false))); // Deserialize the cuboid and add it to the regions list.
            }
        }
        return regions;
    }
    
    public boolean inRegion(Location location) {
        for (Cuboid region : this.getRegions()) {
            if (region != null & & region.contains(location)) return true;
        }
        return false;
    }
    

    However, it seems to me like you don't really know the basics of Java (e.g. casting, what lists really are, etc.), and I'd recommend learning that before programming Bukkit plugins.
     
    Last edited: Jun 8, 2015
  6. Offline

    mythbusterma

    @KingFaris11

    Why don't you have two Vectors and use Vector#isInAABB(...)? All the work is already done for you...
     
  7. If you're talking about the cuboid class: Yeah, it was my old code, just updated it with contains() for someone else quickly recently, but I don't even use this myself. The OP can replace "x, y, z" variables with Vector but he probably won't know how.

    If you're talking in general, why I didn't get vectors instead of the cuboid class, it's because I didn't have enough time at that time, and it seemed no one really was replying.

    Updated now: https://gist.github.com/KingFaris10/4527fbaf8caa9fd7b800
     
    Last edited: Jun 9, 2015
  8. Offline

    WingedMLGPro

    @KingFaris11
    I tried your Cuboid class but it isnt working.
    Here is what i did:
    Code:
    package me.WingedMLGPro.Listeners;
    
    import me.WingedMLGPro.Regions;
    import me.WingedMLGPro.Util.Cuboid;
    import org.bukkit.Location;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    /**
    * © This Document and Code is STRICTLY copyrited(©) to Ben.
    * © If anyone takes any part of this code and uses it for
    * © Something public, Share with someone, uses it for API's,
    * © implementing it to their code and taking the rights for
    * © it is NOT allowed unless you get permission from me!
    * © This project SwiftyGuard was created by 35047
    * © at 7/06/15 at 9:40 AM
    */
    public class UtilListeners implements Listener {
    
        public List<Cuboid> getRegions() {
            List<Cuboid> regions = new ArrayList<Cuboid>();
            if (Regions.getInstance().getConfig().isConfigurationSection("SwiftyGuard")) { // Check if the "SwiftyGuard" key exists in the config and is a config section.
                for (Map.Entry<String, Object> configEntry : Regions.getInstance().getConfig().getConfigurationSection("SwiftyGuard").getValues(false).entrySet()) { // Loop through all the keys and values in the config section.
                    if (configEntry.getValue() instanceof ConfigurationSection) regions.add(Cuboid.deserialize(((ConfigurationSection) configEntry.getValue()).getValues(false))); // Deserialize the cuboid and add it to the regions list.
                }
            }
            return regions;
        }
    
        public boolean inRegion(Location location) {
            for (Cuboid region : this.getRegions()) {
                if (region != null && region.containsLocation(location)) return true;
            }
            return false;
        }
    
        @EventHandler
        public void onBlock(BlockBreakEvent e){
            Player p = e.getPlayer();
            //SwiftyPlayer user = new SwiftyPlayer(p);
            //if (!(user.getRank().equals(RankManager.HEADADMIN)|| user.getRank().equals(RankManager.OWNER))){
            if (inRegion(p.getLocation())) {
                e.setCancelled(true);
                p.sendMessage(Regions.Error + "You cannot break blocks in this area!");
            }
            //}
        }
        @EventHandler
        public void onBlock(BlockPlaceEvent e){
            Player p = e.getPlayer();
            //SwiftyPlayer user = new SwiftyPlayer(p);
            //if (!user.getRank().equals(RankManager.HEADADMIN)|| user.getRank().equals(RankManager.OWNER)){
            if (inRegion(p.getLocation())) {
                e.setCancelled(true);
                p.sendMessage(Regions.Error + "You cannot place blocks in this area!");
            }
            //}
        }
    }
    
    Plz tell me if im doing anything wrong!
    I feel like such an idiot :(
     
  9. Post your config. The regions must be a serialized form of the region, which it probably isn't.
    Also, print out the size of "getRegions()" in BlockBreakEvent.
     
  10. Offline

    WingedMLGPro

    @KingFaris11 this is what my Config looks like:
    Code:
    SwiftyGuard:
      test1:
        Creator: WingedMLGPro
        Location 1:
          ==: org.bukkit.Location
          world: world
          x: 273.0
          y: 70.0
          z: 305.0
          pitch: 0.0
          yaw: 0.0
        Location 2:
          ==: org.bukkit.Location
          world: world
          x: 271.0
          y: 70.0
          z: 309.0
          pitch: 0.0
          yaw: 0.0
        PvP: 'false'
        EnterMsg: 'null'
        LeaveMsg: 'null'
    
    And why do I have to print out the size of getRegions? I dont get what u mean
     
  11. So sorry for the late reply, been busy with exams. I told you to print the size to test if any regions were actually being loaded, but don't worry now, it seems none were loaded as: the issue is that the regions aren't even in the style of a serialized cuboid.

    To fix this, remove "Location 1" and 2 from the config, and add:
    Code:
    Locations:
      worldName: world
      x1: 271.0
      x2: 273.0
      y1: 70.0
      y2: 70.0
      z1: 305.0
      z2: 309.0
    
    Then change the getRegions method to:
    Code:
    public List<Cuboid> getRegions() {
        List<Cuboid> regions = new ArrayList<Cuboid>();
        if (Regions.getInstance().getConfig().isConfigurationSection("SwiftyGuard")) { // Check if the "SwiftyGuard" key exists in the config and is a config section.
            for (String regionName : Regions.getInstance().getConfig().getConfigurationSection("SwiftyGuard").getKeys(false)) {
                if (Regions.getInstance().getConfig().isConfigurationSection("SwiftyGuard." + regionName + ".Locations")) regions.add(Cuboid.deserialize(Regions.getInstance().getConfig().getConfigurationSection("SwiftyGuard." + regionName + ".Locations").getValues(false))); // Deserialize the cuboid and add it to the regions list.
            }
        }
        return regions;
    }
    

    Or if you really want to keep it as Location 1 and 2, change the Cuboid class so that its serialize & deserialize method is:
    Code:
        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> serializedCuboid = new HashMap<>();
            serializedCuboid.put("Location 1", this.minimumPoint.toLocation(this.getWorld()).serialize());
            serializedCuboid.put("Location 2", this.maximumPoint.toLocation(this.getWorld()).serialize());
            return serializedCuboid;
        }
    
        public static Cuboid deserialize(Map<String, Object> serializedCuboid) {
            try {
                Location loc1 = Location.deserialize(((ConfigurationSection) serializedCuboid.get("Location 1")).getValues(false));
                Location loc2 = Location.deserialize(((ConfigurationSection) serializedCuboid.get("Location 2")).getValues(false));
    
                return new Cuboid(loc1, loc2);
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    
    And then still keep the same getRegions() method as the one I posted a few days ago. This is not the recommended way if you want to serialize the Cuboid to save it (cuboid.serialize()) as the region name isn't stored, etc.
     
    Last edited: Jun 12, 2015
  12. Offline

    mythbusterma

    @KingFaris11

    Why are catching Exception and returning null? That's terrible exception handling, just pass the exception on up.
     
  13. That's the whole point. I'm handling it myself globally, so I won't have to do the same thing again. This way, it'll print the exception, then continue in the for loop which checks if the Cuboid isn't null. Instead of, in that getRegions() code, having to catch it there, and do things individually, as there's no real point. Unless, he uses Cuboid.deserialize() in other places too.

    I would not have done this if I didn't have to use it only once in a method.

    Tip to OP: By the way, you might want to store the cuboids in one Map like Map<String, Cuboid>, the String being the region name and Cuboid being the cuboid (region). This way, you won't have to keep deserializing and looking through the config, making it faster.
     
  14. Offline

    mythbusterma

    @KingFaris11

    The point being if an Exception is thrown at that location, it would be because of a programming error, and not because of something going wrong at runtime.
     
  15. True, I mean, the reason is different to mine, but I guess in the first place I shouldn't be catching it, but instead, checking. I didn't do it here because I was typing stuff on my iPad, slow typing = rushed. The only reason I was doing a try {} and catch{} was because incase the Map doesn't contain Location 1 or 2 (i.e. config doesn't contain this), but instead I should have just checked if it does or doesn't.
     
    mythbusterma likes this.
  16. Offline

    WingedMLGPro

    @KingFaris11 i tried it but it still doesnt work.
    This is what the config looks like now:
    SwiftyGuard:
    test:
    Creator: WingedMLGPro
    Locations:
    world: world
    x1: 232.0
    y1: 61.0
    z1: 228.0
    x2: 234.0
    y2: 63.0
    z2: 226.0
    PvP: 'false'
    EnterMsg: 'null'
    LeaveMsg: 'null'
     
  17. How are you loading it? Send the code, as well as the cuboid serialize and deserialize code.

    Also, print out the size of getRegions() again and send the output number.
     
  18. Offline

    WingedMLGPro

    @KingFaris11 Sorry for the long break,
    I printed the size of the getRegions() and it printed out the write number of Regions :D
    Here is the listener to see whether player is in Region:
    Code:
    package me.WingedMLGPro.Listeners;
    
    import me.WingedMLGPro.Regions;
    import me.WingedMLGPro.Util.Cuboid;
    import org.bukkit.Location;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    /**
    * © This Document and Code is STRICTLY copyrited(©) to Ben.
    * © If anyone takes any part of this code and uses it for
    * © Something public, Share with someone, uses it for API's,
    * © implementing it to their code and taking the rights for
    * © it is NOT allowed unless you get permission from me!
    * © This project SwiftyGuard was created by 35047
    * © at 7/06/15 at 9:40 AM
    */
    public class UtilListeners implements Listener {
    
        public static List<Cuboid> getRegions() {
            List<Cuboid> regions = new ArrayList<Cuboid>();
            if (Regions.getInstance().getConfig().isConfigurationSection("SwiftyGuard")) { // Check if the "SwiftyGuard" key exists in the config and is a config section.
                for (String regionName : Regions.getInstance().getConfig().getConfigurationSection("SwiftyGuard").getKeys(false)) {
                    if (Regions.getInstance().getConfig().isConfigurationSection("SwiftyGuard." + regionName + ".Locations")) regions.add(Cuboid.deserialize(Regions.getInstance().getConfig().getConfigurationSection("SwiftyGuard." + regionName + ".Locations").getValues(false))); // Deserialize the cuboid and add it to the regions list.
                }
            }
            return regions;
        }
    
    
        public boolean inRegion(Location location) {
            for (Cuboid region : this.getRegions()) {
                if (region != null && region.containsLocation(location)) return true;
            }
            return false;
        }
    
        @EventHandler
        public void onBlock(BlockBreakEvent e){
            Player p = e.getPlayer();
            if (inRegion(p.getLocation())) {
                e.setCancelled(true);
                p.sendMessage(Regions.Error + "You cannot break blocks in this area!");
            }
        }
        @EventHandler
        public void onBlock2(BlockPlaceEvent e){
            Player p = e.getPlayer();
            if (inRegion(p.getLocation())) {
                e.setCancelled(true);
                p.sendMessage(Regions.Error + "You cannot place blocks in this area!");
            }
        }
    }
    
    The listener is registered!
     
  19. And your Cuboid class?

    Currently, the code you provided is fine, but maybe your Cuboid deserialize() and serialize() method is different to that in the config.
     
  20. Offline

    WingedMLGPro

    @KingFaris11
    Here is the Cuboid Class:
    Code:
    package me.WingedMLGPro.Util;
    
    import org.bukkit.*;
    import org.bukkit.block.Block;
    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    import org.bukkit.util.Vector;
    
    import java.util.*;
    
    public class Cuboid implements Cloneable, ConfigurationSerializable, Iterable<Block> {
    
        protected String worldName;
        protected final Vector minimumPoint, maximumPoint;
    
        public Cuboid(Cuboid cuboid) {
            this(cuboid.worldName, cuboid.minimumPoint.getX(), cuboid.minimumPoint.getY(), cuboid.minimumPoint.getZ(), cuboid.maximumPoint.getX(), cuboid.maximumPoint.getY(), cuboid.maximumPoint.getZ());
        }
    
        public Cuboid(Location loc) {
            this(loc, loc);
        }
    
        public Cuboid(Location loc1, Location loc2) {
            if (loc1 != null && loc2 != null) {
                if (loc1.getWorld() != null && loc2.getWorld() != null) {
                    if (!loc1.getWorld().getUID().equals(loc2.getWorld().getUID()))
                        throw new IllegalStateException("The 2 locations of the cuboid must be in the same world!");
                } else {
                    throw new NullPointerException("One/both of the worlds is/are null!");
                }
                this.worldName = loc1.getWorld().getName();
    
                double xPos1 = Math.min(loc1.getX(), loc2.getX());
                double yPos1 = Math.min(loc1.getY(), loc2.getY());
                double zPos1 = Math.min(loc1.getZ(), loc2.getZ());
                double xPos2 = Math.max(loc1.getX(), loc2.getX());
                double yPos2 = Math.max(loc1.getY(), loc2.getY());
                double zPos2 = Math.max(loc1.getZ(), loc2.getZ());
                this.minimumPoint = new Vector(xPos1, yPos1, zPos1);
                this.maximumPoint = new Vector(xPos2, yPos2, zPos2);
            } else {
                throw new NullPointerException("One/both of the locations is/are null!");
            }
        }
    
        public Cuboid(String worldName, double x1, double y1, double z1, double x2, double y2, double z2) {
            if (worldName == null || Bukkit.getServer().getWorld(worldName) == null)
                throw new NullPointerException("One/both of the worlds is/are null!");
            this.worldName = worldName;
    
            double xPos1 = Math.min(x1, x2);
            double xPos2 = Math.max(x1, x2);
            double yPos1 = Math.min(y1, y2);
            double yPos2 = Math.max(y1, y2);
            double zPos1 = Math.min(z1, z2);
            double zPos2 = Math.max(z1, z2);
            this.minimumPoint = new Vector(xPos1, yPos1, zPos1);
            this.maximumPoint = new Vector(xPos2, yPos2, zPos2);
        }
    
        public boolean containsLocation(Location location) {
            return location != null && location.toVector().isInAABB(this.minimumPoint, this.maximumPoint);
        }
    
        public boolean containsVector(Vector vector) {
            return vector != null && vector.isInAABB(this.minimumPoint, this.maximumPoint);
        }
    
        public List<Block> getBlocks() {
            List<Block> blockList = new ArrayList<Block>();
            World world = this.getWorld();
            if (world != null) {
                for (int x = this.minimumPoint.getBlockX(); x <= this.maximumPoint.getBlockX(); x++) {
                    for (int y = this.minimumPoint.getBlockY(); y <= this.maximumPoint.getBlockY() && y <= world.getMaxHeight(); y++) {
                        for (int z = this.minimumPoint.getBlockZ(); z <= this.maximumPoint.getBlockZ(); z++) {
                            blockList.add(world.getBlockAt(x, y, z));
                        }
                    }
                }
            }
            return blockList;
        }
    
        public Location getLowerLocation() {
            return this.minimumPoint.toLocation(this.getWorld());
        }
    
        public double getLowerX() {
            return this.minimumPoint.getX();
        }
    
        public double getLowerY() {
            return this.minimumPoint.getY();
        }
    
        public double getLowerZ() {
            return this.minimumPoint.getZ();
        }
    
        public Location getUpperLocation() {
            return this.maximumPoint.toLocation(this.getWorld());
        }
    
        public double getUpperX() {
            return this.maximumPoint.getX();
        }
    
        public double getUpperY() {
            return this.maximumPoint.getY();
        }
    
        public double getUpperZ() {
            return this.maximumPoint.getZ();
        }
    
        public double getVolume() {
            return (this.getUpperX() - this.getLowerX() + 1) * (this.getUpperY() - this.getLowerY() + 1) * (this.getUpperZ() - this.getLowerZ() + 1);
        }
    
        public World getWorld() {
            World world = Bukkit.getServer().getWorld(this.worldName);
            if (world == null) throw new NullPointerException("World '" + this.worldName + "' is not loaded.");
            return world;
        }
    
        public void setWorld(World world) {
            if (world != null) this.worldName = world.getName();
            else throw new NullPointerException("The world cannot be null.");
        }
    
        @Override
        public Cuboid clone() {
            return new Cuboid(this);
        }
    
        @Override
        public ListIterator<Block> iterator() {
            return this.getBlocks().listIterator();
        }
    
        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> serializedCuboid = new HashMap<String, Object>();
            serializedCuboid.put("worldName", this.worldName);
            serializedCuboid.put("x1", this.minimumPoint.getX());
            serializedCuboid.put("x2", this.maximumPoint.getX());
            serializedCuboid.put("y1", this.minimumPoint.getY());
            serializedCuboid.put("y2", this.maximumPoint.getY());
            serializedCuboid.put("z1", this.minimumPoint.getZ());
            serializedCuboid.put("z2", this.maximumPoint.getZ());
            return serializedCuboid;
        }
    
        public static Cuboid deserialize(Map<String, Object> serializedCuboid) {
            try {
                String worldName = (String) serializedCuboid.get("worldName");
    
                double xPos1 = (Double) serializedCuboid.get("x1");
                double xPos2 = (Double) serializedCuboid.get("x2");
                double yPos1 = (Double) serializedCuboid.get("y1");
                double yPos2 = (Double) serializedCuboid.get("y2");
                double zPos1 = (Double) serializedCuboid.get("z1");
                double zPos2 = (Double) serializedCuboid.get("z2");
    
                return new Cuboid(worldName, xPos1, yPos1, zPos1, xPos2, yPos2, zPos2);
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    
    }
    BTW: thanks for everything u have done so far :D U are a great help!

    @KingFaris11 is it because u do
    String worldName = (String) serializedCuboid.get("worldName");
    and in the config i just have world?

    @KingFaris11 Yep that was it.
    Thank you sooo much for your help :D

    @KingFaris11
    Actually, sorry to bother u again, but is there a way to get what region a player is in?
    I need it for the PvP so if the Player is in the Region which in the Config - PvP is set to false, it will cancel it.

    @KingFaris11 So basically i need a way of know what the Region name is of the Region that the PLayer is in.

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jun 24, 2015
  21. Offline

    xTrollxDudex

    Screen Shot 2015-06-23 at 10.29.36 PM.png

    Edit your post please, the button is just to the left of the report button
     
  22. Offline

    WingedMLGPro

  23. Offline

    timtower Administrator Administrator Moderator

    So I won't have to merge your posts
     
  24. Offline

    WingedMLGPro

  25. If you want a region where you can get properties such as the location and PvP, you might as well make a region class that's serializable. I'll make a small class soon.

    Example with PvP below. You can add enter messages, etc once you understand how I made this.

    To load the regions, at start up, call Region.registerRegions(config);
    'config' being the configuration for the regions.

    To save regions (for example, if a player creates a new region using a command), first make a new Region instance (new Region(params)), then save it to the config using region.serialize(). Also register the region to the region Map, using Region.registerRegion(region);

    Note: I made the Creator a UUID as it makes more sense (supports name-changing). You must use a name fetcher (by UUID) to get their username, or just change the code so it uses usernames (String).

    Code:
    import org.bukkit.Location;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.serialization.ConfigurationSerializable;
    
    import java.util.*;
    
    public class Region implements ConfigurationSerializable {
    
        private static Map<String, Region> regionMap = new HashMap<>(); // Caches all the regions.
    
        private String regionName = null;
        private UUID regionCreator = null;
        private Cuboid regionCuboid = null;
    
        private boolean pvp = false;
    
        public Region(String name, Cuboid cuboid) {
            this.regionName = name;
            this.regionCuboid = cuboid;
        }
    
        public UUID getCreator() {
            return this.regionCreator;
        }
    
        public Cuboid getCuboid() {
            return this.regionCuboid;
        }
    
        public String getName() {
            return this.regionName;
        }
    
        public boolean isPvPEnabled() {
            return this.pvp;
        }
    
        public void setCreator(UUID creatorUUID) {
            this.regionCreator = creatorUUID;
        }
    
        public void setPvP(boolean flag) {
            this.pvp = flag;
        }
    
        @Override
        public Map<String, Object> serialize() {
            Map<String, Object> serializedRegion = new HashMap<>();
            serializedRegion.put("Name", this.regionName);
            if (this.regionCreator != null) serializedRegion.put("Creator", this.regionCreator);
            if (this.regionCuboid != null) serializedRegion.put("Locations", this.regionCuboid.serialize());
            return serializedRegion;
        }
    
        public static Region deserialize(Map<String, Object> serializedRegion) {
            if (serializedRegion.get("Name") != null) {
                String name = (String) serializedRegion.get("Name");
                if (serializedRegion.containsKey("Locations")) {
                    UUID creator = serializedRegion.containsKey("Creator") && isUUID(serializedRegion.get("Creator").toString()) ? UUID.fromString(serializedRegion.get("Creator").toString()) : null;
                    Cuboid regionCuboid = Cuboid.deserialize(((ConfigurationSection) serializedRegion.get("Locations")).getValues(false));
                    if (regionCuboid != null) {
                        Region region = new Region(name, regionCuboid);
                        region.setCreator(creator);
                        if (serializedRegion.containsKey("PvP")) region.setPvP((Boolean) serializedRegion.get("PvP"));
                        return region;
                    }
                }
            }
            return null;
        }
    
        public static Region getRegion(String regionName) {
            return regionMap.get(regionName.toLowerCase());
        }
    
        public static List<Region> getRegions(Location location) {
            List<Region> regionList = new ArrayList<>();
            if (location != null) {
                for (Region region : regionMap.values()) {
                    if (region.getCuboid().containsLocation(location)) regionList.add(region);
                }
            }
            return regionList;
        }
    
        public static void registerRegion(Region region) {
           if (region != null) regionMap.put(region.getName().toLowerCase(), region);
        }
    
        public static void registerRegions(FileConfiguration fileConfig) {
            regionMap.clear();
            if (fileConfig != null && fileConfig.isConfigurationSection("SwiftyGuard")) {
                for (Map.Entry<String, Object> regionEntry : fileConfig.getConfigurationSection("SwiftyGuard").getValues(false).entrySet()) {
                    if (regionEntry.getValue() instanceof ConfigurationSection) {
                        Region region = deserialize(((ConfigurationSection) regionEntry.getValue()).getValues(false));
                        if (region != null) regionMap.put(region.getName().toLowerCase(), region);
                        else System.err.println("Failed to load the region '" + regionEntry.getKey() + "'!");
                    }
                }
            }
        }
    
        private static boolean isUUID(String aString) {
            try {
                UUID.fromString(aString);
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
    
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  26. Offline

    WingedMLGPro

    @KingFaris11 So instead of doing Config setting in the command i do the Region.setPvp(blabla);?
    Do i still need to setup the config at startup?
     
  27. Offline

    WingedMLGPro

    @KingFaris11 what would i do for the Cuboid when i make the Region instance in the Command?
    I assume by doing this also i dont need to put creator and PvP in the config file. Correct me if im wrong, i just need to set it when i create the command by making a new Region instance (Reed the Top, i need help)
    Wait no sorry, i put it in the config by using Region.serialize(), my fault

    @KingFaris11 when i make the Cuboid in the Region, i can delete all the setting in the Config and just make the Region thing .serialize()?
    and to make aR Region instance it is new Region(String name, Cuboid cuboid)
    What do i put for the cuboid?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  28. This thread is getting long, add me on Skype: <removed>
    I'll be able to help you easily, as well as share screens if required. This is very simple but I just don't like typing a lot in the night.
     
Thread Status:
Not open for further replies.

Share This Page