8000
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
51 changes: 26 additions & 25 deletions src/main/java/cn/nukkit/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ public abstract class Block extends Position implements Metadatable, Cloneable,

@SuppressWarnings("UnnecessaryBoxing")
public static final int MAX_BLOCK_ID = Integer.valueOf("2048");
public static final int DATA_BITS = 6;
public static final int DATA_BITS = 13;
public static final int DATA_SIZE = 1 << DATA_BITS;
public static final int DATA_MASK = DATA_SIZE - 1;

public static final int CACHED_DATA_SIZE = 64;

public static final BlockLayer LAYER_NORMAL = BlockLayer.NORMAL;
public static final BlockLayer LAYER_WATERLOGGED = BlockLayer.WATERLOGGED;

Expand Down Expand Up @@ -70,7 +72,7 @@ protected Block() {
public static void init() {
if (list == null) {
list = new Class[MAX_BLOCK_ID];
fullList = new Block[MAX_BLOCK_ID * DATA_SIZE];
fullList = new Block[MAX_BLOCK_ID * CACHED_DATA_SIZE];
light = new int[MAX_BLOCK_ID];
lightFilter = new int[MAX_BLOCK_ID];
solid = new boolean[MAX_BLOCK_ID];
Expand All @@ -90,8 +92,8 @@ public static void init() {
@SuppressWarnings("rawtypes")
Constructor constructor = c.getDeclaredConstructor(int.class);
constructor.setAccessible(true);
for (int data = 0; data < (1 << DATA_BITS); ++data) {
int fullId = (id << DATA_BITS) | data;
for (int data = 0; data < CACHED_DATA_SIZE; ++data) {
int cacheIndex = (id * CACHED_DATA_SIZE) | data;
Block blockState;
try {
blockState = (Block) constructor.newInstance(data);
Expand All @@ -102,13 +104,12 @@ public static void init() {
Server.getInstance().getLogger().error("Error while registering " + c.getName(), e);
blockState = new BlockUnknown(id, data);
}
fullList[fullId] = blockState;
fullList[cacheIndex] = blockState;
}
hasMeta[id] = true;
} catch (NoSuchMethodException ignore) {
for (int data = 0; data < DATA_SIZE; ++data) {
int fullId = (id << DATA_BITS) | data;
fullList[fullId] = block;
for (int data = 0; data < CACHED_DATA_SIZE; ++data) {
fullList[(id * CACHED_DATA_SIZE) | data] = block;
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -139,8 +140,8 @@ public static void init() {
}
} else {
lightFilter[id] = 1;
for (int data = 0; data < DATA_SIZE; ++data) {
fullList[(id << DATA_BITS) | data] = new BlockUnknown(id, data);
for (int data = 0; data < CACHED_DATA_SIZE; ++data) {
fullList[(id * CACHED_DATA_SIZE) | data] = new BlockUnknown(id, data);
}
}
}
Expand All @@ -158,22 +159,30 @@ public static Block get(MaterialType type, Integer meta) {
return get(type.getLegacyId(), meta);
}

public static Block getPrototype(int id, int meta) {
if (meta < CACHED_DATA_SIZE) {
return fullList[(id * CACHED_DATA_SIZE) | meta];
}

Block block = fullList[id * CACHED_DATA_SIZE].clone();
block.setDamage(meta & DATA_MASK);
return block;
}

public static Block get(int id) {
if (id < 0) {
id = 255 - id;
}

return fullList[id << DATA_BITS].clone();
return fullList[id * CACHED_DATA_SIZE].clone();
}

public static Block get(int id, Integer meta) {
if (id < 0) {
id = 255 - id;
}

int fullId = meta == null ? (id << DATA_BITS ) : ((id << DATA_BITS) | meta);

return fullList[fullId].clone();
return getPrototype(id, meta == null ? 0 : meta).clone();
}

public static Block get(int id, Integer meta, Position pos) {
Expand All @@ -185,13 +194,7 @@ public static Block get(int id, Integer meta, Position pos, BlockLayer layer) {
id = 255 - id;
}

Block block;
if (meta != null && meta > DATA_SIZE) {
block = fullList[id << DATA_BITS].clone();
block.setDamage(meta);
} else {
block = fullList[(id << DATA_BITS) | (meta == null ? 0 : meta)].clone();
}
Block block = getPrototype(id, meta == null ? 0 : meta).clone();

if (pos != null) {
block.x = pos.x;
Expand All @@ -208,17 +211,15 @@ public static Block get(int id, int data) {
id = 255 - id;
}

int fullId = (id << DATA_BITS ) | data;

return fullList[fullId].clone();
return getPrototype(id, data).clone();
}

public static Block get(int fullId, Level level, int x, int y, int z) {
return get(fullId, level, x, y, z, LAYER_NORMAL);
}

public static Block get(int fullId, Level level, int x, int y, int z, BlockLayer layer) {
Block block = fullList[fullId].clone();
Block block = getPrototype(fullId >> DATA_BITS, fullId & DATA_MASK).clone();

block.x = x;
block.y = y;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockBlackstoneWall.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.nukkit.block;

public class BlockBlackstoneWall extends BlockWall {
public class BlockBlackstoneWall extends BlockWallIndependentID {

public BlockBlackstoneWall() {
this(0);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockMudBrickWall.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.nukkit.item.Item;
import cn.nukkit.utils.BlockColor;

public class BlockMudBrickWall extends BlockWall {
public class BlockMudBrickWall extends BlockWallIndependentID {

public BlockMudBrickWall() {
this(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.nukkit.block;

public class BlockPolishedBlackstoneBrickWall extends BlockWall {
public class BlockPolishedBlackstoneBrickWall extends BlockWallIndependentID {

public BlockPolishedBlackstoneBrickWall() {
this(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.nukkit.block;

public class BlockPolishedBlackstoneWall extends BlockWall {
public class BlockPolishedBlackstoneWall extends BlockWallIndependentID {

public BlockPolishedBlackstoneWall() {
this(0);
Expand Down
Loading
Loading
0