Add queryApplicableFlags method

This commit is contained in:
Luck 2020-05-26 22:25:04 +01:00
parent 1d1d4ab7d9
commit 15301c540f
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
10 changed files with 145 additions and 9 deletions

View File

@ -39,6 +39,15 @@ public interface IWorldGuardImplementation {
*/
<T> Optional<T> queryFlag(Player player, @NonNull Location location, @NonNull IWrappedFlag<T> flag);
/**
* Queries all applicable flags for a given player at a given location.
*
* @param player The player
* @param location The location
* @return The flags
*/
Map<IWrappedFlag<?>, Object> queryApplicableFlags(Player player, Location location);
/**
* Returns the flag with the given name.
*

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.legacy;
import com.google.common.collect.Maps;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
@ -79,6 +80,38 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return queryValue(player, location, wrappedFlag.getHandle()).flatMap(value -> wrappedFlag.fromWGValue(value));
}
@Override
public Map<IWrappedFlag<?>, Object> queryApplicableFlags(Player player, Location location) {
ApplicableRegionSet applicableSet = getApplicableRegions(location).orElse(null);
if (applicableSet == null) {
return Collections.emptyMap();
}
LocalPlayer localPlayer = wrapPlayer(player).orElse(null);
Map<IWrappedFlag<?>, Object> flags = new HashMap<>();
Set<String> seen = new HashSet<>();
for (ProtectedRegion region : applicableSet.getRegions()) {
for (Flag<?> flag : region.getFlags().keySet()) {
if (seen.add(flag.getName())) {
Object value = applicableSet.queryValue(localPlayer, flag);
if (value == null) {
continue;
}
try {
Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
flags.put(wrapped.getKey(), wrapped.getValue());
} catch (IllegalArgumentException e) {
return null; // Unsupported flag type
}
}
}
}
return flags;
}
@Override
public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) {
throw new UnsupportedOperationException("Custom flags aren't supported in this version of WorldGuard!");

View File

@ -80,9 +80,8 @@ public class WrappedRegion implements IWrappedRegion {
handle.getFlags().forEach((flag, value) -> {
if (value != null) {
try {
IWrappedFlag<?> wrappedFlag = WorldGuardFlagUtilities.wrapFixType(flag, value.getClass());
Optional<?> wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value);
wrappedValue.ifPresent(val -> result.put(wrappedFlag, val));
Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
result.put(wrapped.getKey(), wrapped.getValue());
} catch (IllegalArgumentException ignored) {/* Unsupported flag type */}
}
});

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.legacy.utility;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
@ -10,11 +11,14 @@ import org.bukkit.World;
import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import org.codemc.worldguardwrapper.implementation.legacy.flag.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.implementation.legacy.flag.WrappedPrimitiveFlag;
import org.codemc.worldguardwrapper.implementation.legacy.flag.WrappedStatusFlag;
import lombok.experimental.UtilityClass;
import java.util.Map;
@UtilityClass
public class WorldGuardFlagUtilities {
@ -60,6 +64,12 @@ public class WorldGuardFlagUtilities {
return wrap(flag, type);
}
public Map.Entry<IWrappedFlag<?>, Object> wrap(Flag<?> flag, Object value) {
IWrappedFlag<?> wrappedFlag = wrapFixType(flag, value.getClass());
Object wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value).get(); // value is non-null
return Maps.immutableEntry(wrappedFlag, wrappedValue);
}
public Vector adaptVector(com.sk89q.worldedit.Vector vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.v6;
import com.google.common.collect.Maps;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
@ -79,6 +80,38 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return queryValue(player, location, wrappedFlag.getHandle()).flatMap(value -> wrappedFlag.fromWGValue(value));
}
@Override
public Map<IWrappedFlag<?>, Object> queryApplicableFlags(Player player, Location location) {
ApplicableRegionSet applicableSet = getApplicableRegions(location).orElse(null);
if (applicableSet == null) {
return Collections.emptyMap();
}
LocalPlayer localPlayer = wrapPlayer(player).orElse(null);
Map<IWrappedFlag<?>, Object> flags = new HashMap<>();
Set<String> seen = new HashSet<>();
for (ProtectedRegion region : applicableSet.getRegions()) {
for (Flag<?> flag : region.getFlags().keySet()) {
if (seen.add(flag.getName())) {
Object value = applicableSet.queryValue(localPlayer, flag);
if (value == null) {
continue;
}
try {
Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
flags.put(wrapped.getKey(), wrapped.getValue());
} catch (IllegalArgumentException e) {
return null; // Unsupported flag type
}
}
}
}
return flags;
}
@SuppressWarnings("unchecked")
@Override
public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) {

View File

@ -80,9 +80,8 @@ public class WrappedRegion implements IWrappedRegion {
handle.getFlags().forEach((flag, value) -> {
if (value != null) {
try {
IWrappedFlag<?> wrappedFlag = WorldGuardFlagUtilities.wrapFixType(flag, value.getClass());
Optional<?> wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value);
wrappedValue.ifPresent(val -> result.put(wrappedFlag, val));
Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
result.put(wrapped.getKey(), wrapped.getValue());
} catch (IllegalArgumentException ignored) {/* Unsupported flag type */}
}
});

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.v6.utility;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
@ -9,11 +10,14 @@ import org.bukkit.World;
import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import org.codemc.worldguardwrapper.implementation.v6.flag.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedPrimitiveFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedStatusFlag;
import lombok.experimental.UtilityClass;
import java.util.Map;
@UtilityClass
public class WorldGuardFlagUtilities {
@ -59,6 +63,12 @@ public class WorldGuardFlagUtilities {
return wrap(flag, type);
}
public Map.Entry<IWrappedFlag<?>, Object> wrap(Flag<?> flag, Object value) {
IWrappedFlag<?> wrappedFlag = wrapFixType(flag, value.getClass());
Object wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value).get(); // value is non-null
return Maps.immutableEntry(wrappedFlag, wrappedValue);
}
public Vector adaptVector(com.sk89q.worldedit.Vector vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.v7;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.math.BlockVector2;
@ -83,6 +84,38 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return queryValue(player, location, wrappedFlag.getHandle()).flatMap(value -> wrappedFlag.fromWGValue(value));
}
@Override
public Map<IWrappedFlag<?>, Object> queryApplicableFlags(Player player, Location location) {
ApplicableRegionSet applicableSet = getApplicableRegions(location).orElse(null);
if (applicableSet == null) {
return Collections.emptyMap();
}
LocalPlayer localPlayer = wrapPlayer(player).orElse(null);
Map<IWrappedFlag<?>, Object> flags = new HashMap<>();
Set<String> seen = new HashSet<>();
for (ProtectedRegion region : applicableSet.getRegions()) {
for (Flag<?> flag : region.getFlags().keySet()) {
if (seen.add(flag.getName())) {
Object value = applicableSet.queryValue(localPlayer, flag);
if (value == null) {
continue;
}
try {
Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
flags.put(wrapped.getKey(), wrapped.getValue());
} catch (IllegalArgumentException e) {
return null; // Unsupported flag type
}
}
}
}
return flags;
}
@SuppressWarnings("unchecked")
@Override
public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) {

View File

@ -80,9 +80,8 @@ public class WrappedRegion implements IWrappedRegion {
handle.getFlags().forEach((flag, value) -> {
if (value != null) {
try {
IWrappedFlag<?> wrappedFlag = WorldGuardFlagUtilities.wrapFixType(flag, value.getClass());
Optional<?> wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value);
wrappedValue.ifPresent(val -> result.put(wrappedFlag, val));
Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
result.put(wrapped.getKey(), wrapped.getValue());
} catch (IllegalArgumentException ignored) {/* Unsupported flag type */}
}
});

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.v7.utility;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
@ -8,11 +9,15 @@ import org.bukkit.Location;
import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import org.codemc.worldguardwrapper.implementation.v7.flag.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.implementation.v7.flag.WrappedPrimitiveFlag;
import org.codemc.worldguardwrapper.implementation.v7.flag.WrappedStatusFlag;
import lombok.experimental.UtilityClass;
import java.util.Map;
import java.util.Optional;
@UtilityClass
public class WorldGuardFlagUtilities {
@ -58,6 +63,12 @@ public class WorldGuardFlagUtilities {
return wrap(flag, type);
}
public Map.Entry<IWrappedFlag<?>, Object> wrap(Flag<?> flag, Object value) {
IWrappedFlag<?> wrappedFlag = wrapFixType(flag, value.getClass());
Object wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value).get(); // value is non-null
return Maps.immutableEntry(wrappedFlag, wrappedValue);
}
public Vector adaptVector(Vector3 vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}