mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-25 02:04:44 +02:00
Flag tweaks
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
package com.intellectualcrafters.plot.flag;
|
||||
|
||||
import com.intellectualcrafters.plot.object.Plot;
|
||||
|
||||
public class BooleanFlag extends Flag<Boolean> {
|
||||
|
||||
public BooleanFlag(String name) {
|
||||
@ -34,4 +36,14 @@ public class BooleanFlag extends Flag<Boolean> {
|
||||
@Override public String getValueDescription() {
|
||||
return "Flag value must be a boolean (true|false)";
|
||||
}
|
||||
|
||||
public boolean isTrue(Plot plot) {
|
||||
Boolean value = FlagManager.getPlotFlagRaw(plot, this);
|
||||
return Boolean.TRUE == value;
|
||||
}
|
||||
|
||||
public boolean isFalse(Plot plot) {
|
||||
Boolean value = FlagManager.getPlotFlagRaw(plot, this);
|
||||
return Boolean.FALSE == value;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.intellectualcrafters.plot.flag;
|
||||
|
||||
import com.intellectualcrafters.plot.object.Plot;
|
||||
|
||||
public class Flag<V> {
|
||||
|
||||
private String name;
|
||||
@ -35,4 +37,8 @@ public class Flag<V> {
|
||||
public final String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public boolean isSet(Plot plot) {
|
||||
return FlagManager.getPlotFlagRaw(plot, this) != null;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package com.intellectualcrafters.plot.flag;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.intellectualcrafters.plot.PS;
|
||||
import com.intellectualcrafters.plot.database.DBFunc;
|
||||
import com.intellectualcrafters.plot.object.Plot;
|
||||
import com.intellectualcrafters.plot.object.PlotArea;
|
||||
@ -9,7 +11,7 @@ import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.object.PlotSettings;
|
||||
import com.intellectualcrafters.plot.util.EventUtil;
|
||||
import com.intellectualcrafters.plot.util.Permissions;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -27,6 +29,35 @@ public class FlagManager {
|
||||
|
||||
private static final HashSet<Flag<?>> reserved = Sets.newHashSet(Flags.ANALYSIS, Flags.DONE);
|
||||
|
||||
/**
|
||||
* Some events can be called millions of times each second (e.g. physics) and reusing is a lot faster.
|
||||
*/
|
||||
private static Optional MUTABLE_OPTIONAL;
|
||||
private static Field MUTABLE_OPTIONAL_FIELD;
|
||||
static {
|
||||
MUTABLE_OPTIONAL = Optional.of(new Object());
|
||||
try {
|
||||
MUTABLE_OPTIONAL_FIELD = MUTABLE_OPTIONAL.getClass().getDeclaredField("reference");
|
||||
MUTABLE_OPTIONAL_FIELD.setAccessible(true);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static <V> Optional<V> getPlotFlag(Plot plot, Flag<V> key) {
|
||||
V value = FlagManager.getPlotFlagRaw(plot, key);
|
||||
if (value != null) {
|
||||
if (PS.get().isMainThread(Thread.currentThread())) {
|
||||
try {
|
||||
MUTABLE_OPTIONAL_FIELD.set(MUTABLE_OPTIONAL, value);
|
||||
return MUTABLE_OPTIONAL;
|
||||
} catch (IllegalAccessException e) {e.printStackTrace();}
|
||||
}
|
||||
return Optional.of(value);
|
||||
}
|
||||
return Optional.absent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reserve a flag so that it cannot be set by players
|
||||
* @param flag
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.intellectualcrafters.plot.flag;
|
||||
|
||||
import com.intellectualcrafters.plot.object.Plot;
|
||||
import java.util.Collection;
|
||||
|
||||
public abstract class ListFlag<V extends Collection<?>> extends Flag<V> {
|
||||
@ -7,4 +8,9 @@ public abstract class ListFlag<V extends Collection<?>> extends Flag<V> {
|
||||
public ListFlag(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public boolean contains(Plot plot, Object value) {
|
||||
V existing = plot.getFlag(this, null);
|
||||
return existing != null && existing.contains(value);
|
||||
}
|
||||
}
|
||||
|
@ -959,7 +959,7 @@ public class Plot {
|
||||
* @param key
|
||||
*/
|
||||
public <V> Optional<V> getFlag(Flag<V> key) {
|
||||
return Optional.fromNullable(FlagManager.getPlotFlagRaw(this, key));
|
||||
return FlagManager.getPlotFlag(this, key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user