PlotBlock cache / events / flags

This commit is contained in:
Jesse Boyd
2016-06-04 06:20:13 +10:00
parent f84766074e
commit ca5d3a818b
26 changed files with 171 additions and 138 deletions

View File

@ -62,7 +62,7 @@ public class FlagCmd extends SubCommand {
Flag<?> flag = null;
if (args.length > 1) {
flag = FlagManager.getFlag(args[1]);
if (flag == null || FlagManager.isReserved(flag)) {
if (flag == null || flag.isReserved()) {
MainUtil.sendMessage(player, C.NOT_VALID_FLAG);
return false;
}

View File

@ -49,7 +49,7 @@ public class Music extends SubCommand {
};
int index = 0;
for (int i = 2256; i < 2268; i++) {
String name = "&r&6" + WorldUtil.IMP.getClosestMatchingName(new PlotBlock((short) i, (byte) 0));
String name = "&r&6" + WorldUtil.IMP.getClosestMatchingName(PlotBlock.get((short) i, (byte) 0));
String[] lore = {"&r&aClick to play!"};
PlotItemStack item = new PlotItemStack(i, (byte) 0, 1, name, lore);
inv.setItem(index, item);

View File

@ -5,6 +5,7 @@ import com.intellectualcrafters.plot.object.Plot;
public abstract class Flag<V> {
private final String name;
private boolean reserved = false;
/**
* Flag object used to store basic information for a Plot. Flags are a
@ -17,6 +18,20 @@ public abstract class Flag<V> {
this.name = name;
}
public Flag reserve() {
reserved = true;
return this;
}
public boolean isReserved() {
return reserved;
}
public Flag unreserve() {
reserved = false;
return this;
}
public abstract String valueToString(Object value);
@Override

View File

@ -1,7 +1,6 @@
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;
@ -11,7 +10,6 @@ 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;
@ -28,8 +26,6 @@ import java.util.Set;
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.
*/
@ -67,7 +63,11 @@ public class FlagManager {
* @return false if the flag was already reserved, otherwise true
*/
public static boolean reserveFlag(Flag<?> flag) {
return reserved.add(flag);
if (flag.isReserved()) {
return false;
}
flag.reserve();
return true;
}
/**
@ -76,7 +76,7 @@ public class FlagManager {
* @return true if the flag is reserved, false otherwise
*/
public static boolean isReserved(Flag<?> flag) {
return reserved.contains(flag);
return flag.isReserved();
}
/**
@ -84,7 +84,13 @@ public class FlagManager {
* @return a set of reserved flags
*/
public static Set<Flag<?>> getReservedFlags() {
return Collections.unmodifiableSet(reserved);
HashSet<Flag<?>> reserved = new HashSet<>();
for (Flag flag : Flags.getFlags()) {
if (flag.isReserved()) {
reserved.add(flag);
}
}
return reserved;
}
/**
@ -93,7 +99,11 @@ public class FlagManager {
* @return true if the flag was unreserved
*/
public static boolean unreserveFlag(Flag<?> flag) {
return reserved.remove(flag);
if (flag.isReserved()) {
flag.unreserve();
return true;
}
return false;
}
public static String toString(HashMap<Flag<?>, Object> flags) {
@ -175,22 +185,24 @@ public class FlagManager {
}
public static HashMap<Flag<?>, Object> getPlotFlags(PlotArea area, PlotSettings settings, boolean ignorePluginflags) {
HashMap<Flag<?>, Object> flags = new HashMap<>();
HashMap<Flag<?>, Object> flags = null;
if (area != null && !area.DEFAULT_FLAGS.isEmpty()) {
flags = new HashMap<>(area.DEFAULT_FLAGS.size());
flags.putAll(area.DEFAULT_FLAGS);
}
if (ignorePluginflags) {
if (flags == null) {
flags = new HashMap<>(settings.flags.size());
}
for (Map.Entry<Flag<?>, Object> flag : settings.flags.entrySet()) {
if (isReserved(flag.getKey())) {
if (flag.getKey().isReserved()) {
continue;
}
flags.put(flag.getKey(), flag.getValue());
}
} else {
flags.putAll(settings.flags);
return flags;
}
return flags;
return settings.flags;
}
public static Map<Flag<?>, Object> getSettingFlags(PlotArea area, PlotSettings settings) {
@ -288,29 +300,15 @@ public class FlagManager {
* @return the {@code Flag} object defined by {@code string}
*/
public static Flag<?> getFlag(String string) {
for (Flag flag : Flags.getFlags()) {
if (flag.getName().equalsIgnoreCase(string)) {
if (isReserved(flag)) {
return null;
}
return flag;
}
}
return null;
return Flags.getFlag(string);
}
public static Flag<?> getFlag(String string, boolean ignoreReserved) {
for (Flag flag : Flags.getFlags()) {
if (flag.getName().equalsIgnoreCase(string)) {
if (!ignoreReserved) {
if (isReserved(flag)) {
return null;
}
}
return flag;
}
Flag<?> flag = Flags.getFlag(string);
if (!ignoreReserved && flag != null && flag.isReserved()) {
return null;
}
return null;
return flag;
}

View File

@ -1,17 +1,14 @@
package com.intellectualcrafters.plot.flag;
import com.google.common.collect.Sets;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotArea;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.MathMan;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
public class Flags {
@ -115,46 +112,51 @@ public class Flags {
};
public static final BooleanFlag SLEEP = new BooleanFlag("sleep");
private static final HashSet<Flag<?>> flags = Sets.newHashSet(MUSIC, DESCRIPTION, ANALYSIS, GREETING, FAREWELL, FEED, HEAL,
GAMEMODE,
DONE,
REDSTONE,
FLY, NOTIFY_LEAVE, NOTIFY_ENTER, TIME, WEATHER, KEEP, PRICE, EXPLOSION, GRASS_GROW, VINE_GROW, MYCEL_GROW, DISABLE_PHYSICS, SNOW_MELT,
ICE_MELT,
FIRE_SPREAD, BLOCK_BURN, BLOCK_IGNITION, SOIL_DRY, BLOCKED_CMDS, USE, BREAK, PLACE, DEVICE_INTERACT, VEHICLE_BREAK, VEHICLE_PLACE,
VEHICLE_USE,
HANGING_BREAK, HANGING_PLACE, HANGING_INTERACT, MISC_PLACE, MISC_BREAK, MISC_INTERACT, PLAYER_INTERACT, TAMED_ATTACK, TAMED_INTERACT,
ANIMAL_ATTACK, ANIMAL_INTERACT, HOSTILE_ATTACK, HOSTILE_INTERACT, MOB_PLACE, FORCEFIELD, INVINCIBLE, ITEM_DROP, INSTABREAK,
DROP_PROTECTION, PVP,
PVE, NO_WORLDEDIT, MISC_CAP, ENTITY_CAP, MOB_CAP, ANIMAL_CAP, HOSTILE_CAP, VEHICLE_CAP);
private static final HashMap<String, Flag<?>> flags;
static {
flags = new HashMap<>();
try {
for (Field field : Flags.class.getDeclaredFields()) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
String fieldName = field.getName().replace("_","-").toLowerCase();
Object fieldValue = field.get(null);
if (!(fieldValue instanceof Flag)) {
continue;
}
Flag flag = (Flag) fieldValue;
if (!flag.getName().equals(fieldName)) {
PS.debug(Flags.class + "Field doesn't match: " + fieldName + " != " + flag.getName());
}
flags.put(flag.getName(), flag);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* Get an immutable set of registered flags.
*
* @return a set of registered flags.
*/
public static Set<Flag<?>> getFlags() {
return Collections.unmodifiableSet(flags);
public static Collection<Flag<?>> getFlags() {
return flags.values();
}
public static Flag<?> getFlag(String flag) {
return flags.get(flag);
}
public static void registerFlag(final Flag<?> flag) {
Iterator<Flag<?>> iterator = flags.iterator();
Flag<?> duplicate = null;
while (iterator.hasNext()){
duplicate = iterator.next();
if (flag.getName().equalsIgnoreCase(duplicate.getName())) {
iterator.remove();
flags.add(flag);
break;
}
}
final Flag<?> dupFinal = duplicate;
final Flag<?> duplicate = flags.put(flag.getName(), flag);
PS.get().foreachPlotArea(new RunnableVal<PlotArea>() {
@Override public void run(PlotArea value) {
if (dupFinal != null) {
if (duplicate != null) {
Object remove;
if (value.DEFAULT_FLAGS.containsKey(dupFinal)) {
remove = value.DEFAULT_FLAGS.remove(dupFinal);
if (value.DEFAULT_FLAGS.containsKey(duplicate)) {
remove = value.DEFAULT_FLAGS.remove(duplicate);
if (!(remove instanceof String)) {
//error message? maybe?
return;
@ -166,10 +168,10 @@ public class Flags {
});
PS.get().foreachPlotRaw(new RunnableVal<Plot>() {
@Override public void run(Plot value) {
if (dupFinal != null) {
if (duplicate != null) {
Object remove = null;
if (value.getFlags().containsKey(dupFinal)) {
remove = value.getFlags().remove(dupFinal);
if (value.getFlags().containsKey(duplicate)) {
remove = value.getFlags().remove(duplicate);
}
if (!(remove instanceof String)) {
//error message? maybe?

View File

@ -1,5 +1,7 @@
package com.intellectualcrafters.plot.flag;
import com.intellectualcrafters.plot.object.Plot;
public class IntegerFlag extends Flag<Integer> {
public IntegerFlag(String name) {
@ -21,4 +23,19 @@ public class IntegerFlag extends Flag<Integer> {
return null;
}
}
public boolean isEqual(Plot plot, int value) {
Integer existing = FlagManager.getPlotFlagRaw(plot, this);
return existing != null && existing == value;
}
public boolean isMore(Plot plot, int value) {
Integer existing = FlagManager.getPlotFlagRaw(plot, this);
return existing != null && existing > value;
}
public boolean isLess(Plot plot, int value) {
Integer existing = FlagManager.getPlotFlagRaw(plot, this);
return existing != null && existing < value;
}
}

View File

@ -34,7 +34,7 @@ public class PlotBlockListFlag extends ListFlag<HashSet<PlotBlock>> {
data = -1;
}
short id = Short.parseShort(split[0]);
block = new PlotBlock(id, data);
block = PlotBlock.get(id, data);
} catch (NumberFormatException ignored) {
StringComparison<PlotBlock>.ComparisonResult str = WorldUtil.IMP.getClosestBlock(value);
if (str == null || str.match > 1) {

View File

@ -106,7 +106,7 @@ public class AugmentedUtils {
primaryMask = result;
}
PlotChunk<?> secondaryMask;
PlotBlock air = new PlotBlock((short) 0, (byte) 0);
PlotBlock air = PlotBlock.get((short) 0, (byte) 0);
if (area.TERRAIN == 2) {
PlotManager manager = area.getPlotManager();
final boolean[][] canPlace = new boolean[16][16];

View File

@ -272,9 +272,9 @@ public class ClassicPlotManager extends SquarePlotManager {
int ez = pos2.getZ() + 2;
MainUtil.setSimpleCuboidAsync(plotArea.worldname,
new Location(plotArea.worldname, sx, Math.min(dpw.WALL_HEIGHT, dpw.ROAD_HEIGHT) + 1, sz + 1),
new Location(plotArea.worldname, ex, 255, ez - 1), new PlotBlock((short) 0, (byte) 0));
new Location(plotArea.worldname, ex, 255, ez - 1), PlotBlock.get((short) 0, (byte) 0));
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, 0, sz + 1),
new Location(plotArea.worldname, ex, 0, ez - 1), new PlotBlock((short) 7,
new Location(plotArea.worldname, ex, 0, ez - 1), PlotBlock.get((short) 7,
(byte) 0));
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, 1, sz + 1),
new Location(plotArea.worldname, sx, dpw.WALL_HEIGHT, ez - 1), dpw.WALL_FILLING);
@ -302,9 +302,9 @@ public class ClassicPlotManager extends SquarePlotManager {
int ex = pos2.getX() + 2;
MainUtil.setSimpleCuboidAsync(plotArea.worldname,
new Location(plotArea.worldname, sx + 1, Math.min(dpw.WALL_HEIGHT, dpw.ROAD_HEIGHT) + 1, sz),
new Location(plotArea.worldname, ex - 1, 255, ez), new PlotBlock((short) 0, (byte) 0));
new Location(plotArea.worldname, ex - 1, 255, ez), PlotBlock.get((short) 0, (byte) 0));
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 0, sz),
new Location(plotArea.worldname, ex - 1, 0, ez), new PlotBlock((short) 7, (byte) 0));
new Location(plotArea.worldname, ex - 1, 0, ez), PlotBlock.get((short) 7, (byte) 0));
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 1, sz),
new Location(plotArea.worldname, ex - 1, dpw.WALL_HEIGHT, sz), dpw.WALL_FILLING);
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, dpw.WALL_HEIGHT + 1, sz),
@ -329,10 +329,10 @@ public class ClassicPlotManager extends SquarePlotManager {
int sz = pos2.getZ() + 1;
int ez = sz + dpw.ROAD_WIDTH - 1;
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, dpw.ROAD_HEIGHT + 1, sz + 1),
new Location(plotArea.worldname, ex - 1, 255, ez - 1), new PlotBlock(
new Location(plotArea.worldname, ex - 1, 255, ez - 1), PlotBlock.get(
(short) 0, (byte) 0));
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 0, sz + 1),
new Location(plotArea.worldname, ex - 1, 0, ez - 1), new PlotBlock((short) 7, (byte) 0));
new Location(plotArea.worldname, ex - 1, 0, ez - 1), PlotBlock.get((short) 7, (byte) 0));
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 1, sz + 1),
new Location(plotArea.worldname, ex - 1, dpw.ROAD_HEIGHT, ez - 1), dpw.ROAD_BLOCK);
return true;
@ -348,7 +348,7 @@ public class ClassicPlotManager extends SquarePlotManager {
int sz = pos1.getZ() - 1;
int ez = pos2.getZ() + 1;
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, Math.min(dpw.PLOT_HEIGHT, dpw.ROAD_HEIGHT) + 1, sz),
new Location(plotArea.worldname, ex, 255, ez), new PlotBlock((short) 0, (byte) 0));
new Location(plotArea.worldname, ex, 255, ez), PlotBlock.get((short) 0, (byte) 0));
MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, 1, sz + 1),
new Location(plotArea.worldname, ex, dpw.PLOT_HEIGHT - 1, ez - 1), dpw.MAIN_BLOCK);
MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, dpw.PLOT_HEIGHT, sz + 1),
@ -366,7 +366,7 @@ public class ClassicPlotManager extends SquarePlotManager {
int sx = pos1.getX() - 1;
int ex = pos2.getX() + 1;
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, Math.min(dpw.PLOT_HEIGHT, dpw.ROAD_HEIGHT) + 1, sz),
new Location(plotArea.worldname, ex, 255, ez), new PlotBlock((short) 0, (byte) 0));
new Location(plotArea.worldname, ex, 255, ez), PlotBlock.get((short) 0, (byte) 0));
MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 1, sz),
new Location(plotArea.worldname, ex - 1, dpw.PLOT_HEIGHT - 1, ez), dpw.MAIN_BLOCK);
MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, dpw.PLOT_HEIGHT, sz),
@ -383,7 +383,7 @@ public class ClassicPlotManager extends SquarePlotManager {
int sz = location.getZ() + 1;
int ez = sz + dpw.ROAD_WIDTH - 1;
MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, dpw.ROAD_HEIGHT + 1, sz),
new Location(plotArea.worldname, ex, 255, ez), new PlotBlock((short) 0, (byte) 0));
new Location(plotArea.worldname, ex, 255, ez), PlotBlock.get((short) 0, (byte) 0));
MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, 1, sz),
new Location(plotArea.worldname, ex, dpw.ROAD_HEIGHT - 1, ez), dpw.MAIN_BLOCK);
MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, dpw.ROAD_HEIGHT, sz),

View File

@ -12,12 +12,12 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld {
public int ROAD_HEIGHT = 64;
public int PLOT_HEIGHT = 64;
public int WALL_HEIGHT = 64;
public PlotBlock[] MAIN_BLOCK = new PlotBlock[] { new PlotBlock((short) 1, (byte) 0) };
public PlotBlock[] TOP_BLOCK = new PlotBlock[] { new PlotBlock((short) 2, (byte) 0) };
public PlotBlock WALL_BLOCK = new PlotBlock((short) 44, (byte) 0);
public PlotBlock CLAIMED_WALL_BLOCK = new PlotBlock((short) 44, (byte) 1);
public PlotBlock WALL_FILLING = new PlotBlock((short) 1, (byte) 0);
public PlotBlock ROAD_BLOCK = new PlotBlock((short) 155, (byte) 0);
public PlotBlock[] MAIN_BLOCK = new PlotBlock[] { PlotBlock.get((short) 1, (byte) 0) };
public PlotBlock[] TOP_BLOCK = new PlotBlock[] { PlotBlock.get((short) 2, (byte) 0) };
public PlotBlock WALL_BLOCK = PlotBlock.get((short) 44, (byte) 0);
public PlotBlock CLAIMED_WALL_BLOCK = PlotBlock.get((short) 44, (byte) 1);
public PlotBlock WALL_FILLING = PlotBlock.get((short) 1, (byte) 0);
public PlotBlock ROAD_BLOCK = PlotBlock.get((short) 155, (byte) 0);
public boolean PLOT_BEDROCK = true;
public ClassicPlotWorld(String worldName, String id, IndependentPlotGenerator generator, PlotId min, PlotId max) {

View File

@ -151,11 +151,11 @@ public class HybridPlotManager extends ClassicPlotManager {
final PlotBlock[] filling = dpw.MAIN_BLOCK;
final PlotBlock bedrock;
if (dpw.PLOT_BEDROCK) {
bedrock = new PlotBlock((short) 7, (byte) 0);
bedrock = PlotBlock.get((short) 7, (byte) 0);
} else {
bedrock = new PlotBlock((short) 0, (byte) 0);
bedrock = PlotBlock.get((short) 0, (byte) 0);
}
final PlotBlock air = new PlotBlock((short) 0, (byte) 0);
final PlotBlock air = PlotBlock.get((short) 0, (byte) 0);
final String biome = WorldUtil.IMP.getBiomeList()[dpw.PLOT_BIOME];
ChunkManager.chunkTask(pos1, pos2, new RunnableVal<int[]>() {
@Override

View File

@ -237,7 +237,7 @@ public class HybridPlotWorld extends ClassicPlotWorld {
}
this.ROAD_SCHEMATIC_ENABLED = true;
// Do not populate road if using schematic population
this.ROAD_BLOCK = new PlotBlock(this.ROAD_BLOCK.id, (byte) 0);
this.ROAD_BLOCK = PlotBlock.get(this.ROAD_BLOCK.id, (byte) 0);
short[] ids1 = schematic1.getIds();
byte[] datas1 = schematic1.getDatas();
@ -300,6 +300,6 @@ public class HybridPlotWorld extends ClassicPlotWorld {
existing = new HashMap<>();
this.G_SCH.put(pair, existing);
}
existing.put((int) y, new PlotBlock(id, data));
existing.put((int) y, PlotBlock.get(id, data));
}
}

View File

@ -163,7 +163,7 @@ public abstract class HybridUtils {
whenDone.value += checkModified(plot.getArea().worldname, bx, ex, cpw.PLOT_HEIGHT, cpw.PLOT_HEIGHT, bz, ez, cpw.TOP_BLOCK);
whenDone.value += checkModified(
plot.getArea().worldname, bx, ex, cpw.PLOT_HEIGHT + 1, 255, bz, ez,
new PlotBlock[]{new PlotBlock((short) 0, (byte) 0)});
new PlotBlock[]{PlotBlock.get((short) 0, (byte) 0)});
}
}, this, 5);

View File

@ -3,6 +3,18 @@ package com.intellectualcrafters.plot.object;
public class PlotBlock {
public static final PlotBlock EVERYTHING = new PlotBlock((short) 0, (byte) 0);
private static final PlotBlock[] CACHE = new PlotBlock[65535];
static {
for (int i = 0; i < 65535; i++) {
short id = (short) (i >> 4);
byte data = (byte) (i & 15);
CACHE[i] = new PlotBlock(id, data);
}
}
public static PlotBlock get(int id, int data) {
return CACHE[(id << 4) + data];
}
public final short id;
public final byte data;

View File

@ -325,7 +325,7 @@ public abstract class SchematicHandler {
SetQueue.IMP.setBlock(plot.getArea().worldname, xx, yy, zz, id);
break;
default:
SetQueue.IMP.setBlock(plot.getArea().worldname, xx, yy, zz, new PlotBlock((short) id, datas[i]));
SetQueue.IMP.setBlock(plot.getArea().worldname, xx, yy, zz, PlotBlock.get((short) id, datas[i]));
break;
}
}

View File

@ -217,7 +217,7 @@ public class ProcessedWEExtent extends AbstractDelegateExtent {
break;
default:
if (Settings.EXPERIMENTAL_FAST_ASYNC_WORLDEDIT) {
SetQueue.IMP.setBlock(this.world, x, y, z, new PlotBlock((short) id, (byte) block.getData()));
SetQueue.IMP.setBlock(this.world, x, y, z, PlotBlock.get((short) id, (byte) block.getData()));
} else {
super.setBlock(location, block);
}