Add basic region getters

Only supports names and flags for now.
This commit is contained in:
Eric 2018-08-03 17:26:23 +02:00
parent ad9ff41b7b
commit ae7fcbb8aa
4 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package org.codemc.worldguardwrapper.implementation;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public abstract class AbstractRegion {
private String id;
public abstract Object getFlag(String name);
public abstract Map<String, Object> getFlags();
}

View File

@ -1,8 +1,11 @@
package org.codemc.worldguardwrapper.implementation;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
@ -73,4 +76,33 @@ public interface IWorldGuardImplementation {
* @return Whether the flag has been registered
*/
<T> boolean registerFlag(@NonNull AbstractFlag<T> flag);
/**
* Get a region by its ID.
*
* @param world The world
* @param id ID of the region
* @return The region
*/
Optional<AbstractRegion> getRegion(World world, String id);
/**
* Get an unmodifiable map of regions containing the state of the
* index at the time of call.
*
* <p>This call is relatively heavy (and may block other threads),
* so refrain from calling it frequently.</p>
*
* @param world The world
* @return A map of regions
*/
Map<String, AbstractRegion> getRegions(World world);
/**
* Get a set of regions at the given location.
*
* @param location The location
* @return A set of regions
*/
Set<AbstractRegion> getRegions(Location location);
}

View File

@ -1,6 +1,10 @@
package org.codemc.worldguardwrapper.implementation.v6;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
@ -12,12 +16,14 @@ import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.codemc.worldguardwrapper.implementation.AbstractFlag;
import org.codemc.worldguardwrapper.implementation.AbstractRegion;
import org.codemc.worldguardwrapper.implementation.AbstractWorldGuardImplementation;
import lombok.NonNull;
@ -52,6 +58,22 @@ public class WorldGuardImplementation extends AbstractWorldGuardImplementation {
return getApplicableRegions(location).map(applicableRegions -> applicableRegions.queryState(wrapPlayer(player).orElse(null), stateFlags));
}
private AbstractRegion toRegion(ProtectedRegion region) {
return new AbstractRegion(region.getId()) {
@Override
public Map<String, Object> getFlags() {
Map<String, Object> map = new HashMap<>();
region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value));
return map;
}
@Override
public Object getFlag(String name) {
return region.getFlag(flagRegistry.get(name));
}
};
}
@Override
public JavaPlugin getWorldGuardPlugin() {
return WorldGuardPlugin.inst();
@ -123,4 +145,33 @@ public class WorldGuardImplementation extends AbstractWorldGuardImplementation {
}
return false;
}
@Override
public Optional<AbstractRegion> getRegion(World world, String id) {
return getWorldManager(world).map(regionManager -> toRegion(regionManager.getRegion(id)));
}
@Override
public Map<String, AbstractRegion> getRegions(World world) {
RegionManager regionManager = plugin.getRegionManager(world);
Map<String, ProtectedRegion> regions = regionManager.getRegions();
Map<String, AbstractRegion> map = new HashMap<>();
regions.forEach((name, region) -> map.put(name, toRegion(region)));
return map;
}
@Override
public Set<AbstractRegion> getRegions(Location location) {
ApplicableRegionSet regionSet = getApplicableRegions(location).orElse(null);
Set<AbstractRegion> set = new HashSet<>();
if (regionSet == null) {
return set;
}
regionSet.forEach(region -> set.add(toRegion(region)));
return set;
}
}

View File

@ -1,8 +1,13 @@
package org.codemc.worldguardwrapper.implementation.v7;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
@ -15,6 +20,7 @@ import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import org.bukkit.Bukkit;
@ -23,6 +29,7 @@ import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.codemc.worldguardwrapper.implementation.AbstractFlag;
import org.codemc.worldguardwrapper.implementation.AbstractRegion;
import org.codemc.worldguardwrapper.implementation.AbstractWorldGuardImplementation;
import lombok.NonNull;
@ -65,6 +72,22 @@ public class WorldGuardImplementation extends AbstractWorldGuardImplementation {
return getApplicableRegions(location).map(applicableRegions -> applicableRegions.queryState(wrapPlayer(player).orElse(null), stateFlags));
}
private AbstractRegion toRegion(ProtectedRegion region) {
return new AbstractRegion(region.getId()) {
@Override
public Map<String, Object> getFlags() {
Map<String, Object> map = new HashMap<>();
region.getFlags().forEach((flag, value) -> map.put(flag.getName(), value));
return map;
}
@Override
public Object getFlag(String name) {
return region.getFlag(flagRegistry.get(name));
}
};
}
@Override
public JavaPlugin getWorldGuardPlugin() {
return WorldGuardPlugin.inst();
@ -136,4 +159,33 @@ public class WorldGuardImplementation extends AbstractWorldGuardImplementation {
}
return false;
}
@Override
public Optional<AbstractRegion> getRegion(World world, String id) {
return getWorldManager(world).map(regionManager -> toRegion(regionManager.getRegion(id)));
}
@Override
public Map<String, AbstractRegion> getRegions(World world) {
RegionManager regionManager = container.get(new BukkitWorld(world));
Map<String, ProtectedRegion> regions = regionManager.getRegions();
Map<String, AbstractRegion> map = new HashMap<>();
regions.forEach((name, region) -> map.put(name, toRegion(region)));
return map;
}
@Override
public Set<AbstractRegion> getRegions(Location location) {
ApplicableRegionSet regionSet = getApplicableRegions(location).orElse(null);
Set<AbstractRegion> set = new HashSet<>();
if (regionSet == null) {
return set;
}
regionSet.forEach(region -> set.add(toRegion(region)));
return set;
}
}