some work on adventure
This commit is contained in:
parent
c95db4fcaa
commit
bb1351cb8d
8 changed files with 51 additions and 27 deletions
|
@ -14,10 +14,10 @@ Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
|
|||
|
||||
diff --git a/src/main/java/io/papermc/paper/adventure/AdventureCodecs.java b/src/main/java/io/papermc/paper/adventure/AdventureCodecs.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..b33e394d88517c7afc2f549bae5a2063316ada73
|
||||
index 0000000000000000000000000000000000000000..87391401d1627009e5f2be3847f59c9a752fe52a
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/adventure/AdventureCodecs.java
|
||||
@@ -0,0 +1,458 @@
|
||||
@@ -0,0 +1,459 @@
|
||||
+package io.papermc.paper.adventure;
|
||||
+
|
||||
+import com.google.gson.JsonElement;
|
||||
|
@ -152,7 +152,7 @@ index 0000000000000000000000000000000000000000..b33e394d88517c7afc2f549bae5a2063
|
|||
+ static Codec<HoverEvent.ShowItem> showItemCodec(final Codec<Component> componentCodec) {
|
||||
+ return net.minecraft.network.chat.HoverEvent.ItemStackInfo.CODEC.xmap(isi -> {
|
||||
+ @Subst("key") final String typeKey = isi.item.unwrapKey().orElseThrow().toString();
|
||||
+ return HoverEvent.ShowItem.showItem(Key.key(typeKey), isi.count, PaperAdventure.asBinaryTagHolder(isi.tag.orElse(null)));
|
||||
+ return HoverEvent.ShowItem.showItem(Key.key(typeKey), isi.count, PaperAdventure.dataComponents(isi.getItemStack()));
|
||||
+ }, si -> {
|
||||
+ final Item itemType = BuiltInRegistries.ITEM.get(PaperAdventure.asVanilla(si.item()));
|
||||
+ final ItemStack stack;
|
||||
|
@ -168,7 +168,7 @@ index 0000000000000000000000000000000000000000..b33e394d88517c7afc2f549bae5a2063
|
|||
+
|
||||
+ static final HoverEventType<HoverEvent.ShowEntity> SHOW_ENTITY_HOVER_EVENT_TYPE = new HoverEventType<>(AdventureCodecs::showEntityCodec, HoverEvent.Action.SHOW_ENTITY, "show_entity", AdventureCodecs::legacyDeserializeEntity);
|
||||
+ static final HoverEventType<HoverEvent.ShowItem> SHOW_ITEM_HOVER_EVENT_TYPE = new HoverEventType<>(AdventureCodecs::showItemCodec, HoverEvent.Action.SHOW_ITEM, "show_item", AdventureCodecs::legacyDeserializeItem);
|
||||
+ static final HoverEventType<Component> SHOW_TEXT_HOVER_EVENT_TYPE = new HoverEventType<>(identity(), HoverEvent.Action.SHOW_TEXT, "show_text", DataResult::success);
|
||||
+ static final HoverEventType<Component> SHOW_TEXT_HOVER_EVENT_TYPE = new HoverEventType<>(identity(), HoverEvent.Action.SHOW_TEXT, "show_text", (component, registryOps, codec) -> DataResult.success(component));
|
||||
+ static final Codec<HoverEventType<?>> HOVER_EVENT_TYPE_CODEC = StringRepresentable.fromValues(() -> new HoverEventType<?>[]{ SHOW_ENTITY_HOVER_EVENT_TYPE, SHOW_ITEM_HOVER_EVENT_TYPE, SHOW_TEXT_HOVER_EVENT_TYPE });
|
||||
+
|
||||
+ static DataResult<HoverEvent.ShowEntity> legacyDeserializeEntity(final Component component, final @Nullable RegistryOps<?> ops, final Codec<Component> componentCodec) {
|
||||
|
@ -188,11 +188,12 @@ index 0000000000000000000000000000000000000000..b33e394d88517c7afc2f549bae5a2063
|
|||
+ try {
|
||||
+ final CompoundTag tag = TagParser.parseTag(PlainTextComponentSerializer.plainText().serialize(component));
|
||||
+ final DynamicOps<Tag> dynamicOps = ops != null ? ops.withParent(NbtOps.INSTANCE) : NbtOps.INSTANCE;
|
||||
+ final DataResult<org.bukkit.inventory.ItemStack> stackResult = ItemStack.CODEC.parse(dynamicOps, tag).map(CraftItemStack::asCraftMirror);
|
||||
+ final DataResult<ItemStack> stackResult = ItemStack.CODEC.parse(dynamicOps, tag);
|
||||
+ return stackResult.map(stack -> {
|
||||
+ return HoverEvent.ShowItem.showItem(stack.getType().key(), stack.getAmount(), /* TODO */);
|
||||
+ final CraftItemStack craft = CraftItemStack.asCraftMirror(stack);
|
||||
+ return HoverEvent.ShowItem.showItem(craft.getType().key(), stack.getCount(), PaperAdventure.dataComponents(stack));
|
||||
+ });
|
||||
+ } catch (final CommandSyntaxException | IOException ex) {
|
||||
+ } catch (final CommandSyntaxException ex) {
|
||||
+ return DataResult.error(() -> "Failed to parse item tag: " + ex.getMessage());
|
||||
+ }
|
||||
+ }
|
||||
|
@ -1159,16 +1160,17 @@ index 0000000000000000000000000000000000000000..2fd6c3e65354071af71c7d8ebb97b559
|
|||
+}
|
||||
diff --git a/src/main/java/io/papermc/paper/adventure/PaperAdventure.java b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
|
||||
new file mode 100644
|
||||
index 0000000000000000000000000000000000000000..5bacfadf01777171e51858d450a68135d422bf0f
|
||||
index 0000000000000000000000000000000000000000..0a428c392fec7c088648ab2633c92f0600f404b3
|
||||
--- /dev/null
|
||||
+++ b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
|
||||
@@ -0,0 +1,421 @@
|
||||
@@ -0,0 +1,445 @@
|
||||
+package io.papermc.paper.adventure;
|
||||
+
|
||||
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
+import io.netty.util.AttributeKey;
|
||||
+import java.io.IOException;
|
||||
+import java.util.ArrayList;
|
||||
+import java.util.HashMap;
|
||||
+import java.util.List;
|
||||
+import java.util.Locale;
|
||||
+import java.util.Map;
|
||||
|
@ -1186,6 +1188,7 @@ index 0000000000000000000000000000000000000000..5bacfadf01777171e51858d450a68135
|
|||
+import net.kyori.adventure.text.Component;
|
||||
+import net.kyori.adventure.text.TranslatableComponent;
|
||||
+import net.kyori.adventure.text.TranslationArgument;
|
||||
+import net.kyori.adventure.text.event.DataComponentValue;
|
||||
+import net.kyori.adventure.text.flattener.ComponentFlattener;
|
||||
+import net.kyori.adventure.text.format.Style;
|
||||
+import net.kyori.adventure.text.format.TextColor;
|
||||
|
@ -1201,11 +1204,14 @@ index 0000000000000000000000000000000000000000..5bacfadf01777171e51858d450a68135
|
|||
+import net.minecraft.Util;
|
||||
+import net.minecraft.commands.CommandSourceStack;
|
||||
+import net.minecraft.core.Holder;
|
||||
+import net.minecraft.core.component.TypedDataComponent;
|
||||
+import net.minecraft.core.registries.BuiltInRegistries;
|
||||
+import net.minecraft.locale.Language;
|
||||
+import net.minecraft.nbt.CompoundTag;
|
||||
+import net.minecraft.nbt.ListTag;
|
||||
+import net.minecraft.nbt.NbtOps;
|
||||
+import net.minecraft.nbt.StringTag;
|
||||
+import net.minecraft.nbt.Tag;
|
||||
+import net.minecraft.nbt.TagParser;
|
||||
+import net.minecraft.network.chat.ComponentUtils;
|
||||
+import net.minecraft.network.protocol.Packet;
|
||||
|
@ -1541,6 +1547,25 @@ index 0000000000000000000000000000000000000000..5bacfadf01777171e51858d450a68135
|
|||
+
|
||||
+ // NBT
|
||||
+
|
||||
+ public static Map<Key, ? extends DataComponentValue> dataComponents(
|
||||
+ final ItemStack stack
|
||||
+ ) {
|
||||
+ final Map<Key, DataComponentValue> map = new HashMap<>();
|
||||
+ for (final TypedDataComponent<?> component : stack.getComponents()) {
|
||||
+ final ResourceLocation key = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(component.type());
|
||||
+ final DataComponentValue value = new DataComponentValue.TagSerializable() {
|
||||
+ @Override
|
||||
+ public @NotNull BinaryTagHolder asBinaryTag() {
|
||||
+ return BinaryTagHolder.binaryTagHolder(
|
||||
+ component.encodeValue(NbtOps.INSTANCE).map(Tag::getAsString).getOrThrow()
|
||||
+ );
|
||||
+ }
|
||||
+ };
|
||||
+ map.put(Key.key(key.toString()), value);
|
||||
+ }
|
||||
+ return map;
|
||||
+ }
|
||||
+
|
||||
+ public static @Nullable BinaryTagHolder asBinaryTagHolder(final @Nullable CompoundTag tag) {
|
||||
+ if (tag == null) {
|
||||
+ return null;
|
||||
|
@ -4609,10 +4634,10 @@ index 4dd9a80af9901287ab6740b072f2b89678c3d0cb..b2586684295b295a3196a2a9cf724cec
|
|||
public String getTitle() {
|
||||
return this.title;
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java
|
||||
index 01963ef944da9251c038208c20012939afc77830..e5ee4a2e5cca2fd3a74ac58335ccf589ab87c9c5 100644
|
||||
index 01963ef944da9251c038208c20012939afc77830..cc142d8c3736f8f6817256865e9166b471de0344 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java
|
||||
@@ -501,4 +501,17 @@ public final class CraftItemFactory implements ItemFactory {
|
||||
@@ -501,4 +501,16 @@ public final class CraftItemFactory implements ItemFactory {
|
||||
CraftItemStack craft = (CraftItemStack) itemStack;
|
||||
return CraftItemStack.asCraftMirror(EnchantmentHelper.enchantItem(MinecraftServer.getServer().getWorldData().enabledFeatures(), source, craft.handle, level, allowTreasures));
|
||||
}
|
||||
|
@ -4620,8 +4645,7 @@ index 01963ef944da9251c038208c20012939afc77830..e5ee4a2e5cca2fd3a74ac58335ccf589
|
|||
+ // Paper start - Adventure
|
||||
+ @Override
|
||||
+ public net.kyori.adventure.text.event.HoverEvent<net.kyori.adventure.text.event.HoverEvent.ShowItem> asHoverEvent(final ItemStack item, final java.util.function.UnaryOperator<net.kyori.adventure.text.event.HoverEvent.ShowItem> op) {
|
||||
+ final net.minecraft.nbt.CompoundTag tag = CraftItemStack.asNMSCopy(item).getTag();
|
||||
+ return net.kyori.adventure.text.event.HoverEvent.showItem(op.apply(net.kyori.adventure.text.event.HoverEvent.ShowItem.showItem(item.getType().getKey(), item.getAmount(), io.papermc.paper.adventure.PaperAdventure.asBinaryTagHolder(tag))));
|
||||
+ return net.kyori.adventure.text.event.HoverEvent.showItem(op.apply(net.kyori.adventure.text.event.HoverEvent.ShowItem.showItem(item.getType().getKey(), item.getAmount(), io.papermc.paper.adventure.PaperAdventure.dataComponents(CraftItemStack.asNMSCopy(item)))));
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue