Paper/patches/server/0619-Add-more-advancement-API.patch

220 lines
10 KiB
Diff
Raw Normal View History

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
Subject: [PATCH] Add more advancement API
2021-10-20 21:59:13 +02:00
2023-09-22 15:15:57 +02:00
== 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>
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
2023-12-06 19:07:47 +01:00
index 0000000000000000000000000000000000000000..adac21ce6db3ff7a56dbcd6bffc02143c2db4046
2021-10-20 21:59:13 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/advancement/PaperAdvancementDisplay.java
@@ -0,0 +1,69 @@
2021-10-20 21:59:13 +02:00
+package io.papermc.paper.advancement;
+
+import io.papermc.paper.adventure.PaperAdventure;
+import net.kyori.adventure.text.Component;
+import net.minecraft.advancements.Advancement;
2023-12-06 19:07:47 +01:00
+import net.minecraft.advancements.AdvancementType;
2021-10-20 21:59:13 +02:00
+import net.minecraft.advancements.DisplayInfo;
+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() {
2023-12-06 19:07:47 +01:00
+ return asPaperFrame(this.handle.getType());
2021-10-20 21:59:13 +02:00
+ }
+
+ @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() {
2023-12-06 19:07:47 +01:00
+ return this.handle.getBackground().map(CraftNamespacedKey::fromMinecraft).orElse(null);
2021-10-20 21:59:13 +02:00
+ }
+
+ @Override
+ public @NotNull Component displayName() {
2023-09-22 15:19:33 +02:00
+ return PaperAdventure.asAdventure(Advancement.decorateName(java.util.Objects.requireNonNull(this.handle, "cannot build display name for null handle, invalid state")));
+ }
+
2023-12-06 19:07:47 +01:00
+ public static @NotNull Frame asPaperFrame(final @NotNull AdvancementType frameType) {
2021-10-20 21:59:13 +02:00
+ 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 3751b43469e92b8a3d70ec3d5413bc1571147e6e..8057d42f2484bca8e5555e3010c27b898c4245b5 100644
2021-10-20 21:59:13 +02:00
--- a/src/main/java/net/minecraft/advancements/DisplayInfo.java
+++ b/src/main/java/net/minecraft/advancements/DisplayInfo.java
@@ -34,6 +34,7 @@ public class DisplayInfo {
2021-10-20 21:59:13 +02:00
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 - Add more advancement API
2021-10-20 21:59:13 +02:00
public DisplayInfo(
ItemStack icon,
2021-10-20 21:59:13 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
index 52baf818579a6841b77ff80e42f4f1b9f635ea08..8dfaca2efa0b0bdc97b75aaa83158a2e46361e4e 100644
2021-10-20 21:59:13 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
+++ b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancement.java
2023-09-23 01:03:34 +02:00
@@ -29,12 +29,47 @@ public class CraftAdvancement implements org.bukkit.advancement.Advancement {
2023-09-22 15:15:57 +02:00
return Collections.unmodifiableCollection(this.handle.value().criteria().keySet());
2021-10-20 21:59:13 +02:00
}
+ // Paper start - Add more advancement API
@Override
- public AdvancementDisplay getDisplay() {
2023-09-22 15:15:57 +02:00
- if (this.handle.value().display().isEmpty()) {
- return null;
2021-10-20 21:59:13 +02:00
+ public io.papermc.paper.advancement.AdvancementDisplay getDisplay() {
2023-09-22 15:15:57 +02:00
+ return this.handle.value().display().map(d -> d.paper).orElse(null);
2023-03-23 22:57:03 +01:00
+ }
2021-10-20 21:59:13 +02:00
+
2023-09-22 15:15:57 +02:00
+ @Deprecated
+ @io.papermc.paper.annotation.DoNotUse
+ public AdvancementDisplay getDisplay0() { // May be called by plugins via Commodore
2023-09-22 15:15:57 +02:00
+ return this.handle.value().display().map(CraftAdvancementDisplay::new).orElse(null);
+ }
+
2021-10-20 21:59:13 +02:00
+ @Override
+ public net.kyori.adventure.text.Component displayName() {
2023-09-23 01:03:34 +02:00
+ return io.papermc.paper.adventure.PaperAdventure.asAdventure(net.minecraft.advancements.Advancement.name(this.handle));
+ }
+
+ @Override
2021-10-20 21:59:13 +02:00
+ public org.bukkit.advancement.Advancement getParent() {
2023-09-22 15:15:57 +02:00
+ return this.handle.value().parent()
+ .map(net.minecraft.server.MinecraftServer.getServer().getAdvancements()::get)
+ .map(AdvancementHolder::toBukkit)
+ .orElse(null);
2021-10-20 21:59:13 +02:00
+ }
+
+ @Override
+ public Collection<org.bukkit.advancement.Advancement> getChildren() {
2023-09-23 01:03:34 +02:00
+ 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);
2023-09-22 15:15:57 +02:00
+ if (advancementNode != null) {
2023-09-23 01:03:34 +02:00
+ for (final net.minecraft.advancements.AdvancementNode child : advancementNode.children()) {
2023-09-22 15:15:57 +02:00
+ children.add(child.holder().toBukkit());
+ }
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
2023-09-22 15:15:57 +02:00
- return new CraftAdvancementDisplay(this.handle.value().display().get());
2021-10-20 21:59:13 +02:00
+ @Override
+ public org.bukkit.advancement.Advancement getRoot() {
2023-09-23 01:03:34 +02:00
+ final net.minecraft.advancements.AdvancementNode advancementNode = net.minecraft.server.MinecraftServer.getServer().getAdvancements().tree().get(this.handle);
2023-09-22 15:15:57 +02:00
+ return java.util.Objects.requireNonNull(advancementNode, "could not find internal advancement node for advancement " + this.handle.id()).root().holder().toBukkit();
2023-03-23 22:57:03 +01:00
}
+ // Paper end - Add more advancement API
2021-10-20 21:59:13 +02:00
}
diff --git a/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java b/src/main/java/org/bukkit/craftbukkit/advancement/CraftAdvancementDisplay.java
2023-12-06 05:49:31 +01:00
index 8ca86852319d7463f60832bc98b825b0b4325995..62ada73302c6b3ce3fb2dcc8c31a1d9c0ac4fd09 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
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10164) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 63c208dd Remove no longer used import 70be76c7 PR-958: Further clarify deprecation of TAG_CONTAINER_ARRAY ae21f4ac PR-955: Add methods to place structures with block/entity transformers e3d960f2 SPIGOT-7547: Remark that Damageable#setAbsorptionAmount() is capped to a specific value b125516c Fix typo in RecipeChoice.ExactChoice docs 309497c1 Add EntityMountEvent and EntityDismount Event 2fd45ae3 Improve ItemFactory#enchantItem consistency 2b198268 PR-933: Define native persistent data types for lists CraftBukkit Changes: 771182f70 PR-1327: Add methods to place structures with block/entity transformers e41ad4c82 SPIGOT-7567: SpawnReason for SNOWMAN is reported as BUILD_IRONGOLEM 76931e8bd Add EntityMountEvent and EntityDismount Event 9b29b21c7 PR-1183: Better handle lambda expression and renaming of classes in Commodore 1462ebe85 Reformat Commodore.java 9fde4c037 PR-1324: Improve ItemFactory#enchantItem consistency 4e419c774 PR-1295: Define native persistent data types for lists dd8cca388 SPIGOT-7562: Fix Score#getScore and Score#isScoreSet 690278200 Only fetch an online UUID in online mode 1da8d9a53 Fire PreLogin events even in offline mode 2e88514ad PR-1325: Use CraftBlockType and CraftItemType instead of CraftMagicNumbers to convert between minecraft and bukkit block / item representation Spigot Changes: 864e4acc Restore accidentally removed package-info.java f91a10d5 Remove obsolete EntityMountEvent and EntityDismountEvent 828f0593 SPIGOT-7558: Deprecate silenceable lightning API as sound is now client-side and cannot be removed cdc4e035 Remove obsolete patch fetching correct mode UUIDs 49e36b8e Merge related BungeeCord patches 6e87b9ab Remove obsolete firing of PreLogin events in offline mode 5c76b183 Remove redundant patch dealing with exceptions in the crash reporter 3a2219d1 Remove redundant patch logging cause of unexpected exception
2024-01-14 10:46:04 +01:00
index a06352499b9f2c6fb84e585459077b4aa1886d6f..6d6a7abe1aa39a2e4ecf3ac5f55b1f227e1a9db9 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/Commodore.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/Commodore.java
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10164) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 63c208dd Remove no longer used import 70be76c7 PR-958: Further clarify deprecation of TAG_CONTAINER_ARRAY ae21f4ac PR-955: Add methods to place structures with block/entity transformers e3d960f2 SPIGOT-7547: Remark that Damageable#setAbsorptionAmount() is capped to a specific value b125516c Fix typo in RecipeChoice.ExactChoice docs 309497c1 Add EntityMountEvent and EntityDismount Event 2fd45ae3 Improve ItemFactory#enchantItem consistency 2b198268 PR-933: Define native persistent data types for lists CraftBukkit Changes: 771182f70 PR-1327: Add methods to place structures with block/entity transformers e41ad4c82 SPIGOT-7567: SpawnReason for SNOWMAN is reported as BUILD_IRONGOLEM 76931e8bd Add EntityMountEvent and EntityDismount Event 9b29b21c7 PR-1183: Better handle lambda expression and renaming of classes in Commodore 1462ebe85 Reformat Commodore.java 9fde4c037 PR-1324: Improve ItemFactory#enchantItem consistency 4e419c774 PR-1295: Define native persistent data types for lists dd8cca388 SPIGOT-7562: Fix Score#getScore and Score#isScoreSet 690278200 Only fetch an online UUID in online mode 1da8d9a53 Fire PreLogin events even in offline mode 2e88514ad PR-1325: Use CraftBlockType and CraftItemType instead of CraftMagicNumbers to convert between minecraft and bukkit block / item representation Spigot Changes: 864e4acc Restore accidentally removed package-info.java f91a10d5 Remove obsolete EntityMountEvent and EntityDismountEvent 828f0593 SPIGOT-7558: Deprecate silenceable lightning API as sound is now client-side and cannot be removed cdc4e035 Remove obsolete patch fetching correct mode UUIDs 49e36b8e Merge related BungeeCord patches 6e87b9ab Remove obsolete firing of PreLogin events in offline mode 5c76b183 Remove redundant patch dealing with exceptions in the crash reporter 3a2219d1 Remove redundant patch logging cause of unexpected exception
2024-01-14 10:46:04 +01:00
@@ -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();
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10164) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 63c208dd Remove no longer used import 70be76c7 PR-958: Further clarify deprecation of TAG_CONTAINER_ARRAY ae21f4ac PR-955: Add methods to place structures with block/entity transformers e3d960f2 SPIGOT-7547: Remark that Damageable#setAbsorptionAmount() is capped to a specific value b125516c Fix typo in RecipeChoice.ExactChoice docs 309497c1 Add EntityMountEvent and EntityDismount Event 2fd45ae3 Improve ItemFactory#enchantItem consistency 2b198268 PR-933: Define native persistent data types for lists CraftBukkit Changes: 771182f70 PR-1327: Add methods to place structures with block/entity transformers e41ad4c82 SPIGOT-7567: SpawnReason for SNOWMAN is reported as BUILD_IRONGOLEM 76931e8bd Add EntityMountEvent and EntityDismount Event 9b29b21c7 PR-1183: Better handle lambda expression and renaming of classes in Commodore 1462ebe85 Reformat Commodore.java 9fde4c037 PR-1324: Improve ItemFactory#enchantItem consistency 4e419c774 PR-1295: Define native persistent data types for lists dd8cca388 SPIGOT-7562: Fix Score#getScore and Score#isScoreSet 690278200 Only fetch an online UUID in online mode 1da8d9a53 Fire PreLogin events even in offline mode 2e88514ad PR-1325: Use CraftBlockType and CraftItemType instead of CraftMagicNumbers to convert between minecraft and bukkit block / item representation Spigot Changes: 864e4acc Restore accidentally removed package-info.java f91a10d5 Remove obsolete EntityMountEvent and EntityDismountEvent 828f0593 SPIGOT-7558: Deprecate silenceable lightning API as sound is now client-side and cannot be removed cdc4e035 Remove obsolete patch fetching correct mode UUIDs 49e36b8e Merge related BungeeCord patches 6e87b9ab Remove obsolete firing of PreLogin events in offline mode 5c76b183 Remove redundant patch dealing with exceptions in the crash reporter 3a2219d1 Remove redundant patch logging cause of unexpected exception
2024-01-14 10:46:04 +01:00
private static Map<String, String> initReplacementsMap() {
Map<String, String> getAndRemove = new HashMap<>();
@@ -388,6 +389,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
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10164) Upstream has released updates that appear to apply and compile correctly. This update has not been tested by PaperMC and as with ANY update, please do your own testing Bukkit Changes: 63c208dd Remove no longer used import 70be76c7 PR-958: Further clarify deprecation of TAG_CONTAINER_ARRAY ae21f4ac PR-955: Add methods to place structures with block/entity transformers e3d960f2 SPIGOT-7547: Remark that Damageable#setAbsorptionAmount() is capped to a specific value b125516c Fix typo in RecipeChoice.ExactChoice docs 309497c1 Add EntityMountEvent and EntityDismount Event 2fd45ae3 Improve ItemFactory#enchantItem consistency 2b198268 PR-933: Define native persistent data types for lists CraftBukkit Changes: 771182f70 PR-1327: Add methods to place structures with block/entity transformers e41ad4c82 SPIGOT-7567: SpawnReason for SNOWMAN is reported as BUILD_IRONGOLEM 76931e8bd Add EntityMountEvent and EntityDismount Event 9b29b21c7 PR-1183: Better handle lambda expression and renaming of classes in Commodore 1462ebe85 Reformat Commodore.java 9fde4c037 PR-1324: Improve ItemFactory#enchantItem consistency 4e419c774 PR-1295: Define native persistent data types for lists dd8cca388 SPIGOT-7562: Fix Score#getScore and Score#isScoreSet 690278200 Only fetch an online UUID in online mode 1da8d9a53 Fire PreLogin events even in offline mode 2e88514ad PR-1325: Use CraftBlockType and CraftItemType instead of CraftMagicNumbers to convert between minecraft and bukkit block / item representation Spigot Changes: 864e4acc Restore accidentally removed package-info.java f91a10d5 Remove obsolete EntityMountEvent and EntityDismountEvent 828f0593 SPIGOT-7558: Deprecate silenceable lightning API as sound is now client-side and cannot be removed cdc4e035 Remove obsolete patch fetching correct mode UUIDs 49e36b8e Merge related BungeeCord patches 6e87b9ab Remove obsolete firing of PreLogin events in offline mode 5c76b183 Remove redundant patch dealing with exceptions in the crash reporter 3a2219d1 Remove redundant patch logging cause of unexpected exception
2024-01-14 10:46:04 +01:00
if (modern) {
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..47535c27d7cffc4b5ee6cc9e145b1087477eef79
2021-10-20 21:59:13 +02:00
--- /dev/null
+++ b/src/test/java/io/papermc/paper/advancement/AdvancementFrameTest.java
@@ -0,0 +1,24 @@
2021-10-20 21:59:13 +02:00
+package io.papermc.paper.advancement;
+
+import io.papermc.paper.adventure.PaperAdventure;
+import net.kyori.adventure.text.format.TextColor;
+import net.minecraft.advancements.AdvancementType;
2022-06-08 17:31:27 +02:00
+import net.minecraft.network.chat.contents.TranslatableContents;
+import org.junit.jupiter.api.Test;
2021-10-20 21:59:13 +02:00
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
2021-10-20 21:59:13 +02:00
+
+public class AdvancementFrameTest {
+
+ @Test
+ public void test() {
+ for (final AdvancementType advancementType : AdvancementType.values()) {
+ final TextColor expectedColor = PaperAdventure.asAdventure(advancementType.getChatColor());
+ final String expectedTranslationKey = ((TranslatableContents) advancementType.getDisplayName().getContents()).getKey();
+ final var frame = PaperAdvancementDisplay.asPaperFrame(advancementType);
+ assertEquals(expectedTranslationKey, frame.translationKey(), "The translation keys should be the same");
+ assertEquals(expectedColor, frame.color(), "The frame colors should be the same");
+ assertEquals(advancementType.getSerializedName(), AdvancementDisplay.Frame.NAMES.key(frame));
2021-10-20 21:59:13 +02:00
+ }
+ }
+}