Trying to get more compiled but brain is officially fried for the day!

This commit is contained in:
Camotoy 2024-04-20 22:42:25 -04:00
parent 94e533ea7c
commit 8bd2df0828
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
20 changed files with 210 additions and 314 deletions

View file

@ -27,24 +27,16 @@ package org.geysermc.geyser.entity.type;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack; import com.github.steveice10.mc.protocol.data.game.item.ItemStack;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.cloudburstmc.math.vector.Vector3f; import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.nbt.NbtType;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes; import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
import org.cloudburstmc.protocol.bedrock.packet.SetEntityMotionPacket; import org.cloudburstmc.protocol.bedrock.packet.SetEntityMotionPacket;
import org.geysermc.floodgate.util.DeviceOs;
import org.geysermc.geyser.entity.EntityDefinition; import org.geysermc.geyser.entity.EntityDefinition;
import org.geysermc.geyser.entity.type.player.PlayerEntity; import org.geysermc.geyser.entity.type.player.PlayerEntity;
import org.geysermc.geyser.level.FireworkColor; import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.util.MathUtils; import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt; import java.util.OptionalInt;
import java.util.UUID; import java.util.UUID;
@ -59,80 +51,16 @@ public class FireworkEntity extends Entity {
if (item == null) { if (item == null) {
return; return;
} }
CompoundTag tag = item.getNbt(); DataComponents components = item.getDataComponents();
if (components == null) {
if (tag == null) {
return; return;
} }
// TODO: Remove once Mojang fixes bugs with fireworks crashing clients on these specific devices. // TODO this looked the same, so I'm going to assume it is and (keep below comment if true)
// https://bugs.mojang.com/browse/MCPE-89115 // Translate using item methods to get firework NBT for Bedrock
if (session.getClientData().getDeviceOs() == DeviceOs.XBOX BedrockItemBuilder builder = new BedrockItemBuilder();
|| session.getClientData().getDeviceOs() == DeviceOs.PS4) { Items.FIREWORK_ROCKET.translateComponentsToBedrock(session, components, builder);
return;
}
CompoundTag fireworks = tag.get("Fireworks");
if (fireworks == null) {
// Thank you Mineplex very cool
return;
}
NbtMapBuilder fireworksBuilder = NbtMap.builder();
if (fireworks.get("Flight") != null) {
fireworksBuilder.putByte("Flight", MathUtils.getNbtByte(fireworks.get("Flight").getValue()));
}
List<NbtMap> explosions = new ArrayList<>();
if (fireworks.get("Explosions") != null) {
for (Tag effect : ((ListTag) fireworks.get("Explosions")).getValue()) {
CompoundTag effectData = (CompoundTag) effect;
NbtMapBuilder effectBuilder = NbtMap.builder();
if (effectData.get("Type") != null) {
effectBuilder.putByte("FireworkType", MathUtils.getNbtByte(effectData.get("Type").getValue()));
}
if (effectData.get("Colors") != null) {
int[] oldColors = (int[]) effectData.get("Colors").getValue();
byte[] colors = new byte[oldColors.length];
int i = 0;
for (int color : oldColors) {
colors[i++] = FireworkColor.fromJavaRGB(color);
}
effectBuilder.putByteArray("FireworkColor", colors);
}
if (effectData.get("FadeColors") != null) {
int[] oldColors = (int[]) effectData.get("FadeColors").getValue();
byte[] colors = new byte[oldColors.length];
int i = 0;
for (int color : oldColors) {
colors[i++] = FireworkColor.fromJavaRGB(color);
}
effectBuilder.putByteArray("FireworkFade", colors);
}
if (effectData.get("Trail") != null) {
effectBuilder.putByte("FireworkTrail", MathUtils.getNbtByte(effectData.get("Trail").getValue()));
}
if (effectData.get("Flicker") != null) {
effectBuilder.putByte("FireworkFlicker", MathUtils.getNbtByte(effectData.get("Flicker").getValue()));
}
explosions.add(effectBuilder.build());
}
}
fireworksBuilder.putList("Explosions", NbtType.COMPOUND, explosions);
NbtMapBuilder builder = NbtMap.builder();
builder.put("Fireworks", fireworksBuilder.build());
dirtyMetadata.put(EntityDataTypes.DISPLAY_FIREWORK, builder.build()); dirtyMetadata.put(EntityDataTypes.DISPLAY_FIREWORK, builder.build());
} }

View file

@ -27,8 +27,9 @@ package org.geysermc.geyser.entity.type;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack; import com.github.steveice10.mc.protocol.data.game.item.ItemStack;
import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
import com.github.steveice10.opennbt.tag.builtin.Tag; import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.data.game.item.component.PotionContents;
import org.cloudburstmc.math.vector.Vector3f; import org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes; import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag; import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
@ -58,16 +59,17 @@ public class ThrownPotionEntity extends ThrowableItemEntity {
setFlag(EntityFlag.LINGERING, false); setFlag(EntityFlag.LINGERING, false);
} else { } else {
// As of Java 1.19.3, the server/client doesn't seem to care of the item is actually a potion? // As of Java 1.19.3, the server/client doesn't seem to care of the item is actually a potion?
if (itemStack.getNbt() != null) { DataComponents components = itemStack.getDataComponents();
Tag potionTag = itemStack.getNbt().get("Potion"); if (components != null) {
if (potionTag instanceof StringTag) { PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
Potion potion = Potion.getByJavaIdentifier(((StringTag) potionTag).getValue()); if (potionContents != null) {
Potion potion = Potion.VALUES[potionContents.getPotionId()];
if (potion != null) { if (potion != null) {
dirtyMetadata.put(EntityDataTypes.EFFECT_COLOR, (int) potion.getBedrockId()); dirtyMetadata.put(EntityDataTypes.EFFECT_COLOR, (int) potion.getBedrockId());
setFlag(EntityFlag.ENCHANTED, !NON_ENCHANTED_POTIONS.contains(potion)); setFlag(EntityFlag.ENCHANTED, !NON_ENCHANTED_POTIONS.contains(potion));
} else { } else {
dirtyMetadata.put(EntityDataTypes.EFFECT_COLOR, 0); dirtyMetadata.put(EntityDataTypes.EFFECT_COLOR, 0);
GeyserImpl.getInstance().getLogger().debug("Unknown java potion: " + potionTag.getValue()); GeyserImpl.getInstance().getLogger().debug("Unknown java potion: " + potionContents.getPotionId());
} }
} }

View file

@ -36,6 +36,7 @@ import java.util.Locale;
*/ */
@Getter @Getter
public enum TippedArrowPotion { public enum TippedArrowPotion {
WATER(-1, ArrowParticleColors.NONE), // Guessing this based off of the Potion enum. TODO merge?
MUNDANE(2, ArrowParticleColors.NONE), // 3 is extended? MUNDANE(2, ArrowParticleColors.NONE), // 3 is extended?
THICK(4, ArrowParticleColors.NONE), THICK(4, ArrowParticleColors.NONE),
AWKWARD(5, ArrowParticleColors.NONE), AWKWARD(5, ArrowParticleColors.NONE),
@ -94,6 +95,10 @@ public enum TippedArrowPotion {
this.javaColor = arrowParticleColor.getColor(); this.javaColor = arrowParticleColor.getColor();
} }
public static TippedArrowPotion of(int id) {
return VALUES[id];
}
public static @Nullable TippedArrowPotion getByJavaIdentifier(String javaIdentifier) { public static @Nullable TippedArrowPotion getByJavaIdentifier(String javaIdentifier) {
for (TippedArrowPotion potion : VALUES) { for (TippedArrowPotion potion : VALUES) {
if (potion.javaIdentifier.equals(javaIdentifier)) { if (potion.javaIdentifier.equals(javaIdentifier)) {

View file

@ -26,8 +26,6 @@
package org.geysermc.geyser.item.type; 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.ItemStack;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType;
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.StringTag;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData; import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
@ -48,7 +46,7 @@ public class ArrowItem extends Item {
if (tippedArrowPotion != null) { if (tippedArrowPotion != null) {
itemStack = Items.TIPPED_ARROW.newItemStack(itemStack.getAmount(), itemStack.getDataComponents()); itemStack = Items.TIPPED_ARROW.newItemStack(itemStack.getAmount(), itemStack.getDataComponents());
StringTag potionTag = new StringTag("Potion", tippedArrowPotion.getJavaIdentifier()); StringTag potionTag = new StringTag("Potion", tippedArrowPotion.getJavaIdentifier());
itemStack.getDataComponents().put(DataComponentType.POTION_CONTENTS, new PotionContents()); //itemStack.getDataComponents().put(DataComponentType.POTION_CONTENTS, new PotionContents());
} }
return itemStack; return itemStack;
} }

View file

@ -56,14 +56,14 @@ public class BannerItem extends BlockItem {
static { static {
// Construct what an ominous banner is supposed to look like // Construct what an ominous banner is supposed to look like
OMINOUS_BANNER_PATTERN = List.of( OMINOUS_BANNER_PATTERN = List.of(
new BannerPatternLayer("mr", 9), // new BannerPatternLayer("mr", 9),
new BannerPatternLayer("bs", 8), // new BannerPatternLayer("bs", 8),
new BannerPatternLayer("cs", 7), // new BannerPatternLayer("cs", 7),
new BannerPatternLayer("bo", 8), // new BannerPatternLayer("bo", 8),
new BannerPatternLayer("ms", 15), // new BannerPatternLayer("ms", 15),
new BannerPatternLayer("hh", 8), // new BannerPatternLayer("hh", 8),
new BannerPatternLayer("mc", 8), // new BannerPatternLayer("mc", 8),
new BannerPatternLayer("bo", 15) // new BannerPatternLayer("bo", 15)
); );
} }
@ -103,7 +103,8 @@ public class BannerItem extends BlockItem {
* @return The Java edition format pattern nbt * @return The Java edition format pattern nbt
*/ */
public static CompoundTag getJavaBannerPattern(NbtMap pattern) { public static CompoundTag getJavaBannerPattern(NbtMap pattern) {
return new BannerPatternLayer(pattern.getString("Pattern"), 15 - pattern.getInt("Color")); //return new BannerPatternLayer(0/*pattern.getString("Pattern")*/, 15 - pattern.getInt("Color"));
return null;
} }
/** /**
@ -128,13 +129,13 @@ public class BannerItem extends BlockItem {
List<BannerPatternLayer> patterns = components.get(DataComponentType.BANNER_PATTERNS); List<BannerPatternLayer> patterns = components.get(DataComponentType.BANNER_PATTERNS);
if (patterns != null) { if (patterns != null) {
if (patterns.equals(OMINOUS_BANNER_PATTERN)) { // if (patterns.equals(OMINOUS_BANNER_PATTERN)) {
// Remove the current patterns and set the ominous banner type // // Remove the current patterns and set the ominous banner type
builder.putInt("Type", 1); // builder.putInt("Type", 1);
} else { // } else {
invertBannerColors(patterns); // invertBannerColors(patterns);
tag.put(patterns); // tag.put(patterns);
} // }
} }
} }
@ -146,7 +147,7 @@ public class BannerItem extends BlockItem {
// Ominous banner pattern // Ominous banner pattern
tag.remove("Type"); tag.remove("Type");
CompoundTag blockEntityTag = new CompoundTag("BlockEntityTag"); CompoundTag blockEntityTag = new CompoundTag("BlockEntityTag");
blockEntityTag.put(OMINOUS_BANNER_PATTERN); //blockEntityTag.put(OMINOUS_BANNER_PATTERN);
tag.put(blockEntityTag); tag.put(blockEntityTag);
} else if (tag.get("Patterns") instanceof ListTag patterns) { } else if (tag.get("Patterns") instanceof ListTag patterns) {

View file

@ -41,7 +41,7 @@ public class DecoratedPotItem extends BlockItem {
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) { public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
super.translateComponentsToBedrock(session, components, builder); super.translateComponentsToBedrock(session, components, builder);
components.get(DataComponentType.POT_DECORATIONS); // TODO components.get(DataComponentType.POT_DECORATIONS); // TODO what does this look like on Bedrock?
// if (tag.remove("BlockEntityTag") instanceof CompoundTag blockEntityTag) { // if (tag.remove("BlockEntityTag") instanceof CompoundTag blockEntityTag) {
// if (blockEntityTag.remove("sherds") instanceof ListTag sherds) { // if (blockEntityTag.remove("sherds") instanceof ListTag sherds) {
// // bedrock wants it on the root level // // bedrock wants it on the root level

View file

@ -25,16 +25,18 @@
package org.geysermc.geyser.item.type; 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.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.mc.protocol.data.game.item.component.ItemEnchantments;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtType;
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 java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
public class EnchantedBookItem extends Item { public class EnchantedBookItem extends Item {
public EnchantedBookItem(String javaIdentifier, Builder builder) { public EnchantedBookItem(String javaIdentifier, Builder builder) {
@ -45,20 +47,19 @@ public class EnchantedBookItem extends Item {
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) { public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
super.translateComponentsToBedrock(session, components, builder); super.translateComponentsToBedrock(session, components, builder);
List<Tag> newTags = new ArrayList<>(); List<NbtMap> bedrockEnchants = new ArrayList<>();
Tag enchantmentTag = tag.remove("StoredEnchantments"); ItemEnchantments enchantments = components.get(DataComponentType.STORED_ENCHANTMENTS);
if (enchantmentTag instanceof ListTag listTag) { if (enchantments != null) { // TODO don't duplicate code?
for (Tag subTag : listTag.getValue()) { for (Map.Entry<Integer, Integer> enchantment : enchantments.getEnchantments().entrySet()) {
if (!(subTag instanceof CompoundTag)) continue; NbtMap bedrockTag = remapEnchantment(session, enchantment.getKey(), enchantment.getValue(), builder);
CompoundTag bedrockTag = remapEnchantment(session, (CompoundTag) subTag, tag);
if (bedrockTag != null) { if (bedrockTag != null) {
newTags.add(bedrockTag); bedrockEnchants.add(bedrockTag);
} }
} }
} }
if (!newTags.isEmpty()) { if (!bedrockEnchants.isEmpty()) {
tag.put(new ListTag("ench", newTags)); builder.putList("ench", NbtType.COMPOUND, bedrockEnchants);
} }
} }
} }

View file

@ -64,16 +64,16 @@ public class GoatHornItem extends Item {
// TODO registry // TODO registry
if (instrument != null) { if (instrument != null) {
// Drop the Minecraft namespace if applicable // Drop the Minecraft namespace if applicable
if (instrument.startsWith("minecraft:")) { // if (instrument.startsWith("minecraft:")) {
instrument = instrument.substring("minecraft:".length()); // instrument = instrument.substring("minecraft:".length());
} // }
//
int damage = INSTRUMENTS.indexOf(instrument); // int damage = INSTRUMENTS.indexOf(instrument);
if (damage == -1) { // if (damage == -1) {
damage = 0; // damage = 0;
GeyserImpl.getInstance().getLogger().debug("Unknown goat horn instrument: " + instrumentTag.getValue()); // GeyserImpl.getInstance().getLogger().debug("Unknown goat horn instrument: " + instrumentTag.getValue());
} // }
builder.damage(damage); // builder.damage(damage);
} }
return builder; return builder;
} }
@ -90,7 +90,7 @@ public class GoatHornItem extends Item {
String instrument = INSTRUMENTS.get(damage); String instrument = INSTRUMENTS.get(damage);
StringTag instrumentTag = new StringTag("instrument", "minecraft:" + instrument); StringTag instrumentTag = new StringTag("instrument", "minecraft:" + instrument);
itemStack.getNbt().put(instrumentTag); //itemStack.getNbt().put(instrumentTag);
return itemStack; return itemStack;
} }

View file

@ -27,15 +27,15 @@ package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.Identifier; import com.github.steveice10.mc.protocol.data.game.Identifier;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack; 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.mc.protocol.data.game.item.component.DataComponentType; 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.ItemEnchantments; import com.github.steveice10.mc.protocol.data.game.item.component.ItemEnchantments;
import com.github.steveice10.opennbt.tag.builtin.*; import com.github.steveice10.opennbt.tag.builtin.*;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder; import org.cloudburstmc.nbt.NbtType;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData; import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.item.Enchantment; import org.geysermc.geyser.inventory.item.Enchantment;
@ -48,7 +48,6 @@ import org.geysermc.geyser.text.MinecraftLocale;
import org.geysermc.geyser.translator.item.BedrockItemBuilder; import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.geyser.translator.item.ItemTranslator; import org.geysermc.geyser.translator.item.ItemTranslator;
import org.geysermc.geyser.translator.text.MessageTranslator; import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.InventoryUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -113,7 +112,7 @@ public class Item {
if (itemData.getTag() == null) { if (itemData.getTag() == null) {
return new ItemStack(javaId, itemData.getCount(), null); return new ItemStack(javaId, itemData.getCount(), null);
} }
return new ItemStack(javaId, itemData.getCount(), ItemTranslator.translateToJavaNBT("", itemData.getTag())); return new ItemStack(javaId, itemData.getCount(), null/*ItemTranslator.translateToJavaNBT("", itemData.getTag())*/);
} }
public ItemMapping toBedrockDefinition(DataComponents components, ItemMappings mappings) { public ItemMapping toBedrockDefinition(DataComponents components, ItemMappings mappings) {
@ -137,23 +136,19 @@ public class Item {
builder.setDamage(damage); builder.setDamage(damage);
} }
List<Tag> newTags = new ArrayList<>(); List<NbtMap> enchantNbtList = new ArrayList<>();
ItemEnchantments enchantments = components.get(DataComponentType.ENCHANTMENTS); ItemEnchantments enchantments = components.get(DataComponentType.ENCHANTMENTS);
if (enchantments != null) { if (enchantments != null) {
for (Map.Entry<Integer, Integer> enchantment : enchantments.getEnchantments().entrySet()) {
} NbtMap enchantNbt = remapEnchantment(session, enchantment.getKey(), enchantment.getValue(), builder);
if (enchantmentTag instanceof ListTag listTag) { if (enchantNbt != null) {
for (Tag subTag : listTag.getValue()) { enchantNbtList.add(enchantNbt);
if (!(subTag instanceof CompoundTag)) continue;
CompoundTag bedrockTag = remapEnchantment(session, (CompoundTag) subTag, tag);
if (bedrockTag != null) {
newTags.add(bedrockTag);
} }
} }
} }
if (!newTags.isEmpty()) { if (!enchantNbtList.isEmpty()) {
tag.put(new ListTag("ench", newTags)); builder.putList("ench", NbtType.COMPOUND, enchantNbtList);
} }
} }
@ -220,45 +215,30 @@ public class Item {
} }
} }
protected final @Nullable NbtMap remapEnchantment(GeyserSession session, ItemEnchantments, NbtMapBuilder rootBuilder) { protected final @Nullable NbtMap remapEnchantment(GeyserSession session, int enchantId, int level, BedrockItemBuilder builder) {
// TODO verify
Enchantment enchantment = Enchantment.getByJavaIdentifier(((StringTag) javaEnchId).getValue()); // TODO streamline Enchantment process
Enchantment.JavaEnchantment enchantment = Enchantment.JavaEnchantment.of(enchantId);
if (enchantment == Enchantment.JavaEnchantment.SWEEPING_EDGE) {
addSweeping(session, builder, level);
return null;
}
if (enchantment == null) { if (enchantment == null) {
if (Identifier.formalize((String) javaEnchId.getValue()).equals("minecraft:sweeping")) { GeyserImpl.getInstance().getLogger().debug("Unknown Java enchantment while NBT item translating: " + enchantId);
Tag javaEnchLvl = tag.get("lvl");
int sweepingLvl = javaEnchLvl != null && javaEnchLvl.getValue() instanceof Number lvl ? lvl.intValue() : 0;
addSweeping(session, rootTag, sweepingLvl);
return null;
}
GeyserImpl.getInstance().getLogger().debug("Unknown Java enchantment while NBT item translating: " + javaEnchId.getValue());
return null; return null;
} }
Tag javaEnchLvl = tag.get("lvl"); return NbtMap.builder()
.putShort("id", (short) Enchantment.valueOf(enchantment.name()).ordinal())
NbtMapBuilder builder = NbtMap.builder(); .putShort("lvl", (short) level)
builder.putShort("id", (short) enchantment.ordinal()); .build();
builder.putShort("lvl", );
return builder.build();
} }
private void addSweeping(GeyserSession session, CompoundTag itemTag, int level) { private void addSweeping(GeyserSession session, BedrockItemBuilder builder, int level) {
CompoundTag displayTag = itemTag.get("display");
if (displayTag == null) {
displayTag = new CompoundTag("display");
itemTag.put(displayTag);
}
ListTag loreTag = displayTag.get("Lore");
if (loreTag == null) {
loreTag = new ListTag("Lore");
displayTag.put(loreTag);
}
String sweepingTranslation = MinecraftLocale.getLocaleString("enchantment.minecraft.sweeping", session.locale()); String sweepingTranslation = MinecraftLocale.getLocaleString("enchantment.minecraft.sweeping", session.locale());
String lvlTranslation = MinecraftLocale.getLocaleString("enchantment.level." + level, session.locale()); String lvlTranslation = MinecraftLocale.getLocaleString("enchantment.level." + level, session.locale());
loreTag.add(new StringTag("", ChatColor.RESET + ChatColor.GRAY + sweepingTranslation + " " + lvlTranslation)); builder.getOrCreateLore().add(ChatColor.RESET + ChatColor.GRAY + sweepingTranslation + " " + lvlTranslation);
} }
/* Translation methods end */ /* Translation methods end */

View file

@ -38,7 +38,6 @@ import org.geysermc.geyser.inventory.item.Potion;
import org.geysermc.geyser.registry.type.ItemMapping; import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings; import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.translator.item.CustomItemTranslator; import org.geysermc.geyser.translator.item.CustomItemTranslator;
import org.geysermc.geyser.translator.item.ItemTranslator;
public class PotionItem extends Item { public class PotionItem extends Item {
public PotionItem(String javaIdentifier, Builder builder) { public PotionItem(String javaIdentifier, Builder builder) {
@ -52,14 +51,14 @@ public class PotionItem extends Item {
if (potionContents != null) { if (potionContents != null) {
ItemDefinition customItemDefinition = CustomItemTranslator.getCustomItem(components, mapping); ItemDefinition customItemDefinition = CustomItemTranslator.getCustomItem(components, mapping);
if (customItemDefinition == null) { if (customItemDefinition == null) {
Potion potion = Potion.getByJavaIdentifier(((StringTag) potionTag).getValue()); Potion potion = Potion.VALUES[potionContents.getPotionId()];
if (potion != null) { if (potion != null) {
return ItemData.builder() return ItemData.builder()
.definition(mapping.getBedrockDefinition()) .definition(mapping.getBedrockDefinition())
.damage(potion.getBedrockId()) .damage(potion.getBedrockId())
.count(count); .count(count);
} }
GeyserImpl.getInstance().getLogger().debug("Unknown Java potion: " + potionTag.getValue()); GeyserImpl.getInstance().getLogger().debug("Unknown Java potion: " + potionContents.getPotionId());
} else { } else {
return ItemData.builder() return ItemData.builder()
.definition(customItemDefinition) .definition(customItemDefinition)
@ -75,7 +74,7 @@ public class PotionItem extends Item {
ItemStack itemStack = super.translateToJava(itemData, mapping, mappings); ItemStack itemStack = super.translateToJava(itemData, mapping, mappings);
if (potion != null) { if (potion != null) {
StringTag potionTag = new StringTag("Potion", potion.getJavaIdentifier()); StringTag potionTag = new StringTag("Potion", potion.getJavaIdentifier());
itemStack.getNbt().put(potionTag); //itemStack.getNbt().put(potionTag);
} }
return itemStack; return itemStack;
} }

View file

@ -26,10 +26,6 @@
package org.geysermc.geyser.item.type; package org.geysermc.geyser.item.type;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents; 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.IntTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.item.components.ToolTier; import org.geysermc.geyser.item.components.ToolTier;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
@ -44,21 +40,22 @@ public class ShieldItem extends Item {
public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) { public void translateComponentsToBedrock(@NonNull GeyserSession session, @NonNull DataComponents components, @NonNull BedrockItemBuilder builder) {
super.translateComponentsToBedrock(session, components, builder); super.translateComponentsToBedrock(session, components, builder);
if (tag.remove("BlockEntityTag") instanceof CompoundTag blockEntityTag) { // TODO figure out patterns first.
if (blockEntityTag.get("Patterns") instanceof ListTag patterns) { // if (tag.remove("BlockEntityTag") instanceof CompoundTag blockEntityTag) {
for (Tag pattern : patterns) { // if (blockEntityTag.get("Patterns") instanceof ListTag patterns) {
if (((CompoundTag) pattern).get("Color") instanceof IntTag color) { // for (Tag pattern : patterns) {
color.setValue(15 - color.getValue()); // if (((CompoundTag) pattern).get("Color") instanceof IntTag color) {
} // color.setValue(15 - color.getValue());
} // }
// Bedrock looks for patterns at the root // }
tag.put(patterns); // // Bedrock looks for patterns at the root
} // tag.put(patterns);
if (blockEntityTag.get("Base") instanceof IntTag base) { // }
base.setValue(15 - base.getValue()); // if (blockEntityTag.get("Base") instanceof IntTag base) {
tag.put(base); // base.setValue(15 - base.getValue());
} // tag.put(base);
} // }
// }
} }
@Override @Override

View file

@ -25,15 +25,14 @@
package org.geysermc.geyser.item.type; 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.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.mc.protocol.data.game.item.component.PotionContents;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData; import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.item.TippedArrowPotion; import org.geysermc.geyser.inventory.item.TippedArrowPotion;
import org.geysermc.geyser.registry.type.ItemMapping; import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings; import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.translator.item.ItemTranslator;
public class TippedArrowItem extends ArrowItem { public class TippedArrowItem extends ArrowItem {
public TippedArrowItem(String javaIdentifier, Builder builder) { public TippedArrowItem(String javaIdentifier, Builder builder) {
@ -42,16 +41,16 @@ public class TippedArrowItem extends ArrowItem {
@Override @Override
public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) { public ItemData.Builder translateToBedrock(int count, DataComponents components, ItemMapping mapping, ItemMappings mappings) {
Tag potionTag = itemStack.getNbt().get("Potion"); PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
if (potionTag instanceof StringTag) { if (potionContents != null) {
TippedArrowPotion tippedArrowPotion = TippedArrowPotion.getByJavaIdentifier(((StringTag) potionTag).getValue()); TippedArrowPotion tippedArrowPotion = TippedArrowPotion.of(potionContents.getPotionId());
if (tippedArrowPotion != null) { if (tippedArrowPotion != null) {
return ItemData.builder() return ItemData.builder()
.definition(mapping.getBedrockDefinition()) .definition(mapping.getBedrockDefinition())
.damage(tippedArrowPotion.getBedrockId()) .damage(tippedArrowPotion.getBedrockId())
.count(count); .count(count);
} }
GeyserImpl.getInstance().getLogger().debug("Unknown Java potion (tipped arrow): " + potionTag.getValue()); GeyserImpl.getInstance().getLogger().debug("Unknown Java potion (tipped arrow): " + potionContents.getPotionId());
} }
return super.translateToBedrock(count, components, mapping, mappings); return super.translateToBedrock(count, components, mapping, mappings);
} }

View file

@ -27,7 +27,8 @@ 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.DataComponentType;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents; import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.opennbt.tag.builtin.*; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.Style;
@ -39,7 +40,6 @@ import org.geysermc.geyser.text.MinecraftLocale;
import org.geysermc.geyser.translator.item.BedrockItemBuilder; import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import org.geysermc.geyser.translator.text.MessageTranslator; import org.geysermc.geyser.translator.text.MessageTranslator;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TropicalFishBucketItem extends Item { public class TropicalFishBucketItem extends Item {
@ -57,24 +57,24 @@ public class TropicalFishBucketItem extends Item {
builder.putByte("AppendCustomName", (byte) 1); builder.putByte("AppendCustomName", (byte) 1);
builder.putString("CustomName", MinecraftLocale.getLocaleString("entity.minecraft.tropical_fish", session.locale())); builder.putString("CustomName", MinecraftLocale.getLocaleString("entity.minecraft.tropical_fish", session.locale()));
// Add Java's client side lore tag // Add Java's client side lore tag
components.get(DataComponentType) // Do you know how frequently Java NBT used to be before 1.20.5? It was a lot. And now it's just this lowly check.
if (bucketVariantTag instanceof IntTag) { CompoundTag entityTag = components.get(DataComponentType.BUCKET_ENTITY_DATA);
CompoundTag displayTag = tag.get("display"); if (entityTag != null && !entityTag.isEmpty()) {
if (displayTag == null) { //TODO test
displayTag = new CompoundTag("display"); Tag bucketVariant = entityTag.get("BucketVariantTag");
tag.put(displayTag); if (bucketVariant == null || !(bucketVariant.getValue() instanceof Number)) {
return;
} }
List<String> lore = builder.getOrCreateLore();
List<Tag> lore = new ArrayList<>(); int varNumber = ((Number) bucketVariant.getValue()).intValue();
int varNumber = ((IntTag) bucketVariantTag).getValue();
int predefinedVariantId = TropicalFishEntity.getPredefinedId(varNumber); int predefinedVariantId = TropicalFishEntity.getPredefinedId(varNumber);
if (predefinedVariantId != -1) { if (predefinedVariantId != -1) {
Component tooltip = Component.translatable("entity.minecraft.tropical_fish.predefined." + predefinedVariantId, LORE_STYLE); Component tooltip = Component.translatable("entity.minecraft.tropical_fish.predefined." + predefinedVariantId, LORE_STYLE);
lore.add(0, new StringTag("", MessageTranslator.convertMessage(tooltip, session.locale()))); lore.add(0, MessageTranslator.convertMessage(tooltip, session.locale()));
} else { } else {
Component typeTooltip = Component.translatable("entity.minecraft.tropical_fish.type." + TropicalFishEntity.getVariantName(varNumber), LORE_STYLE); Component typeTooltip = Component.translatable("entity.minecraft.tropical_fish.type." + TropicalFishEntity.getVariantName(varNumber), LORE_STYLE);
lore.add(0, new StringTag("", MessageTranslator.convertMessage(typeTooltip, session.locale()))); lore.add(0, MessageTranslator.convertMessage(typeTooltip, session.locale()));
byte baseColor = TropicalFishEntity.getBaseColor(varNumber); byte baseColor = TropicalFishEntity.getBaseColor(varNumber);
byte patternColor = TropicalFishEntity.getPatternColor(varNumber); byte patternColor = TropicalFishEntity.getPatternColor(varNumber);
@ -83,14 +83,8 @@ public class TropicalFishBucketItem extends Item {
colorTooltip = colorTooltip.append(Component.text(", ", LORE_STYLE)) colorTooltip = colorTooltip.append(Component.text(", ", LORE_STYLE))
.append(Component.translatable("color.minecraft." + TropicalFishEntity.getColorName(patternColor), LORE_STYLE)); .append(Component.translatable("color.minecraft." + TropicalFishEntity.getColorName(patternColor), LORE_STYLE));
} }
lore.add(1, new StringTag("", MessageTranslator.convertMessage(colorTooltip, session.locale()))); lore.add(1, MessageTranslator.convertMessage(colorTooltip, session.locale()));
} }
ListTag loreTag = displayTag.get("Lore");
if (loreTag != null) {
lore.addAll(loreTag.getValue());
}
displayTag.put(new ListTag("Lore", lore));
} }
} }
} }

View file

@ -25,13 +25,14 @@
package org.geysermc.geyser.session.cache; package org.geysermc.geyser.session.cache;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.GlobalPos;
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.LodestoneTracker; import com.github.steveice10.mc.protocol.data.game.item.component.LodestoneTracker;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.GeyserItemStack; import org.geysermc.geyser.inventory.GeyserItemStack;
import java.util.Map; import java.util.Map;
@ -54,22 +55,27 @@ public final class LodestoneCache {
private int id = 1; private int id = 1;
public void cacheInventoryItem(GeyserItemStack itemStack) { public void cacheInventoryItem(GeyserItemStack itemStack) {
CompoundTag tag = itemStack.getNbt(); DataComponents components = itemStack.getComponents();
if (tag == null) { if (components == null) {
// invalid // invalid
return; return;
} }
CompoundTag lodestonePos = tag.get("LodestonePos"); LodestoneTracker tracker = components.get(DataComponentType.LODESTONE_TRACKER);
if (lodestonePos == null) { if (tracker == null) {
// invalid
return; return;
} }
// Get all info needed for tracking GlobalPos position = tracker.getPos();
int x = ((IntTag) lodestonePos.get("X")).getValue();
int y = ((IntTag) lodestonePos.get("Y")).getValue(); if (position == null) {
int z = ((IntTag) lodestonePos.get("Z")).getValue(); GeyserImpl.getInstance().getLogger().error("Position is null. Find out why.");
String dim = ((StringTag) tag.get("LodestoneDimension")).getValue(); Thread.dumpStack();
return;
}
int x = position.getX();
int y = position.getY();
int z = position.getZ();
String dim = position.getDimension();
for (LodestonePos pos : this.activeLodestones.values()) { for (LodestonePos pos : this.activeLodestones.values()) {
if (pos.equals(x, y, z, dim)) { if (pos.equals(x, y, z, dim)) {
@ -90,17 +96,17 @@ public final class LodestoneCache {
} }
public int store(LodestoneTracker tracker) { public int store(LodestoneTracker tracker) {
CompoundTag lodestonePos = tag.get("LodestonePos"); GlobalPos position = tracker.getPos();
if (lodestonePos == null) {
// invalid
return 0;
}
// Get all info needed for tracking if (position == null) {
int x = ((IntTag) lodestonePos.get("X")).getValue(); GeyserImpl.getInstance().getLogger().error("Position is null. Find out why.");
int y = ((IntTag) lodestonePos.get("Y")).getValue(); Thread.dumpStack();
int z = ((IntTag) lodestonePos.get("Z")).getValue(); return -1;
String dim = ((StringTag) tag.get("LodestoneDimension")).getValue(); }
int x = position.getX();
int y = position.getY();
int z = position.getZ();
String dim = position.getDimension();
for (LodestonePos pos : this.activeLodestones.values()) { for (LodestonePos pos : this.activeLodestones.values()) {
if (pos.equals(x, y, z, dim)) { if (pos.equals(x, y, z, dim)) {

View file

@ -25,6 +25,7 @@
package org.geysermc.geyser.translator.inventory; package org.geysermc.geyser.translator.inventory;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.ListTag;
@ -51,6 +52,7 @@ import org.geysermc.geyser.item.type.DyeItem;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator { public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
@ -154,9 +156,10 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
GeyserItemStack inputCopy = inventory.getItem(0).copy(1); GeyserItemStack inputCopy = inventory.getItem(0).copy(1);
inputCopy.setNetId(session.getNextItemNetId()); inputCopy.setNetId(session.getNextItemNetId());
// Add the pattern manually, for better item synchronization // Add the pattern manually, for better item synchronization
if (inputCopy.getNbt() == null) { if (inputCopy.getComponents() == null) {
inputCopy.setNbt(new CompoundTag("")); inputCopy.setComponents(new DataComponents(new HashMap<>()));
} }
//TODO
CompoundTag blockEntityTag = inputCopy.getNbt().get("BlockEntityTag"); CompoundTag blockEntityTag = inputCopy.getNbt().get("BlockEntityTag");
CompoundTag javaBannerPattern = BannerItem.getJavaBannerPattern(pattern); CompoundTag javaBannerPattern = BannerItem.getJavaBannerPattern(pattern);

View file

@ -142,13 +142,12 @@ public final class BedrockItemBuilder {
} }
/** /**
* Creates item NBT with count, name, 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) {
NbtMapBuilder builder = NbtMap.builder(); NbtMapBuilder builder = NbtMap.builder();
builder.putByte("Count", (byte) count);
builder.putString("Name", mapping.getBedrockIdentifier()); builder.putString("Name", mapping.getBedrockIdentifier());
builder.putByte("Count", (byte) count);
builder.putShort("Damage", (short) damage); builder.putShort("Damage", (short) damage);
return builder; return builder;
} }

View file

@ -30,52 +30,42 @@ import com.github.steveice10.mc.protocol.data.game.Identifier;
import com.github.steveice10.mc.protocol.data.game.entity.attribute.ModifierOperation; import com.github.steveice10.mc.protocol.data.game.entity.attribute.ModifierOperation;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack; import com.github.steveice10.mc.protocol.data.game.item.ItemStack;
import com.github.steveice10.mc.protocol.data.game.item.component.AdventureModePredicate; import com.github.steveice10.mc.protocol.data.game.item.component.AdventureModePredicate;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponentType; 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.ItemAttributeModifiers; import com.github.steveice10.mc.protocol.data.game.item.component.ItemAttributeModifiers;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag; import com.github.steveice10.opennbt.tag.builtin.*;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.DoubleTag;
import com.github.steveice10.opennbt.tag.builtin.FloatTag;
import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
import com.github.steveice10.opennbt.tag.builtin.IntTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.LongArrayTag;
import com.github.steveice10.opennbt.tag.builtin.LongTag;
import com.github.steveice10.opennbt.tag.builtin.ShortTag;
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.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.cloudburstmc.protocol.bedrock.data.definitions.BlockDefinition;
import org.geysermc.geyser.api.block.custom.CustomBlockData;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.nbt.NbtList; import org.cloudburstmc.nbt.NbtList;
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.BlockDefinition;
import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition; import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData; import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.block.custom.CustomBlockData;
import org.geysermc.geyser.inventory.GeyserItemStack; import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.item.Items; import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.item.type.Item; import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.registry.BlockRegistries; import org.geysermc.geyser.registry.BlockRegistries;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.registry.type.CustomSkull; import org.geysermc.geyser.registry.type.CustomSkull;
import org.geysermc.geyser.registry.type.ItemMapping; import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings; import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.skin.SkinManager;
import org.geysermc.geyser.registry.Registries;
import org.geysermc.geyser.text.ChatColor; import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.text.MinecraftLocale; import org.geysermc.geyser.text.MinecraftLocale;
import org.geysermc.geyser.translator.text.MessageTranslator; import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.InventoryUtils; import org.geysermc.geyser.util.InventoryUtils;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class ItemTranslator { public final class ItemTranslator {
@ -357,7 +347,7 @@ public final class ItemTranslator {
return ItemDefinition.AIR; return ItemDefinition.AIR;
} }
ItemMapping mapping = itemStack.asItem().toBedrockDefinition(itemStack.getNbt(), session.getItemMappings()); ItemMapping mapping = itemStack.asItem().toBedrockDefinition(itemStack.getComponents(), session.getItemMappings());
ItemDefinition itemDefinition = mapping.getBedrockDefinition(); ItemDefinition itemDefinition = mapping.getBedrockDefinition();
CustomBlockData customBlockData = BlockRegistries.CUSTOM_BLOCK_ITEM_OVERRIDES.getOrDefault( CustomBlockData customBlockData = BlockRegistries.CUSTOM_BLOCK_ITEM_OVERRIDES.getOrDefault(
@ -563,20 +553,21 @@ public final class ItemTranslator {
if (components == null) { if (components == null) {
return null; return null;
} }
//TODO
GameProfile profile = components.get(DataComponentType.PROFILE); GameProfile profile = components.get(DataComponentType.PROFILE);
if (profile != null) { if (profile != null) {
if (!(nbt.get("SkullOwner") instanceof CompoundTag skullOwner)) { // if (!(nbt.get("SkullOwner") instanceof CompoundTag skullOwner)) {
// It's a username give up d: // // It's a username give up d:
return null; // return null;
} // }
SkinManager.GameProfileData data = SkinManager.GameProfileData.from(skullOwner); // SkinManager.GameProfileData data = SkinManager.GameProfileData.from(skullOwner);
if (data == null) { // if (data == null) {
session.getGeyser().getLogger().debug("Not sure how to handle skull head item display. " + nbt); // session.getGeyser().getLogger().debug("Not sure how to handle skull head item display. " + nbt);
return null; // return null;
} // }
//
String skinHash = data.skinUrl().substring(data.skinUrl().lastIndexOf('/') + 1); // String skinHash = data.skinUrl().substring(data.skinUrl().lastIndexOf('/') + 1);
return BlockRegistries.CUSTOM_SKULLS.get(skinHash); // return BlockRegistries.CUSTOM_SKULLS.get(skinHash);
} }
return null; return null;
} }

View file

@ -26,6 +26,8 @@
package org.geysermc.geyser.translator.protocol.bedrock; package org.geysermc.geyser.translator.protocol.bedrock;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack; 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.mc.protocol.data.game.item.component.WritableBookContent;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundEditBookPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundEditBookPacket;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.ListTag;
@ -39,10 +41,7 @@ import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.translator.text.MessageTranslator; import org.geysermc.geyser.translator.text.MessageTranslator;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@Translator(packet = BookEditPacket.class) @Translator(packet = BookEditPacket.class)
public class BedrockBookEditTranslator extends PacketTranslator<BookEditPacket> { public class BedrockBookEditTranslator extends PacketTranslator<BookEditPacket> {
@ -56,8 +55,9 @@ public class BedrockBookEditTranslator extends PacketTranslator<BookEditPacket>
GeyserItemStack itemStack = session.getPlayerInventory().getItemInHand(); GeyserItemStack itemStack = session.getPlayerInventory().getItemInHand();
if (itemStack != null) { if (itemStack != null) {
CompoundTag tag = itemStack.getNbt() != null ? itemStack.getNbt() : new CompoundTag(""); DataComponents components = itemStack.getComponents() != null ? itemStack.getComponents() : new DataComponents(new HashMap<>());
ItemStack bookItem = new ItemStack(itemStack.getJavaId(), itemStack.getAmount(), tag); ItemStack bookItem = new ItemStack(itemStack.getJavaId(), itemStack.getAmount(), components);
WritableBookContent
List<Tag> pages = tag.contains("pages") ? new LinkedList<>(((ListTag) tag.get("pages")).getValue()) : new LinkedList<>(); List<Tag> pages = tag.contains("pages") ? new LinkedList<>(((ListTag) tag.get("pages")).getValue()) : new LinkedList<>();
int page = packet.getPageNumber(); int page = packet.getPageNumber();

View file

@ -31,10 +31,7 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.entity.Client
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData; import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.entity.type.Entity; import org.geysermc.geyser.entity.type.Entity;
import org.geysermc.geyser.entity.type.LivingEntity; import org.geysermc.geyser.entity.type.LivingEntity;
import org.geysermc.geyser.entity.type.player.PlayerEntity;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.skin.FakeHeadProvider;
import org.geysermc.geyser.translator.item.ItemTranslator; import org.geysermc.geyser.translator.item.ItemTranslator;
import org.geysermc.geyser.translator.protocol.PacketTranslator; import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator; import org.geysermc.geyser.translator.protocol.Translator;
@ -62,14 +59,15 @@ public class JavaSetEquipmentTranslator extends PacketTranslator<ClientboundSetE
switch (equipment.getSlot()) { switch (equipment.getSlot()) {
case HELMET -> { case HELMET -> {
ItemStack javaItem = equipment.getItem(); ItemStack javaItem = equipment.getItem();
if (livingEntity instanceof PlayerEntity // TODO
&& javaItem != null // if (livingEntity instanceof PlayerEntity
&& javaItem.getId() == Items.PLAYER_HEAD.javaId() // && javaItem != null
&& javaItem.getNbt() != null) { // && javaItem.getId() == Items.PLAYER_HEAD.javaId()
FakeHeadProvider.setHead(session, (PlayerEntity) livingEntity, javaItem.getNbt().get("SkullOwner")); // && javaItem.getNbt() != null) {
} else { // FakeHeadProvider.setHead(session, (PlayerEntity) livingEntity, javaItem.getNbt().get("SkullOwner"));
FakeHeadProvider.restoreOriginalSkin(session, livingEntity); // } else {
} // FakeHeadProvider.restoreOriginalSkin(session, livingEntity);
// }
livingEntity.setHelmet(item); livingEntity.setHelmet(item);
armorUpdated = true; armorUpdated = true;

View file

@ -25,13 +25,12 @@
package org.geysermc.geyser.util; package org.geysermc.geyser.util;
import com.github.steveice10.mc.protocol.data.game.item.ItemStack;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
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.mc.protocol.data.game.item.component.DataComponents;
import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient; import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundPickItemPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundPickItemPacket;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCreativeModeSlotPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundSetCreativeModeSlotPacket;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.math.vector.Vector3i; import org.cloudburstmc.math.vector.Vector3i;
import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtMap;
@ -43,11 +42,7 @@ import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.cloudburstmc.protocol.bedrock.packet.InventorySlotPacket; import org.cloudburstmc.protocol.bedrock.packet.InventorySlotPacket;
import org.cloudburstmc.protocol.bedrock.packet.PlayerHotbarPacket; import org.cloudburstmc.protocol.bedrock.packet.PlayerHotbarPacket;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.Container; import org.geysermc.geyser.inventory.*;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.inventory.Inventory;
import org.geysermc.geyser.inventory.LecternContainer;
import org.geysermc.geyser.inventory.PlayerInventory;
import org.geysermc.geyser.inventory.click.Click; import org.geysermc.geyser.inventory.click.Click;
import org.geysermc.geyser.inventory.recipe.GeyserRecipe; import org.geysermc.geyser.inventory.recipe.GeyserRecipe;
import org.geysermc.geyser.inventory.recipe.GeyserShapedRecipe; import org.geysermc.geyser.inventory.recipe.GeyserShapedRecipe;
@ -256,7 +251,7 @@ public class InventoryUtils {
continue; continue;
} }
// If this is the item we're looking for // If this is the item we're looking for
if (geyserItem.getJavaId() == itemStack.getId() && Objects.equals(geyserItem.getNbt(), itemStack.getNbt())) { if (geyserItem.getJavaId() == itemStack.getId() && Objects.equals(geyserItem.getComponents(), itemStack.getDataComponents())) { //TODO verify
setHotbarItem(session, i); setHotbarItem(session, i);
// Don't check inventory if item was in hotbar // Don't check inventory if item was in hotbar
return; return;
@ -270,7 +265,7 @@ public class InventoryUtils {
continue; continue;
} }
// If this is the item we're looking for // If this is the item we're looking for
if (geyserItem.getJavaId() == itemStack.getId() && Objects.equals(geyserItem.getNbt(), itemStack.getNbt())) { if (geyserItem.getJavaId() == itemStack.getId() && Objects.equals(geyserItem.getComponents(), itemStack.getDataComponents())) { //TODO verify
ServerboundPickItemPacket packetToSend = new ServerboundPickItemPacket(i); // https://wiki.vg/Protocol#Pick_Item ServerboundPickItemPacket packetToSend = new ServerboundPickItemPacket(i); // https://wiki.vg/Protocol#Pick_Item
session.sendDownstreamGamePacket(packetToSend); session.sendDownstreamGamePacket(packetToSend);
return; return;