From 7866cec8c003344885976638071ecad1d7770099 Mon Sep 17 00:00:00 2001 From: TheFaser Date: Mon, 13 Nov 2023 20:18:11 +0500 Subject: [PATCH] Advancement: fix NMS 1.20.2 --- .../chat/model/advancement/FAdvancement.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/flectone/chat/model/advancement/FAdvancement.java b/src/main/java/net/flectone/chat/model/advancement/FAdvancement.java index bb27615..8c3d01b 100644 --- a/src/main/java/net/flectone/chat/model/advancement/FAdvancement.java +++ b/src/main/java/net/flectone/chat/model/advancement/FAdvancement.java @@ -6,6 +6,7 @@ import javax.annotation.Nullable; import java.util.Arrays; +import java.util.Optional; // Thanks, @CroaBeast, for these methods // Source https://github.com/CroaBeast/AdvancementInfo @@ -20,15 +21,31 @@ public class FAdvancement { private String title; + private static final boolean IS_19_4; + private static final boolean IS_20_2; + + static { + double value = NMSUtil.getVersion(); + IS_19_4 = value >= 19.4; + IS_20_2 = value >= 20.2; + } + public FAdvancement(@NotNull Advancement adv) { Class craftClass = NMSUtil.getBukkitClass("advancement.CraftAdvancement"); if (craftClass == null) return; Object nmsAdv = NMSUtil.getObject(craftClass, craftClass.cast(adv), "getHandle"); - Object display = NMSUtil.getObject(nmsAdv, is_19_4() ? "d" : "c"); + if (IS_20_2) nmsAdv = NMSUtil.getObject(nmsAdv, "b"); + + Object display = NMSUtil.getObject(nmsAdv, IS_19_4 ? "d" : "c"); if (display == null) return; + if (IS_20_2) { + Optional o = ((Optional) display); + if (o.isPresent()) display = o.get(); + } + Object rawTitle = NMSUtil.getObject(display, "a"); Object rawDesc = NMSUtil.getObject(display, "b"); @@ -51,10 +68,6 @@ public FAdvancement(@NotNull Advancement adv) { } } - private static boolean is_19_4() { - return NMSUtil.getVersion() >= 19.4; - } - private static boolean getBool(String string) { return string.matches("(?i)true|false") && string.matches("(?i)true"); }