Enhance the region area/selection api, add region owner/members getters

This commit is contained in:
Gabriele C 2018-11-22 15:18:33 +01:00
parent 946809ebad
commit 19ef34bd0f
7 changed files with 171 additions and 170 deletions

View File

@ -1,11 +0,0 @@
package org.codemc.worldguardwrapper.region;
import org.bukkit.Location;
import java.util.Set;
public interface WrappedPolygonalRegion extends WrappedRegion {
Set<Location> getPoints();
}

View File

@ -1,16 +1,16 @@
package org.codemc.worldguardwrapper.region; package org.codemc.worldguardwrapper.region;
import org.bukkit.Location; import org.bukkit.Location;
import org.codemc.worldguardwrapper.selection.Selection;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID;
public interface WrappedRegion { public interface WrappedRegion {
Location getMinimumPoint(); Selection getSelection();
Location getMaximumPoint();
String getId(); String getId();
@ -20,6 +20,10 @@ public interface WrappedRegion {
int getPriority(); int getPriority();
Set<UUID> getOwners();
Set<UUID> getMembers();
boolean contains(Location location); boolean contains(Location location);
} }

View File

@ -0,0 +1,11 @@
package org.codemc.worldguardwrapper.selection;
import org.bukkit.Location;
public interface CuboidSelection extends Selection {
Location getMinimumPoint();
Location getMaximumPoint();
}

View File

@ -0,0 +1,15 @@
package org.codemc.worldguardwrapper.selection;
import org.bukkit.Location;
import java.util.Set;
public interface PolygonalSelection extends Selection {
Set<Location> getPoints();
int getMinimumY();
int getMaximumY();
}

View File

@ -0,0 +1,4 @@
package org.codemc.worldguardwrapper.selection;
public interface Selection {
}

View File

@ -23,7 +23,9 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag; import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation; import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation;
import org.codemc.worldguardwrapper.region.WrappedPolygonalRegion; import org.codemc.worldguardwrapper.selection.CuboidSelection;
import org.codemc.worldguardwrapper.selection.PolygonalSelection;
import org.codemc.worldguardwrapper.selection.Selection;
import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.region.WrappedRegion;
import java.util.*; import java.util.*;
@ -75,97 +77,84 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
private WrappedRegion toRegion(World world, ProtectedRegion region) { private WrappedRegion toRegion(World world, ProtectedRegion region) {
if (region instanceof ProtectedPolygonalRegion) { return new WrappedRegion() {
return new WrappedPolygonalRegion() {
@Override @Override
public Location getMinimumPoint() { public Selection getSelection() {
return fromBlockVector(world, region.getMinimumPoint()); if (region instanceof PolygonalSelection) {
} return new PolygonalSelection() {
@Override @Override
public Location getMaximumPoint() { public Set<Location> getPoints() {
return fromBlockVector(world, region.getMaximumPoint()); return region.getPoints().stream()
} .map(vector -> new BlockVector(vector.toVector()))
.map(vector -> fromBlockVector(world, vector))
.collect(Collectors.toSet());
}
@Override @Override
public Set<Location> getPoints() { public int getMinimumY() {
return region.getPoints().stream() return ((PolygonalSelection) region).getMinimumY();
.map(vector -> new BlockVector(vector.toVector())) }
.map(vector -> fromBlockVector(world, vector))
.collect(Collectors.toSet());
}
@Override @Override
public String getId() { public int getMaximumY() {
return region.getId(); return ((PolygonalSelection) region).getMaximumY();
}
};
} }
return new CuboidSelection() {
@Override @Override
public Map<String, Object> getFlags() { public Location getMinimumPoint() {
Map<String, Object> map = new HashMap<>(); return fromBlockVector(world, region.getMinimumPoint());
region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value)); }
return map;
}
@Override @Override
public Optional<Object> getFlag(String name) { public Location getMaximumPoint() {
return Optional.ofNullable(flagRegistry.get(name)) return fromBlockVector(world, region.getMaximumPoint());
.map(region::getFlag); }
} };
}
@Override @Override
public int getPriority() { public String getId() {
return region.getPriority(); return region.getId();
} }
@Override @Override
public boolean contains(Location location) { public Map<String, Object> getFlags() {
return region.contains(toBlockVector(location)); Map<String, Object> map = new HashMap<>();
} region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value));
}; return map;
} else { }
return new WrappedRegion() {
@Override @Override
public Location getMinimumPoint() { public Optional<Object> getFlag(String name) {
return fromBlockVector(world, region.getMinimumPoint()); return Optional.ofNullable(flagRegistry.get(name))
} .map(region::getFlag);
}
@Override @Override
public Location getMaximumPoint() { public int getPriority() {
return fromBlockVector(world, region.getMaximumPoint()); return region.getPriority();
} }
@Override @Override
public String getId() { public Set<UUID> getOwners() {
return region.getId(); return region.getOwners().getUniqueIds();
} }
@Override @Override
public Map<String, Object> getFlags() { public Set<UUID> getMembers() {
Map<String, Object> map = new HashMap<>(); return region.getMembers().getUniqueIds();
region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value)); }
return map;
}
@Override @Override
public Optional<Object> getFlag(String name) { public boolean contains(Location location) {
return Optional.ofNullable(flagRegistry.get(name)) return region.contains(toBlockVector(location));
.map(region::getFlag); }
} };
@Override
public int getPriority() {
return region.getPriority();
}
@Override
public boolean contains(Location location) {
return region.contains(toBlockVector(location));
}
};
}
} }
@Override @Override

View File

@ -25,8 +25,10 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag; import org.codemc.worldguardwrapper.flags.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation; import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation;
import org.codemc.worldguardwrapper.region.WrappedPolygonalRegion;
import org.codemc.worldguardwrapper.region.WrappedRegion; import org.codemc.worldguardwrapper.region.WrappedRegion;
import org.codemc.worldguardwrapper.selection.CuboidSelection;
import org.codemc.worldguardwrapper.selection.PolygonalSelection;
import org.codemc.worldguardwrapper.selection.Selection;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -75,97 +77,84 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
} }
private WrappedRegion toRegion(World world, ProtectedRegion region) { private WrappedRegion toRegion(World world, ProtectedRegion region) {
if (region instanceof ProtectedPolygonalRegion) { return new WrappedRegion() {
return new WrappedPolygonalRegion() {
@Override @Override
public Location getMinimumPoint() { public Selection getSelection() {
return BukkitAdapter.adapt(world, region.getMinimumPoint()); if (region instanceof PolygonalSelection) {
} return new PolygonalSelection() {
@Override @Override
public Location getMaximumPoint() { public Set<Location> getPoints() {
return BukkitAdapter.adapt(world, region.getMaximumPoint()); return region.getPoints().stream()
} .map(BlockVector2::toBlockVector3)
.map(vector -> BukkitAdapter.adapt(world, vector))
.collect(Collectors.toSet());
}
@Override @Override
public Set<Location> getPoints() { public int getMinimumY() {
return region.getPoints().stream() return ((PolygonalSelection) region).getMinimumY();
.map(BlockVector2::toBlockVector3) }
.map(vector -> BukkitAdapter.adapt(world, vector))
.collect(Collectors.toSet());
}
@Override @Override
public String getId() { public int getMaximumY() {
return region.getId(); return ((PolygonalSelection) region).getMaximumY();
}
};
} }
return new CuboidSelection() {
@Override @Override
public Map<String, Object> getFlags() { public Location getMinimumPoint() {
Map<String, Object> map = new HashMap<>(); return BukkitAdapter.adapt(world, region.getMinimumPoint());
region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value)); }
return map;
}
@Override @Override
public Optional<Object> getFlag(String name) { public Location getMaximumPoint() {
return Optional.ofNullable(flagRegistry.get(name)) return BukkitAdapter.adapt(world, region.getMaximumPoint());
.map(region::getFlag); }
} };
}
@Override @Override
public int getPriority() { public String getId() {
return region.getPriority(); return region.getId();
} }
@Override @Override
public boolean contains(Location location) { public Map<String, Object> getFlags() {
return region.contains(BukkitAdapter.asBlockVector(location)); Map<String, Object> map = new HashMap<>();
} region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value));
}; return map;
} else { }
return new WrappedRegion() {
@Override @Override
public Location getMinimumPoint() { public Optional<Object> getFlag(String name) {
return BukkitAdapter.adapt(world, region.getMinimumPoint()); return Optional.ofNullable(flagRegistry.get(name))
} .map(region::getFlag);
}
@Override @Override
public Location getMaximumPoint() { public int getPriority() {
return BukkitAdapter.adapt(world, region.getMaximumPoint()); return region.getPriority();
} }
@Override @Override
public String getId() { public Set<UUID> getOwners() {
return region.getId(); return region.getOwners().getUniqueIds();
} }
@Override @Override
public Map<String, Object> getFlags() { public Set<UUID> getMembers() {
Map<String, Object> map = new HashMap<>(); return region.getMembers().getUniqueIds();
region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value)); }
return map;
}
@Override @Override
public Optional<Object> getFlag(String name) { public boolean contains(Location location) {
return Optional.ofNullable(flagRegistry.get(name)) return region.contains(BukkitAdapter.asBlockVector(location));
.map(region::getFlag); }
} };
@Override
public int getPriority() {
return region.getPriority();
}
@Override
public boolean contains(Location location) {
return region.contains(BukkitAdapter.asBlockVector(location));
}
};
}
} }
@Override @Override