Update the item parts

This commit is contained in:
Camotoy 2024-04-20 19:20:00 -04:00
parent 6d8021f155
commit aed7f1bed7
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
17 changed files with 171 additions and 154 deletions

View file

@ -43,11 +43,11 @@ public class CompassItem extends Item {
}
@Override
public ItemData.Builder translateToBedrock(ItemStack itemStack, ItemMapping mapping, ItemMappings mappings) {
if (isLodestoneCompass(itemStack.getDataComponents())) {
return super.translateToBedrock(itemStack, mappings.getLodestoneCompass(), mappings);
public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {
if (isLodestoneCompass(components)) {
return super.translateToBedrock(count, components, mappings.getLodestoneCompass(), mappings);
}
return super.translateToBedrock(itemStack, mapping, mappings);
return super.translateToBedrock(count, components, mapping, mappings);
}
@Override

View file

@ -25,10 +25,8 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings;
@ -39,12 +37,12 @@ public class FilledMapItem extends MapItem {
}
@Override
public ItemData.Builder translateToBedrock(ItemStack itemStack, ItemMapping mapping, ItemMappings mappings) {
ItemData.Builder builder = super.translateToBedrock(itemStack, mapping, mappings);
DataComponents components = itemStack.getDataComponents();
public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {
ItemData.Builder builder = super.translateToBedrock(count, components, mapping, mappings);
if (components == null) {
// This is a fallback for maps with no nbt (Change added back in June 2020; is it needed in 2023?)
return builder.tag(NbtMap.builder().putInt("map", 0).build());
//return builder.tag(NbtMap.builder().putInt("map", 0).build()); TODO if this is *still* broken, let's move it to translateComponentsToBedrock
return builder;
} else {
Integer mapColor = components.get(DataComponentType.MAP_COLOR);
if (mapColor != null) {

View file

@ -32,6 +32,7 @@ import com.github.steveice10.opennbt.tag.builtin.*;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.nbt.NbtType;
import org.geysermc.geyser.level.FireworkColor;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.session.GeyserSession;
@ -63,9 +64,10 @@ public class FireworkRocketItem extends Item {
}
List<NbtMap> explosionNbt = new ArrayList<>();
for (Fireworks.FireworkExplosion explosion : explosions) {
explosionNbt.add(translateExplosionToBedrock(explosion, ""));
explosionNbt.add(translateExplosionToBedrock(explosion));
}
fireworksNbt.putList("Explosions", NbtType.COMPOUND, explosionNbt);
builder.putCompound("Fireworks", fireworksNbt.build());
}
@Override
@ -73,17 +75,15 @@ public class FireworkRocketItem extends Item {
super.translateNbtToJava(tag, mapping);
}
static NbtMap translateExplosionToBedrock(Fireworks.FireworkExplosion explosion, String newName) {
static NbtMap translateExplosionToBedrock(Fireworks.FireworkExplosion explosion) {
NbtMapBuilder newExplosionData = NbtMap.builder();
if (explosion.get("Type") != null) {
newExplosionData.put(new ByteTag("FireworkType", MathUtils.getNbtByte(explosion.get("Type").getValue())));
}
// if (explosion.get("Type") != null) {
// newExplosionData.put(new ByteTag("FireworkType", MathUtils.getNbtByte(explosion.get("Type").getValue())));
// }
//newExplosionData.putByte("FireworkType", explosion.get) //TODO???
// TODO do we need length checks
if (explosion.get("Colors") != null) {
int[] oldColors = (int[]) explosion.get("Colors").getValue();
int[] oldColors = explosion.getColors();
byte[] colors = new byte[oldColors.length];
int i = 0;
@ -91,20 +91,17 @@ public class FireworkRocketItem extends Item {
colors[i++] = FireworkColor.fromJavaRGB(color);
}
newExplosionData.put(new ByteArrayTag("FireworkColor", colors));
}
newExplosionData.putByteArray("FireworkColor", colors);
if (explosion.get("FadeColors") != null) {
int[] oldColors = (int[]) explosion.get("FadeColors").getValue();
byte[] colors = new byte[oldColors.length];
oldColors = explosion.getFadeColors();
colors = new byte[oldColors.length];
int i = 0;
i = 0;
for (int color : oldColors) {
colors[i++] = FireworkColor.fromJavaRGB(color);
}
newExplosionData.put(new ByteArrayTag("FireworkFade", colors));
}
newExplosionData.putByteArray("FireworkFade", colors);
newExplosionData.putBoolean("FireworkTrail", explosion.isHasTrail());
newExplosionData.putBoolean("FireworkFlicker", explosion.isHasTwinkle()); // TODO verify

View file

@ -25,12 +25,13 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.data.game.item.component.Fireworks;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.nbt.NbtMap;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
@ -44,19 +45,15 @@ public class FireworkStarItem extends Item {
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
super.translateComponentsToBedrock(session, components, builder);
Tag explosion = tag.remove("Explosion");
if (explosion instanceof CompoundTag) {
CompoundTag newExplosion = FireworkRocketItem.translateExplosionToBedrock((CompoundTag) explosion, "FireworksItem");
tag.put(newExplosion);
Tag color = ((CompoundTag) explosion).get("Colors");
if (color instanceof IntArrayTag) {
Fireworks.FireworkExplosion explosion = components.get(DataComponentType.FIREWORK_EXPLOSION);
if (explosion != null) {
NbtMap newExplosion = FireworkRocketItem.translateExplosionToBedrock(explosion);
builder.putCompound("FireworksItem", newExplosion);
int[] colors = explosion.getColors();
if (colors.length != 0) {
// Determine the custom color, if any.
// Mostly replicates Java's own rendering code, as Java determines the final firework star color client-side
// while Bedrock determines it server-side.
int[] colors = ((IntArrayTag) color).getValue();
if (colors.length == 0) {
return;
}
int finalColor;
if (colors.length == 1) {
finalColor = colors[0];
@ -77,7 +74,7 @@ public class FireworkStarItem extends Item {
finalColor = r << 16 | g << 8 | b;
}
tag.put(new IntTag("customColor", finalColor));
builder.putInt("customColor", finalColor);
}
}
}

View file

@ -26,8 +26,6 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
@ -42,11 +40,7 @@ public class FishingRodItem extends Item {
super.translateComponentsToBedrock(session, components, builder);
// Fix damage inconsistency
Tag damage = tag.get("Damage");
if (damage instanceof IntTag) {
int originalDurability = ((IntTag) damage).getValue();
tag.put(new IntTag("Damage", getBedrockDamage(originalDurability)));
}
builder.getDamage().ifPresent(damage -> builder.setDamage(getBedrockDamage(damage)));
}
public static int getBedrockDamage(int javaDamage) {

View file

@ -26,6 +26,9 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.data.game.item.component.Instrument;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
@ -52,10 +55,14 @@ public class GoatHornItem extends Item {
}
@Override
public ItemData.Builder translateToBedrock(ItemStack itemStack, ItemMapping mapping, ItemMappings mappings) {
ItemData.Builder builder = super.translateToBedrock(itemStack, mapping, mappings);
if (itemStack.getNbt() != null && itemStack.getNbt().get("instrument") instanceof StringTag instrumentTag) {
String instrument = instrumentTag.getValue();
public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {
ItemData.Builder builder = super.translateToBedrock(count, components, mapping, mappings);
if (components == null) {
return builder;
}
Instrument instrument = components.get(DataComponentType.INSTRUMENT);
// TODO registry
if (instrument != null) {
// Drop the Minecraft namespace if applicable
if (instrument.startsWith("minecraft:")) {
instrument = instrument.substring("minecraft:".length());

View file

@ -39,6 +39,7 @@ import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.item.Enchantment;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.session.GeyserSession;
@ -93,20 +94,16 @@ public class Item {
/* Translation methods to Bedrock and back */
public ItemData.Builder translateToBedrock(ItemStack itemStack, ItemMapping mapping, ItemMappings mappings) {
if (InventoryUtils.isEmpty(itemStack)) {
public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {
if (this == Items.AIR || count <= 0) {
// Return, essentially, air
return ItemData.builder();
}
ItemData.Builder builder = ItemData.builder()
.definition(mapping.getBedrockDefinition())
.damage(mapping.getBedrockData())
.count(itemStack.getAmount());
if (itemStack.getDataComponents() != null) {
builder.tag(ItemTranslator.translateNbtToBedrock(itemStack.getDataComponents()));
}
.count(count);
DataComponents components = itemStack.getDataComponents();
ItemTranslator.translateCustomItem(components, builder, mapping);
return builder;
@ -135,6 +132,11 @@ public class Item {
}
}
Integer damage = components.get(DataComponentType.DAMAGE);
if (damage != null) {
builder.setDamage(damage);
}
List<Tag> newTags = new ArrayList<>();
ItemEnchantments enchantments = components.get(DataComponentType.ENCHANTMENTS);
if (enchantments != null) {

View file

@ -25,6 +25,7 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.*;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -41,14 +42,14 @@ public class MapItem extends Item {
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
super.translateComponentsToBedrock(session, components, builder);
Tag mapId = tag.remove("map");
if (mapId == null || !(mapId.getValue() instanceof Number number)) return;
Integer mapValue = components.get(DataComponentType.MAP_ID);
if (mapValue == null) {
return;
}
int mapValue = number.intValue();
tag.put(new LongTag("map_uuid", mapValue));
tag.put(new IntTag("map_name_index", mapValue));
tag.put(new ByteTag("map_display_players", (byte) 1));
builder.putLong("map_uuid", mapValue);
builder.putInt("map_name_index", mapValue);
builder.putByte("map_display_players", (byte) 1);
}
@Override

View file

@ -25,16 +25,14 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.text.MinecraftLocale;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.geyser.translator.text.MessageTranslator;
public class PlayerHeadItem extends Item {
public PlayerHeadItem(String javaIdentifier, Builder builder) {
@ -45,35 +43,24 @@ public class PlayerHeadItem extends Item {
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
super.translateComponentsToBedrock(session, components, builder);
CompoundTag displayTag;
if (tag.get("display") instanceof CompoundTag existingDisplayTag) {
displayTag = existingDisplayTag;
} else {
displayTag = new CompoundTag("display");
tag.put(displayTag);
}
if (displayTag.get("Name") instanceof StringTag nameTag) {
// Custom names are always yellow and italic
displayTag.put(new StringTag("Name", ChatColor.YELLOW + ChatColor.ITALIC + MessageTranslator.convertMessageLenient(nameTag.getValue(), session.locale())));
} else {
if (tag.contains("SkullOwner")) {
StringTag name;
Tag skullOwner = tag.get("SkullOwner");
if (skullOwner instanceof StringTag skullName) {
name = skullName;
} else {
if (skullOwner instanceof CompoundTag && ((CompoundTag) skullOwner).get("Name") instanceof StringTag skullName) {
name = skullName;
// TODO verify
// Also - ChatColor.YELLOW + ChatColor.ITALIC + MessageTranslator.convertMessageLenient(nameTag.getValue(), session.locale())) this code existed if a custom name was already present.
// But I think we would always overwrite that because translateDisplayProperties runs after this method.
String customName = builder.getCustomName();
if (customName == null) {
GameProfile profile = components.get(DataComponentType.PROFILE);
if (profile != null) {
String name = profile.getName();
if (name != null) {
// Add correct name of player skull
String displayName = ChatColor.RESET + ChatColor.YELLOW +
MinecraftLocale.getLocaleString("block.minecraft.player_head.named", session.locale()).replace("%s", name);
builder.setCustomName(displayName);
} else {
// No name found so default to "Player Head"
displayTag.put(new StringTag("Name", ChatColor.RESET + ChatColor.YELLOW + MinecraftLocale.getLocaleString("block.minecraft.player_head", session.locale())));
return;
}
}
// Add correct name of player skull
String displayName = ChatColor.RESET + ChatColor.YELLOW + MinecraftLocale.getLocaleString("block.minecraft.player_head.named", session.locale()).replace("%s", name.getValue());
displayTag.put(new StringTag("Name", displayName));
builder.setCustomName(ChatColor.RESET + ChatColor.YELLOW +
MinecraftLocale.getLocaleString("block.minecraft.player_head", session.locale()));
}
}
}
}

View file

@ -27,9 +27,9 @@ package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.data.game.item.component.PotionContents;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
@ -46,29 +46,27 @@ public class PotionItem extends Item {
}
@Override
public ItemData.Builder translateToBedrock(ItemStack itemStack, ItemMapping mapping, ItemMappings mappings) {
if (itemStack.getDataComponents() == null) return super.translateToBedrock(itemStack, mapping, mappings);
PotionContents potionContents = itemStack.getDataComponents().get(DataComponentType.POTION_CONTENTS);
public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {
if (components == null) return super.translateToBedrock(count, components, mapping, mappings);
PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
if (potionContents != null) {
ItemDefinition customItemDefinition = CustomItemTranslator.getCustomItem(itemStack.getDataComponents(), mapping);
ItemDefinition customItemDefinition = CustomItemTranslator.getCustomItem(components, mapping);
if (customItemDefinition == null) {
Potion potion = Potion.getByJavaIdentifier(((StringTag) potionTag).getValue());
if (potion != null) {
return ItemData.builder()
.definition(mapping.getBedrockDefinition())
.damage(potion.getBedrockId())
.count(itemStack.getAmount())
.tag(ItemTranslator.translateNbtToBedrock(itemStack.getNbt()));
.count(count);
}
GeyserImpl.getInstance().getLogger().debug("Unknown Java potion: " + potionTag.getValue());
} else {
return ItemData.builder()
.definition(customItemDefinition)
.count(itemStack.getAmount())
.tag(ItemTranslator.translateNbtToBedrock(itemStack.getNbt()));
.count(count);
}
}
return super.translateToBedrock(itemStack, mapping, mappings);
return super.translateToBedrock(count, components, mapping, mappings);
}
@Override

View file

@ -83,7 +83,7 @@ public class ShulkerBoxItem extends BlockItem {
itemsList.add(boxItemNbt.build());
}
builder.getOrCreateNbt().putList("Items", NbtType.COMPOUND, itemsList);
builder.putList("Items", NbtType.COMPOUND, itemsList);
}
@Override

View file

@ -25,7 +25,7 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
@ -41,7 +41,7 @@ public class TippedArrowItem extends ArrowItem {
}
@Override
public ItemData.Builder translateToBedrock(ItemStack itemStack, ItemMapping mapping, ItemMappings mappings) {
public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {
Tag potionTag = itemStack.getNbt().get("Potion");
if (potionTag instanceof StringTag) {
TippedArrowPotion tippedArrowPotion = TippedArrowPotion.getByJavaIdentifier(((StringTag) potionTag).getValue());
@ -49,11 +49,10 @@ public class TippedArrowItem extends ArrowItem {
return ItemData.builder()
.definition(mapping.getBedrockDefinition())
.damage(tippedArrowPotion.getBedrockId())
.count(itemStack.getAmount())
.tag(ItemTranslator.translateNbtToBedrock(itemStack.getNbt()));
.count(count);
}
GeyserImpl.getInstance().getLogger().debug("Unknown Java potion (tipped arrow): " + potionTag.getValue());
}
return super.translateToBedrock(itemStack, mapping, mappings);
return super.translateToBedrock(count, components, mapping, mappings);
}
}

View file

@ -25,6 +25,7 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.*;
import net.kyori.adventure.text.Component;
@ -53,10 +54,10 @@ public class TropicalFishBucketItem extends Item {
super.translateComponentsToBedrock(session, components, builder);
// Prevent name from appearing as "Bucket of"
tag.put(new ByteTag("AppendCustomName", (byte) 1));
tag.put(new StringTag("CustomName", MinecraftLocale.getLocaleString("entity.minecraft.tropical_fish", session.locale())));
builder.putByte("AppendCustomName", (byte) 1);
builder.putString("CustomName", MinecraftLocale.getLocaleString("entity.minecraft.tropical_fish", session.locale()));
// Add Java's client side lore tag
Tag bucketVariantTag = tag.get("BucketVariantTag");
components.get(DataComponentType)
if (bucketVariantTag instanceof IntTag) {
CompoundTag displayTag = tag.get("display");
if (displayTag == null) {

View file

@ -25,12 +25,18 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.data.game.item.component.Filterable;
import com.github.steveice10.mc.protocol.data.game.item.component.WritableBookContent;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.nbt.NbtType;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
@ -48,22 +54,20 @@ public class WritableBookItem extends Item {
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
super.translateComponentsToBedrock(session, components, builder);
ListTag pagesTag = tag.remove("pages");
if (pagesTag == null) {
WritableBookContent bookContent = components.get(DataComponentType.WRITABLE_BOOK_CONTENT);
if (bookContent == null) {
return;
}
List<Tag> pages = new ArrayList<>();
for (Tag subTag : pagesTag.getValue()) {
if (!(subTag instanceof StringTag textTag))
continue;
CompoundTag pageTag = new CompoundTag("");
pageTag.put(new StringTag("photoname", ""));
pageTag.put(new StringTag("text", MessageTranslator.convertMessageLenient(textTag.getValue())));
pages.add(pageTag);
List<NbtMap> bedrockPages = new ArrayList<>();
for (Filterable<String> page : bookContent.getPages()) {
NbtMapBuilder pageBuilder = NbtMap.builder();
pageBuilder.putString("photoname", "");
pageBuilder.putString("text", MessageTranslator.convertMessageLenient(page.getRaw()));
bedrockPages.add(pageBuilder.build());
}
tag.put(new ListTag("pages", pages));
builder.putList("pages", NbtType.COMPOUND, bedrockPages);
}
@Override

View file

@ -25,21 +25,27 @@
package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.data.game.item.component.Filterable;
import com.github.steveice10.mc.protocol.data.game.item.component.WrittenBookContent;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.nbt.NbtType;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.geyser.translator.text.MessageTranslator;
import java.util.ArrayList;
import java.util.List;
public class WrittenBookItem extends WritableBookItem {
public class WrittenBookItem extends Item {
public static final int MAXIMUM_PAGE_EDIT_LENGTH = 1024;
public static final int MAXIMUM_PAGE_LENGTH = 32768;
public static final int MAXIMUM_PAGE_COUNT = 100; // Java edition limit. Bedrock edition has a limit of 50 pages.
@ -51,25 +57,24 @@ public class WrittenBookItem extends WritableBookItem {
@Override
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
boolean isValid = isValidWrittenBook(tag);
if (!isValid) {
tag.remove("pages");
}
super.translateComponentsToBedrock(session, components, builder);
if (!isValid) {
CompoundTag invalidTagPage = new CompoundTag("");
invalidTagPage.put(new StringTag("photoname", ""));
invalidTagPage.put(new StringTag(
"text",
MessageTranslator.convertMessage(
Component.translatable("book.invalid.tag", NamedTextColor.DARK_RED),
session.locale()
)
));
tag.put(new ListTag("pages", List.of(invalidTagPage)));
WrittenBookContent bookContent = components.get(DataComponentType.WRITTEN_BOOK_CONTENT);
if (bookContent == null) {
return;
}
List<NbtMap> bedrockPages = new ArrayList<>();
for (Filterable<Component> page : bookContent.getPages()) {
NbtMapBuilder pageBuilder = NbtMap.builder();
pageBuilder.putString("photoname", "");
pageBuilder.putString("text", MessageTranslator.convertMessage(page.getRaw()));
bedrockPages.add(pageBuilder.build());
}
builder.putList("pages", NbtType.COMPOUND, bedrockPages);
builder.putString("title", bookContent.getTitle().getRaw())
.putString("author", bookContent.getAuthor());
// TODO isResolved
}
private boolean isValidWrittenBook(CompoundTag tag) {

View file

@ -34,6 +34,7 @@ import org.geysermc.geyser.registry.type.ItemMapping;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
/**
* An intermediary class made to allow easy access to work-in-progress NBT, such as lore and display.
@ -44,12 +45,18 @@ public final class BedrockItemBuilder {
private String customName;
@Nullable
private List<String> lore;
private OptionalInt damage = OptionalInt.empty();
/**
* Miscellaneous NBT that will be put into the final item.
*/
@Nullable
private NbtMapBuilder builder;
@Nullable
public String getCustomName() {
return customName;
}
public BedrockItemBuilder setCustomName(String customName) {
this.customName = customName;
return this;
@ -63,6 +70,15 @@ public final class BedrockItemBuilder {
return lore;
}
public OptionalInt getDamage() {
return damage;
}
public BedrockItemBuilder setDamage(int damage) {
this.damage = OptionalInt.of(damage);
return this;
}
@NonNull
public NbtMapBuilder getOrCreateNbt() {
if (builder == null) {
@ -85,6 +101,14 @@ public final class BedrockItemBuilder {
return getOrCreateNbt().putInt(name, value);
}
public <T> NbtMapBuilder putList(String name, NbtType<T> type, List<T> value) {
return getOrCreateNbt().putList(name, type, value);
}
public NbtMapBuilder putLong(String name, long value) {
return getOrCreateNbt().putLong(name, value);
}
public NbtMapBuilder putString(String name, String value) {
return getOrCreateNbt().putString(name, value);
}
@ -108,6 +132,9 @@ public final class BedrockItemBuilder {
}
getOrCreateNbt().put("display", display.build());
}
if (damage.isPresent()) {
getOrCreateNbt().putInt("Damage", damage.getAsInt());
}
if (builder == null) {
return null;
}

View file

@ -161,9 +161,9 @@ public final class ItemTranslator {
addAdvancedTooltips(components, nbtBuilder, javaItem, session.locale());
}
ItemStack itemStack = new ItemStack(javaItem.javaId(), count, components);
ItemData.Builder builder = javaItem.translateToBedrock(itemStack, bedrockItem, session.getItemMappings());
ItemData.Builder builder = javaItem.translateToBedrock(count, components, bedrockItem, session.getItemMappings());
// Finalize the Bedrock NBT
builder.tag(nbtBuilder.build());
if (bedrockItem.isBlock()) {
CustomBlockData customBlockData = BlockRegistries.CUSTOM_BLOCK_ITEM_OVERRIDES.getOrDefault(
bedrockItem.getJavaItem().javaIdentifier(), null);