Little more work

This commit is contained in:
Camotoy 2024-04-21 16:36:54 -04:00
parent 687ddbb520
commit 61907b1851
No known key found for this signature in database
GPG Key ID: 7EEFB66FE798081F
6 changed files with 56 additions and 29 deletions

View File

@ -29,6 +29,7 @@ import com.github.steveice10.mc.protocol.data.game.inventory.ContainerType;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundRenameItemPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundRenameItemPacket;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.text.MessageTranslator; import org.geysermc.geyser.translator.text.MessageTranslator;
@ -72,7 +73,7 @@ public class AnvilContainer extends Container {
String correctRename; String correctRename;
newName = rename; newName = rename;
String originalName = ItemUtils.getCustomName(getInput().getNbt()); Component originalName = ItemUtils.getCustomName(getInput().getComponents());
String plainOriginalName = MessageTranslator.convertToPlainTextLenient(originalName, session.locale()); String plainOriginalName = MessageTranslator.convertToPlainTextLenient(originalName, session.locale());
String plainNewName = MessageTranslator.convertToPlainText(rename); String plainNewName = MessageTranslator.convertToPlainText(rename);

View File

@ -26,6 +26,7 @@
package org.geysermc.geyser.inventory; package org.geysermc.geyser.inventory;
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.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.CompoundTag;
import lombok.AccessLevel; import lombok.AccessLevel;
@ -87,6 +88,30 @@ public class GeyserItemStack {
return isEmpty() ? null : components; return isEmpty() ? null : components;
} }
public <T extends Boolean> boolean getComponent(DataComponentType<T> type, boolean def) {
if (components == null) {
return def;
}
Boolean result = components.get(type);
if (result != null) {
return result;
}
return def;
}
public <T extends Integer> int getComponent(DataComponentType<T> type, int def) {
if (components == null) {
return def;
}
Integer result = components.get(type);
if (result != null) {
return result;
}
return def;
}
public int getNetId() { public int getNetId() {
return isEmpty() ? 0 : netId; return isEmpty() ? 0 : netId;
} }

View File

@ -26,6 +26,7 @@
package org.geysermc.geyser.inventory.updater; package org.geysermc.geyser.inventory.updater;
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.component.DataComponentType;
import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundRenameItemPacket; import com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundRenameItemPacket;
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;
@ -34,6 +35,7 @@ import com.github.steveice10.opennbt.tag.builtin.Tag;
import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntMaps; import it.unimi.dsi.fastutil.objects.Object2IntMaps;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.kyori.adventure.text.Component;
import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder; import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerId; import org.cloudburstmc.protocol.bedrock.data.inventory.ContainerId;
@ -118,7 +120,8 @@ public class AnvilInventoryUpdater extends InventoryUpdater {
// Changing the item in the input slot resets the name field on Bedrock, but // Changing the item in the input slot resets the name field on Bedrock, but
// does not result in a FilterTextPacket // does not result in a FilterTextPacket
String originalName = MessageTranslator.convertToPlainTextLenient(ItemUtils.getCustomName(input.getNbt()), session.locale()); // TODO test
String originalName = MessageTranslator.convertToPlainText(ItemUtils.getCustomName(input.getComponents()));
ServerboundRenameItemPacket renameItemPacket = new ServerboundRenameItemPacket(originalName); ServerboundRenameItemPacket renameItemPacket = new ServerboundRenameItemPacket(originalName);
session.sendDownstreamGamePacket(renameItemPacket); session.sendDownstreamGamePacket(renameItemPacket);
@ -424,38 +427,27 @@ public class AnvilInventoryUpdater extends InventoryUpdater {
} }
// This should really check the name field in all cases, but that requires the localized name // This should really check the name field in all cases, but that requires the localized name
// of the item which can change depending on NBT and Minecraft Edition // of the item which can change depending on NBT and Minecraft Edition
String originalName = ItemUtils.getCustomName(anvilContainer.getInput().getNbt()); Component originalName = ItemUtils.getCustomName(anvilContainer.getInput().getComponents());
if (bedrock && originalName != null && anvilContainer.getNewName() != null) { if (bedrock && originalName != null && anvilContainer.getNewName() != null) {
// Check text and formatting // Check text and formatting
String legacyOriginalName = MessageTranslator.convertMessageLenient(originalName, session.locale()); String legacyOriginalName = MessageTranslator.convertMessage(originalName, session.locale());
return !legacyOriginalName.equals(anvilContainer.getNewName()); return !legacyOriginalName.equals(anvilContainer.getNewName());
} }
return !Objects.equals(originalName, ItemUtils.getCustomName(anvilContainer.getResult().getNbt())); return !Objects.equals(originalName, ItemUtils.getCustomName(anvilContainer.getResult().getComponents()));
}
@SuppressWarnings("SameParameterValue")
private int getTagIntValueOr(GeyserItemStack itemStack, String tagName, int defaultValue) {
if (itemStack.getNbt() != null) {
Tag tag = itemStack.getNbt().get(tagName);
if (tag != null && tag.getValue() instanceof Number value) {
return value.intValue();
}
}
return defaultValue;
} }
private int getRepairCost(GeyserItemStack itemStack) { private int getRepairCost(GeyserItemStack itemStack) {
return getTagIntValueOr(itemStack, "RepairCost", 0); return itemStack.getComponent(DataComponentType.REPAIR_COST, 0);
} }
private boolean hasDurability(GeyserItemStack itemStack) { private boolean hasDurability(GeyserItemStack itemStack) {
if (itemStack.asItem().maxDamage() > 0) { if (itemStack.asItem().maxDamage() > 0) {
return getTagIntValueOr(itemStack, "Unbreakable", 0) == 0; return itemStack.getComponent(DataComponentType.UNBREAKABLE, false);
} }
return false; return false;
} }
private int getDamage(GeyserItemStack itemStack) { private int getDamage(GeyserItemStack itemStack) {
return getTagIntValueOr(itemStack, "Damage", 0); return itemStack.getComponent(DataComponentType.DAMAGE, 0);
} }
} }

View File

@ -245,6 +245,16 @@ public class MessageTranslator {
return GSON_SERIALIZER.serialize(component); return GSON_SERIALIZER.serialize(component);
} }
/**
* Convert legacy format message to plain text
*
* @param message Message to convert
* @return The plain text of the message
*/
public static String convertToPlainText(Component message) {
return PlainTextComponentSerializer.plainText().serialize(message);
}
/** /**
* Convert legacy format message to plain text * Convert legacy format message to plain text
* *

View File

@ -180,7 +180,7 @@ public class InventoryUtils {
public static boolean canStack(GeyserItemStack item1, GeyserItemStack item2) { public static boolean canStack(GeyserItemStack item1, GeyserItemStack item2) {
if (item1.isEmpty() || item2.isEmpty()) if (item1.isEmpty() || item2.isEmpty())
return false; return false;
return item1.getJavaId() == item2.getJavaId() && Objects.equals(item1.getNbt(), item2.getNbt()); return item1.getJavaId() == item2.getJavaId() && Objects.equals(item1.getComponents(), item2.getComponents());
} }
/** /**

View File

@ -25,10 +25,13 @@
package org.geysermc.geyser.util; package org.geysermc.geyser.util;
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.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag; import com.github.steveice10.opennbt.tag.builtin.Tag;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.item.Items; import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.item.type.FishingRodItem; import org.geysermc.geyser.item.type.FishingRodItem;
@ -70,17 +73,13 @@ public class ItemUtils {
} }
/** /**
* @param itemTag the NBT tag of the item * @param components the data components of the item
* @return the custom name of the item * @return the custom name of the item
*/ */
public static @Nullable String getCustomName(CompoundTag itemTag) { public static @Nullable Component getCustomName(DataComponents components) {
if (itemTag != null) { if (components == null) {
if (itemTag.get("display") instanceof CompoundTag displayTag) { return null;
if (displayTag.get("Name") instanceof StringTag nameTag) {
return nameTag.getValue();
}
}
} }
return null; return components.get(DataComponentType.CUSTOM_NAME);
} }
} }