aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0663-Add-more-advancement-API.patch
blob: c7966b2e3ec857a28233b86cce28653da376e441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: syldium <syldium@mailo.com>
Date: Fri, 9 Jul 2021 18:50:40 +0200
Subject: [PATCH] Add more advancement API

== AT ==
public net.minecraft.advancements.Advancement decorateName(Lnet/minecraft/advancements/DisplayInfo;)Lnet/minecraft/network/chat/Component;

Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>

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..1705f65d897114dd0813f6d366124cefafcb450e
--- /dev/null
+++ b/src/main/java/io/papermc/paper/advancement/PaperAdvancementDisplay.java
@@ -0,0 +1,69 @@
+package io.papermc.paper.advancement;
+
+import io.papermc.paper.adventure.PaperAdventure;
+import net.kyori.adventure.text.Component;
+import net.minecraft.advancements.Advancement;
+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());
+    }
+
+    @Override
+    public @NotNull Component displayName() {
+        return PaperAdventure.asAdventure(Advancement.decorateName(java.util.Objects.requireNonNull(this.handle, "cannot build display name for null handle, invalid state")));
+    }
+
+    public static @NotNull Frame asPaperFrame(@NotNull FrameType frameType) {
+        return switch (frameType) {
+            case TASK -> Frame.TASK;
+            case CHALLENGE -> Frame.CHALLENGE;
+            case GOAL -> Frame.GOAL;
+        };
+    }
+}
diff --git a/src/main/java/net/minecraft/advancements/DisplayInfo.java b/src/main/java/net/minecraft/advancements/DisplayInfo.java
index a6ea13198a2de14d57472681b5a2b30baf3c4280..11b26d14b863075d776884544629cdd23f219298 100644
--- a/src/main/java/net/minecraft/advancements/DisplayInfo.java
+++ b/src/main/java/net/minecraft/advancements/DisplayInfo.java
@@ -29,6 +29,7 @@ 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 52baf818579a6841b77ff80e42f4f1b9f635ea08..bd640e0d8d796ee114ff787def7e07edbeffc0a5 100644
--- a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
+++ b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
@@ -29,12 +29,47 @@ public class CraftAdvancement implements org.bukkit.advancement.Advancement {
         return Collections.unmodifiableCollection(this.handle.value().criteria().keySet());
     }
 
+    // Paper start
     @Override
-    public AdvancementDisplay getDisplay() {
-        if (this.handle.value().display().isEmpty()) {
-            return null;
+    public io.papermc.paper.advancement.AdvancementDisplay getDisplay() {
+        return this.handle.value().display().map(d -> d.paper).orElse(null);
+    }
+
+    @Deprecated
+    @io.papermc.paper.annotation.DoNotUse
+    public AdvancementDisplay getDisplay0() { // May be called by plugins via Commodore
+        return this.handle.value().display().map(CraftAdvancementDisplay::new).orElse(null);
+    }
+
+    @Override
+    public net.kyori.adventure.text.Component displayName() {
+        return io.papermc.paper.adventure.PaperAdventure.asAdventure(net.minecraft.advancements.Advancement.name(this.handle));
+    }
+
+    @Override
+    public org.bukkit.advancement.Advancement getParent() {
+        return this.handle.value().parent()
+            .map(net.minecraft.server.MinecraftServer.getServer().getAdvancements()::get)
+            .map(AdvancementHolder::toBukkit)
+            .orElse(null);
+    }
+
+    @Override
+    public Collection<org.bukkit.advancement.Advancement> getChildren() {
+        final com.google.common.collect.ImmutableList.Builder<org.bukkit.advancement.Advancement> children = com.google.common.collect.ImmutableList.<org.bukkit.advancement.Advancement>builder();
+        final net.minecraft.advancements.AdvancementNode advancementNode = net.minecraft.server.MinecraftServer.getServer().getAdvancements().tree().get(this.handle);
+        if (advancementNode != null) {
+            for (final net.minecraft.advancements.AdvancementNode child : advancementNode.children()) {
+                children.add(child.holder().toBukkit());
+            }
         }
+        return children.build();
+    }
 
-        return new CraftAdvancementDisplay(this.handle.value().display().get());
+    @Override
+    public org.bukkit.advancement.Advancement getRoot() {
+        final net.minecraft.advancements.AdvancementNode advancementNode = net.minecraft.server.MinecraftServer.getServer().getAdvancements().tree().get(this.handle);
+        return java.util.Objects.requireNonNull(advancementNode, "could not find internal advancement node for advancement " + this.handle.id()).root().holder().toBukkit();
     }
+    // Paper end
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java
index 4aa8cda2bf72627b153e636a408fb3971caf2309..e29d7c6e1cef10a76c8630855fada11cee583d30 100644
--- a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java
+++ b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java
@@ -6,6 +6,7 @@ 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 e0c1131687fffd7f215505caafe6ef2292ac8672..51e60a68484b590906d83541e56f31305e683bc0 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/Commodore.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/Commodore.java
@@ -56,6 +56,7 @@ 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()
     {
@@ -454,6 +455,11 @@ public class Commodore
                             super.visitMethodInsn(opcode, owner, name, "()Lcom/destroystokyo/paper/profile/PlayerProfile;", itf);
                             return;
                         }
+                        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 )
                         {
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..1a4b930ecf10dac8336dd2eceb4ee2bf9ec780d5
--- /dev/null
+++ b/src/test/java/io/papermc/paper/advancement/AdvancementFrameTest.java
@@ -0,0 +1,24 @@
+package io.papermc.paper.advancement;
+
+import io.papermc.paper.adventure.PaperAdventure;
+import net.kyori.adventure.text.format.TextColor;
+import net.minecraft.advancements.FrameType;
+import net.minecraft.network.chat.contents.TranslatableContents;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class AdvancementFrameTest {
+
+    @Test
+    public void test() {
+        for (FrameType nmsFrameType : FrameType.values()) {
+            final TextColor expectedColor = PaperAdventure.asAdventure(nmsFrameType.getChatColor());
+            final String expectedTranslationKey = ((TranslatableContents) nmsFrameType.getDisplayName().getContents()).getKey();
+            final var frame = PaperAdvancementDisplay.asPaperFrame(nmsFrameType);
+            assertEquals(expectedTranslationKey, frame.translationKey(), "The translation keys should be the same");
+            assertEquals(expectedColor, frame.color(), "The frame colors should be the same");
+            assertEquals(nmsFrameType.getName(), AdvancementDisplay.Frame.NAMES.key(frame));
+        }
+    }
+}