Solved How to get the center of a world guard region?

Discussion in 'Spigot Plugin Development' started by TheSupreme, Apr 19, 2017.

  1. How can I get the location in the center of a world guard region using the world guard API?
    Thanks in advance !
     
  2. Have you at least try googling it.
     
  3. Yes, I have.
     
  4. There is no method to get the location from a region as far as I can tell.
     
  5. Find the difference between the minimum and maximum points.
     
  6. Code (Text):
    public void Region()
        {
            World w = Bukkit.getWorld(getWorld());
            world = w;

            com.sk89q.worldguard.protection.managers.RegionManager manager = Plugins.getWorldGuard().getRegionManager(w);;
            // the region you want to look for the player in. "region" is the id/name of the region you are looking for. would be the one used to create it
            //ProtectedRegion region = manager.getRegion(Id);
            region = manager.getRegion(Id);
            Location top = new Location(w, 0, 0, 0);
            top.setX(region.getMaximumPoint().getX());
            top.setY(region.getMaximumPoint().getY());
            top.setZ(region.getMaximumPoint().getZ());
         
            Location bottom = new Location(w, 0, 0, 0);
            bottom.setX(region.getMaximumPoint().getX());
            bottom.setY(region.getMaximumPoint().getY());
            bottom.setZ(region.getMaximumPoint().getZ());
         
            location.setWorld(w);
            location.setX(top.getX() / bottom.getX());
            location.setY(top.getY() / bottom.getY());
            location.setZ(top.getZ() / bottom.getZ());
        }
    Tried this, doesn't quite work.
     
  7. You are dividing. You have to find the difference, which is subtraction.
     
  8. Code (Text):

        public void Region()
        {
            World w = Bukkit.getWorld(getWorld());
            world = w;

            com.sk89q.worldguard.protection.managers.RegionManager manager = Plugins.getWorldGuard().getRegionManager(w);
            // the region you want to look for the player in. "region" is the id/name of the region you are looking for. would be the one used to create it
            //ProtectedRegion region = manager.getRegion(Id);
            region = manager.getRegion(Id);
            Location top = new Location(w, 0, 0, 0);
            top.setX(region.getMaximumPoint().getX());
            top.setY(region.getMaximumPoint().getY());
            top.setZ(region.getMaximumPoint().getZ());
           
            Location bottom = new Location(w, 0, 0, 0);
            bottom.setX(region.getMaximumPoint().getX());
            bottom.setY(region.getMaximumPoint().getY());
            bottom.setZ(region.getMaximumPoint().getZ());
           
            location.setX(top.getX() - bottom.getX());
            location.setY(top.getY() - bottom.getY());
            location.setZ(top.getZ() - bottom.getZ());
            System.out.println(location.toString());
            spawnEntity();
        }
       
        public void spawnEntity()
        {
            System.out.println("here");
            armorstand = (ArmorStand) world.spawnEntity(location, EntityType.ARMOR_STAND);
            armorstand.teleport(location);
            armorstand.setVisible(true);
            armorstand.setHealth(getHealth());
            armorstand.setGravity(false);
            armorstand.setCustomName("§e§l" + getName() + " §cHealth: " + getHealth() + "/" + getMaxHealth());
            armorstand.setCustomNameVisible(true);
        }
     
    " System.out.println(location.toString());" never shows in console and spawnEntity is never called.
     
  9. Figured it out.

    New code:
    Code (Text):
    public void Region()
        {
            //Get world
            World w = Bukkit.getWorld(getWorld());
            world = w;
           
            //Region manager
            com.sk89q.worldguard.protection.managers.RegionManager manager = Plugins.getWorldGuard().getRegionManager(w);

            //Region ID
            region = manager.getRegion(Id);
           
            //Get top location
            Location top = new Location(w, 0, 0, 0);
            top.setX(region.getMaximumPoint().getX());
            top.setY(region.getMaximumPoint().getY());
            top.setZ(region.getMaximumPoint().getZ());
           
            //Get bottom location
            Location bottom = new Location(w, 0, 0, 0);
            bottom.setX(region.getMinimumPoint().getX());
            bottom.setY(region.getMinimumPoint().getY());
            bottom.setZ(region.getMinimumPoint().getZ());
           
            //Split difference
            double X =  ((bottom.getX() - top.getX())/2) + bottom.getX();
            double Y =  ((bottom.getY() - top.getY())/2) + bottom.getY();
            double Z =  ((bottom.getZ() - top.getZ())/2) + bottom.getZ();
           
            //Setup new location
            location = new Location(w, X, Y, Z);

        }
     
    • Like Like x 1
    • Agree Agree x 1