Support flag query by flag name

This commit is contained in:
Eric 2018-08-03 13:06:52 +02:00
parent 7e0ba6d8e7
commit ad9ff41b7b
3 changed files with 22 additions and 9 deletions

View File

@ -43,6 +43,17 @@ public interface IWorldGuardImplementation {
*/ */
boolean registerStateFlag(@NonNull String flagName, @NonNull Boolean defaultValue); boolean registerStateFlag(@NonNull String flagName, @NonNull Boolean defaultValue);
/**
* Query a flag's value for a given player at a given location.
*
* @param player The player
* @param location The location
* @param flagName The name of the flag
* @param type The type of the flag's value
* @return The flag's value
*/
<T> Optional<T> queryFlag(Player player, @NonNull Location location, @NonNull String flagName, Class<T> type);
/** /**
* Query a flag's value for a given player at a given location. * Query a flag's value for a given player at a given location.
* *
@ -51,7 +62,9 @@ public interface IWorldGuardImplementation {
* @param flag The flag * @param flag The flag
* @return The flag's value * @return The flag's value
*/ */
<T> Optional<T> queryFlag(Player player, @NonNull Location location, @NonNull AbstractFlag<T> flag); default <T> Optional<T> queryFlag(Player player, @NonNull Location location, @NonNull AbstractFlag<T> flag) {
return queryFlag(player, location, flag.getName(), flag.getType());
}
/** /**
* Register a flag to WorldGuard's flag registry. * Register a flag to WorldGuard's flag registry.

View File

@ -83,10 +83,10 @@ public class WorldGuardImplementation extends AbstractWorldGuardImplementation {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Optional<T> queryFlag(Player player, Location location, AbstractFlag<T> flag) { public <T> Optional<T> queryFlag(Player player, Location location, String flagName, Class<T> type) {
Flag<?> wgFlag = flagRegistry.get(flag.getName()); Flag<?> flag = flagRegistry.get(flagName);
Object value = queryValue(player, location, wgFlag).orElse(null); Object value = queryValue(player, location, flag).orElse(null);
if (flag.getType().isInstance(value)) { if (type.isInstance(value)) {
return Optional.of((T) value); return Optional.of((T) value);
} }
return Optional.empty(); return Optional.empty();

View File

@ -96,10 +96,10 @@ public class WorldGuardImplementation extends AbstractWorldGuardImplementation {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> Optional<T> queryFlag(Player player, Location location, AbstractFlag<T> flag) { public <T> Optional<T> queryFlag(Player player, Location location, String flagName, Class<T> type) {
Flag<?> wgFlag = flagRegistry.get(flag.getName()); Flag<?> flag = flagRegistry.get(flagName);
Object value = queryValue(player, location, wgFlag).orElse(null); Object value = queryValue(player, location, flag).orElse(null);
if (flag.getType().isInstance(value)) { if (type.isInstance(value)) {
return Optional.of((T) value); return Optional.of((T) value);
} }
return Optional.empty(); return Optional.empty();