Skip to content

Commit

Permalink
Fix custom items with ItemTranslator#getBedrockItemMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Jul 20, 2022
1 parent 240af3c commit 616c088
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import lombok.Setter;
import lombok.ToString;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.inventory.item.ItemTranslator;
import org.jetbrains.annotations.Range;
Expand Down Expand Up @@ -136,9 +135,9 @@ public void setItem(int slot, @Nonnull GeyserItemStack newItem, GeyserSession se

protected void updateItemNetId(GeyserItemStack oldItem, GeyserItemStack newItem, GeyserSession session) {
if (!newItem.isEmpty()) {
ItemMapping oldMapping = ItemTranslator.getBedrockItemMapping(session, oldItem);
ItemMapping newMapping = ItemTranslator.getBedrockItemMapping(session, newItem);
if (oldMapping.getBedrockId() == newMapping.getBedrockId()) {
int oldMapping = ItemTranslator.getBedrockItemId(session, oldItem);
int newMapping = ItemTranslator.getBedrockItemId(session, newItem);
if (oldMapping == newMapping) {
newItem.setNetId(oldItem.getNetId());
} else {
newItem.setNetId(session.getNextItemNetId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,23 @@ private static String[] getCanModify(ListTag canModifyJava, String[] canModifyBe
}

/**
* Given an item stack, determine the item mapping that should be applied to Bedrock players.
* Given an item stack, determine the Bedrock item ID that should be applied to Bedrock players.
*/
@Nonnull
public static ItemMapping getBedrockItemMapping(GeyserSession session, @Nonnull GeyserItemStack itemStack) {
public static int getBedrockItemId(GeyserSession session, @Nonnull GeyserItemStack itemStack) {
if (itemStack.isEmpty()) {
return ItemMapping.AIR;
return ItemMapping.AIR.getJavaId();
}
int javaId = itemStack.getJavaId();
return ITEM_STACK_TRANSLATORS.getOrDefault(javaId, DEFAULT_TRANSLATOR)
ItemMapping mapping = ITEM_STACK_TRANSLATORS.getOrDefault(javaId, DEFAULT_TRANSLATOR)
.getItemMapping(javaId, itemStack.getNbt(), session.getItemMappings());

int customItemId = getCustomItem(itemStack.getNbt(), mapping);
if (customItemId == -1) {
// No custom item
return mapping.getBedrockId();
} else {
return customItemId;
}
}

private static final ItemTranslator DEFAULT_TRANSLATOR = new ItemTranslator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ private void restoreCorrectBlock(GeyserSession session, Vector3i blockPos, Inven

private boolean isIncorrectHeldItem(GeyserSession session, InventoryTransactionPacket packet) {
int javaSlot = session.getPlayerInventory().getOffsetForHotbar(packet.getHotbarSlot());
int expectedItemId = ItemTranslator.getBedrockItemMapping(session, session.getPlayerInventory().getItem(javaSlot)).getBedrockId();
int expectedItemId = ItemTranslator.getBedrockItemId(session, session.getPlayerInventory().getItem(javaSlot));
int heldItemId = packet.getItemInHand() == null ? ItemData.AIR.getId() : packet.getItemInHand().getId();

if (expectedItemId != heldItemId) {
Expand Down

0 comments on commit 616c088

Please sign in to comment.