I guarantee this is broken. Start some form of string ID for blocks

This commit is contained in:
Matthew Miller
2018-01-03 15:35:51 +10:00
parent e6c6a2cfea
commit 41a80064f5
39 changed files with 349 additions and 118 deletions

View File

@@ -23,8 +23,11 @@ import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
import com.sk89q.worldedit.blocks.type.BlockType;
import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.foundation.Block;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.world.registry.BundledBlockData;
import com.sk89q.worldedit.world.registry.WorldData;
import javax.annotation.Nullable;
@@ -56,15 +59,6 @@ import java.util.Collection;
@SuppressWarnings("deprecation")
public class BaseBlock extends Block implements TileEntityBlock {
/**
* Indicates the highest possible block ID (inclusive) that can be used.
* This value is subject to change depending on the implementation, but
* internally this class only supports a range of 4096 IDs (for space
* reasons), which coincides with the number of possible IDs that official
* Minecraft supports as of version 1.7.
*/
public static final int MAX_ID = 4095;
/**
* Indicates the maximum data value (inclusive) that can be used. A future
* version of Minecraft may abolish block data values.
@@ -74,7 +68,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
// Instances of this class should be _as small as possible_ because there will
// be millions of instances of this object.
private short id;
private BlockType blockType;
private short data;
@Nullable
private CompoundTag nbtData;
@@ -85,11 +79,21 @@ public class BaseBlock extends Block implements TileEntityBlock {
* @param id ID value
* @see #setId(int)
*/
@Deprecated
public BaseBlock(int id) {
internalSetId(id);
setId(id);
internalSetData(0);
}
/**
* Construct a block with the given type and default data.
*
* @param blockType The block type
*/
public BaseBlock(BlockType blockType) {
internalSetType(blockType);
}
/**
* Construct a block with the given ID and data value.
*
@@ -98,8 +102,25 @@ public class BaseBlock extends Block implements TileEntityBlock {
* @see #setId(int)
* @see #setData(int)
*/
@Deprecated
public BaseBlock(int id, int data) {
internalSetId(id);
setId(id);
internalSetData(data);
}
/**
* Construct a block with the given ID and data value.
*
* THIS WILL GET REMOVED SOON.
*
* @param blockType The block type
* @param data data value
* @see #setId(int)
* @see #setData(int)
*/
@Deprecated
public BaseBlock(BlockType blockType, int data) {
internalSetType(blockType);
internalSetData(data);
}
@@ -110,12 +131,29 @@ public class BaseBlock extends Block implements TileEntityBlock {
* @param data data value
* @param nbtData NBT data, which may be null
*/
@Deprecated
public BaseBlock(int id, int data, @Nullable CompoundTag nbtData) {
setId(id);
setData(data);
setNbtData(nbtData);
}
/**
* Construct a block with the given ID, data value and NBT data structure.
*
* THIS WILL GET REMOVED SOON.
*
* @param blockType The block type
* @param data data value
* @param nbtData NBT data, which may be null
*/
@Deprecated
public BaseBlock(BlockType blockType, int data, @Nullable CompoundTag nbtData) {
setType(blockType);
setData(data);
setNbtData(nbtData);
}
/**
* Create a clone of another block.
*
@@ -126,41 +164,48 @@ public class BaseBlock extends Block implements TileEntityBlock {
}
/**
* Get the ID of the block.
* Get the legacy numerical ID of the block.
*
* @return ID (between 0 and {@link #MAX_ID})
* @return legacy numerical ID
*/
@Override
@Deprecated
public int getId() {
return id;
return this.blockType.getLegacyId();
}
/**
* Set the block ID.
*
* @param id block id (between 0 and {@link #MAX_ID}).
* @param type block type
*/
protected final void internalSetId(int id) {
if (id > MAX_ID) {
throw new IllegalArgumentException("Can't have a block ID above "
+ MAX_ID + " (" + id + " given)");
protected final void internalSetType(BlockType type) {
if (type == null) {
throw new IllegalArgumentException("You must provide a BlockType");
}
if (id < 0) {
throw new IllegalArgumentException("Can't have a block ID below 0");
}
this.id = (short) id;
this.blockType = type;
}
/**
* Set the block ID.
*
* @param id block id (between 0 and {@link #MAX_ID}).
* @param id block id
*/
@Override
@Deprecated
public void setId(int id) {
internalSetId(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);
}
/**
@@ -211,6 +256,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
* @see #setData(int)
*/
@Override
@Deprecated
public void setIdAndData(int id, int data) {
setId(id);
setData(data);
@@ -262,17 +308,8 @@ public class BaseBlock extends Block implements TileEntityBlock {
*
* @return the type
*/
public int getType() {
return getId();
}
/**
* Set the type of block.
*
* @param type the type to set
*/
public void setType(int type) {
setId(type);
public BlockType getType() {
return this.blockType;
}
/**
@@ -281,7 +318,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
* @return if air
*/
public boolean isAir() {
return getType() == BlockID.AIR;
return getType().getId().equals(BlockTypes.AIR);
}
/**
@@ -292,7 +329,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
*/
@Deprecated
public int rotate90() {
int newData = BlockData.rotate90(getType(), getData());
int newData = BlockData.rotate90(getType().getLegacyId(), getData());
setData(newData);
return newData;
}
@@ -305,7 +342,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
*/
@Deprecated
public int rotate90Reverse() {
int newData = BlockData.rotate90Reverse(getType(), getData());
int newData = BlockData.rotate90Reverse(getType().getLegacyId(), getData());
setData((short) newData);
return newData;
}
@@ -319,7 +356,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
*/
@Deprecated
public int cycleData(int increment) {
int newData = BlockData.cycle(getType(), getData(), increment);
int newData = BlockData.cycle(getType().getLegacyId(), getData(), increment);
setData((short) newData);
return newData;
}
@@ -332,7 +369,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
*/
@Deprecated
public BaseBlock flip() {
setData((short) BlockData.flip(getType(), getData()));
setData((short) BlockData.flip(getType().getLegacyId(), getData()));
return this;
}
@@ -345,7 +382,7 @@ public class BaseBlock extends Block implements TileEntityBlock {
*/
@Deprecated
public BaseBlock flip(FlipDirection direction) {
setData((short) BlockData.flip(getType(), getData(), direction));
setData((short) BlockData.flip(getType().getLegacyId(), getData(), direction));
return this;
}
@@ -397,14 +434,14 @@ public class BaseBlock extends Block implements TileEntityBlock {
@Override
public int hashCode() {
int ret = getId() << 3;
int ret = getType().hashCode() << 3;
if (getData() != (byte) -1) ret |= getData();
return ret;
}
@Override
public String toString() {
return "Block{ID:" + getId() + ", Data: " + getData() + "}";
return "Block{Type:" + getType().getId() + ", Data: " + getData() + "}";
}
}

View File

@@ -35,7 +35,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* Block types.
*
* {@Deprecated Please use {@link com.sk89q.worldedit.blocks.type.BlockType}}
*/
@Deprecated
public enum BlockType {
AIR(BlockID.AIR, "Air", "air"),
@@ -477,7 +480,6 @@ public enum BlockType {
shouldPlaceLast.add(BlockID.DETECTOR_RAIL);
shouldPlaceLast.add(BlockID.LONG_GRASS);
shouldPlaceLast.add(BlockID.DEAD_BUSH);
shouldPlaceLast.add(BlockID.PISTON_EXTENSION);
shouldPlaceLast.add(BlockID.YELLOW_FLOWER);
shouldPlaceLast.add(BlockID.RED_FLOWER);
shouldPlaceLast.add(BlockID.BROWN_MUSHROOM);

View File

@@ -0,0 +1,66 @@
/*
* 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.BundledBlockData;
public class BlockType {
private String id;
public BlockType(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
/**
* Gets the legacy ID. Needed for legacy reasons.
*
* DO NOT USE THIS.
*
* @return legacy id or 0, if unknown
*/
@Deprecated
public int getLegacyId() {
Integer id = BundledBlockData.getInstance().toLegacyId(this.id);
if (id != null) {
return id;
} else {
return 0;
}
}
public com.sk89q.worldedit.blocks.BlockType getLegacyType() {
return com.sk89q.worldedit.blocks.BlockType.fromID(getLegacyId());
}
@Override
public int hashCode() {
return this.id.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof BlockType && this.id.equals(((BlockType) obj).id);
}
}

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 java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Stores a list of common Block String IDs.
*/
public class BlockTypes {
private BlockTypes() {
}
public static final String AIR = "minecraft:air";
public static final String GRASS = "minecraft:grass";
public static final String WATER = "minecraft:water";
public static final String LAVA = "minecraft:lava";
public static final String WOOL = "minecraft:wool";
public static final String STATIONARY_WATER = "minecraft:stationary_water";
public static final String STATIONARY_LAVA = "minecraft:stationary_lava";
public static final String WALL_SIGN = "minecraft:wall_sign";
public static final String SIGN_POST = "minecraft:sign_post";
private static final Map<String, BlockType> blockMapping = new HashMap<>();
public static void registerBlock(BlockType blockType) {
if (blockMapping.containsKey(blockType.getId())) {
throw new IllegalArgumentException("Existing block with this ID already registered");
}
blockMapping.put(blockType.getId(), blockType);
}
@Nullable
public static BlockType getBlockType(String id) {
return blockMapping.get(id);
}
}