Fix custom items with ItemTranslator#getBedrockItemMapping

This commit is contained in:
Camotoy 2022-07-20 18:35:40 -04:00
parent 240af3cf2e
commit 616c088b66
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 16 additions and 10 deletions

View file

@ -34,7 +34,6 @@ import lombok.Getter;
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;
@ -136,9 +135,9 @@ public abstract class Inventory {
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());

View file

@ -266,16 +266,23 @@ public abstract class ItemTranslator {
}
/**
* 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() {

View file

@ -542,7 +542,7 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
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) {