Flag tweaks

This commit is contained in:
Jesse Boyd
2016-05-13 02:55:57 +10:00
parent 669359cd37
commit 63c7041a34
7 changed files with 123 additions and 85 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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);
}
/**