2021-10-20 21:59:13 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: syldium <syldium@mailo.com>
|
|
|
|
Date: Fri, 9 Jul 2021 18:50:40 +0200
|
2022-11-04 23:59:15 +01:00
|
|
|
Subject: [PATCH] Add more advancement API
|
2021-10-20 21:59:13 +02:00
|
|
|
|
2022-11-25 08:05:48 +01:00
|
|
|
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
|
2021-10-20 21:59:13 +02:00
|
|
|
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/advancement/PaperAdvancementDisplay.java b/src/main/java/io/papermc/paper/advancement/PaperAdvancementDisplay.java
|
|
|
|
new file mode 100644
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/io/papermc/paper/advancement/PaperAdvancementDisplay.java
|
|
|
|
@@ -0,0 +0,0 @@
|
|
|
|
+package io.papermc.paper.advancement;
|
|
|
|
+
|
|
|
|
+import io.papermc.paper.adventure.PaperAdventure;
|
|
|
|
+import net.kyori.adventure.text.Component;
|
2022-11-25 08:05:48 +01:00
|
|
|
+import net.minecraft.advancements.Advancement;
|
2021-10-20 21:59:13 +02:00
|
|
|
+import net.minecraft.advancements.DisplayInfo;
|
|
|
|
+import net.minecraft.advancements.FrameType;
|
|
|
|
+import org.bukkit.NamespacedKey;
|
|
|
|
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
|
|
|
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
|
|
|
+import org.bukkit.inventory.ItemStack;
|
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
+import org.jetbrains.annotations.Nullable;
|
|
|
|
+
|
|
|
|
+public record PaperAdvancementDisplay(DisplayInfo handle) implements AdvancementDisplay {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public @NotNull Frame frame() {
|
|
|
|
+ return asPaperFrame(this.handle.getFrame());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public @NotNull Component title() {
|
|
|
|
+ return PaperAdventure.asAdventure(this.handle.getTitle());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public @NotNull Component description() {
|
|
|
|
+ return PaperAdventure.asAdventure(this.handle.getDescription());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public @NotNull ItemStack icon() {
|
|
|
|
+ return CraftItemStack.asBukkitCopy(this.handle.getIcon());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean doesShowToast() {
|
|
|
|
+ return this.handle.shouldShowToast();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean doesAnnounceToChat() {
|
|
|
|
+ return this.handle.shouldAnnounceChat();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean isHidden() {
|
|
|
|
+ return this.handle.isHidden();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public @Nullable NamespacedKey backgroundPath() {
|
|
|
|
+ return this.handle.getBackground() == null ? null : CraftNamespacedKey.fromMinecraft(this.handle.getBackground());
|
|
|
|
+ }
|
|
|
|
+
|
2022-11-25 08:05:48 +01:00
|
|
|
+ @Override
|
|
|
|
+ public @NotNull Component displayName() {
|
|
|
|
+ return PaperAdventure.asAdventure(Advancement.constructDisplayComponent(null, this.handle));
|
|
|
|
+ }
|
|
|
|
+
|
2021-10-20 21:59:13 +02:00
|
|
|
+ public static @NotNull Frame asPaperFrame(@NotNull FrameType frameType) {
|
|
|
|
+ return switch (frameType) {
|
|
|
|
+ case TASK -> Frame.TASK;
|
|
|
|
+ case CHALLENGE -> Frame.CHALLENGE;
|
|
|
|
+ case GOAL -> Frame.GOAL;
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+}
|
2022-11-25 08:05:48 +01:00
|
|
|
diff --git a/src/main/java/net/minecraft/advancements/Advancement.java b/src/main/java/net/minecraft/advancements/Advancement.java
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/net/minecraft/advancements/Advancement.java
|
|
|
|
+++ b/src/main/java/net/minecraft/advancements/Advancement.java
|
|
|
|
@@ -0,0 +0,0 @@ public class Advancement {
|
|
|
|
parent.addChild(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
- if (display == null) {
|
|
|
|
- this.chatComponent = Component.literal(id.toString());
|
|
|
|
+ // Paper start - moved to static method
|
|
|
|
+ this.chatComponent = constructDisplayComponent(this.id, this.display);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Component constructDisplayComponent(final @Nullable ResourceLocation id, final @Nullable DisplayInfo display) {
|
|
|
|
+ if (id == null && display == null) {
|
|
|
|
+ throw new IllegalArgumentException("can't both be null");
|
|
|
|
+ } else if (display == null) {
|
|
|
|
+ return Component.literal(id.toString());
|
|
|
|
+ // Paper end
|
|
|
|
} else {
|
|
|
|
Component ichatbasecomponent = display.getTitle();
|
|
|
|
ChatFormatting enumchatformat = display.getFrame().getChatColor();
|
|
|
|
@@ -0,0 +0,0 @@ public class Advancement {
|
|
|
|
return chatmodifier.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, ichatmutablecomponent));
|
|
|
|
});
|
|
|
|
|
|
|
|
- this.chatComponent = ComponentUtils.wrapInSquareBrackets(ichatmutablecomponent1).withStyle(enumchatformat);
|
|
|
|
+ return ComponentUtils.wrapInSquareBrackets(ichatmutablecomponent1).withStyle(enumchatformat); // Paper
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-10-20 21:59:13 +02:00
|
|
|
diff --git a/src/main/java/net/minecraft/advancements/DisplayInfo.java b/src/main/java/net/minecraft/advancements/DisplayInfo.java
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/net/minecraft/advancements/DisplayInfo.java
|
|
|
|
+++ b/src/main/java/net/minecraft/advancements/DisplayInfo.java
|
|
|
|
@@ -0,0 +0,0 @@ public class DisplayInfo {
|
|
|
|
private final boolean hidden;
|
|
|
|
private float x;
|
|
|
|
private float y;
|
|
|
|
+ public final io.papermc.paper.advancement.AdvancementDisplay paper = new io.papermc.paper.advancement.PaperAdvancementDisplay(this); // Paper
|
|
|
|
|
|
|
|
public DisplayInfo(ItemStack icon, Component title, Component description, @Nullable ResourceLocation background, FrameType frame, boolean showToast, boolean announceToChat, boolean hidden) {
|
|
|
|
this.title = title;
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
|
|
|
|
@@ -0,0 +0,0 @@ public class CraftAdvancement implements org.bukkit.advancement.Advancement {
|
|
|
|
return Collections.unmodifiableCollection(this.handle.getCriteria().keySet());
|
|
|
|
}
|
2022-06-05 10:43:06 +02:00
|
|
|
|
2021-10-20 21:59:13 +02:00
|
|
|
+ // Paper start
|
2022-06-05 10:43:06 +02:00
|
|
|
@Override
|
|
|
|
- public AdvancementDisplay getDisplay() {
|
|
|
|
- if (this.handle.getDisplay() == null) {
|
|
|
|
- return null;
|
2021-10-20 21:59:13 +02:00
|
|
|
+ public io.papermc.paper.advancement.AdvancementDisplay getDisplay() {
|
|
|
|
+ return this.handle.getDisplay() == null ? null : this.handle.getDisplay().paper;
|
2023-03-23 22:57:03 +01:00
|
|
|
+ }
|
2021-10-20 21:59:13 +02:00
|
|
|
+
|
2022-11-04 23:59:15 +01:00
|
|
|
+ @Deprecated @io.papermc.paper.annotation.DoNotUse
|
|
|
|
+ public AdvancementDisplay getDisplay0() { // May be called by plugins via Commodore
|
|
|
|
+ return this.handle.getDisplay() == null ? null : new CraftAdvancementDisplay(this.handle.getDisplay());
|
|
|
|
+ }
|
|
|
|
+
|
2021-10-20 21:59:13 +02:00
|
|
|
+ @Override
|
2022-11-25 08:05:48 +01:00
|
|
|
+ public net.kyori.adventure.text.Component displayName() {
|
|
|
|
+ return io.papermc.paper.adventure.PaperAdventure.asAdventure(Advancement.constructDisplayComponent(this.handle.getId(), this.handle.getDisplay()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
2021-10-20 21:59:13 +02:00
|
|
|
+ public org.bukkit.advancement.Advancement getParent() {
|
|
|
|
+ return this.handle.getParent() == null ? null : this.handle.getParent().bukkit;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Collection<org.bukkit.advancement.Advancement> getChildren() {
|
|
|
|
+ final var children = com.google.common.collect.ImmutableList.<org.bukkit.advancement.Advancement>builder();
|
|
|
|
+ for (Advancement advancement : this.handle.getChildren()) {
|
|
|
|
+ children.add(advancement.bukkit);
|
2023-03-23 22:57:03 +01:00
|
|
|
}
|
2021-10-20 21:59:13 +02:00
|
|
|
+ return children.build();
|
|
|
|
+ }
|
2023-03-23 22:57:03 +01:00
|
|
|
|
|
|
|
- return new CraftAdvancementDisplay(this.handle.getDisplay());
|
2021-10-20 21:59:13 +02:00
|
|
|
+ @Override
|
|
|
|
+ public org.bukkit.advancement.Advancement getRoot() {
|
|
|
|
+ Advancement advancement = this.handle;
|
|
|
|
+ while (advancement.getParent() != null) {
|
|
|
|
+ advancement = advancement.getParent();
|
|
|
|
+ }
|
|
|
|
+ return advancement.bukkit;
|
2023-03-23 22:57:03 +01:00
|
|
|
}
|
2021-10-20 21:59:13 +02:00
|
|
|
+ // Paper end
|
|
|
|
}
|
2022-11-04 23:59:15 +01:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java
|
|
|
|
@@ -0,0 +0,0 @@ import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
|
|
|
import org.bukkit.craftbukkit.util.CraftChatMessage;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
+@Deprecated // Paper
|
|
|
|
public class CraftAdvancementDisplay implements org.bukkit.advancement.AdvancementDisplay {
|
|
|
|
|
|
|
|
private final DisplayInfo handle;
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/util/Commodore.java b/src/main/java/org/bukkit/craftbukkit/util/Commodore.java
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/util/Commodore.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/util/Commodore.java
|
|
|
|
@@ -0,0 +0,0 @@ public class Commodore
|
|
|
|
) );
|
|
|
|
|
|
|
|
// Paper start - Plugin rewrites
|
|
|
|
+ private static final String CB_PACKAGE = org.bukkit.Bukkit.getServer().getClass().getPackageName().replace('.', '/');
|
|
|
|
private static final Map<String, String> SEARCH_AND_REMOVE = initReplacementsMap();
|
|
|
|
private static Map<String, String> initReplacementsMap()
|
2022-11-05 06:16:57 +01:00
|
|
|
{
|
2022-11-04 23:59:15 +01:00
|
|
|
@@ -0,0 +0,0 @@ public class Commodore
|
2022-12-20 19:56:33 +01:00
|
|
|
super.visitMethodInsn(opcode, owner, name, "()Lcom/destroystokyo/paper/profile/PlayerProfile;", itf);
|
|
|
|
return;
|
2022-11-04 23:59:15 +01:00
|
|
|
}
|
|
|
|
+ if (owner.equals("org/bukkit/advancement/Advancement") && name.equals("getDisplay") && desc.endsWith(")Lorg/bukkit/advancement/AdvancementDisplay;")) {
|
|
|
|
+ super.visitTypeInsn(Opcodes.CHECKCAST, CB_PACKAGE + "/advancement/CraftAdvancement");
|
|
|
|
+ super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CB_PACKAGE + "/advancement/CraftAdvancement", "getDisplay0", desc, false);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
if ( modern )
|
2022-12-20 19:56:33 +01:00
|
|
|
{
|
2021-10-20 21:59:13 +02:00
|
|
|
diff --git a/src/test/java/io/papermc/paper/advancement/AdvancementFrameTest.java b/src/test/java/io/papermc/paper/advancement/AdvancementFrameTest.java
|
|
|
|
new file mode 100644
|
|
|
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/test/java/io/papermc/paper/advancement/AdvancementFrameTest.java
|
|
|
|
@@ -0,0 +0,0 @@
|
|
|
|
+package io.papermc.paper.advancement;
|
|
|
|
+
|
|
|
|
+import io.papermc.paper.adventure.PaperAdventure;
|
|
|
|
+import net.kyori.adventure.text.format.TextColor;
|
|
|
|
+import net.minecraft.advancements.FrameType;
|
2022-06-08 17:31:27 +02:00
|
|
|
+import net.minecraft.network.chat.contents.TranslatableContents;
|
2021-10-20 21:59:13 +02:00
|
|
|
+import org.junit.Test;
|
|
|
|
+
|
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
|
+
|
|
|
|
+public class AdvancementFrameTest {
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void test() {
|
|
|
|
+ for (FrameType nmsFrameType : FrameType.values()) {
|
|
|
|
+ final TextColor expectedColor = PaperAdventure.asAdventure(nmsFrameType.getChatColor());
|
2022-06-08 17:31:27 +02:00
|
|
|
+ final String expectedTranslationKey = ((TranslatableContents) nmsFrameType.getDisplayName().getContents()).getKey();
|
2021-10-20 21:59:13 +02:00
|
|
|
+ final var frame = PaperAdvancementDisplay.asPaperFrame(nmsFrameType);
|
|
|
|
+ assertEquals("The translation keys should be the same", expectedTranslationKey, frame.translationKey());
|
|
|
|
+ assertEquals("The frame colors should be the same", expectedColor, frame.color());
|
|
|
|
+ assertEquals(nmsFrameType.getName(), AdvancementDisplay.Frame.NAMES.key(frame));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|