Further BaseBlock modernisation

This commit is contained in:
Matthew Miller
2018-06-18 17:53:33 +10:00
parent 811f1d4433
commit e99190225e
61 changed files with 344 additions and 787 deletions

View File

@@ -24,19 +24,18 @@ import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
import com.sk89q.worldedit.blocks.type.BlockState;
import com.sk89q.worldedit.blocks.type.BlockStateHolder;
import com.sk89q.worldedit.blocks.type.BlockType;
import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.world.registry.BundledBlockData;
import com.sk89q.worldedit.world.registry.state.State;
import com.sk89q.worldedit.world.registry.state.value.StateValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
@@ -54,13 +53,12 @@ import javax.annotation.Nullable;
* as a "wildcard" block value, even though a {@link Mask} would be
* more appropriate.</p>
*/
public class BaseBlock implements TileEntityBlock {
public class BaseBlock implements BlockStateHolder<BaseBlock>, TileEntityBlock {
// Instances of this class should be _as small as possible_ because there will
// be millions of instances of this object.
private BlockType blockType;
private Map<State, StateValue> states;
private BlockState blockState;
@Nullable
private CompoundTag nbtData;
@@ -68,18 +66,18 @@ public class BaseBlock implements TileEntityBlock {
* Construct a block with the given ID and a data value of 0.
*
* @param id ID value
* @see #setId(int)
*/
@Deprecated
public BaseBlock(int id) {
internalSetId(id);
internalSetData(0);
this.states = new HashMap<>();
}
/**
* Construct a block with a state.
*
* @param blockState The blockstate
*/
public BaseBlock(BlockState blockState) {
this.blockType = blockState.getBlockType();
this.states = blockState.getStates();
this.blockState = blockState;
}
/**
@@ -88,8 +86,18 @@ public class BaseBlock implements TileEntityBlock {
* @param blockType The block type
*/
public BaseBlock(BlockType blockType) {
internalSetType(blockType);
this.states = new HashMap<>();
this.blockState = blockType.getDefaultState();
}
/**
* Construct a block with the given ID, data value and NBT data structure.
*
* @param state The block state
* @param nbtData NBT data, which may be null
*/
public BaseBlock(BlockState state, @Nullable CompoundTag nbtData) {
this.blockState = state;
setNbtData(nbtData);
}
/**
@@ -97,27 +105,9 @@ public class BaseBlock implements TileEntityBlock {
*
* @param id ID value
* @param data data value
* @see #setId(int)
* @see #setData(int)
*/
@Deprecated
public BaseBlock(int id, int data) {
internalSetId(id);
internalSetData(data);
this.states = new HashMap<>();
}
/**
* Construct a block with the given ID and data value.
*
* @param blockType The block type
* @param states The states
* @see #setId(int)
* @see #setData(int)
*/
public BaseBlock(BlockType blockType, Map<State, StateValue> states) {
internalSetType(blockType);
setStates(states);
}
/**
@@ -129,22 +119,6 @@ public class BaseBlock implements TileEntityBlock {
*/
@Deprecated
public BaseBlock(int id, int data, @Nullable CompoundTag nbtData) {
internalSetId(id);
setData(data);
setNbtData(nbtData);
this.states = new HashMap<>();
}
/**
* Construct a block with the given ID, data value and NBT data structure.
*
* @param blockType The block type
* @param states The states
* @param nbtData NBT data, which may be null
*/
public BaseBlock(BlockType blockType, Map<State, StateValue> states, @Nullable CompoundTag nbtData) {
setType(blockType);
setStates(states);
setNbtData(nbtData);
}
@@ -154,7 +128,16 @@ public class BaseBlock implements TileEntityBlock {
* @param other the other block
*/
public BaseBlock(BaseBlock other) {
this(other.getType(), other.getStates(), other.getNbtData());
this(other.getState(), other.getNbtData());
}
/**
* Get the block state
*
* @return The block state
*/
public BlockState getState() {
return this.blockState;
}
/**
@@ -164,45 +147,7 @@ public class BaseBlock implements TileEntityBlock {
*/
@Deprecated
public int getId() {
return this.blockType.getLegacyId();
}
/**
* Set the block ID.
*
* @param type block type
*/
protected final void internalSetType(BlockType type) {
if (type == null) {
throw new IllegalArgumentException("You must provide a BlockType");
}
this.blockType = type;
}
/**
* Set the block ID.
*
* @param id block id
*/
@Deprecated
public void setId(int id) {
internalSetId(id);
}
@Deprecated
private void internalSetId(int id) {
BlockType type = BlockTypes.getBlockType(BundledBlockData.getInstance().fromLegacyId(id));
internalSetType(type);
}
/**
* Set the block type.
*
* @param type block type
*/
public void setType(BlockType type) {
internalSetType(type);
return this.blockState.getBlockType().getLegacyId();
}
/**
@@ -223,16 +168,17 @@ public class BaseBlock implements TileEntityBlock {
* @return The state map
*/
public Map<State, StateValue> getStates() {
return Collections.unmodifiableMap(states);
return this.blockState.getStates();
}
/**
* Sets the states of this block.
*
* @param states The states
*/
private void setStates(Map<State, StateValue> states) {
this.states = states;
@Override
public BlockType getBlockType() {
return this.blockState.getBlockType();
}
@Override
public BaseBlock with(State state, StateValue value) {
return new BaseBlock(this.blockState.with(state, value), getNbtData());
}
/**
@@ -242,26 +188,7 @@ public class BaseBlock implements TileEntityBlock {
* @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.
*
* @param data block data value
*/
@Deprecated
protected final void internalSetData(int data) {
return this.blockState.getState(state);
}
/**
@@ -271,34 +198,6 @@ public class BaseBlock implements TileEntityBlock {
*/
@Deprecated
public void setData(int data) {
internalSetData(data);
}
/**
* Set both the block's ID and data value.
*
* @param id ID value
* @param data data value
* @see #setId(int)
* @see #setData(int)
*/
@Deprecated
public void setIdAndData(int id, int data) {
setId(id);
setData(data);
}
/**
* Returns whether there are no matched states.
*
* @return true if there are no matched states
*/
public boolean hasWildcardData() {
return getStates().isEmpty();
}
public boolean hasWildcardDataFor(State state) {
return getState(state) == null;
}
@Override
@@ -323,21 +222,12 @@ public class BaseBlock implements TileEntityBlock {
@Nullable
@Override
public CompoundTag getNbtData() {
return nbtData;
return this.nbtData;
}
@Override
public void setNbtData(@Nullable CompoundTag nbtData) {
this.nbtData = nbtData;
}
/**
* Get the type of block.
*
* @return the type
*/
public BlockType getType() {
return this.blockType;
throw new UnsupportedOperationException("This class is immutable.");
}
/**
@@ -346,7 +236,7 @@ public class BaseBlock implements TileEntityBlock {
* @return if air
*/
public boolean isAir() {
return getType() == BlockTypes.AIR;
return getBlockType() == BlockTypes.AIR;
}
/**
@@ -357,7 +247,7 @@ public class BaseBlock implements TileEntityBlock {
*/
@Deprecated
public int rotate90() {
int newData = BlockData.rotate90(getType().getLegacyId(), getData());
int newData = BlockData.rotate90(getBlockType().getLegacyId(), getData());
setData(newData);
return newData;
}
@@ -370,7 +260,7 @@ public class BaseBlock implements TileEntityBlock {
*/
@Deprecated
public int rotate90Reverse() {
int newData = BlockData.rotate90Reverse(getType().getLegacyId(), getData());
int newData = BlockData.rotate90Reverse(getBlockType().getLegacyId(), getData());
setData((short) newData);
return newData;
}
@@ -384,7 +274,7 @@ public class BaseBlock implements TileEntityBlock {
*/
@Deprecated
public int cycleData(int increment) {
int newData = BlockData.cycle(getType().getLegacyId(), getData(), increment);
int newData = BlockData.cycle(getBlockType().getLegacyId(), getData(), increment);
setData((short) newData);
return newData;
}
@@ -397,7 +287,7 @@ public class BaseBlock implements TileEntityBlock {
*/
@Deprecated
public BaseBlock flip() {
setData((short) BlockData.flip(getType().getLegacyId(), getData()));
setData((short) BlockData.flip(getBlockType().getLegacyId(), getData()));
return this;
}
@@ -410,7 +300,7 @@ public class BaseBlock implements TileEntityBlock {
*/
@Deprecated
public BaseBlock flip(FlipDirection direction) {
setData((short) BlockData.flip(getType().getLegacyId(), getData(), direction));
setData((short) BlockData.flip(getBlockType().getLegacyId(), getData(), direction));
return this;
}
@@ -425,7 +315,7 @@ public class BaseBlock implements TileEntityBlock {
final BaseBlock otherBlock = (BaseBlock) o;
return getType() == otherBlock.getType() && getData() == otherBlock.getData();
return this.getState().equals(otherBlock.getState()) && Objects.equals(getNbtData(), otherBlock.getNbtData());
}
@@ -436,7 +326,7 @@ public class BaseBlock implements TileEntityBlock {
* @return true if equal
*/
public boolean equalsFuzzy(BaseBlock o) {
if (!getType().equals(o.getType())) {
if (!getBlockType().equals(o.getBlockType())) {
return false;
}
@@ -484,14 +374,16 @@ public class BaseBlock implements TileEntityBlock {
@Override
public int hashCode() {
int ret = getType().hashCode() << 3;
ret += getStates().hashCode();
int ret = getState().hashCode() << 3;
if (hasNbtData()) {
ret += getNbtData().hashCode();
}
return ret;
}
@Override
public String toString() {
return "Block{Type:" + getType().getId() + ", States: " + getStates().toString() + "}";
return "Block{State: " + this.getState().toString() + ", NBT: " + String.valueOf(getNbtData()) + "}";
}
}

View File

@@ -19,12 +19,11 @@
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.blocks.type.ItemType;
import com.sk89q.worldedit.blocks.type.ItemTypes;
import com.sk89q.worldedit.world.registry.BundledItemData;
import com.sk89q.worldedit.world.NbtValued;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Represents an item, without an amount value. See {@link BaseItemStack}
@@ -32,11 +31,11 @@ import java.util.Map;
*
* <p>This class may be removed in the future.</p>
*/
public class BaseItem {
public class BaseItem implements NbtValued {
private ItemType itemType;
private short damage;
private final Map<Integer, Integer> enchantments = new HashMap<>();
@Nullable
private CompoundTag nbtData;
/**
* Construct the object.
@@ -45,7 +44,6 @@ public class BaseItem {
*/
@Deprecated
public BaseItem(int id) {
this(id, (short) 0);
}
/**
@@ -57,27 +55,15 @@ public class BaseItem {
this.itemType = itemType;
}
/**
* Construct the object.
*
* @param id ID of the item
* @param data data value of the item
*/
@Deprecated
public BaseItem(int id, short data) {
setLegacyId(id);
this.damage = data;
}
/**
* Construct the object.
*
* @param itemType Type of the item
* @param damage Damage value of the item
* @param tag NBT Compound tag
*/
public BaseItem(ItemType itemType, short damage) {
public BaseItem(ItemType itemType, CompoundTag tag) {
this.itemType = itemType;
this.damage = damage;
this.nbtData = tag;
}
/**
@@ -90,17 +76,6 @@ public class BaseItem {
return this.itemType.getLegacyId();
}
/**
* Set the type of item.
*
* @param id the id to set
*/
@Deprecated
public void setLegacyId(int id) {
ItemType type = ItemTypes.getItemType(BundledItemData.getInstance().fromLegacyId(id));
setType(type);
}
/**
* Get the type of item.
*
@@ -119,50 +94,19 @@ public class BaseItem {
this.itemType = itemType;
}
/**
* Get the damage value.
*
* @return the damage
*/
public short getDamage() {
return this.damage;
@Override
public boolean hasNbtData() {
return this.nbtData != null;
}
/**
* Get the data value.
*
* @return the data
*/
@Deprecated
public short getData() {
return this.damage;
@Nullable
@Override
public CompoundTag getNbtData() {
return this.nbtData;
}
/**
* Set the data value.
*
* @param damage the damage to set
*/
public void setDamage(short damage) {
this.damage = damage;
}
/**
* Set the data value.
*
* @param data the damage to set
*/
@Deprecated
public void setData(short data) {
this.damage = data;
}
/**
* Get the map of enchantments.
*
* @return map of enchantments
*/
public Map<Integer, Integer> getEnchantments() {
return enchantments;
@Override
public void setNbtData(@Nullable CompoundTag nbtData) {
this.nbtData = nbtData;
}
}

View File

@@ -19,6 +19,7 @@
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.blocks.type.ItemType;
/**
@@ -81,7 +82,7 @@ public class BaseItemStack extends BaseItem {
*/
@Deprecated
public BaseItemStack(int id, int amount, short data) {
super(id, data);
super(id);
this.amount = amount;
}
@@ -89,11 +90,11 @@ public class BaseItemStack extends BaseItem {
* Construct the object.
*
* @param id The item type
* @param tag Tag value
* @param amount amount in the stack
* @param damage Damage value
*/
public BaseItemStack(ItemType id, int amount, short damage) {
super(id, damage);
public BaseItemStack(ItemType id, CompoundTag tag, int amount) {
super(id, tag);
this.amount = amount;
}

View File

@@ -1540,7 +1540,7 @@ public enum BlockType {
addIdentities(BlockID.DOUBLE_WOODEN_STEP, 7); // rule 3
addIdentities(BlockID.WOODEN_STEP, 7); // rule 1
nonDataBlockBagItems.put(BlockID.COCOA_PLANT, new BaseItem(ItemID.INK_SACK, (short) (15 - ClothColor.ID.BROWN))); // rule 3
nonDataBlockBagItems.put(BlockID.COCOA_PLANT, new BaseItem(ItemID.INK_SACK)); // rule 3 TODO data removed
addIdentity(BlockID.SANDSTONE_STAIRS); // rule 1
nonDataBlockBagItems.put(BlockID.EMERALD_ORE, new BaseItem(ItemID.EMERALD)); // rule 5
addIdentity(BlockID.ENDER_CHEST); // rule 3
@@ -1569,7 +1569,8 @@ public enum BlockType {
addIdentity(BlockID.HOPPER); // rule 1
addIdentities(BlockID.QUARTZ_BLOCK, 1); // rule 4
for (int i = 2; i <= 4; i++) {
dataBlockBagItems.put(typeDataKey(BlockID.QUARTZ_BLOCK, i), new BaseItem(BlockID.QUARTZ_BLOCK, (short) 2)); // rule 4, quartz pillars
dataBlockBagItems.put(typeDataKey(BlockID.QUARTZ_BLOCK, i), new BaseItem(BlockID.QUARTZ_BLOCK)); // rule 4, quartz pillars TODO data
// removed
}
addIdentity(BlockID.QUARTZ_STAIRS); // rule 1
addIdentity(BlockID.ACTIVATOR_RAIL); // rule 1
@@ -1629,7 +1630,7 @@ public enum BlockType {
private static void addIdentities(int type, int maxData) {
for (int data = 0; data < maxData; ++data) {
dataBlockBagItems.put(typeDataKey(type, data), new BaseItem(type, (short) data));
dataBlockBagItems.put(typeDataKey(type, data), new BaseItem(type)); // TODO data removed
}
}

View File

@@ -34,7 +34,7 @@ import java.util.Map;
* An immutable class that represents the state a block can be in.
*/
@SuppressWarnings("unchecked")
public class BlockState {
public class BlockState implements BlockStateHolder<BlockState> {
private final BlockType blockType;
private final Map<State, StateValue> values;
@@ -67,7 +67,7 @@ public class BlockState {
for(final Map.Entry<State, StateValue> entry : this.values.entrySet()) {
final State state = entry.getKey();
state.getValues().stream().forEach(value -> {
state.getValues().forEach(value -> {
if(value != entry.getValue()) {
states.put(state, (StateValue) value, stateMap.get(this.withValue(state, (StateValue) value)));
}
@@ -83,22 +83,12 @@ public class BlockState {
return values;
}
/**
* Get the block type
*
* @return The type
*/
@Override
public BlockType getBlockType() {
return this.blockType;
}
/**
* Returns a BlockState with the given state and value applied.
*
* @param state The state
* @param value The value
* @return The modified state, or same if could not be applied
*/
@Override
public BlockState with(State state, StateValue value) {
if (fuzzy) {
return setState(state, value);
@@ -108,21 +98,12 @@ public class BlockState {
}
}
/**
* Gets the value at the given state
*
* @param state The state
* @return The value
*/
@Override
public StateValue getState(State state) {
return this.values.get(state);
}
/**
* Gets an immutable collection of the states.
*
* @return The states
*/
@Override
public Map<State, StateValue> getStates() {
return Collections.unmodifiableMap(this.values);
}
@@ -136,7 +117,7 @@ public class BlockState {
* @param value The value
* @return The blockstate, for chaining
*/
BlockState setState(State state, StateValue value) {
private BlockState setState(State state, StateValue value) {
this.values.put(state, value);
return this;
}

View File

@@ -0,0 +1,59 @@
/*
* 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.blocks.type;
import com.sk89q.worldedit.world.registry.state.State;
import com.sk89q.worldedit.world.registry.state.value.StateValue;
import java.util.Map;
public interface BlockStateHolder<T extends BlockStateHolder> {
/**
* Get the block type
*
* @return The type
*/
BlockType getBlockType();
/**
* Returns a BlockState with the given state and value applied.
*
* @param state The state
* @param value The value
* @return The modified state, or same if could not be applied
*/
T with(State state, StateValue value);
/**
* Gets the value at the given state
*
* @param state The state
* @return The value
*/
StateValue getState(State state);
/**
* Gets an immutable collection of the states.
*
* @return The states
*/
Map<State, StateValue> getStates();
}