Small optimizations; use array for Java -> Bedrock block mappings

Java runtime IDs are in order starting from 0; an array is all that is needed to map Java IDs to Bedrock IDs.
This commit is contained in:
Camotoy 2021-08-27 18:08:49 -04:00
parent 3c18eb44aa
commit bb92c89273
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 19 additions and 11 deletions

View file

@ -32,6 +32,8 @@ import com.nukkitx.protocol.bedrock.v440.Bedrock_v440;
import com.nukkitx.protocol.bedrock.v448.Bedrock_v448;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
@ -137,7 +139,7 @@ public class BlockRegistryPopulator {
BiFunction<String, NbtMapBuilder, String> stateMapper = STATE_MAPPER.getOrDefault(palette.getKey(), (i, s) -> null);
Int2IntMap javaToBedrockBlockMap = new Int2IntOpenHashMap();
IntList javaToBedrockBlockMap = new IntArrayList();
Int2IntMap bedrockToJavaBlockMap = new Int2IntOpenHashMap();
Map<String, NbtMap> flowerPotBlocks = new Object2ObjectOpenHashMap<>();
@ -189,7 +191,7 @@ public class BlockRegistryPopulator {
javaIdentifierToBedrockTag.put(cleanJavaIdentifier.intern(), blocksTag.get(bedrockRuntimeId));
}
javaToBedrockBlockMap.put(javaRuntimeId, bedrockRuntimeId);
javaToBedrockBlockMap.add(bedrockRuntimeId);
}
if (commandBlockRuntimeId == -1) {
@ -218,7 +220,7 @@ public class BlockRegistryPopulator {
BlockRegistries.BLOCKS.register(PALETTE_VERSIONS.getInt(palette.getKey()), builder.blockStateVersion(stateVersion)
.emptyChunkSection(new ChunkSection(new BlockStorage[]{new BlockStorage(airRuntimeId)}))
.javaToBedrockBlockMap(javaToBedrockBlockMap)
.javaToBedrockBlockMap(javaToBedrockBlockMap.toIntArray())
.bedrockToJavaBlockMap(bedrockToJavaBlockMap)
.javaIdentifierToBedrockTag(javaIdentifierToBedrockTag)
.itemFrames(itemFrames)

View file

@ -45,7 +45,7 @@ public class BlockMappings {
ChunkSection emptyChunkSection;
Int2IntMap javaToBedrockBlockMap;
int[] javaToBedrockBlockMap;
Int2IntMap bedrockToJavaBlockMap;
NbtList<NbtMap> bedrockBlockStates;
@ -62,7 +62,10 @@ public class BlockMappings {
Map<String, NbtMap> flowerPotBlocks;
public int getBedrockBlockId(int state) {
return this.javaToBedrockBlockMap.get(state);
if (state >= this.javaToBedrockBlockMap.length) {
return bedrockAirId;
}
return this.javaToBedrockBlockMap[state];
}
public int getJavaBlockState(int bedrockId) {

View file

@ -257,8 +257,9 @@ public class ChunkUtils {
while (i < blockEntities.length) {
CompoundTag tag = blockEntities[i];
String tagName;
if (tag.contains("id")) {
tagName = (String) tag.get("id").getValue();
Tag idTag = tag.get("id");
if (idTag != null) {
tagName = (String) idTag.getValue();
} else {
tagName = "Empty";
// Sometimes legacy tags have their ID be a StringTag with empty value
@ -277,18 +278,20 @@ public class ChunkUtils {
}
String id = BlockEntityUtils.getBedrockBlockEntityId(tagName);
Position pos = new Position((int) tag.get("x").getValue(), (int) tag.get("y").getValue(), (int) tag.get("z").getValue());
int x = (int) tag.get("x").getValue();
int y = (int) tag.get("y").getValue();
int z = (int) tag.get("z").getValue();
// Get Java blockstate ID from block entity position
int blockState = 0;
Chunk section = column.getChunks()[(pos.getY() >> 4) - yOffset];
Chunk section = column.getChunks()[(y >> 4) - yOffset];
if (section != null) {
blockState = section.get(pos.getX() & 0xF, pos.getY() & 0xF, pos.getZ() & 0xF);
blockState = section.get(x & 0xF, y & 0xF, z & 0xF);
}
if (tagName.equals("minecraft:lectern") && BlockStateValues.getLecternBookStates().get(blockState)) {
// If getLecternBookStates is false, let's just treat it like a normal block entity
bedrockBlockEntities[i] = session.getConnector().getWorldManager().getLecternDataAt(session, pos.getX(), pos.getY(), pos.getZ(), true);
bedrockBlockEntities[i] = session.getConnector().getWorldManager().getLecternDataAt(session, x, y, z, true);
i++;
continue;
}