Skip to content

Commit

Permalink
Fix some villager block trades being unable to stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Mar 8, 2021
1 parent 44e9dba commit 3f14624
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ItemRegistry;
import org.geysermc.connector.network.translators.item.ItemTranslator;
import org.geysermc.connector.network.translators.world.block.BlockTranslator;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -140,6 +141,12 @@ private NbtMap getItemTag(GeyserSession session, ItemStack stack, int specialPri
NbtMap tag = itemData.getTag().toBuilder().build();
builder.put("tag", tag);
}
NbtMap blockTag = BlockTranslator.getBedrockBlockNbt(itemEntry.getJavaIdentifier());
if (blockTag != null) {
// This fixes certain blocks being unable to stack after grabbing one
builder.putCompound("Block", blockTag);
builder.putShort("Damage", (short) 0);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public class BlockTranslator {

public static final int JAVA_RUNTIME_SPAWNER_ID;

/**
* Contains a map of Java blocks to their respective Bedrock block tag, if the Java identifier is different from Bedrock.
* Required to fix villager trades with these blocks.
*/
private static final Map<String, NbtMap> JAVA_IDENTIFIER_TO_BEDROCK_TAG;

private static final int BLOCK_STATE_VERSION = 17825808;

static {
Expand All @@ -112,6 +118,8 @@ public class BlockTranslator {
throw new AssertionError("Unable to get blocks from runtime block states", e);
}

JAVA_IDENTIFIER_TO_BEDROCK_TAG = new Object2ObjectOpenHashMap<>(blocksTag.size());

// New since 1.16.100 - find the block runtime ID by the order given to us in the block palette,
// as we no longer send a block palette
Object2IntMap<NbtMap> blockStateOrderedMap = new Object2IntOpenHashMap<>(blocksTag.size());
Expand Down Expand Up @@ -188,15 +196,19 @@ public class BlockTranslator {
BlockStateValues.storeBlockStateValues(entry.getKey(), javaRuntimeId, entry.getValue());

String cleanJavaIdentifier = entry.getKey().split("\\[")[0];
String bedrockIdentifier = entry.getValue().get("bedrock_identifier").asText();

boolean javaIdentifierSameAsBedrock = cleanJavaIdentifier.equals(bedrockIdentifier);

if (!JAVA_ID_TO_JAVA_IDENTIFIER_MAP.containsValue(cleanJavaIdentifier)) {
uniqueJavaId++;
JAVA_ID_TO_JAVA_IDENTIFIER_MAP.put(uniqueJavaId, cleanJavaIdentifier);
if (!javaIdentifierSameAsBedrock) {
JAVA_IDENTIFIER_TO_BEDROCK_TAG.put(cleanJavaIdentifier, blockTag);
}
}

String bedrockIdentifier = entry.getValue().get("bedrock_identifier").asText();

if (!cleanJavaIdentifier.equals(bedrockIdentifier)) {
if (!javaIdentifierSameAsBedrock) {
JAVA_TO_BEDROCK_IDENTIFIERS.put(cleanJavaIdentifier, bedrockIdentifier);
}

Expand Down Expand Up @@ -393,4 +405,13 @@ public static String getPickItem(int javaId) {
public static String[] getAllBlockIdentifiers() {
return JAVA_ID_TO_JAVA_IDENTIFIER_MAP.values().toArray(new String[0]);
}

/**
* @param cleanJavaIdentifier the clean Java identifier of the block to look up
*
* @return the block tag of the block name mapped from Java to Bedrock.
*/
public static NbtMap getBedrockBlockNbt(String cleanJavaIdentifier) {
return JAVA_IDENTIFIER_TO_BEDROCK_TAG.get(cleanJavaIdentifier);
}
}

0 comments on commit 3f14624

Please sign in to comment.