Skip to content

Commit

Permalink
I guarantee this is broken. Start some form of string ID for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Jan 3, 2018
1 parent 50f60da commit 644d576
Show file tree
Hide file tree
Showing 39 changed files with 348 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public boolean setBlock(Vector position, BaseBlock block, boolean notifyAndLight
return adapter.setBlock(BukkitAdapter.adapt(getWorld(), position), block, notifyAndLight);
} else {
Block bukkitBlock = getWorld().getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ());
return bukkitBlock.setTypeIdAndData(block.getType(), (byte) block.getData(), notifyAndLight);
return bukkitBlock.setTypeIdAndData(block.getType().getLegacyId(), (byte) block.getData(), notifyAndLight);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ public int makeForest(Vector basePosition, int size, double density, TreeGenerat

for (int y = basePosition.getBlockY(); y >= basePosition.getBlockY() - 10; --y) {
// Check if we hit the ground
int t = getBlock(new Vector(x, y, z)).getType();
int t = getBlock(new Vector(x, y, z)).getType().getLegacyId();
if (t == BlockID.GRASS || t == BlockID.DIRT) {
treeGenerator.generate(this, new Vector(x, y + 1, z));
++affected;
Expand Down Expand Up @@ -1963,7 +1963,7 @@ protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial)
final Vector scaled = current.subtract(zero).divide(unit);

try {
if (expression.evaluate(scaled.getX(), scaled.getY(), scaled.getZ(), defaultMaterial.getType(), defaultMaterial.getData()) <= 0) {
if (expression.evaluate(scaled.getX(), scaled.getY(), scaled.getZ(), defaultMaterial.getType().getLegacyId(), defaultMaterial.getData()) <= 0) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public Set<Integer> getBlockIDs(Player player, String list, boolean allBlocksAll
String[] items = list.split(",");
Set<Integer> blocks = new HashSet<Integer>();
for (String s : items) {
blocks.add(getBlock(player, s, allBlocksAllowed).getType());
blocks.add(getBlock(player, s, allBlocksAllowed).getType().getLegacyId());
}
return blocks;
}
Expand Down
129 changes: 83 additions & 46 deletions worldedit-core/src/main/java/com/sk89q/worldedit/blocks/BaseBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
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;
Expand Down Expand Up @@ -56,15 +59,6 @@
@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.
Expand All @@ -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;
Expand All @@ -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.
*
Expand All @@ -98,8 +102,25 @@ public BaseBlock(int id) {
* @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);
}

Expand All @@ -110,12 +131,29 @@ public BaseBlock(int id, int data) {
* @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.
*
Expand All @@ -126,41 +164,48 @@ public BaseBlock(BaseBlock other) {
}

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

if (id < 0) {
throw new IllegalArgumentException("Can't have a block ID below 0");
protected final void internalSetType(BlockType type) {
if (type == null) {
throw new IllegalArgumentException("You must provide a BlockType");
}

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

/**
Expand Down Expand Up @@ -211,6 +256,7 @@ public void setData(int data) {
* @see #setData(int)
*/
@Override
@Deprecated
public void setIdAndData(int id, int data) {
setId(id);
setData(data);
Expand Down Expand Up @@ -262,17 +308,8 @@ public void setNbtData(@Nullable CompoundTag nbtData) {
*
* @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;
}

/**
Expand All @@ -281,7 +318,7 @@ public void setType(int type) {
* @return if air
*/
public boolean isAir() {
return getType() == BlockID.AIR;
return getType().getId().equals(BlockTypes.AIR);
}

/**
Expand All @@ -292,7 +329,7 @@ public boolean isAir() {
*/
@Deprecated
public int rotate90() {
int newData = BlockData.rotate90(getType(), getData());
int newData = BlockData.rotate90(getType().getLegacyId(), getData());
setData(newData);
return newData;
}
Expand All @@ -305,7 +342,7 @@ public int rotate90() {
*/
@Deprecated
public int rotate90Reverse() {
int newData = BlockData.rotate90Reverse(getType(), getData());
int newData = BlockData.rotate90Reverse(getType().getLegacyId(), getData());
setData((short) newData);
return newData;
}
Expand All @@ -319,7 +356,7 @@ public int rotate90Reverse() {
*/
@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;
}
Expand All @@ -332,7 +369,7 @@ public int cycleData(int increment) {
*/
@Deprecated
public BaseBlock flip() {
setData((short) BlockData.flip(getType(), getData()));
setData((short) BlockData.flip(getType().getLegacyId(), getData()));
return this;
}

Expand All @@ -345,7 +382,7 @@ public BaseBlock flip() {
*/
@Deprecated
public BaseBlock flip(FlipDirection direction) {
setData((short) BlockData.flip(getType(), getData(), direction));
setData((short) BlockData.flip(getType().getLegacyId(), getData(), direction));
return this;
}

Expand Down Expand Up @@ -397,14 +434,14 @@ public static boolean containsFuzzy(Collection<BaseBlock> collection, BaseBlock

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@

/**
* Block types.
*
* {@Deprecated Please use {@link com.sk89q.worldedit.blocks.type.BlockType}}
*/
@Deprecated
public enum BlockType {

AIR(BlockID.AIR, "Air", "air"),
Expand Down Expand Up @@ -477,7 +480,6 @@ public String getName() {
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);
Expand Down
Loading

0 comments on commit 644d576

Please sign in to comment.