Revert "perf: 优化代码" #4

Merged
MiniDay merged 1 commits from dev into master 2024-03-24 13:42:34 +08:00
7 changed files with 174 additions and 39 deletions
Showing only changes of commit f3e4becf35 - Show all commits

View File

@@ -14,6 +14,7 @@ public class CoreCommand extends ParentCommand {
addChildCommand(GCCommand.INSTANCE);
addChildCommand(YamlCommand.INSTANCE);
addChildCommand(InfoModeCommand.INSTANCE);
addChildCommand(ReloadCommand.INSTANCE);
addChildCommand(MemoryCommand.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.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -17,6 +18,8 @@ import java.util.Map;
@SuppressWarnings("unused")
public class PageConfig implements InventoryHolder {
@NotNull
private final Plugin plugin;
@NotNull
private final ConfigurationSection rawConfig;
@@ -35,7 +38,8 @@ public class PageConfig implements InventoryHolder {
@NotNull
private final Inventory inventory;
public PageConfig(@NotNull ConfigurationSection rawConfig) {
public PageConfig(@NotNull Plugin plugin, @NotNull ConfigurationSection rawConfig) {
this.plugin = plugin;
this.rawConfig = rawConfig;
title = rawConfig.getString("title", "").replace("&", "§");
@@ -159,6 +163,11 @@ public class PageConfig implements InventoryHolder {
throw new IllegalArgumentException("未找到名称为 " + groupName + " 的按钮编组");
}
@NotNull
public Plugin getPlugin() {
return plugin;
}
@NotNull
public ConfigurationSection getRawConfig() {
return rawConfig;
@@ -188,10 +197,10 @@ public class PageConfig implements InventoryHolder {
@Override
public String toString() {
return "PageConfig{" +
", title='" + title + '\'' +
", graphic=" + graphic +
", buttonMap=" + buttons +
", buttonGroups=" + buttonGroups +
'}';
", title='" + title + '\'' +
", graphic=" + graphic +
", buttonMap=" + buttons +
", 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 org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -13,11 +12,15 @@ import java.util.List;
* 固定页面的 GUI
*/
@SuppressWarnings("unused")
public abstract class FixedPageHandler extends PageHandler {
public FixedPageHandler(@NotNull Player player) {
public class FixedPageHandler extends PageHandler {
public FixedPageHandler(@NotNull HumanEntity player) {
super(player);
}
public FixedPageHandler(@NotNull PageConfig config, @NotNull HumanEntity player) {
super(config, player);
}
@Override
public void initPage() {
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.PageConfig;
import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils;
import lombok.Getter;
import cn.hamster3.mc.plugin.core.bukkit.page.PageManager;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
@@ -22,29 +20,21 @@ import java.util.Map;
* GUI 处理类
*/
@SuppressWarnings("unused")
@Getter
public abstract class PageHandler implements InventoryHolder {
@NotNull
private final PageConfig pageConfig;
@NotNull
private final Player player;
private final HumanEntity player;
protected Inventory inventory;
public PageHandler(@NotNull Player player) {
pageConfig = loadPageConfig();
public PageHandler(@NotNull HumanEntity player) {
pageConfig = PageManager.getPageConfig(getClass());
this.player = player;
}
@NotNull
public PageConfig loadPageConfig() {
String simpleName = getClass().getSimpleName();
YamlConfiguration config = CoreBukkitUtils.getPluginConfig(getPlugin(), "pages/" + simpleName + ".yml");
return new PageConfig(config);
public PageHandler(@NotNull PageConfig pageConfig, @NotNull HumanEntity player) {
this.pageConfig = pageConfig;
this.player = player;
}
@NotNull
public abstract Plugin getPlugin();
public abstract void initPage();
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) {
if (!(player instanceof Player)) {
return;
}
PageConfig config = getPageConfig();
String buttonName = getButtonGroup().getButtonName(index);
Sound sound = config.getButtonSound(buttonName);
@@ -80,7 +73,7 @@ public abstract class PageHandler implements InventoryHolder {
if (sound == null) {
return;
}
player.playSound(player.getLocation(), sound, 1, 1);
((Player) player).playSound(player.getLocation(), sound, 1, 1);
}
public void onDrag(@NotNull InventoryDragEvent event) {
@@ -101,11 +94,21 @@ public abstract class PageHandler implements InventoryHolder {
if (init) {
initPage();
}
Bukkit.getScheduler().runTask(getPlugin(), () -> player.openInventory(getInventory()));
Bukkit.getScheduler().runTask(pageConfig.getPlugin(), () -> player.openInventory(getInventory()));
}
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
@@ -115,9 +118,9 @@ public abstract class PageHandler implements InventoryHolder {
@NotNull
public String getTitle() {
String title = getPageConfig().getTitle();
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
title = PlaceholderAPI.setPlaceholders(player, title);
String title = pageConfig.getTitle();
if (player instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
title = PlaceholderAPI.setPlaceholders((Player) player, title);
}
for (Map.Entry<String, String> entry : getPageVariables().entrySet()) {
title = title.replace(entry.getKey(), entry.getValue());

View File

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