Fix wrapping of non-primitive flags

This commit is contained in:
Eric 2019-05-31 15:50:47 +02:00
parent 7edf3711a7
commit e8f873cb9a
13 changed files with 222 additions and 20 deletions

View File

@ -2,6 +2,10 @@ package org.codemc.worldguardwrapper.implementation.legacy.flag;
import com.sk89q.worldguard.protection.flags.Flag;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.implementation.legacy.utility.WorldGuardFlagUtilities;
import java.util.Optional;
public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
@ -10,13 +14,24 @@ public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
super(handle);
}
@SuppressWarnings("unchecked")
@Override
public Optional<T> fromWGValue(Object value) {
if (value instanceof com.sk89q.worldedit.Location) {
return Optional.of((T) WorldGuardFlagUtilities.adaptLocation((com.sk89q.worldedit.Location) value));
} else if (value instanceof com.sk89q.worldedit.Vector) {
return Optional.of((T) WorldGuardFlagUtilities.adaptVector((com.sk89q.worldedit.Vector) value));
}
return Optional.ofNullable((T) value);
}
@Override
public Optional<Object> fromWrapperValue(T value) {
if (value instanceof Location) {
return Optional.of(WorldGuardFlagUtilities.adaptLocation((Location) value));
} else if (value instanceof Vector) {
return Optional.of(WorldGuardFlagUtilities.adaptVector((Vector) value));
}
return Optional.ofNullable(value);
}

View File

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

View File

@ -1,11 +1,13 @@
package org.codemc.worldguardwrapper.implementation.legacy.utility;
import java.util.Vector;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import org.bukkit.Bukkit;
import org.bukkit.Location;
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.WrappedPrimitiveFlag;
@ -42,4 +44,42 @@ public class WorldGuardFlagUtilities {
return wrappedFlag;
}
// Used when the flag's type is not known, so it has to be derived from a sample value's class
public IWrappedFlag<?> wrapFixType(Flag<?> flag, Class<?> type) {
if (StateFlag.State.class.isAssignableFrom(type)) {
// StateFlag
type = WrappedState.class;
} else if (com.sk89q.worldedit.Location.class.isAssignableFrom(type)) {
// LocationFlag
type = org.bukkit.Location.class;
} else if (com.sk89q.worldedit.Vector.class.isAssignableFrom(type)) {
// VectorFlag
type = Vector.class;
}
return wrap(flag, type);
}
public Vector adaptVector(com.sk89q.worldedit.Vector vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}
public com.sk89q.worldedit.Vector adaptVector(Vector vector) {
return new com.sk89q.worldedit.Vector(vector.getX(), vector.getY(), vector.getZ());
}
public Location adaptLocation(com.sk89q.worldedit.Location location) {
com.sk89q.worldedit.Vector pos = location.getPosition();
World world = location.getWorld() instanceof BukkitWorld
? ((BukkitWorld) location.getWorld()).getWorld()
: Bukkit.getWorld(location.getWorld().getName());
return new Location(world, pos.getX(), pos.getY(), pos.getZ());
}
public com.sk89q.worldedit.Location adaptLocation(Location location) {
return new com.sk89q.worldedit.Location(new BukkitWorld(location.getWorld()),
new com.sk89q.worldedit.Vector(location.getX(), location.getY(), location.getZ()));
}
}

View File

@ -37,6 +37,12 @@
<version>6.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>6.1.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-core</artifactId>

View File

@ -2,6 +2,10 @@ package org.codemc.worldguardwrapper.implementation.v6.flag;
import com.sk89q.worldguard.protection.flags.Flag;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.implementation.v6.utility.WorldGuardFlagUtilities;
import java.util.Optional;
public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
@ -10,13 +14,24 @@ public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
super(handle);
}
@SuppressWarnings("unchecked")
@Override
public Optional<T> fromWGValue(Object value) {
if (value instanceof com.sk89q.worldedit.util.Location) {
return Optional.of((T) WorldGuardFlagUtilities.adaptLocation((com.sk89q.worldedit.util.Location) value));
} else if (value instanceof com.sk89q.worldedit.Vector) {
return Optional.of((T) WorldGuardFlagUtilities.adaptVector((com.sk89q.worldedit.Vector) value));
}
return Optional.ofNullable((T) value);
}
@Override
public Optional<Object> fromWrapperValue(T value) {
if (value instanceof Location) {
return Optional.of(WorldGuardFlagUtilities.adaptLocation((Location) value));
} else if (value instanceof Vector) {
return Optional.of(WorldGuardFlagUtilities.adaptVector((Vector) value));
}
return Optional.ofNullable(value);
}

View File

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

View File

@ -1,11 +1,12 @@
package org.codemc.worldguardwrapper.implementation.v6.utility;
import java.util.Vector;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import org.bukkit.Location;
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.WrappedPrimitiveFlag;
@ -42,4 +43,40 @@ public class WorldGuardFlagUtilities {
return wrappedFlag;
}
// Used when the flag's type is not known, so it has to be derived from a sample value's class
public IWrappedFlag<?> wrapFixType(Flag<?> flag, Class<?> type) {
if (StateFlag.State.class.isAssignableFrom(type)) {
// StateFlag
type = WrappedState.class;
} else if (com.sk89q.worldedit.util.Location.class.isAssignableFrom(type)) {
// LocationFlag
type = org.bukkit.Location.class;
} else if (com.sk89q.worldedit.Vector.class.isAssignableFrom(type)) {
// VectorFlag
type = Vector.class;
}
return wrap(flag, type);
}
public Vector adaptVector(com.sk89q.worldedit.Vector vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}
public com.sk89q.worldedit.Vector adaptVector(Vector vector) {
return new com.sk89q.worldedit.Vector(vector.getX(), vector.getY(), vector.getZ());
}
public Location adaptLocation(com.sk89q.worldedit.util.Location location) {
World world = location.getExtent() instanceof BukkitWorld
? ((BukkitWorld) location.getExtent()).getWorld() : null;
return new Location(world, location.getX(), location.getY(), location.getZ());
}
public com.sk89q.worldedit.util.Location adaptLocation(Location location) {
return new com.sk89q.worldedit.util.Location(new BukkitWorld(location.getWorld()),
location.getX(), location.getY(), location.getZ());
}
}

View File

@ -1,7 +1,13 @@
package org.codemc.worldguardwrapper.implementation.v7.flag;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldguard.protection.flags.Flag;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.implementation.v7.utility.WorldGuardFlagUtilities;
import java.util.Optional;
public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
@ -10,13 +16,24 @@ public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
super(handle);
}
@SuppressWarnings("unchecked")
@Override
public Optional<T> fromWGValue(Object value) {
if (value instanceof com.sk89q.worldedit.util.Location) {
return Optional.of((T) BukkitAdapter.adapt((com.sk89q.worldedit.util.Location) value));
} else if (value instanceof Vector3) {
return Optional.of((T) WorldGuardFlagUtilities.adaptVector((Vector3) value));
}
return Optional.ofNullable((T) value);
}
@Override
public Optional<Object> fromWrapperValue(T value) {
if (value instanceof Location) {
return Optional.of(BukkitAdapter.adapt((Location) value));
} else if (value instanceof Vector) {
return Optional.of(WorldGuardFlagUtilities.adaptVector((Vector) value));
}
return Optional.ofNullable(value);
}

View File

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

View File

@ -1,11 +1,11 @@
package org.codemc.worldguardwrapper.implementation.v7.utility;
import java.util.Vector;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
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.WrappedPrimitiveFlag;
@ -42,4 +42,28 @@ public class WorldGuardFlagUtilities {
return wrappedFlag;
}
// Used when the flag's type is not known, so it has to be derived from a sample value's class
public IWrappedFlag<?> wrapFixType(Flag<?> flag, Class<?> type) {
if (StateFlag.State.class.isAssignableFrom(type)) {
// StateFlag
type = WrappedState.class;
} else if (com.sk89q.worldedit.util.Location.class.isAssignableFrom(type)) {
// LocationFlag
type = org.bukkit.Location.class;
} else if (Vector3.class.isAssignableFrom(type)) {
// VectorFlag
type = Vector.class;
}
return wrap(flag, type);
}
public Vector adaptVector(Vector3 vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}
public Vector3 adaptVector(Vector vector) {
return Vector3.at(vector.getX(), vector.getY(), vector.getZ());
}
}

View File

@ -1,7 +1,13 @@
package org.codemc.worldguardwrapper.implementation.v7fawe.flag;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldguard.protection.flags.Flag;
import org.bukkit.Location;
import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.implementation.v7.utility.WorldGuardFlagUtilities;
import java.util.Optional;
public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
@ -10,13 +16,24 @@ public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
super(handle);
}
@SuppressWarnings("unchecked")
@Override
public Optional<T> fromWGValue(Object value) {
if (value instanceof com.sk89q.worldedit.util.Location) {
return Optional.of((T) BukkitAdapter.adapt((com.sk89q.worldedit.util.Location) value));
} else if (value instanceof Vector3) {
return Optional.of((T) WorldGuardFlagUtilities.adaptVector((Vector3) value));
}
return Optional.ofNullable((T) value);
}
@Override
public Optional<Object> fromWrapperValue(T value) {
if (value instanceof Location) {
return Optional.of(BukkitAdapter.adapt((Location) value));
} else if (value instanceof Vector) {
return Optional.of(WorldGuardFlagUtilities.adaptVector((Vector) value));
}
return Optional.ofNullable(value);
}

View File

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

View File

@ -1,11 +1,10 @@
package org.codemc.worldguardwrapper.implementation.v7fawe.utility;
import java.util.Vector;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
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.v7fawe.flag.WrappedPrimitiveFlag;
@ -42,4 +41,28 @@ public class WorldGuardFlagUtilities {
return wrappedFlag;
}
// Used when the flag's type is not known, so it has to be derived from a sample value's class
public IWrappedFlag<?> wrapFixType(Flag<?> flag, Class<?> type) {
if (StateFlag.State.class.isAssignableFrom(type)) {
// StateFlag
type = WrappedState.class;
} else if (com.sk89q.worldedit.util.Location.class.isAssignableFrom(type)) {
// LocationFlag
type = org.bukkit.Location.class;
} else if (Vector3.class.isAssignableFrom(type)) {
// VectorFlag
type = Vector.class;
}
return wrap(flag, type);
}
public Vector adaptVector(Vector3 vector) {
return new Vector(vector.getX(), vector.getY(), vector.getZ());
}
public Vector3 adaptVector(Vector vector) {
return Vector3.at(vector.getX(), vector.getY(), vector.getZ());
}
}