Show potions & custom items in the shulker box tooltip (#4822)

* Fix: Potions/Custom items showing up improperly in shulker box previews

* oops
This commit is contained in:
chris 2024-07-08 00:41:00 +02:00 committed by GitHub
parent e0af0a54b7
commit 0a928c41b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 3 deletions

View file

@ -29,15 +29,19 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder; import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.nbt.NbtType; import org.cloudburstmc.nbt.NbtType;
import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition;
import org.geysermc.geyser.inventory.item.Potion;
import org.geysermc.geyser.item.Items; import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.level.block.type.Block; import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.registry.type.ItemMapping; import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.BedrockItemBuilder; import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.geyser.translator.item.CustomItemTranslator;
import org.geysermc.geyser.translator.item.ItemTranslator; import org.geysermc.geyser.translator.item.ItemTranslator;
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack; import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType; import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentType;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents; import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.PotionContents;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -64,12 +68,35 @@ public class ShulkerBoxItem extends BlockItem {
} }
ItemMapping boxMapping = session.getItemMappings().getMapping(item.getId()); ItemMapping boxMapping = session.getItemMappings().getMapping(item.getId());
NbtMapBuilder boxItemNbt = BedrockItemBuilder.createItemNbt(boxMapping, item.getAmount(), boxMapping.getBedrockData()); // Final item tag to add to the list int bedrockData = boxMapping.getBedrockData();
String bedrockIdentifier = boxMapping.getBedrockIdentifier();
DataComponents boxComponents = item.getDataComponents();
if (boxComponents != null) {
// Check for custom items
ItemDefinition customItemDefinition = CustomItemTranslator.getCustomItem(boxComponents, boxMapping);
if (customItemDefinition != null) {
bedrockIdentifier = customItemDefinition.getIdentifier();
bedrockData = 0;
} else {
// Manual checks for potions/tipped arrows
if (boxMapping.getJavaItem() instanceof PotionItem || boxMapping.getJavaItem() instanceof ArrowItem) {
PotionContents potionContents = boxComponents.get(DataComponentType.POTION_CONTENTS);
if (potionContents != null) {
Potion potion = Potion.getByJavaId(potionContents.getPotionId());
if (potion != null) {
bedrockData = potion.getBedrockId();
}
}
}
}
}
NbtMapBuilder boxItemNbt = BedrockItemBuilder.createItemNbt(bedrockIdentifier, item.getAmount(), bedrockData); // Final item tag to add to the list
boxItemNbt.putByte("Slot", (byte) slot); boxItemNbt.putByte("Slot", (byte) slot);
boxItemNbt.putByte("WasPickedUp", (byte) 0); // ??? TODO might not be needed boxItemNbt.putByte("WasPickedUp", (byte) 0); // ??? TODO might not be needed
// Only the display name is what we have interest in, so just translate that if relevant // Only the display name is what we have interest in, so just translate that if relevant
DataComponents boxComponents = item.getDataComponents();
if (boxComponents != null) { if (boxComponents != null) {
String customName = ItemTranslator.getCustomName(session, boxComponents, boxMapping, '7'); String customName = ItemTranslator.getCustomName(session, boxComponents, boxMapping, '7');
if (customName != null) { if (customName != null) {

View file

@ -145,8 +145,15 @@ public final class BedrockItemBuilder {
* Creates item NBT to nest within NBT with name, count, and damage set. * Creates item NBT to nest within NBT with name, count, and damage set.
*/ */
public static NbtMapBuilder createItemNbt(ItemMapping mapping, int count, int damage) { public static NbtMapBuilder createItemNbt(ItemMapping mapping, int count, int damage) {
return createItemNbt(mapping.getBedrockIdentifier(), count, damage);
}
/**
* Creates item NBT to nest within NBT with name, count, and damage set.
*/
public static NbtMapBuilder createItemNbt(String bedrockIdentifier, int count, int damage) {
NbtMapBuilder builder = NbtMap.builder(); NbtMapBuilder builder = NbtMap.builder();
builder.putString("Name", mapping.getBedrockIdentifier()); builder.putString("Name", bedrockIdentifier);
builder.putByte("Count", (byte) count); builder.putByte("Count", (byte) count);
builder.putShort("Damage", (short) damage); builder.putShort("Damage", (short) damage);
return builder; return builder;