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(); + } +}