Skip to content

Commit

Permalink
fix: chest pairing
Browse files Browse the repository at this point in the history
  • Loading branch information
lt-name committed Nov 21, 2024
1 parent 8470a57 commit 8a96d42
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
44 changes: 32 additions & 12 deletions src/main/java/cn/nukkit/level/BlockPalette.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntMaps;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import lombok.extern.log4j.Log4j2;

import java.io.BufferedInputStream;
Expand Down Expand Up @@ -61,17 +62,36 @@ private ListTag<CompoundTag> paletteFor(int protocol) {
}

private void loadBlockStates(ListTag<CompoundTag> blockStates) {
List<CompoundTag> stateOverloads = new ObjectArrayList<>();
for (CompoundTag state : blockStates.getAll()) {
int id = state.getInt("id");
int data = state.getShort("data");
int runtimeId = state.getInt("runtimeId");
int legacyId = id << 6 | data;
legacyToRuntimeId.put(legacyId, runtimeId);
if (!runtimeIdToLegacy.containsKey(runtimeId)) {
runtimeIdToLegacy.put(runtimeId, legacyId);
if (!this.registerBlockState(state, false)) {
stateOverloads.add(state);
}
stateToLegacy.put(state, legacyId);
}

for (CompoundTag state : stateOverloads) {
log.info("[{}] Registering block palette overload: {}", this.getProtocol(), state.getString("name"));
this.registerBlockState(state, true);
}
}

private boolean registerBlockState(CompoundTag state, boolean force) {
int id = state.getInt("id");
int data = state.getShort("data");
int runtimeId = state.getInt("runtimeId");
boolean stateOverload = state.getBoolean("stateOverload");

if (stateOverload && !force) {
return false;
}

CompoundTag vanillaState = state
.remove("id")
.remove("data")
.remove("runtimeId")
.remove("stateOverload");
this.registerState(id, data, runtimeId, vanillaState);
return true;
}

/**
Expand All @@ -88,7 +108,7 @@ private void loadBlockStatesExtras() {
int id = Utils.toInt(map.get("id"));
int data = Utils.toInt(map.getOrDefault("data", 0));
int runtimeId = Utils.toInt(map.get("runtimeId"));
int legacyId = id << 6 | data;
int legacyId = id << Block.DATA_BITS | data;
legacyToRuntimeId.put(legacyId, runtimeId);
if (!runtimeIdToLegacy.containsKey(runtimeId)) {
runtimeIdToLegacy.put(runtimeId, legacyId);
Expand Down Expand Up @@ -153,16 +173,16 @@ public int getRuntimeId(int id) {
}

public int getRuntimeId(int id, int meta) {
int legacyId = protocol >= 388 ? ((id << 6) | meta) : ((id << 4) | meta);
int legacyId = protocol >= 388 ? ((id << Block.DATA_BITS) | meta) : ((id << 4) | meta);
int runtimeId;
runtimeId = legacyToRuntimeId.get(legacyId);
if (runtimeId == -1) {
runtimeId = legacyToRuntimeId.get(id << 6);
runtimeId = legacyToRuntimeId.get(id << Block.DATA_BITS);
if (runtimeId == -1) {
Integer cache = legacyToRuntimeIdCache.getIfPresent(legacyId);
if (cache == null) {
log.info("(" + protocol + ") Missing block runtime id mappings for " + id + ':' + meta);
runtimeId = legacyToRuntimeId.get(BlockID.INFO_UPDATE << 6);
runtimeId = legacyToRuntimeId.get(BlockID.INFO_UPDATE << Block.DATA_BITS);
legacyToRuntimeIdCache.put(legacyId, runtimeId);
} else {
runtimeId = cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public BlockStateSnapshot getState(int legacyId, int data) {
public BlockStateSnapshot getState(int runtimeId) {
BlockStateSnapshot blockStateSnapshot = this.runtime2State.get(runtimeId);
if (blockStateSnapshot == null) {
log.warn("Can not find state! No runtime2State mapping for " + runtimeId);
log.warn("Can not find state! No runtime2State mapping for {}", runtimeId);
return this.getDefaultState();
}
return blockStateSnapshot;
Expand Down Expand Up @@ -203,7 +203,7 @@ public BlockStateSnapshot getBlockState(NbtMap tag, NbtMap newTag) {
public int getRuntimeId(int legacyId, int data) {
int runtimeId = this.legacyMapper.legacyToRuntime(legacyId, data);
if (runtimeId == -1) {
log.warn("Can not find runtimeId! No legacy2runtime mapping for " + legacyId + ":" + data);
log.warn("Can not find runtimeId! No legacy2runtime mapping for {}:{}", legacyId, data);
return this.getDefaultRuntimeId();
}
return runtimeId;
Expand All @@ -212,7 +212,7 @@ public int getRuntimeId(int legacyId, int data) {
public int getFullId(int runtimeId) {
int fullId = this.legacyMapper.runtimeToFullId(runtimeId);
if (fullId == -1) {
log.warn("Can not find legacyId! No runtime2FullId mapping for " + runtimeId);
log.warn("Can not find legacyId! No runtime2FullId mapping for {}", runtimeId);
fullId = this.legacyMapper.runtimeToFullId(this.getDefaultRuntimeId());
Preconditions.checkArgument(fullId != -1, "Can not find fullId for default runtimeId: " + this.getDefaultRuntimeId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import cn.nukkit.utils.Utils;
import cn.nukkit.utils.Zlib;
import lombok.extern.log4j.Log4j2;
import org.cloudburstmc.nbt.NbtMap;

import javax.annotation.Nullable;
import java.io.IOException;
Expand Down Expand Up @@ -276,7 +275,7 @@ public boolean setBlockAtLayer(int x, int y, int z, int layer, int blockId, int
@Override
public Block getAndSetBlock(int x, int y, int z, int layer, Block block) {
int fullId;
NbtMap state = null;
BlockStateSnapshot state = null;
int previous;
try {
this.writeLock.lock();
Expand All @@ -293,7 +292,7 @@ public Block getAndSetBlock(int x, int y, int z, int layer, Block block) {
storage = this.storages[layer];

if (block instanceof BlockStorageContainer container) {
state = container.getStateNbt();
state = BlockStateMapping.get().getStateUnsafe(container.getStateNbt());
}

fullId = block.getFullId();
Expand All @@ -302,7 +301,7 @@ public Block getAndSetBlock(int x, int y, int z, int layer, Block block) {
previous = storage.get(x, y, z);

if (block instanceof BlockStorageContainer container) {
state = container.getStateNbt();
state = BlockStateMapping.get().getStateUnsafe(container.getStateNbt());
}

fullId = block.getFullId();
Expand All @@ -313,7 +312,7 @@ public Block getAndSetBlock(int x, int y, int z, int layer, Block block) {
}

if (state != null) {
storage.set(x, y, z, BlockStateMapping.get().getState(state));
storage.set(x, y, z, state);
} else {
storage.set(x, y, z, fullId);
}
Expand Down
Binary file modified src/main/resources/runtime_block_states_685.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_712.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_729.dat
Binary file not shown.
Binary file modified src/main/resources/runtime_block_states_748.dat
Binary file not shown.

0 comments on commit 8a96d42

Please sign in to comment.