Merge branch 'pr/33'

This commit is contained in:
Gabriele C 2020-06-07 02:53:01 +02:00
commit 354e352ef8
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); <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. * Returns the flag with the given name.
* *

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.legacy; package org.codemc.worldguardwrapper.implementation.legacy;
import com.google.common.collect.Maps;
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet; 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)); 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 @Override
public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) { 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!"); 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) -> { handle.getFlags().forEach((flag, value) -> {
if (value != null) { if (value != null) {
try { try {
IWrappedFlag<?> wrappedFlag = WorldGuardFlagUtilities.wrapFixType(flag, value.getClass()); Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
Optional<?> wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value); result.put(wrapped.getKey(), wrapped.getValue());
wrappedValue.ifPresent(val -> result.put(wrappedFlag, val));
} catch (IllegalArgumentException ignored) {/* Unsupported flag type */} } catch (IllegalArgumentException ignored) {/* Unsupported flag type */}
} }
}); });

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.legacy.utility; package org.codemc.worldguardwrapper.implementation.legacy.utility;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.flags.StateFlag;
@ -10,11 +11,14 @@ import org.bukkit.World;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag; import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState; 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.WrappedPrimitiveFlag;
import org.codemc.worldguardwrapper.implementation.legacy.flag.WrappedStatusFlag; import org.codemc.worldguardwrapper.implementation.legacy.flag.WrappedStatusFlag;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import java.util.Map;
@UtilityClass @UtilityClass
public class WorldGuardFlagUtilities { public class WorldGuardFlagUtilities {
@ -60,6 +64,12 @@ public class WorldGuardFlagUtilities {
return wrap(flag, type); 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) { public Vector adaptVector(com.sk89q.worldedit.Vector vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ()); return new Vector(vector.getX(), vector.getY(), vector.getZ());
} }

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.v6; package org.codemc.worldguardwrapper.implementation.v6;
import com.google.common.collect.Maps;
import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet; 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)); 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") @SuppressWarnings("unchecked")
@Override @Override
public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) { 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) -> { handle.getFlags().forEach((flag, value) -> {
if (value != null) { if (value != null) {
try { try {
IWrappedFlag<?> wrappedFlag = WorldGuardFlagUtilities.wrapFixType(flag, value.getClass()); Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
Optional<?> wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value); result.put(wrapped.getKey(), wrapped.getValue());
wrappedValue.ifPresent(val -> result.put(wrappedFlag, val));
} catch (IllegalArgumentException ignored) {/* Unsupported flag type */} } catch (IllegalArgumentException ignored) {/* Unsupported flag type */}
} }
}); });

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.v6.utility; package org.codemc.worldguardwrapper.implementation.v6.utility;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.flags.StateFlag;
@ -9,11 +10,14 @@ import org.bukkit.World;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag; import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState; 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.WrappedPrimitiveFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedStatusFlag; import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedStatusFlag;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import java.util.Map;
@UtilityClass @UtilityClass
public class WorldGuardFlagUtilities { public class WorldGuardFlagUtilities {
@ -59,6 +63,12 @@ public class WorldGuardFlagUtilities {
return wrap(flag, type); 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) { public Vector adaptVector(com.sk89q.worldedit.Vector vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ()); return new Vector(vector.getX(), vector.getY(), vector.getZ());
} }

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.v7; package org.codemc.worldguardwrapper.implementation.v7;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.math.BlockVector2; 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)); 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") @SuppressWarnings("unchecked")
@Override @Override
public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) { 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) -> { handle.getFlags().forEach((flag, value) -> {
if (value != null) { if (value != null) {
try { try {
IWrappedFlag<?> wrappedFlag = WorldGuardFlagUtilities.wrapFixType(flag, value.getClass()); Map.Entry<IWrappedFlag<?>, Object> wrapped = WorldGuardFlagUtilities.wrap(flag, value);
Optional<?> wrappedValue = ((AbstractWrappedFlag<?>) wrappedFlag).fromWGValue(value); result.put(wrapped.getKey(), wrapped.getValue());
wrappedValue.ifPresent(val -> result.put(wrappedFlag, val));
} catch (IllegalArgumentException ignored) {/* Unsupported flag type */} } catch (IllegalArgumentException ignored) {/* Unsupported flag type */}
} }
}); });

View File

@ -1,5 +1,6 @@
package org.codemc.worldguardwrapper.implementation.v7.utility; package org.codemc.worldguardwrapper.implementation.v7.utility;
import com.google.common.collect.Maps;
import com.sk89q.worldedit.math.Vector3; import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.flags.StateFlag;
@ -8,11 +9,15 @@ import org.bukkit.Location;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag; import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState; 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.WrappedPrimitiveFlag;
import org.codemc.worldguardwrapper.implementation.v7.flag.WrappedStatusFlag; import org.codemc.worldguardwrapper.implementation.v7.flag.WrappedStatusFlag;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import java.util.Map;
import java.util.Optional;
@UtilityClass @UtilityClass
public class WorldGuardFlagUtilities { public class WorldGuardFlagUtilities {
@ -58,6 +63,12 @@ public class WorldGuardFlagUtilities {
return wrap(flag, type); 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) { public Vector adaptVector(Vector3 vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ()); return new Vector(vector.getX(), vector.getY(), vector.getZ());
} }