aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0696-Add-advancement-display-API.patch
blob: dd860e3b4d082c3359e73de09b48f0f8ea177223 (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
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 advancement display API


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..0567e500c40d3d424ddc19062c4f6da902e8586e
--- /dev/null
+++ b/src/main/java/io/papermc/paper/advancement/PaperAdvancementDisplay.java
@@ -0,0 +1,63 @@
+package io.papermc.paper.advancement;
+
+import io.papermc.paper.adventure.PaperAdventure;
+import net.kyori.adventure.text.Component;
+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());
+    }
+
+    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 db939a754e9308ad68f1b09a970f7a1b00a673bf..538f19f15b553d14ad95f09b1c81359f4c68b17f 100644
--- a/src/main/java/net/minecraft/advancements/DisplayInfo.java
+++ b/src/main/java/net/minecraft/advancements/DisplayInfo.java
@@ -28,6 +28,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 c47cae84f3b6970247d78382f48ae8ddbc202b59..0a46eeefa7d704350321828166f0049d497e3e41 100644
--- a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
+++ b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
@@ -29,12 +29,33 @@ public class CraftAdvancement implements org.bukkit.advancement.Advancement {
         return Collections.unmodifiableCollection(this.handle.getCriteria().keySet());
     }
 
+    // Paper start
     @Override
-    public AdvancementDisplay getDisplay() {
-        if (this.handle.getDisplay() == null) {
-            return null;
+    public io.papermc.paper.advancement.AdvancementDisplay getDisplay() {
+        return this.handle.getDisplay() == null ? null : this.handle.getDisplay().paper;
+    }
+
+    @Override
+    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);
         }
+        return children.build();
+    }
 
-        return new CraftAdvancementDisplay(this.handle.getDisplay());
+    @Override
+    public org.bukkit.advancement.Advancement getRoot() {
+        Advancement advancement = this.handle;
+        while (advancement.getParent() != null) {
+            advancement = advancement.getParent();
+        }
+        return advancement.bukkit;
     }
+    // Paper end
 }
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..4d043e0e43ef8bb75788e195f95b5a50a51a2a48
--- /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.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());
+            final String expectedTranslationKey = ((TranslatableContents) nmsFrameType.getDisplayName().getContents()).getKey();
+            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));
+        }
+    }
+}