From d01457c3dad065a2fb044a6850fc9c3a82e2348a Mon Sep 17 00:00:00 2001 From: ZhangYuheng Date: Sat, 12 Oct 2024 15:22:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=9C=A81.21+?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=B8=8B=E6=97=A0=E6=B3=95=E6=89=93=E5=BC=80?= =?UTF-8?q?=E9=93=81=E7=A0=A7=E6=96=87=E6=9C=ACUI=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../minecraftpluginutils/scui/CuiManager.java | 7 +++-- .../scui/CuiTextInput.java | 14 +-------- .../scui/OpenAnvilInventoryXVersion.java | 31 +++++++++++++++++++ .../scui_v1_20_1/OpenAnvilInventory.java | 23 ++++++++++++++ .../scui_v1_21/OpenAnvilInventory.java | 23 ++++++++++++++ 6 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/OpenAnvilInventoryXVersion.java create mode 100644 v1_20_1/src/main/java/cn/lunadeer/minecraftpluginutils/scui_v1_20_1/OpenAnvilInventory.java create mode 100644 v1_21/src/main/java/cn/lunadeer/minecraftpluginutils/scui_v1_21/OpenAnvilInventory.java diff --git a/build.gradle.kts b/build.gradle.kts index f14c3c4..191b31a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ plugins { } group = "cn.lunadeer" -version = "2.0.2" +version = "2.0.7" java { toolchain.languageVersion.set(JavaLanguageVersion.of(17)) diff --git a/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/CuiManager.java b/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/CuiManager.java index b402fc3..d1bdbcc 100644 --- a/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/CuiManager.java +++ b/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/CuiManager.java @@ -22,6 +22,7 @@ import static cn.lunadeer.minecraftpluginutils.XVersionEventsRegister.registerEv public class CuiManager implements Listener { + public static XVersionEventsRegister.APIVersion ImplVersion = null; public static CuiManager instance; private final JavaPlugin plugin; private final Map cuis = new HashMap<>(); @@ -31,12 +32,12 @@ public class CuiManager implements Listener { public CuiManager(JavaPlugin plugin) { instance = this; this.plugin = plugin; - XVersionEventsRegister.APIVersion version = GetAPIVersion(plugin); - if (version == null) { + ImplVersion = GetAPIVersion(plugin); + if (ImplVersion == null) { return; } try { - switch (version) { + switch (ImplVersion) { case v1_21: XLogger.debug("Load API version: 1.21"); registerEvents(plugin, "cn.lunadeer.minecraftpluginutils.scui_v1_21.CuiEvents"); diff --git a/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/CuiTextInput.java b/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/CuiTextInput.java index e9903ff..5cc2f25 100644 --- a/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/CuiTextInput.java +++ b/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/CuiTextInput.java @@ -8,9 +8,7 @@ import net.kyori.adventure.text.TextComponent; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.AnvilInventory; -import org.bukkit.inventory.InventoryView; import java.util.HashMap; import java.util.Map; @@ -48,17 +46,7 @@ public class CuiTextInput implements CuiView { return; } // open anvil inventory - InventoryView inv_view = audience.openAnvil(null, true); - if (inv_view == null) { - XLogger.debug("inv_view is null"); - return; - } - if (inv_view.getTopInventory().getType() != InventoryType.ANVIL) { - XLogger.debug("inv_view.getTopInventory().getType() != InventoryType.ANVIL"); - return; - } - inv_view.setTitle(title.content()); - inv = (AnvilInventory) inv_view.getTopInventory(); + inv = OpenAnvilInventoryXVersion.open(audience, title.content()); ItemStackButton btn_1 = ItemStackButton.create(this, Material.NAME_TAG) .title(text); diff --git a/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/OpenAnvilInventoryXVersion.java b/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/OpenAnvilInventoryXVersion.java new file mode 100644 index 0000000..5945dbf --- /dev/null +++ b/core/src/main/java/cn/lunadeer/minecraftpluginutils/scui/OpenAnvilInventoryXVersion.java @@ -0,0 +1,31 @@ +package cn.lunadeer.minecraftpluginutils.scui; + +import cn.lunadeer.minecraftpluginutils.XLogger; +import org.bukkit.entity.Player; +import org.bukkit.inventory.AnvilInventory; + +public class OpenAnvilInventoryXVersion { + + public static AnvilInventory open(Player audience, String title) { + try { + return switch (CuiManager.ImplVersion) { + case v1_21 -> { + Class clazz = Class.forName("cn.lunadeer.minecraftpluginutils.scui_v1_21.OpenAnvilInventory"); + yield (AnvilInventory) clazz.getMethod("open", Player.class, String.class).invoke(null, audience, title); + } + case v1_20_1 -> { + Class clazz = Class.forName("cn.lunadeer.minecraftpluginutils.scui_v1_20_1.OpenAnvilInventory"); + yield (AnvilInventory) clazz.getMethod("open", Player.class, String.class).invoke(null, audience, title); + } + default -> { + XLogger.err("Unsupported API version: %s for XVersionOpenAnvilInventory", CuiManager.ImplVersion); + yield null; + } + }; + } catch (Exception e) { + XLogger.err(e.getMessage()); + return null; + } + } + +} diff --git a/v1_20_1/src/main/java/cn/lunadeer/minecraftpluginutils/scui_v1_20_1/OpenAnvilInventory.java b/v1_20_1/src/main/java/cn/lunadeer/minecraftpluginutils/scui_v1_20_1/OpenAnvilInventory.java new file mode 100644 index 0000000..fe5d2ed --- /dev/null +++ b/v1_20_1/src/main/java/cn/lunadeer/minecraftpluginutils/scui_v1_20_1/OpenAnvilInventory.java @@ -0,0 +1,23 @@ +package cn.lunadeer.minecraftpluginutils.scui_v1_20_1; + +import cn.lunadeer.minecraftpluginutils.XLogger; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.AnvilInventory; +import org.bukkit.inventory.InventoryView; + +public class OpenAnvilInventory { + public static AnvilInventory open(Player audience, String title) { + InventoryView inv_view = audience.openAnvil(null, true); + if (inv_view == null) { + XLogger.debug("inv_view is null"); + return null; + } + if (inv_view.getTopInventory().getType() != InventoryType.ANVIL) { + XLogger.debug("inv_view.getTopInventory().getType() != InventoryType.ANVIL"); + return null; + } + inv_view.setTitle(title); + return (AnvilInventory) inv_view.getTopInventory(); + } +} diff --git a/v1_21/src/main/java/cn/lunadeer/minecraftpluginutils/scui_v1_21/OpenAnvilInventory.java b/v1_21/src/main/java/cn/lunadeer/minecraftpluginutils/scui_v1_21/OpenAnvilInventory.java new file mode 100644 index 0000000..51764f3 --- /dev/null +++ b/v1_21/src/main/java/cn/lunadeer/minecraftpluginutils/scui_v1_21/OpenAnvilInventory.java @@ -0,0 +1,23 @@ +package cn.lunadeer.minecraftpluginutils.scui_v1_21; + +import cn.lunadeer.minecraftpluginutils.XLogger; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.AnvilInventory; +import org.bukkit.inventory.InventoryView; + +public class OpenAnvilInventory { + public static AnvilInventory open(Player audience, String title) { + InventoryView inv_view = audience.openAnvil(null, true); + if (inv_view == null) { + XLogger.debug("inv_view is null"); + return null; + } + if (inv_view.getTopInventory().getType() != InventoryType.ANVIL) { + XLogger.debug("inv_view.getTopInventory().getType() != InventoryType.ANVIL"); + return null; + } + inv_view.setTitle(title); + return (AnvilInventory) inv_view.getTopInventory(); + } +}