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.item.ItemStack;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.github.steveice10.mc.protocol.data.game.item.component.DataComponents;
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.packet.SetEntityMotionPacket;
import org.geysermc.floodgate.util.DeviceOs;
import org.geysermc.geyser.entity.EntityDefinition;
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.util.MathUtils;
import org.geysermc.geyser.translator.item.BedrockItemBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
import java.util.UUID;
@ -59,80 +51,16 @@ public class FireworkEntity extends Entity {
if (item == null) {
return;
}
CompoundTag tag = item.getNbt();
if (tag == null) {
DataComponents components = item.getDataComponents();
if (components == null) {
return;
}
// TODO: Remove once Mojang fixes bugs with fireworks crashing clients on these specific devices.
// https://bugs.mojang.com/browse/MCPE-89115
if (session.getClientData().getDeviceOs() == DeviceOs.XBOX
|| session.getClientData().getDeviceOs() == DeviceOs.PS4) {
return;
}
// TODO this looked the same, so I'm going to assume it is and (keep below comment if true)
// Translate using item methods to get firework NBT for Bedrock
BedrockItemBuilder builder = new BedrockItemBuilder();
Items.FIREWORK_ROCKET.translateComponentsToBedrock(session, components, builder);
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());
}

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.item.ItemStack;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
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 org.cloudburstmc.math.vector.Vector3f;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
@ -58,16 +59,17 @@ public class ThrownPotionEntity extends ThrowableItemEntity {
setFlag(EntityFlag.LINGERING, false);
} else {
// 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) {
Tag potionTag = itemStack.getNbt().get("Potion");
if (potionTag instanceof StringTag) {
Potion potion = Potion.getByJavaIdentifier(((StringTag) potionTag).getValue());
DataComponents components = itemStack.getDataComponents();
if (components != null) {
PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
if (potionContents != null) {
Potion potion = Potion.VALUES[potionContents.getPotionId()];
if (potion != null) {
dirtyMetadata.put(EntityDataTypes.EFFECT_COLOR, (int) potion.getBedrockId());
setFlag(EntityFlag.ENCHANTED, !NON_ENCHANTED_POTIONS.contains(potion));
} else {
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
public enum TippedArrowPotion {
WATER(-1, ArrowParticleColors.NONE), // Guessing this based off of the Potion enum. TODO merge?
MUNDANE(2, ArrowParticleColors.NONE), // 3 is extended?
THICK(4, ArrowParticleColors.NONE),
AWKWARD(5, ArrowParticleColors.NONE),
@ -94,6 +95,10 @@ public enum TippedArrowPotion {
this.javaColor = arrowParticleColor.getColor();
}
public static TippedArrowPotion of(int id) {
return VALUES[id];
}
public static @Nullable TippedArrowPotion getByJavaIdentifier(String javaIdentifier) {
for (TippedArrowPotion potion : VALUES) {
if (potion.javaIdentifier.equals(javaIdentifier)) {

View file

@ -26,8 +26,6 @@
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.PotionContents;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
@ -48,7 +46,7 @@ public class ArrowItem extends Item {
if (tippedArrowPotion != null) {
itemStack = Items.TIPPED_ARROW.newItemStack(itemStack.getAmount(), itemStack.getDataComponents());
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;
}

View file

@ -56,14 +56,14 @@ public class BannerItem extends BlockItem {
static {
// Construct what an ominous banner is supposed to look like
OMINOUS_BANNER_PATTERN = List.of(
new BannerPatternLayer("mr", 9),
new BannerPatternLayer("bs", 8),
new BannerPatternLayer("cs", 7),
new BannerPatternLayer("bo", 8),
new BannerPatternLayer("ms", 15),
new BannerPatternLayer("hh", 8),
new BannerPatternLayer("mc", 8),
new BannerPatternLayer("bo", 15)
// new BannerPatternLayer("mr", 9),
// new BannerPatternLayer("bs", 8),
// new BannerPatternLayer("cs", 7),
// new BannerPatternLayer("bo", 8),
// new BannerPatternLayer("ms", 15),
// new BannerPatternLayer("hh", 8),
// new BannerPatternLayer("mc", 8),
// new BannerPatternLayer("bo", 15)
);
}
@ -103,7 +103,8 @@ public class BannerItem extends BlockItem {
* @return The Java edition format pattern nbt
*/
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);
if (patterns != null) {
if (patterns.equals(OMINOUS_BANNER_PATTERN)) {
// Remove the current patterns and set the ominous banner type
builder.putInt("Type", 1);
} else {
invertBannerColors(patterns);
tag.put(patterns);
}
// if (patterns.equals(OMINOUS_BANNER_PATTERN)) {
// // Remove the current patterns and set the ominous banner type
// builder.putInt("Type", 1);
// } else {
// invertBannerColors(patterns);
// tag.put(patterns);
// }
}
}
@ -146,7 +147,7 @@ public class BannerItem extends BlockItem {
// Ominous banner pattern
tag.remove("Type");
CompoundTag blockEntityTag = new CompoundTag("BlockEntityTag");
blockEntityTag.put(OMINOUS_BANNER_PATTERN);
//blockEntityTag.put(OMINOUS_BANNER_PATTERN);
tag.put(blockEntityTag);
} 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) {
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 (blockEntityTag.remove("sherds") instanceof ListTag sherds) {
// // bedrock wants it on the root level

View file

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

View file

@ -64,16 +64,16 @@ public class GoatHornItem extends Item {
// TODO registry
if (instrument != null) {
// Drop the Minecraft namespace if applicable
if (instrument.startsWith("minecraft:")) {
instrument = instrument.substring("minecraft:".length());
}
int damage = INSTRUMENTS.indexOf(instrument);
if (damage == -1) {
damage = 0;
GeyserImpl.getInstance().getLogger().debug("Unknown goat horn instrument: " + instrumentTag.getValue());
}
builder.damage(damage);
// if (instrument.startsWith("minecraft:")) {
// instrument = instrument.substring("minecraft:".length());
// }
//
// int damage = INSTRUMENTS.indexOf(instrument);
// if (damage == -1) {
// damage = 0;
// GeyserImpl.getInstance().getLogger().debug("Unknown goat horn instrument: " + instrumentTag.getValue());
// }
// builder.damage(damage);
}
return builder;
}
@ -90,7 +90,7 @@ public class GoatHornItem extends Item {
String instrument = INSTRUMENTS.get(damage);
StringTag instrumentTag = new StringTag("instrument", "minecraft:" + instrument);
itemStack.getNbt().put(instrumentTag);
//itemStack.getNbt().put(instrumentTag);
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.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.DataComponents;
import com.github.steveice10.mc.protocol.data.game.item.component.ItemEnchantments;
import com.github.steveice10.opennbt.tag.builtin.*;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
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.geysermc.geyser.GeyserImpl;
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.ItemTranslator;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.InventoryUtils;
import java.util.ArrayList;
import java.util.List;
@ -113,7 +112,7 @@ public class Item {
if (itemData.getTag() == 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) {
@ -137,23 +136,19 @@ public class Item {
builder.setDamage(damage);
}
List<Tag> newTags = new ArrayList<>();
List<NbtMap> enchantNbtList = new ArrayList<>();
ItemEnchantments enchantments = components.get(DataComponentType.ENCHANTMENTS);
if (enchantments != null) {
}
if (enchantmentTag instanceof ListTag listTag) {
for (Tag subTag : listTag.getValue()) {
if (!(subTag instanceof CompoundTag)) continue;
CompoundTag bedrockTag = remapEnchantment(session, (CompoundTag) subTag, tag);
if (bedrockTag != null) {
newTags.add(bedrockTag);
for (Map.Entry<Integer, Integer> enchantment : enchantments.getEnchantments().entrySet()) {
NbtMap enchantNbt = remapEnchantment(session, enchantment.getKey(), enchantment.getValue(), builder);
if (enchantNbt != null) {
enchantNbtList.add(enchantNbt);
}
}
}
if (!newTags.isEmpty()) {
tag.put(new ListTag("ench", newTags));
if (!enchantNbtList.isEmpty()) {
builder.putList("ench", NbtType.COMPOUND, enchantNbtList);
}
}
@ -220,45 +215,30 @@ public class Item {
}
}
protected final @Nullable NbtMap remapEnchantment(GeyserSession session, ItemEnchantments, NbtMapBuilder rootBuilder) {
Enchantment enchantment = Enchantment.getByJavaIdentifier(((StringTag) javaEnchId).getValue());
protected final @Nullable NbtMap remapEnchantment(GeyserSession session, int enchantId, int level, BedrockItemBuilder builder) {
// TODO verify
// 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 (Identifier.formalize((String) javaEnchId.getValue()).equals("minecraft:sweeping")) {
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());
GeyserImpl.getInstance().getLogger().debug("Unknown Java enchantment while NBT item translating: " + enchantId);
return null;
}
Tag javaEnchLvl = tag.get("lvl");
NbtMapBuilder builder = NbtMap.builder();
builder.putShort("id", (short) enchantment.ordinal());
builder.putShort("lvl", );
return builder.build();
}
private void addSweeping(GeyserSession session, CompoundTag itemTag, 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);
return NbtMap.builder()
.putShort("id", (short) Enchantment.valueOf(enchantment.name()).ordinal())
.putShort("lvl", (short) level)
.build();
}
private void addSweeping(GeyserSession session, BedrockItemBuilder builder, int level) {
String sweepingTranslation = MinecraftLocale.getLocaleString("enchantment.minecraft.sweeping", 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 */

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

View file

@ -26,10 +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.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.geysermc.geyser.item.components.ToolTier;
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) {
super.translateComponentsToBedrock(session, components, builder);
if (tag.remove("BlockEntityTag") instanceof CompoundTag blockEntityTag) {
if (blockEntityTag.get("Patterns") instanceof ListTag patterns) {
for (Tag pattern : patterns) {
if (((CompoundTag) pattern).get("Color") instanceof IntTag color) {
color.setValue(15 - color.getValue());
}
}
// Bedrock looks for patterns at the root
tag.put(patterns);
}
if (blockEntityTag.get("Base") instanceof IntTag base) {
base.setValue(15 - base.getValue());
tag.put(base);
}
}
// TODO figure out patterns first.
// if (tag.remove("BlockEntityTag") instanceof CompoundTag blockEntityTag) {
// if (blockEntityTag.get("Patterns") instanceof ListTag patterns) {
// for (Tag pattern : patterns) {
// if (((CompoundTag) pattern).get("Color") instanceof IntTag color) {
// color.setValue(15 - color.getValue());
// }
// }
// // Bedrock looks for patterns at the root
// tag.put(patterns);
// }
// if (blockEntityTag.get("Base") instanceof IntTag base) {
// base.setValue(15 - base.getValue());
// tag.put(base);
// }
// }
}
@Override

View file

@ -25,15 +25,14 @@
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.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.github.steveice10.mc.protocol.data.game.item.component.PotionContents;
import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.item.TippedArrowPotion;
import org.geysermc.geyser.registry.type.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings;
import org.geysermc.geyser.translator.item.ItemTranslator;
public class TippedArrowItem extends ArrowItem {
public TippedArrowItem(String javaIdentifier, Builder builder) {
@ -42,16 +41,16 @@ public class TippedArrowItem extends ArrowItem {
@Override
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());
PotionContents potionContents = components.get(DataComponentType.POTION_CONTENTS);
if (potionContents != null) {
TippedArrowPotion tippedArrowPotion = TippedArrowPotion.of(potionContents.getPotionId());
if (tippedArrowPotion != null) {
return ItemData.builder()
.definition(mapping.getBedrockDefinition())
.damage(tippedArrowPotion.getBedrockId())
.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);
}

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.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.format.NamedTextColor;
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.text.MessageTranslator;
import java.util.ArrayList;
import java.util.List;
public class TropicalFishBucketItem extends Item {
@ -57,24 +57,24 @@ public class TropicalFishBucketItem extends Item {
builder.putByte("AppendCustomName", (byte) 1);
builder.putString("CustomName", MinecraftLocale.getLocaleString("entity.minecraft.tropical_fish", session.locale()));
// Add Java's client side lore tag
components.get(DataComponentType)
if (bucketVariantTag instanceof IntTag) {
CompoundTag displayTag = tag.get("display");
if (displayTag == null) {
displayTag = new CompoundTag("display");
tag.put(displayTag);
// 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.
CompoundTag entityTag = components.get(DataComponentType.BUCKET_ENTITY_DATA);
if (entityTag != null && !entityTag.isEmpty()) {
//TODO test
Tag bucketVariant = entityTag.get("BucketVariantTag");
if (bucketVariant == null || !(bucketVariant.getValue() instanceof Number)) {
return;
}
List<String> lore = builder.getOrCreateLore();
List<Tag> lore = new ArrayList<>();
int varNumber = ((IntTag) bucketVariantTag).getValue();
int varNumber = ((Number) bucketVariant.getValue()).intValue();
int predefinedVariantId = TropicalFishEntity.getPredefinedId(varNumber);
if (predefinedVariantId != -1) {
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 {
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 patternColor = TropicalFishEntity.getPatternColor(varNumber);
@ -83,14 +83,8 @@ public class TropicalFishBucketItem extends Item {
colorTooltip = colorTooltip.append(Component.text(", ", 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;
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.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.Int2ObjectOpenHashMap;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.GeyserItemStack;
import java.util.Map;
@ -54,22 +55,27 @@ public final class LodestoneCache {
private int id = 1;
public void cacheInventoryItem(GeyserItemStack itemStack) {
CompoundTag tag = itemStack.getNbt();
if (tag == null) {
DataComponents components = itemStack.getComponents();
if (components == null) {
// invalid
return;
}
CompoundTag lodestonePos = tag.get("LodestonePos");
if (lodestonePos == null) {
// invalid
LodestoneTracker tracker = components.get(DataComponentType.LODESTONE_TRACKER);
if (tracker == null) {
return;
}
// Get all info needed for tracking
int x = ((IntTag) lodestonePos.get("X")).getValue();
int y = ((IntTag) lodestonePos.get("Y")).getValue();
int z = ((IntTag) lodestonePos.get("Z")).getValue();
String dim = ((StringTag) tag.get("LodestoneDimension")).getValue();
GlobalPos position = tracker.getPos();
if (position == null) {
GeyserImpl.getInstance().getLogger().error("Position is null. Find out why.");
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()) {
if (pos.equals(x, y, z, dim)) {
@ -90,17 +96,17 @@ public final class LodestoneCache {
}
public int store(LodestoneTracker tracker) {
CompoundTag lodestonePos = tag.get("LodestonePos");
if (lodestonePos == null) {
// invalid
return 0;
}
GlobalPos position = tracker.getPos();
// Get all info needed for tracking
int x = ((IntTag) lodestonePos.get("X")).getValue();
int y = ((IntTag) lodestonePos.get("Y")).getValue();
int z = ((IntTag) lodestonePos.get("Z")).getValue();
String dim = ((StringTag) tag.get("LodestoneDimension")).getValue();
if (position == null) {
GeyserImpl.getInstance().getLogger().error("Position is null. Find out why.");
Thread.dumpStack();
return -1;
}
int x = position.getX();
int y = position.getY();
int z = position.getZ();
String dim = position.getDimension();
for (LodestonePos pos : this.activeLodestones.values()) {
if (pos.equals(x, y, z, dim)) {

View file

@ -25,6 +25,7 @@
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.opennbt.tag.builtin.CompoundTag;
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 java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
@ -154,9 +156,10 @@ public class LoomInventoryTranslator extends AbstractBlockInventoryTranslator {
GeyserItemStack inputCopy = inventory.getItem(0).copy(1);
inputCopy.setNetId(session.getNextItemNetId());
// Add the pattern manually, for better item synchronization
if (inputCopy.getNbt() == null) {
inputCopy.setNbt(new CompoundTag(""));
if (inputCopy.getComponents() == null) {
inputCopy.setComponents(new DataComponents(new HashMap<>()));
}
//TODO
CompoundTag blockEntityTag = inputCopy.getNbt().get("BlockEntityTag");
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) {
NbtMapBuilder builder = NbtMap.builder();
builder.putByte("Count", (byte) count);
builder.putString("Name", mapping.getBedrockIdentifier());
builder.putByte("Count", (byte) count);
builder.putShort("Damage", (short) damage);
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.item.ItemStack;
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.DataComponents;
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.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 com.github.steveice10.opennbt.tag.builtin.*;
import net.kyori.adventure.text.Component;
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.Nullable;
import org.cloudburstmc.nbt.NbtList;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
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.inventory.ItemData;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.block.custom.CustomBlockData;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.item.type.Item;
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.ItemMapping;
import org.geysermc.geyser.registry.type.ItemMappings;
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.MinecraftLocale;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.geyser.util.InventoryUtils;
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 {
@ -357,7 +347,7 @@ public final class ItemTranslator {
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();
CustomBlockData customBlockData = BlockRegistries.CUSTOM_BLOCK_ITEM_OVERRIDES.getOrDefault(
@ -563,20 +553,21 @@ public final class ItemTranslator {
if (components == null) {
return null;
}
//TODO
GameProfile profile = components.get(DataComponentType.PROFILE);
if (profile != null) {
if (!(nbt.get("SkullOwner") instanceof CompoundTag skullOwner)) {
// It's a username give up d:
return null;
}
SkinManager.GameProfileData data = SkinManager.GameProfileData.from(skullOwner);
if (data == null) {
session.getGeyser().getLogger().debug("Not sure how to handle skull head item display. " + nbt);
return null;
}
String skinHash = data.skinUrl().substring(data.skinUrl().lastIndexOf('/') + 1);
return BlockRegistries.CUSTOM_SKULLS.get(skinHash);
// if (!(nbt.get("SkullOwner") instanceof CompoundTag skullOwner)) {
// // It's a username give up d:
// return null;
// }
// SkinManager.GameProfileData data = SkinManager.GameProfileData.from(skullOwner);
// if (data == null) {
// session.getGeyser().getLogger().debug("Not sure how to handle skull head item display. " + nbt);
// return null;
// }
//
// String skinHash = data.skinUrl().substring(data.skinUrl().lastIndexOf('/') + 1);
// return BlockRegistries.CUSTOM_SKULLS.get(skinHash);
}
return null;
}

View file

@ -26,6 +26,8 @@
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.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.opennbt.tag.builtin.CompoundTag;
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.text.MessageTranslator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
@Translator(packet = BookEditPacket.class)
public class BedrockBookEditTranslator extends PacketTranslator<BookEditPacket> {
@ -56,8 +55,9 @@ public class BedrockBookEditTranslator extends PacketTranslator<BookEditPacket>
GeyserItemStack itemStack = session.getPlayerInventory().getItemInHand();
if (itemStack != null) {
CompoundTag tag = itemStack.getNbt() != null ? itemStack.getNbt() : new CompoundTag("");
ItemStack bookItem = new ItemStack(itemStack.getJavaId(), itemStack.getAmount(), tag);
DataComponents components = itemStack.getComponents() != null ? itemStack.getComponents() : new DataComponents(new HashMap<>());
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<>();
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.geysermc.geyser.entity.type.Entity;
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.skin.FakeHeadProvider;
import org.geysermc.geyser.translator.item.ItemTranslator;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
@ -62,14 +59,15 @@ public class JavaSetEquipmentTranslator extends PacketTranslator<ClientboundSetE
switch (equipment.getSlot()) {
case HELMET -> {
ItemStack javaItem = equipment.getItem();
if (livingEntity instanceof PlayerEntity
&& javaItem != null
&& javaItem.getId() == Items.PLAYER_HEAD.javaId()
&& javaItem.getNbt() != null) {
FakeHeadProvider.setHead(session, (PlayerEntity) livingEntity, javaItem.getNbt().get("SkullOwner"));
} else {
FakeHeadProvider.restoreOriginalSkin(session, livingEntity);
}
// TODO
// if (livingEntity instanceof PlayerEntity
// && javaItem != null
// && javaItem.getId() == Items.PLAYER_HEAD.javaId()
// && javaItem.getNbt() != null) {
// FakeHeadProvider.setHead(session, (PlayerEntity) livingEntity, javaItem.getNbt().get("SkullOwner"));
// } else {
// FakeHeadProvider.restoreOriginalSkin(session, livingEntity);
// }
livingEntity.setHelmet(item);
armorUpdated = true;

View file

@ -25,13 +25,12 @@
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.item.ItemStack;
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.packet.ingame.serverbound.inventory.ServerboundPickItemPacket;
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.cloudburstmc.math.vector.Vector3i;
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.PlayerHotbarPacket;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.inventory.Container;
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.*;
import org.geysermc.geyser.inventory.click.Click;
import org.geysermc.geyser.inventory.recipe.GeyserRecipe;
import org.geysermc.geyser.inventory.recipe.GeyserShapedRecipe;
@ -256,7 +251,7 @@ public class InventoryUtils {
continue;
}
// 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);
// Don't check inventory if item was in hotbar
return;
@ -270,7 +265,7 @@ public class InventoryUtils {
continue;
}
// 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
session.sendDownstreamGamePacket(packetToSend);
return;