Convert the data system to a state system. This doesn't work, needs new data

This commit is contained in:
Matthew Miller 2018-06-14 16:35:56 +10:00
parent 1cc735e359
commit a71e39d777
21 changed files with 236 additions and 270 deletions

View File

@ -23,9 +23,13 @@ import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.type.BlockType; import com.sk89q.worldedit.blocks.type.BlockType;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.world.registry.state.State;
import com.sk89q.worldedit.world.registry.state.value.StateValue;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Map;
/** /**
* A implementation of a lazy block for {@link Extent#getLazyBlock(Vector)} * A implementation of a lazy block for {@link Extent#getLazyBlock(Vector)}
* that takes the block's ID and metadata, but will defer loading of NBT * that takes the block's ID and metadata, but will defer loading of NBT
@ -78,13 +82,13 @@ public class LazyBlock extends BaseBlock {
* Create a new lazy block. * Create a new lazy block.
* *
* @param type the block type * @param type the block type
* @param data the data value * @param states the block states
* @param extent the extent to later load the full block data from * @param extent the extent to later load the full block data from
* @param position the position to later load the full block data from * @param position the position to later load the full block data from
*/ */
@Deprecated @Deprecated
public LazyBlock(BlockType type, int data, Extent extent, Vector position) { public LazyBlock(BlockType type, Map<State, StateValue> states, Extent extent, Vector position) {
super(type, data); super(type, states);
checkNotNull(extent); checkNotNull(extent);
checkNotNull(position); checkNotNull(position);
this.extent = extent; this.extent = extent;
@ -123,6 +127,11 @@ public class LazyBlock extends BaseBlock {
throw new UnsupportedOperationException("This object is immutable"); throw new UnsupportedOperationException("This object is immutable");
} }
@Override
public void setState(State state, StateValue stateValue) {
throw new UnsupportedOperationException("This object is immutable");
}
@Override @Override
public CompoundTag getNbtData() { public CompoundTag getNbtData() {
if (!loaded) { if (!loaded) {

View File

@ -67,7 +67,7 @@ public class BlockMask extends AbstractMask {
public boolean matches(EditSession editSession, Vector position) { public boolean matches(EditSession editSession, Vector position) {
BaseBlock block = editSession.getBlock(position); BaseBlock block = editSession.getBlock(position);
return blocks.contains(block) return blocks.contains(block)
|| blocks.contains(new BaseBlock(block.getType(), -1)); || blocks.contains(new BaseBlock(block.getType()));
} }
} }

View File

@ -28,10 +28,15 @@ import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.foundation.Block; import com.sk89q.worldedit.foundation.Block;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.world.registry.BundledBlockData; import com.sk89q.worldedit.world.registry.BundledBlockData;
import com.sk89q.worldedit.world.registry.WorldData; import com.sk89q.worldedit.world.registry.state.State;
import com.sk89q.worldedit.world.registry.state.value.StateValue;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collection;
/** /**
* Represents a mutable "snapshot" of a block. * Represents a mutable "snapshot" of a block.
@ -42,15 +47,6 @@ import java.util.Collection;
* snapshot of blocks correctly, so, for example, the NBT data for a block * snapshot of blocks correctly, so, for example, the NBT data for a block
* may be missing.</p> * may be missing.</p>
* *
* <p>This class identifies blocks using an integer ID. However, IDs for
* a given block may differ between worlds so it is important that users of
* this class convert the ID from one "world space" to another "world space,"
* a task that that is assisted with by working with the source and
* destination {@link WorldData} instances. Numeric IDs are utilized because
* they are more space efficient to store, and it also implies that internal
* uses of this class (i.e. history, etc.) do not need to worry about
* interning block string IDs.</p>
*
* <p>A peculiar detail of this class is that it accepts {@code -1} as a * <p>A peculiar detail of this class is that it accepts {@code -1} as a
* valid data value. This is due to legacy reasons: WorldEdit uses -1 * valid data value. This is due to legacy reasons: WorldEdit uses -1
* as a "wildcard" block value, even though a {@link Mask} would be * as a "wildcard" block value, even though a {@link Mask} would be
@ -59,17 +55,11 @@ import java.util.Collection;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class BaseBlock extends Block implements TileEntityBlock { public class BaseBlock extends Block implements TileEntityBlock {
/**
* Indicates the maximum data value (inclusive) that can be used. A future
* version of Minecraft may abolish block data values.
*/
public static final int MAX_DATA = 15;
// Instances of this class should be _as small as possible_ because there will // Instances of this class should be _as small as possible_ because there will
// be millions of instances of this object. // be millions of instances of this object.
private BlockType blockType; private BlockType blockType;
private short data; private Map<State, StateValue> states;
@Nullable @Nullable
private CompoundTag nbtData; private CompoundTag nbtData;
@ -83,6 +73,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
public BaseBlock(int id) { public BaseBlock(int id) {
internalSetId(id); internalSetId(id);
internalSetData(0); internalSetData(0);
this.states = new HashMap<>();
} }
/** /**
@ -92,6 +83,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
*/ */
public BaseBlock(BlockType blockType) { public BaseBlock(BlockType blockType) {
internalSetType(blockType); internalSetType(blockType);
this.states = new HashMap<>();
} }
/** /**
@ -106,22 +98,20 @@ public class BaseBlock extends Block implements TileEntityBlock {
public BaseBlock(int id, int data) { public BaseBlock(int id, int data) {
internalSetId(id); internalSetId(id);
internalSetData(data); internalSetData(data);
this.states = new HashMap<>();
} }
/** /**
* Construct a block with the given ID and data value. * Construct a block with the given ID and data value.
* *
* THIS WILL GET REMOVED SOON.
*
* @param blockType The block type * @param blockType The block type
* @param data data value * @param states The states
* @see #setId(int) * @see #setId(int)
* @see #setData(int) * @see #setData(int)
*/ */
@Deprecated public BaseBlock(BlockType blockType, Map<State, StateValue> states) {
public BaseBlock(BlockType blockType, int data) {
internalSetType(blockType); internalSetType(blockType);
internalSetData(data); setStates(states);
} }
/** /**
@ -136,21 +126,19 @@ public class BaseBlock extends Block implements TileEntityBlock {
internalSetId(id); internalSetId(id);
setData(data); setData(data);
setNbtData(nbtData); setNbtData(nbtData);
this.states = new HashMap<>();
} }
/** /**
* Construct a block with the given ID, data value and NBT data structure. * Construct a block with the given ID, data value and NBT data structure.
* *
* THIS WILL GET REMOVED SOON.
*
* @param blockType The block type * @param blockType The block type
* @param data data value * @param states The states
* @param nbtData NBT data, which may be null * @param nbtData NBT data, which may be null
*/ */
@Deprecated public BaseBlock(BlockType blockType, Map<State, StateValue> states, @Nullable CompoundTag nbtData) {
public BaseBlock(BlockType blockType, int data, @Nullable CompoundTag nbtData) {
setType(blockType); setType(blockType);
setData(data); setStates(states);
setNbtData(nbtData); setNbtData(nbtData);
} }
@ -160,7 +148,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
* @param other the other block * @param other the other block
*/ */
public BaseBlock(BaseBlock other) { public BaseBlock(BaseBlock other) {
this(other.getId(), other.getData(), other.getNbtData()); this(other.getType(), other.getStates(), other.getNbtData());
} }
/** /**
@ -216,38 +204,70 @@ public class BaseBlock extends Block implements TileEntityBlock {
/** /**
* Get the block's data value. * Get the block's data value.
* *
* Broken - do not use
*
* @return data value (0-15) * @return data value (0-15)
*/ */
@Override @Override
@Deprecated
public int getData() { public int getData() {
return data; return 0;
} }
/** /**
* Set the block's data value. * Gets a map of state to statevalue
* *
* @param data block data value (between 0 and {@link #MAX_DATA}). * @return The state map
*/ */
protected final void internalSetData(int data) { public Map<State, StateValue> getStates() {
if (data > MAX_DATA) { return Collections.unmodifiableMap(states);
throw new IllegalArgumentException( }
"Can't have a block data value above " + MAX_DATA + " ("
+ data + " given)");
}
if (data < -1) { /**
throw new IllegalArgumentException("Can't have a block data value below -1"); * Sets the states of this block.
} *
* @param states The states
*/
private void setStates(Map<State, StateValue> states) {
this.states = states;
}
this.data = (short) data; /**
* Gets the State for this Block.
*
* @param state The state to get the value for
* @return The state value
*/
public StateValue getState(State state) {
return states.get(state);
}
/**
* Sets a state to a specific value
*
* @param state The state
* @param stateValue The value
*/
public void setState(State state, StateValue stateValue) {
this.states.put(state, stateValue);
} }
/** /**
* Set the block's data value. * Set the block's data value.
* *
* @param data block data value (between 0 and {@link #MAX_DATA}). * @param data block data value
*/
@Deprecated
protected final void internalSetData(int data) {
}
/**
* Set the block's data value.
*
* @param data block data value
*/ */
@Override @Override
@Deprecated
public void setData(int data) { public void setData(int data) {
internalSetData(data); internalSetData(data);
} }
@ -274,6 +294,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
* @return true if the data value is -1 * @return true if the data value is -1
*/ */
@Override @Override
@Deprecated
public boolean hasWildcardData() { public boolean hasWildcardData() {
return getData() == -1; return getData() == -1;
} }
@ -446,7 +467,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
@Override @Override
public String toString() { public String toString() {
return "Block{Type:" + getType().getId() + ", Data: " + getData() + "}"; return "Block{Type:" + getType().getId() + ", States: " + getStates().toString() + "}";
} }
} }

View File

@ -23,23 +23,12 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.blocks.ClothColor;
import com.sk89q.worldedit.blocks.MobSpawnerBlock; import com.sk89q.worldedit.blocks.MobSpawnerBlock;
import com.sk89q.worldedit.blocks.NoteBlock; import com.sk89q.worldedit.blocks.NoteBlock;
import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor; import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Platform; import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.registry.BundledBlockData;
import com.sk89q.worldedit.world.registry.SimpleState;
import com.sk89q.worldedit.world.registry.State;
import com.sk89q.worldedit.world.registry.StateValue;
import java.util.Map;
import java.util.Map.Entry;
/** /**
* Looks up information about a block. * Looks up information about a block.
@ -57,13 +46,12 @@ public class QueryTool implements BlockTool {
World world = (World) clicked.getExtent(); World world = (World) clicked.getExtent();
EditSession editSession = session.createEditSession(player); EditSession editSession = session.createEditSession(player);
BaseBlock block = editSession.getBlock(clicked.toVector()); BaseBlock block = editSession.getBlock(clicked.toVector());
BlockType type = BlockType.fromID(block.getType().getLegacyId());
player.print("\u00A79@" + clicked.toVector() + ": " + "\u00A7e" player.print("\u00A79@" + clicked.toVector() + ": " + "\u00A7e"
+ "#" + block.getType() + "\u00A77" + " (" + "#" + block.getType() + "\u00A77" + " ("
+ (type == null ? "Unknown" : type.getName()) + ") " + block.getType().getId() + ") "
+ "\u00A7f" + "\u00A7f"
+ "[" + block.getData() + "]" + " (" + world.getBlockLightLevel(clicked.toVector()) + "/" + world.getBlockLightLevel(clicked.toVector().add(0, 1, 0)) + ")"); + "[" + block.getStates().toString() + "]" + " (" + world.getBlockLightLevel(clicked.toVector()) + "/" + world.getBlockLightLevel(clicked.toVector().add(0, 1, 0)) + ")");
if (block instanceof MobSpawnerBlock) { if (block instanceof MobSpawnerBlock) {
player.printRaw("\u00A7e" + "Mob Type: " player.printRaw("\u00A7e" + "Mob Type: "
@ -73,36 +61,6 @@ public class QueryTool implements BlockTool {
+ ((NoteBlock) block).getNote()); + ((NoteBlock) block).getNote());
} }
Map<String, ? extends State> states = BundledBlockData.getInstance().getStatesById(block.getType().getId());
if (states == null || states.isEmpty()) return true;
StringBuilder builder = new StringBuilder();
builder.append("States: ");
boolean first = true;
boolean hasVisibleStates = false;
for (Entry<String, ? extends State> e : states.entrySet()) {
String name = e.getKey();
State state = e.getValue();
if (state instanceof SimpleState && ((SimpleState) state).getDataMask() == 0) {
continue; // don't try to determine states that aren't reflected in their data value
}
hasVisibleStates = true;
if (!first) {
builder.append(", ");
}
first = false;
String valName = "";
for (Entry<String, ? extends StateValue> entry : state.valueMap().entrySet()) {
if (entry.getValue().isSet(block)) {
valName = entry.getKey();
break;
}
}
builder.append("\u00A79").append(name).append(": \u00A7f").append(valName != null ? valName : "set");
}
if (hasVisibleStates) {
player.printRaw(builder.toString());
}
return true; return true;
} }

View File

@ -19,6 +19,8 @@
package com.sk89q.worldedit.extent.transform; package com.sk89q.worldedit.extent.transform;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
@ -26,13 +28,13 @@ import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.world.registry.BlockRegistry; import com.sk89q.worldedit.world.registry.BlockRegistry;
import com.sk89q.worldedit.world.registry.State; import com.sk89q.worldedit.world.registry.state.DirectionalState;
import com.sk89q.worldedit.world.registry.StateValue; import com.sk89q.worldedit.world.registry.state.State;
import com.sk89q.worldedit.world.registry.state.value.DirectionalStateValue;
import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull; import javax.annotation.Nullable;
/** /**
* Transforms blocks themselves (but not their position) according to a * Transforms blocks themselves (but not their position) according to a
@ -131,12 +133,12 @@ public class BlockTransformExtent extends AbstractDelegateExtent {
} }
for (State state : states.values()) { for (State state : states.values()) {
if (state.hasDirection()) { if (state instanceof DirectionalState) {
StateValue value = state.getValue(block); DirectionalStateValue value = (DirectionalStateValue) block.getState(state);
if (value != null && value.getDirection() != null) { if (value != null && value.getData() != null) {
StateValue newValue = getNewStateValue(state, transform, value.getDirection()); DirectionalStateValue newValue = getNewStateValue((DirectionalState) state, transform, value.getDirection());
if (newValue != null) { if (newValue != null) {
newValue.set(changedBlock); changedBlock.setState(state, newValue);
} }
} }
} }
@ -154,13 +156,13 @@ public class BlockTransformExtent extends AbstractDelegateExtent {
* @return a new state or null if none could be found * @return a new state or null if none could be found
*/ */
@Nullable @Nullable
private static StateValue getNewStateValue(State state, Transform transform, Vector oldDirection) { private static DirectionalStateValue getNewStateValue(DirectionalState state, Transform transform, Vector oldDirection) {
Vector newDirection = transform.apply(oldDirection).subtract(transform.apply(Vector.ZERO)).normalize(); Vector newDirection = transform.apply(oldDirection).subtract(transform.apply(Vector.ZERO)).normalize();
StateValue newValue = null; DirectionalStateValue newValue = null;
double closest = -2; double closest = -2;
boolean found = false; boolean found = false;
for (StateValue v : state.valueMap().values()) { for (DirectionalStateValue v : state.getValues()) {
if (v.getDirection() != null) { if (v.getDirection() != null) {
double dot = v.getDirection().normalize().dot(newDirection); double dot = v.getDirection().normalize().dot(newDirection);
if (dot >= closest) { if (dot >= closest) {

View File

@ -104,7 +104,7 @@ public class ExtentBlockCopy implements RegionFunction {
builder.putByte("Rot", (byte) MCDirections.toRotation(newDirection)); builder.putByte("Rot", (byte) MCDirections.toRotation(newDirection));
return new BaseBlock(state.getType(), state.getData(), builder.build()); return new BaseBlock(state.getType(), state.getStates(), builder.build());
} }
} }
} }

View File

@ -23,7 +23,6 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.type.BlockTypes; import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.pattern.BlockPattern; import com.sk89q.worldedit.function.pattern.BlockPattern;
@ -97,7 +96,7 @@ public class FloraGenerator implements RegionFunction {
*/ */
public static Pattern getTemperatePattern() { public static Pattern getTemperatePattern() {
RandomPattern pattern = new RandomPattern(); RandomPattern pattern = new RandomPattern();
pattern.add(new BlockPattern(new BaseBlock(BlockTypes.TALL_GRASS, 1)), 300); pattern.add(new BlockPattern(new BaseBlock(BlockTypes.GRASS)), 300);
pattern.add(new BlockPattern(new BaseBlock(BlockTypes.POPPY)), 5); pattern.add(new BlockPattern(new BaseBlock(BlockTypes.POPPY)), 5);
pattern.add(new BlockPattern(new BaseBlock(BlockTypes.DANDELION)), 5); pattern.add(new BlockPattern(new BaseBlock(BlockTypes.DANDELION)), 5);
return pattern; return pattern;

View File

@ -190,7 +190,7 @@ public class GardenPatchGenerator implements RegionFunction {
public static Pattern getPumpkinPattern() { public static Pattern getPumpkinPattern() {
RandomPattern pattern = new RandomPattern(); RandomPattern pattern = new RandomPattern();
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
pattern.add(new BlockPattern(new BaseBlock(BlockTypes.PUMPKIN, i)), 100); // TODO pattern.add(new BlockPattern(new BaseBlock(BlockTypes.CARVED_PUMPKIN, i)), 100);
} }
return pattern; return pattern;
} }

View File

@ -95,7 +95,7 @@ public class BlockMask extends AbstractExtentMask {
@Override @Override
public boolean test(Vector vector) { public boolean test(Vector vector) {
BaseBlock block = getExtent().getBlock(vector); BaseBlock block = getExtent().getBlock(vector);
return blocks.contains(block) || blocks.contains(new BaseBlock(block.getType(), -1)); return blocks.contains(block) || blocks.contains(new BaseBlock(block.getType()));
} }
@Nullable @Nullable

View File

@ -41,7 +41,7 @@ public class FuzzyBlockMask extends BlockMask {
Extent extent = getExtent(); Extent extent = getExtent();
Collection<BaseBlock> blocks = getBlocks(); Collection<BaseBlock> blocks = getBlocks();
BaseBlock lazyBlock = extent.getLazyBlock(vector); BaseBlock lazyBlock = extent.getLazyBlock(vector);
BaseBlock compare = new BaseBlock(lazyBlock.getType(), lazyBlock.getData()); BaseBlock compare = new BaseBlock(lazyBlock.getType(), lazyBlock.getStates());
return Blocks.containsFuzzy(blocks, compare); return Blocks.containsFuzzy(blocks, compare);
} }
} }

View File

@ -27,7 +27,6 @@ import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BaseItem; import com.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.blocks.BaseItemStack;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.BlockType; import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.blocks.type.BlockTypes; import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.extension.platform.Platform; import com.sk89q.worldedit.extension.platform.Platform;
@ -37,15 +36,16 @@ import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.util.Direction; import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.TreeGenerator.TreeType; import com.sk89q.worldedit.util.TreeGenerator.TreeType;
import javax.annotation.Nullable;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import javax.annotation.Nullable;
/** /**
* An abstract implementation of {@link World}. * An abstract implementation of {@link World}.
*/ */
public abstract class AbstractWorld implements World { public abstract class AbstractWorld implements World {
private final PriorityQueue<QueuedEffect> effectQueue = new PriorityQueue<QueuedEffect>(); private final PriorityQueue<QueuedEffect> effectQueue = new PriorityQueue<>();
private int taskId = -1; private int taskId = -1;
@Override @Override
@ -65,7 +65,7 @@ public abstract class AbstractWorld implements World {
@Override @Override
public final void setBlockData(Vector position, int data) { public final void setBlockData(Vector position, int data) {
try { try {
setBlock(position, new BaseBlock(getLazyBlock(position).getType(), data)); setBlock(position, new BaseBlock(getLazyBlock(position).getType().getLegacyId(), data));
} catch (WorldEditException ignored) { } catch (WorldEditException ignored) {
} }
} }
@ -103,10 +103,10 @@ public abstract class AbstractWorld implements World {
@Override @Override
public Mask createLiquidMask() { public Mask createLiquidMask() {
return new BlockMask(this, return new BlockMask(this,
new BaseBlock(BlockTypes.LAVA, -1), new BaseBlock(BlockTypes.LAVA),
new BaseBlock(BlockTypes.FLOWING_LAVA, -1), new BaseBlock(BlockTypes.FLOWING_LAVA),
new BaseBlock(BlockTypes.WATER, -1), new BaseBlock(BlockTypes.WATER),
new BaseBlock(BlockTypes.FLOWING_WATER, -1)); new BaseBlock(BlockTypes.FLOWING_WATER));
} }
@Override @Override

View File

@ -21,6 +21,7 @@ package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockMaterial; import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.world.registry.state.State;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;

View File

@ -26,6 +26,8 @@ import com.google.gson.reflect.TypeToken;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockMaterial; import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.util.gson.VectorAdapter; import com.sk89q.worldedit.util.gson.VectorAdapter;
import com.sk89q.worldedit.world.registry.state.SimpleState;
import com.sk89q.worldedit.world.registry.state.State;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
@ -53,8 +55,8 @@ public class BundledBlockData {
private static final Logger log = Logger.getLogger(BundledBlockData.class.getCanonicalName()); private static final Logger log = Logger.getLogger(BundledBlockData.class.getCanonicalName());
private static final BundledBlockData INSTANCE = new BundledBlockData(); private static final BundledBlockData INSTANCE = new BundledBlockData();
private final Map<String, BlockEntry> idMap = new HashMap<String, BlockEntry>(); private final Map<String, BlockEntry> idMap = new HashMap<>();
private final Map<Integer, BlockEntry> legacyMap = new HashMap<Integer, BlockEntry>(); // Trove usage removed temporarily private final Map<Integer, BlockEntry> legacyMap = new HashMap<>(); // Trove usage removed temporarily
/** /**
* Create a new instance. * Create a new instance.
@ -84,7 +86,6 @@ public class BundledBlockData {
List<BlockEntry> entries = gson.fromJson(data, new TypeToken<List<BlockEntry>>() {}.getType()); List<BlockEntry> entries = gson.fromJson(data, new TypeToken<List<BlockEntry>>() {}.getType());
for (BlockEntry entry : entries) { for (BlockEntry entry : entries) {
entry.postDeserialization();
idMap.put(entry.id, entry); idMap.put(entry.id, entry);
legacyMap.put(entry.legacyId, entry); legacyMap.put(entry.legacyId, entry);
} }
@ -196,12 +197,6 @@ public class BundledBlockData {
private List<String> aliases; private List<String> aliases;
private Map<String, SimpleState> states = new HashMap<>(); private Map<String, SimpleState> states = new HashMap<>();
private SimpleBlockMaterial material = new SimpleBlockMaterial(); private SimpleBlockMaterial material = new SimpleBlockMaterial();
void postDeserialization() {
for (SimpleState state : states.values()) {
state.postDeserialization();
}
}
} }
} }

View File

@ -22,6 +22,7 @@ package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockMaterial; import com.sk89q.worldedit.blocks.BlockMaterial;
import com.sk89q.worldedit.blocks.type.BlockTypes; import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.world.registry.state.State;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;

View File

@ -1,71 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.registry;
import com.sk89q.worldedit.blocks.BaseBlock;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Map;
public class SimpleState implements State {
private Byte dataMask;
private Map<String, SimpleStateValue> values;
@Override
public Map<String, SimpleStateValue> valueMap() {
return Collections.unmodifiableMap(values);
}
@Nullable
@Override
public StateValue getValue(BaseBlock block) {
for (StateValue value : values.values()) {
if (value.isSet(block)) {
return value;
}
}
return null;
}
public byte getDataMask() {
return dataMask != null ? dataMask : 0xF;
}
@Override
public boolean hasDirection() {
for (SimpleStateValue value : values.values()) {
if (value.getDirection() != null) {
return true;
}
}
return false;
}
void postDeserialization() {
for (SimpleStateValue v : values.values()) {
v.setState(this);
}
}
}

View File

@ -0,0 +1,26 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.registry.state;
import com.sk89q.worldedit.world.registry.state.value.DirectionalStateValue;
public class DirectionalState extends SimpleState<DirectionalStateValue> {
}

View File

@ -0,0 +1,37 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.registry.state;
import com.sk89q.worldedit.world.registry.state.value.SimpleStateValue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SimpleState<T extends SimpleStateValue> implements State<T> {
private List<T> values = new ArrayList<>();
@Override
public List<T> getValues() {
return Collections.unmodifiableList(values);
}
}

View File

@ -17,12 +17,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.world.registry; package com.sk89q.worldedit.world.registry.state;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.world.registry.state.value.SimpleStateValue;
import javax.annotation.Nullable; import java.util.List;
import java.util.Map;
/** /**
* Describes a state property of a block. * Describes a state property of a block.
@ -30,32 +29,13 @@ import java.util.Map;
* <p>Example states include "variant" (indicating material or type) and * <p>Example states include "variant" (indicating material or type) and
* "facing" (indicating orientation).</p> * "facing" (indicating orientation).</p>
*/ */
public interface State { public interface State<T extends SimpleStateValue> {
/** /**
* Return a map of available values for this state. * Return a list of available values for this state.
* *
* <p>Keys are the value of state and map values describe that * @return the list of state values
* particular state value.</p>
*
* @return the map of state values
*/ */
Map<String, ? extends StateValue> valueMap(); List<T> getValues();
/**
* Get the value that the block is set to.
*
* @param block the block
* @return the state, otherwise null if the block isn't set to any of the values
*/
@Nullable
StateValue getValue(BaseBlock block);
/**
* Returns whether this state contains directional data.
*
* @return true if directional data is available
*/
boolean hasDirection();
} }

View File

@ -17,39 +17,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.world.registry; package com.sk89q.worldedit.world.registry.state.value;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
class SimpleStateValue implements StateValue { public class DirectionalStateValue extends SimpleStateValue {
private SimpleState state;
private Byte data;
private Vector direction;
void setState(SimpleState state) {
this.state = state;
}
@Override
public boolean isSet(BaseBlock block) {
return data != null && (block.getData() & state.getDataMask()) == data;
}
@Override
public boolean set(BaseBlock block) {
if (data != null) {
block.setData((block.getData() & ~state.getDataMask()) | data);
return true;
} else {
return false;
}
}
@Override
public Vector getDirection() { public Vector getDirection() {
return direction; return new Vector(); // TODO
} }
} }

View File

@ -0,0 +1,41 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.world.registry.state.value;
public class SimpleStateValue implements StateValue {
private String data;
@Override
public boolean isSet() {
return data != null;
}
@Override
public void set(String data) {
this.data = data;
}
@Override
public String getData() {
return this.data;
}
}

View File

@ -17,10 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.world.registry; package com.sk89q.worldedit.world.registry.state.value;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -32,25 +29,21 @@ public interface StateValue {
/** /**
* Return whether this state is set on the given block. * Return whether this state is set on the given block.
* *
* @param block the block
* @return true if this value is set * @return true if this value is set
*/ */
boolean isSet(BaseBlock block); boolean isSet();
/** /**
* Set the state to this value on the given block. * Set the state to the given value.
*
* @param block the block to change
* @return true if the value was set successfully
*/ */
boolean set(BaseBlock block); void set(String data);
/** /**
* Return the direction associated with this value. * Returns the data associated with this value.
* *
* @return the direction, otherwise null * @return The data, otherwise null
*/ */
@Nullable @Nullable
Vector getDirection(); String getData();
} }