1.15.2 WorldGuard API Custom Regions

Discussion in 'Spigot Plugin Development' started by LinuxSquare, Feb 12, 2020.

  1. Hey folks of the Spigot-Community

    Right now, I'm trying to create a plugin using the WorldGuard 7 and WorldEdit 7 API.
    In this plugin, I'm trying to create a cityregion plugin with towns, districts and home-regions.

    In order to work properly, I thought, I could create a custom class, that's extending the `ProtectedCuboidRegion.class` with an additional attribute called "areatype", which I will set with the constructor and get the value using a get-method.

    Code (Java):
    import com.sk89q.worldedit.math.BlockVector3;
    import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;

    public class CityRegions extends ProtectedCuboidRegion {

        private String areatype;

        public CityRegions(String areatype, String id, BlockVector3 pt1, BlockVector3 pt2) {
            super(id, pt1, pt2);
            this.areatype = areatype;
        }

        public String getAreaType() {
            return this.areatype;
        }
       
    }

    But when I tried to call all regions out of the `ApplicableRegionSet` within a foreach loop using my `CityRegions.java` class,
    Code (Java):
    ApplicableRegionSet ars = regionmanager.getApplicableRegions(pl.getLocation().toVector().toBlockPoint());

    for (CityRegions region : ars ) {
       ...
    }
    my IDE told me,
    Code (Text):
    Type mismatch: cannot convert from element type ProtectedRegion to ProtectedCuboidRegion
    After this, I tried to extend the `ProtectedRegion` Class and noticed, that it wasn't possible, because the class was abstract.

    The last thing I tried was, to cast the `ars` variable as a `ProtectedCuboidRegion`. Well, it didn't work either, that's why I'm asking you. My IDE told me once again:
    Code (Text):
    Can only iterate over an array or an instance of java.lang.Iterable
    Shouldn't be the `ProtectedCuboidRegion` iterable aswell, because it's extending `ProtectedRegion`?

    Now my question is, how do I create a custom region, which I can use within a foreach loop with the `ApplicableRegionSet` to get the type of the my region, or is it possible to convert the `ProtectedRegion` inside the `ApplicableRegionSet` to `ProtectedCuboidRegions`?

    I need those, to create an if (or switch) statement, so I can print the correct message to the player like
    Code (Text):

    Region Info
    Town: Town-Name
    List of Owners
    List of Members
     
    I hope someone can help me with my problem. :)

    Greets LinuxSquare