Revert "perf: 优化代码"

This reverts commit 86bd28e134.
This commit is contained in:
2024-03-24 13:41:40 +08:00
parent e2a8cc236e
commit f3e4becf35
7 changed files with 174 additions and 39 deletions

View File

@@ -14,6 +14,7 @@ public class CoreCommand extends ParentCommand {
addChildCommand(GCCommand.INSTANCE); addChildCommand(GCCommand.INSTANCE);
addChildCommand(YamlCommand.INSTANCE); addChildCommand(YamlCommand.INSTANCE);
addChildCommand(InfoModeCommand.INSTANCE); addChildCommand(InfoModeCommand.INSTANCE);
addChildCommand(ReloadCommand.INSTANCE);
addChildCommand(MemoryCommand.INSTANCE); addChildCommand(MemoryCommand.INSTANCE);
addChildCommand(SystemCommand.INSTANCE); addChildCommand(SystemCommand.INSTANCE);
} }

View File

@@ -0,0 +1,49 @@
package cn.hamster3.mc.plugin.core.bukkit.command.core.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.page.PageManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class ReloadCommand extends ChildCommand {
public static final ReloadCommand INSTANCE = new ReloadCommand();
private ReloadCommand() {
}
@Override
public @NotNull String getName() {
return "reload";
}
@Override
public @NotNull String getUsage() {
return "reload";
}
@Override
public boolean hasPermission(@NotNull CommandSender sender) {
return true;
}
@Override
public @NotNull String getDescription() {
return "重载页面配置文件";
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
PageManager.PAGE_CONFIG.clear();
sender.sendMessage("§a已重载页面配置文件");
return true;
}
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
return Collections.emptyList();
}
}

View File

@@ -7,6 +7,7 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -17,6 +18,8 @@ import java.util.Map;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class PageConfig implements InventoryHolder { public class PageConfig implements InventoryHolder {
@NotNull
private final Plugin plugin;
@NotNull @NotNull
private final ConfigurationSection rawConfig; private final ConfigurationSection rawConfig;
@@ -35,7 +38,8 @@ public class PageConfig implements InventoryHolder {
@NotNull @NotNull
private final Inventory inventory; private final Inventory inventory;
public PageConfig(@NotNull ConfigurationSection rawConfig) { public PageConfig(@NotNull Plugin plugin, @NotNull ConfigurationSection rawConfig) {
this.plugin = plugin;
this.rawConfig = rawConfig; this.rawConfig = rawConfig;
title = rawConfig.getString("title", "").replace("&", "§"); title = rawConfig.getString("title", "").replace("&", "§");
@@ -159,6 +163,11 @@ public class PageConfig implements InventoryHolder {
throw new IllegalArgumentException("未找到名称为 " + groupName + " 的按钮编组"); throw new IllegalArgumentException("未找到名称为 " + groupName + " 的按钮编组");
} }
@NotNull
public Plugin getPlugin() {
return plugin;
}
@NotNull @NotNull
public ConfigurationSection getRawConfig() { public ConfigurationSection getRawConfig() {
return rawConfig; return rawConfig;
@@ -188,10 +197,10 @@ public class PageConfig implements InventoryHolder {
@Override @Override
public String toString() { public String toString() {
return "PageConfig{" + return "PageConfig{" +
", title='" + title + '\'' + ", title='" + title + '\'' +
", graphic=" + graphic + ", graphic=" + graphic +
", buttonMap=" + buttons + ", buttonMap=" + buttons +
", buttonGroups=" + buttonGroups + ", buttonGroups=" + buttonGroups +
'}'; '}';
} }
} }

View File

@@ -0,0 +1,61 @@
package cn.hamster3.mc.plugin.core.bukkit.page;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
public class PageManager {
public static final HashMap<String, PageConfig> PAGE_CONFIG = new HashMap<>();
@NotNull
public static PageConfig getPageConfig(@NotNull Class<?> clazz) {
PageConfig pageConfig = PAGE_CONFIG.get(clazz.getName());
if (pageConfig != null) {
return pageConfig;
}
if (!clazz.isAnnotationPresent(PluginPage.class)) {
throw new IllegalArgumentException(clazz.getName() + " 未被 @PluginPage 注解修饰");
}
PluginPage annotation = clazz.getAnnotation(PluginPage.class);
String pluginName = annotation.value();
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
if (plugin == null) {
throw new IllegalArgumentException("未找到插件 " + pluginName + " ");
}
pageConfig = getPageConfig(plugin, clazz.getSimpleName());
PAGE_CONFIG.put(clazz.getName(), pageConfig);
return pageConfig;
}
@NotNull
public static PageConfig getPageConfig(@NotNull Plugin plugin, @NotNull String pageName) {
String pluginName = plugin.getName();
File pageFolder = new File(plugin.getDataFolder(), "pages");
if (pageFolder.mkdirs()) {
plugin.getLogger().info("创建页面配置文件夹");
}
String filename = pageName + ".yml";
File pageConfigFile = new File(pageFolder, filename);
if (pageConfigFile.exists()) {
YamlConfiguration config = YamlConfiguration.loadConfiguration(pageConfigFile);
return new PageConfig(plugin, config);
}
try (InputStream stream = plugin.getResource("pages/" + filename)) {
if (stream != null) {
Files.copy(stream, pageConfigFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
throw new IllegalArgumentException("为插件 " + pluginName + " 加载页面配置文件 " + filename + " 时出错", e);
}
YamlConfiguration config = YamlConfiguration.loadConfiguration(pageConfigFile);
return new PageConfig(plugin, config);
}
}

View File

@@ -4,7 +4,6 @@ import cn.hamster3.mc.plugin.core.bukkit.page.ButtonGroup;
import cn.hamster3.mc.plugin.core.bukkit.page.PageConfig; import cn.hamster3.mc.plugin.core.bukkit.page.PageConfig;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
@@ -13,11 +12,15 @@ import java.util.List;
* 固定页面的 GUI * 固定页面的 GUI
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public abstract class FixedPageHandler extends PageHandler { public class FixedPageHandler extends PageHandler {
public FixedPageHandler(@NotNull Player player) { public FixedPageHandler(@NotNull HumanEntity player) {
super(player); super(player);
} }
public FixedPageHandler(@NotNull PageConfig config, @NotNull HumanEntity player) {
super(config, player);
}
@Override @Override
public void initPage() { public void initPage() {
if (inventory == null) { if (inventory == null) {

View File

@@ -2,17 +2,15 @@ package cn.hamster3.mc.plugin.core.bukkit.page.handler;
import cn.hamster3.mc.plugin.core.bukkit.page.ButtonGroup; import cn.hamster3.mc.plugin.core.bukkit.page.ButtonGroup;
import cn.hamster3.mc.plugin.core.bukkit.page.PageConfig; import cn.hamster3.mc.plugin.core.bukkit.page.PageConfig;
import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils; import cn.hamster3.mc.plugin.core.bukkit.page.PageManager;
import lombok.Getter;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.*; import org.bukkit.event.inventory.*;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.HashMap; import java.util.HashMap;
@@ -22,29 +20,21 @@ import java.util.Map;
* GUI 处理类 * GUI 处理类
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Getter
public abstract class PageHandler implements InventoryHolder { public abstract class PageHandler implements InventoryHolder {
@NotNull
private final PageConfig pageConfig; private final PageConfig pageConfig;
@NotNull private final HumanEntity player;
private final Player player;
protected Inventory inventory; protected Inventory inventory;
public PageHandler(@NotNull Player player) { public PageHandler(@NotNull HumanEntity player) {
pageConfig = loadPageConfig(); pageConfig = PageManager.getPageConfig(getClass());
this.player = player; this.player = player;
} }
@NotNull public PageHandler(@NotNull PageConfig pageConfig, @NotNull HumanEntity player) {
public PageConfig loadPageConfig() { this.pageConfig = pageConfig;
String simpleName = getClass().getSimpleName(); this.player = player;
YamlConfiguration config = CoreBukkitUtils.getPluginConfig(getPlugin(), "pages/" + simpleName + ".yml");
return new PageConfig(config);
} }
@NotNull
public abstract Plugin getPlugin();
public abstract void initPage(); public abstract void initPage();
public void onOpen(@NotNull InventoryOpenEvent event) { public void onOpen(@NotNull InventoryOpenEvent event) {
@@ -65,6 +55,9 @@ public abstract class PageHandler implements InventoryHolder {
} }
public void onPlayButtonSound(@NotNull ClickType clickType, @NotNull InventoryAction action, int index) { public void onPlayButtonSound(@NotNull ClickType clickType, @NotNull InventoryAction action, int index) {
if (!(player instanceof Player)) {
return;
}
PageConfig config = getPageConfig(); PageConfig config = getPageConfig();
String buttonName = getButtonGroup().getButtonName(index); String buttonName = getButtonGroup().getButtonName(index);
Sound sound = config.getButtonSound(buttonName); Sound sound = config.getButtonSound(buttonName);
@@ -80,7 +73,7 @@ public abstract class PageHandler implements InventoryHolder {
if (sound == null) { if (sound == null) {
return; return;
} }
player.playSound(player.getLocation(), sound, 1, 1); ((Player) player).playSound(player.getLocation(), sound, 1, 1);
} }
public void onDrag(@NotNull InventoryDragEvent event) { public void onDrag(@NotNull InventoryDragEvent event) {
@@ -101,11 +94,21 @@ public abstract class PageHandler implements InventoryHolder {
if (init) { if (init) {
initPage(); initPage();
} }
Bukkit.getScheduler().runTask(getPlugin(), () -> player.openInventory(getInventory())); Bukkit.getScheduler().runTask(pageConfig.getPlugin(), () -> player.openInventory(getInventory()));
} }
public void close() { public void close() {
Bukkit.getScheduler().runTask(getPlugin(), player::closeInventory); Bukkit.getScheduler().runTask(pageConfig.getPlugin(), player::closeInventory);
}
@NotNull
public PageConfig getPageConfig() {
return pageConfig;
}
@NotNull
public HumanEntity getPlayer() {
return player;
} }
@NotNull @NotNull
@@ -115,9 +118,9 @@ public abstract class PageHandler implements InventoryHolder {
@NotNull @NotNull
public String getTitle() { public String getTitle() {
String title = getPageConfig().getTitle(); String title = pageConfig.getTitle();
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { if (player instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
title = PlaceholderAPI.setPlaceholders(player, title); title = PlaceholderAPI.setPlaceholders((Player) player, title);
} }
for (Map.Entry<String, String> entry : getPageVariables().entrySet()) { for (Map.Entry<String, String> entry : getPageVariables().entrySet()) {
title = title.replace(entry.getKey(), entry.getValue()); title = title.replace(entry.getKey(), entry.getValue());

View File

@@ -46,18 +46,24 @@ public abstract class PageableHandler<E> extends FixedPageHandler {
@Getter @Getter
private int page; private int page;
public PageableHandler(@NotNull Player player) { public PageableHandler(@NotNull HumanEntity player) {
super(player); super(player);
this.page = 0; this.page = 0;
elementSlot = new HashMap<>(); elementSlot = new HashMap<>();
} }
public PageableHandler(@NotNull Player player, int page) { public PageableHandler(@NotNull HumanEntity player, int page) {
super(player); super(player);
this.page = page; this.page = page;
elementSlot = new HashMap<>(); elementSlot = new HashMap<>();
} }
public PageableHandler(@NotNull PageConfig config, @NotNull HumanEntity player, int page) {
super(config, player);
this.page = page;
elementSlot = new HashMap<>();
}
@NotNull @NotNull
public abstract List<E> getPageElements(); public abstract List<E> getPageElements();
@@ -89,7 +95,10 @@ public abstract class PageableHandler<E> extends FixedPageHandler {
@Override @Override
public void onPlayButtonSound(@NotNull ClickType clickType, @NotNull InventoryAction action, int index) { public void onPlayButtonSound(@NotNull ClickType clickType, @NotNull InventoryAction action, int index) {
Player player = getPlayer(); HumanEntity player = getPlayer();
if (!(player instanceof Player)) {
return;
}
PageConfig config = getPageConfig(); PageConfig config = getPageConfig();
String buttonName = getButtonGroup().getButtonName(index); String buttonName = getButtonGroup().getButtonName(index);
Sound sound = config.getButtonSound(buttonName); Sound sound = config.getButtonSound(buttonName);
@@ -111,7 +120,7 @@ public abstract class PageableHandler<E> extends FixedPageHandler {
if (sound == null) { if (sound == null) {
return; return;
} }
player.playSound(player.getLocation(), sound, 1, 1); ((Player) player).playSound(player.getLocation(), sound, 1, 1);
} }
@Override @Override
@@ -168,14 +177,14 @@ public abstract class PageableHandler<E> extends FixedPageHandler {
for (Map.Entry<String, String> entry : variables.entrySet()) { for (Map.Entry<String, String> entry : variables.entrySet()) {
displayName = displayName.replace(entry.getKey(), entry.getValue()); displayName = displayName.replace(entry.getKey(), entry.getValue());
} }
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { if (player instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
displayName = PlaceholderAPI.setPlaceholders((OfflinePlayer) player, displayName); displayName = PlaceholderAPI.setPlaceholders((OfflinePlayer) player, displayName);
} }
meta.setDisplayName(displayName); meta.setDisplayName(displayName);
} }
List<String> lore = meta.getLore(); List<String> lore = meta.getLore();
if (lore != null) { if (lore != null) {
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { if (player instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
lore.replaceAll(s -> { lore.replaceAll(s -> {
for (Map.Entry<String, String> entry : variables.entrySet()) { for (Map.Entry<String, String> entry : variables.entrySet()) {
s = s.replace(entry.getKey(), entry.getValue()); s = s.replace(entry.getKey(), entry.getValue());