修复了在1.21+版本下无法打开铁砧文本UI的问题

This commit is contained in:
ZhangYuheng 2024-10-12 15:22:35 +08:00
parent e24f29fa30
commit d01457c3da
6 changed files with 83 additions and 17 deletions

View File

@ -10,7 +10,7 @@ plugins {
}
group = "cn.lunadeer"
version = "2.0.2"
version = "2.0.7"
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))

View File

@ -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<UUID, CuiView> 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");

View File

@ -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);

View File

@ -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;
}
}
}

View File

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

View File

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