Fix non primitive flag types, api changes, bump version

This commit is contained in:
Gabriele C 2018-12-21 13:45:33 +01:00
parent 6ed239a176
commit c0da5c95ab
20 changed files with 246 additions and 92 deletions

View File

@ -21,7 +21,7 @@ How to include WorldEditWrapper into your maven project:
<dependency>
<groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</dependency>
</dependencies>
```

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-parent</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</parent>
<artifactId>worldguardwrapper-api</artifactId>

View File

@ -0,0 +1,4 @@
package org.codemc.worldguardwrapper.flag;
public interface IWrappedStatusFlag extends IWrappedFlag<WrappedState> {
}

View File

@ -17,8 +17,6 @@ public interface IWrappedRegion {
<T> void setFlag(IWrappedFlag<T> flag, T value);
Map<IWrappedFlag<?>, Object> getFlags();
int getPriority();
IWrappedDomain getOwners();

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-parent</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</parent>
<artifactId>worldguardwrapper-implementation</artifactId>

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-implementation</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</parent>
<artifactId>worldguardwrapper-implementation-v6</artifactId>
@ -18,7 +18,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-api</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -20,7 +20,9 @@ import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation;
import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedPrimitiveFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedStatusFlag;
import org.codemc.worldguardwrapper.implementation.v6.region.WrappedRegion;
import org.codemc.worldguardwrapper.implementation.v6.utility.WorldGuardVectorUtilities;
import org.codemc.worldguardwrapper.region.IWrappedRegion;
@ -56,6 +58,32 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
.orElse(null), flag));
}
// TODO: find a better way to define wrapper mappings and register mappings
@SuppressWarnings("unchecked")
private <T> IWrappedFlag<T> wrap(Flag<?> flag, Class<T> type) {
final IWrappedFlag<T> wrappedFlag;
if (type.equals(WrappedState.class)) {
wrappedFlag = (IWrappedFlag<T>) new WrappedStatusFlag((Flag<StateFlag.State>) flag);
} else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Double.class) || type.equals(double.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Enum.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Integer.class) || type.equals(int.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Location.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(String.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Vector.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else {
throw new IllegalArgumentException("Unsupported flag type " + type.getName());
}
return wrappedFlag;
}
@Override
public JavaPlugin getWorldGuardPlugin() {
return WorldGuardPlugin.inst();
@ -66,46 +94,44 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return 6;
}
@Override
public <T> Optional<T> queryFlag(Player player, Location location, IWrappedFlag<T> flag) {
Flag<T> wrappedFlag = ((WrappedFlag<T>) flag).getHandle();
return queryValue(player, location, wrappedFlag);
}
@SuppressWarnings("unchecked")
@Override
public <T> Optional<IWrappedFlag<T>> getFlag(String name, Class<T> type) {
return Optional.ofNullable(flagRegistry.get(name))
.map(flag -> (IWrappedFlag<T>) new WrappedFlag(flag));
.map(flag -> wrap(flag, type));
}
@Override
public <T> Optional<T> queryFlag(Player player, Location location, IWrappedFlag<T> flag) {
AbstractWrappedFlag<T> wrappedFlag = (AbstractWrappedFlag<T>) flag;
return wrappedFlag.fromWGValue(queryValue(player, location, wrappedFlag.getHandle()));
}
@SuppressWarnings("unchecked")
@Override
public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) {
final Flag<T> wrappedFlag;
if (type.equals(Boolean.class) || type.equals(boolean.class)) {
wrappedFlag = (Flag<T>) new BooleanFlag(name);
final Flag<?> flag;
if (type.equals(WrappedState.class)) {
flag = new StateFlag(name, defaultValue == WrappedState.ALLOW);
} else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
flag = new BooleanFlag(name);
} else if (type.equals(Double.class) || type.equals(double.class)) {
wrappedFlag = (Flag<T>) new DoubleFlag(name);
flag = new DoubleFlag(name);
} else if (type.equals(Enum.class)) {
wrappedFlag = new EnumFlag(name, type);
flag = new EnumFlag(name, type);
} else if (type.equals(Integer.class) || type.equals(int.class)) {
wrappedFlag = (Flag<T>) new IntegerFlag(name);
flag = new IntegerFlag(name);
} else if (type.equals(Location.class)) {
wrappedFlag = (Flag<T>) new LocationFlag(name);
} else if (type.equals(WrappedState.class)) {
wrappedFlag = (Flag<T>) new StateFlag(name, defaultValue == WrappedState.ALLOW);
flag = new LocationFlag(name);
} else if (type.equals(String.class)) {
wrappedFlag = (Flag<T>) new StringFlag(name, (String) defaultValue);
flag = new StringFlag(name, (String) defaultValue);
} else if (type.equals(Vector.class)) {
wrappedFlag = (Flag<T>) new VectorFlag(name);
flag = new VectorFlag(name);
} else {
throw new IllegalArgumentException("Unsupported flag type " + type.getName());
}
try {
flagRegistry.register(wrappedFlag);
return Optional.of(new WrappedFlag<>(wrappedFlag));
flagRegistry.register(flag);
return Optional.of(wrap(flag, type));
} catch (FlagConflictException ignored) {
}
return Optional.empty();

View File

@ -9,18 +9,22 @@ import java.util.Optional;
@AllArgsConstructor
@Getter
public class WrappedFlag<T> implements IWrappedFlag<T> {
public abstract class AbstractWrappedFlag<T> implements IWrappedFlag<T> {
private final Flag<T> handle;
private final Flag<?> handle;
@Override
public String getName() {
return handle.getName();
}
public abstract Optional<T> fromWGValue(Object value);
public abstract Optional<Object> fromWrapperValue(T value);
@Override
public Optional<T> getDefaultValue() {
return Optional.ofNullable(handle.getDefault());
return fromWGValue(handle.getDefault());
}
}

View File

@ -0,0 +1,23 @@
package org.codemc.worldguardwrapper.implementation.v6.flag;
import com.sk89q.worldguard.protection.flags.Flag;
import java.util.Optional;
public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
public WrappedPrimitiveFlag(Flag<T> handle) {
super(handle);
}
@Override
public Optional<T> fromWGValue(Object value) {
return Optional.ofNullable((T) value);
}
@Override
public Optional<Object> fromWrapperValue(T value) {
return Optional.ofNullable(value);
}
}

View File

@ -0,0 +1,28 @@
package org.codemc.worldguardwrapper.implementation.v6.flag;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import org.codemc.worldguardwrapper.flag.IWrappedStatusFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import java.util.Optional;
public class WrappedStatusFlag extends AbstractWrappedFlag<WrappedState> implements IWrappedStatusFlag {
public WrappedStatusFlag(Flag<StateFlag.State> handle) {
super(handle);
}
@Override
public Optional<WrappedState> fromWGValue(Object value) {
return Optional.ofNullable(value)
.map(state -> state == StateFlag.State.ALLOW ? WrappedState.ALLOW : WrappedState.DENY);
}
@Override
public Optional<Object> fromWrapperValue(WrappedState value) {
return Optional.ofNullable(value)
.map(state -> state == WrappedState.ALLOW ? StateFlag.State.ALLOW : StateFlag.State.DENY);
}
}

View File

@ -8,7 +8,7 @@ import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.World;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.WrappedFlag;
import org.codemc.worldguardwrapper.implementation.v6.flag.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.implementation.v6.utility.WorldGuardVectorUtilities;
import org.codemc.worldguardwrapper.region.IWrappedDomain;
import org.codemc.worldguardwrapper.region.IWrappedRegion;
@ -72,23 +72,16 @@ public class WrappedRegion implements IWrappedRegion {
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getFlag(IWrappedFlag<T> flag) {
Flag wrappedFlag = ((WrappedFlag) flag).getHandle();
return Optional.ofNullable(handle.getFlag(wrappedFlag))
.map(value -> (T) value);
AbstractWrappedFlag<T> wrappedFlag = (AbstractWrappedFlag<T>) flag;
return Optional.ofNullable(handle.getFlag(wrappedFlag.getHandle()))
.map(value -> (T) wrappedFlag.fromWGValue(value));
}
@SuppressWarnings("unchecked")
@Override
public <T> void setFlag(IWrappedFlag<T> flag, T value) {
Flag wrappedFlag = ((WrappedFlag) flag).getHandle();
handle.setFlag(wrappedFlag, value);
}
@Override
public Map<IWrappedFlag<?>, Object> getFlags() {
Map<IWrappedFlag<?>, Object> map = new HashMap<>();
handle.getFlags().forEach((flag, value) -> map.put(new WrappedFlag<>(flag), value));
return map;
AbstractWrappedFlag<T> wrappedFlag = (AbstractWrappedFlag<T>) flag;
handle.setFlag((Flag<Object>) wrappedFlag.getHandle(), wrappedFlag.fromWrapperValue(value).orElse(null));
}
@Override

View File

@ -23,4 +23,5 @@ public class WorldGuardVectorUtilities {
public List<BlockVector2D> toBlockVector2DList(List<Location> locations) {
return locations.stream().map(location -> new BlockVector2D(location.getX(), location.getZ())).collect(Collectors.toList());
}
}

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-implementation</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</parent>
<artifactId>worldguardwrapper-implementation-v7</artifactId>
@ -18,7 +18,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-api</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -24,7 +24,9 @@ import org.bukkit.util.Vector;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import org.codemc.worldguardwrapper.implementation.IWorldGuardImplementation;
import org.codemc.worldguardwrapper.implementation.v7.flag.WrappedFlag;
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 org.codemc.worldguardwrapper.implementation.v7.region.WrappedRegion;
import org.codemc.worldguardwrapper.region.IWrappedRegion;
@ -60,6 +62,32 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
.orElse(null), flag));
}
// TODO: find a better way to define wrapper mappings and register mappings
@SuppressWarnings("unchecked")
private <T> IWrappedFlag<T> wrap(Flag<?> flag, Class<T> type) {
final IWrappedFlag<T> wrappedFlag;
if (type.equals(WrappedState.class)) {
wrappedFlag = (IWrappedFlag<T>) new WrappedStatusFlag((Flag<StateFlag.State>) flag);
} else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Double.class) || type.equals(double.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Enum.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Integer.class) || type.equals(int.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Location.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(String.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else if (type.equals(Vector.class)) {
wrappedFlag = new WrappedPrimitiveFlag(flag);
} else {
throw new IllegalArgumentException("Unsupported flag type " + type.getName());
}
return wrappedFlag;
}
@Override
public JavaPlugin getWorldGuardPlugin() {
return WorldGuardPlugin.inst();
@ -70,45 +98,44 @@ public class WorldGuardImplementation implements IWorldGuardImplementation {
return 7;
}
@Override
public <T> Optional<T> queryFlag(Player player, Location location, IWrappedFlag<T> flag) {
Flag<T> wrappedFlag = ((WrappedFlag<T>) flag).getHandle();
return queryValue(player, location, wrappedFlag);
}
@SuppressWarnings("unchecked")
@Override
public <T> Optional<IWrappedFlag<T>> getFlag(String name, Class<T> type) {
return Optional.ofNullable(flagRegistry.get(name))
.map(flag -> (IWrappedFlag<T>) new WrappedFlag(flag));
.map(flag -> wrap(flag, type));
}
@Override
public <T> Optional<T> queryFlag(Player player, Location location, IWrappedFlag<T> flag) {
AbstractWrappedFlag<T> wrappedFlag = (AbstractWrappedFlag<T>) flag;
return wrappedFlag.fromWGValue(queryValue(player, location, wrappedFlag.getHandle()));
}
@SuppressWarnings("unchecked")
@Override
public <T> Optional<IWrappedFlag<T>> registerFlag(String name, Class<T> type, T defaultValue) {
final Flag<T> wrappedFlag;
if (type.equals(Boolean.class) || type.equals(boolean.class)) {
wrappedFlag = (Flag<T>) new BooleanFlag(name);
final Flag<?> flag;
if (type.equals(WrappedState.class)) {
flag = new StateFlag(name, defaultValue == WrappedState.ALLOW);
} else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
flag = new BooleanFlag(name);
} else if (type.equals(Double.class) || type.equals(double.class)) {
wrappedFlag = (Flag<T>) new DoubleFlag(name);
flag = new DoubleFlag(name);
} else if (type.equals(Enum.class)) {
wrappedFlag = new EnumFlag(name, type);
flag = new EnumFlag(name, type);
} else if (type.equals(Integer.class) || type.equals(int.class)) {
wrappedFlag = (Flag<T>) new IntegerFlag(name);
flag = new IntegerFlag(name);
} else if (type.equals(Location.class)) {
wrappedFlag = (Flag<T>) new LocationFlag(name);
} else if (type.equals(WrappedState.class)) {
wrappedFlag = (Flag<T>) new StateFlag(name, defaultValue == WrappedState.ALLOW);
flag = new LocationFlag(name);
} else if (type.equals(String.class)) {
wrappedFlag = (Flag<T>) new StringFlag(name, (String) defaultValue);
flag = new StringFlag(name, (String) defaultValue);
} else if (type.equals(Vector.class)) {
wrappedFlag = (Flag<T>) new VectorFlag(name);
flag = new VectorFlag(name);
} else {
throw new IllegalArgumentException("Unsupported flag type " + type.getName());
}
try {
flagRegistry.register(wrappedFlag);
return Optional.of(new WrappedFlag<>(wrappedFlag));
flagRegistry.register(flag);
return Optional.of(wrap(flag, type));
} catch (FlagConflictException ignored) {
}
return Optional.empty();

View File

@ -9,18 +9,22 @@ import java.util.Optional;
@AllArgsConstructor
@Getter
public class WrappedFlag<T> implements IWrappedFlag<T> {
public abstract class AbstractWrappedFlag<T> implements IWrappedFlag<T> {
private final Flag<T> handle;
private final Flag<?> handle;
@Override
public String getName() {
return handle.getName();
}
public abstract Optional<T> fromWGValue(Object value);
public abstract Optional<Object> fromWrapperValue(T value);
@Override
public Optional<T> getDefaultValue() {
return Optional.ofNullable(handle.getDefault());
return fromWGValue(handle.getDefault());
}
}

View File

@ -0,0 +1,23 @@
package org.codemc.worldguardwrapper.implementation.v7.flag;
import com.sk89q.worldguard.protection.flags.Flag;
import java.util.Optional;
public class WrappedPrimitiveFlag<T> extends AbstractWrappedFlag<T> {
public WrappedPrimitiveFlag(Flag<T> handle) {
super(handle);
}
@Override
public Optional<T> fromWGValue(Object value) {
return Optional.ofNullable((T) value);
}
@Override
public Optional<Object> fromWrapperValue(T value) {
return Optional.ofNullable(value);
}
}

View File

@ -0,0 +1,28 @@
package org.codemc.worldguardwrapper.implementation.v7.flag;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import org.codemc.worldguardwrapper.flag.IWrappedStatusFlag;
import org.codemc.worldguardwrapper.flag.WrappedState;
import java.util.Optional;
public class WrappedStatusFlag extends AbstractWrappedFlag<WrappedState> implements IWrappedStatusFlag {
public WrappedStatusFlag(Flag<StateFlag.State> handle) {
super(handle);
}
@Override
public Optional<WrappedState> fromWGValue(Object value) {
return Optional.ofNullable(value)
.map(state -> state == StateFlag.State.ALLOW ? WrappedState.ALLOW : WrappedState.DENY);
}
@Override
public Optional<Object> fromWrapperValue(WrappedState value) {
return Optional.ofNullable(value)
.map(state -> state == WrappedState.ALLOW ? StateFlag.State.ALLOW : StateFlag.State.DENY);
}
}

View File

@ -9,14 +9,16 @@ import lombok.Getter;
import org.bukkit.Location;
import org.bukkit.World;
import org.codemc.worldguardwrapper.flag.IWrappedFlag;
import org.codemc.worldguardwrapper.implementation.v7.flag.WrappedFlag;
import org.codemc.worldguardwrapper.implementation.v7.flag.AbstractWrappedFlag;
import org.codemc.worldguardwrapper.region.IWrappedDomain;
import org.codemc.worldguardwrapper.region.IWrappedRegion;
import org.codemc.worldguardwrapper.selection.ICuboidSelection;
import org.codemc.worldguardwrapper.selection.IPolygonalSelection;
import org.codemc.worldguardwrapper.selection.ISelection;
import java.util.*;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
@AllArgsConstructor
@ -72,23 +74,16 @@ public class WrappedRegion implements IWrappedRegion {
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getFlag(IWrappedFlag<T> flag) {
Flag wrappedFlag = ((WrappedFlag) flag).getHandle();
return Optional.ofNullable(handle.getFlag(wrappedFlag))
.map(value -> (T) value);
AbstractWrappedFlag<T> wrappedFlag = (AbstractWrappedFlag<T>) flag;
return Optional.ofNullable(handle.getFlag(wrappedFlag.getHandle()))
.map(value -> (T) wrappedFlag.fromWGValue(value));
}
@SuppressWarnings("unchecked")
@Override
public <T> void setFlag(IWrappedFlag<T> flag, T value) {
Flag wrappedFlag = ((WrappedFlag) flag).getHandle();
handle.setFlag(wrappedFlag, value);
}
@Override
public Map<IWrappedFlag<?>, Object> getFlags() {
Map<IWrappedFlag<?>, Object> map = new HashMap<>();
handle.getFlags().forEach((flag, value) -> map.put(new WrappedFlag<>(flag), value));
return map;
AbstractWrappedFlag<T> wrappedFlag = (AbstractWrappedFlag<T>) flag;
handle.setFlag((Flag<Object>) wrappedFlag.getHandle(), wrappedFlag.fromWrapperValue(value).orElse(null));
}
@Override

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-parent</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</parent>
<artifactId>worldguardwrapper</artifactId>
@ -18,17 +18,17 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-api</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-implementation-v6</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>worldguardwrapper-implementation-v7</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -6,7 +6,7 @@
<groupId>org.codemc.worldguardwrapper</groupId>
<artifactId>worldguardwrapper-parent</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.1.2-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>