perf: 优化代码
This commit is contained in:
@@ -14,7 +14,6 @@ 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);
|
||||||
}
|
}
|
||||||
|
@@ -1,49 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -7,7 +7,6 @@ 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;
|
||||||
|
|
||||||
@@ -18,8 +17,6 @@ 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;
|
||||||
|
|
||||||
@@ -38,8 +35,7 @@ public class PageConfig implements InventoryHolder {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private final Inventory inventory;
|
private final Inventory inventory;
|
||||||
|
|
||||||
public PageConfig(@NotNull Plugin plugin, @NotNull ConfigurationSection rawConfig) {
|
public PageConfig(@NotNull ConfigurationSection rawConfig) {
|
||||||
this.plugin = plugin;
|
|
||||||
this.rawConfig = rawConfig;
|
this.rawConfig = rawConfig;
|
||||||
title = rawConfig.getString("title", "").replace("&", "§");
|
title = rawConfig.getString("title", "").replace("&", "§");
|
||||||
|
|
||||||
@@ -163,11 +159,6 @@ 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;
|
||||||
@@ -197,10 +188,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 +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,61 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -4,6 +4,7 @@ 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;
|
||||||
@@ -12,15 +13,11 @@ import java.util.List;
|
|||||||
* 固定页面的 GUI
|
* 固定页面的 GUI
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class FixedPageHandler extends PageHandler {
|
public abstract class FixedPageHandler extends PageHandler {
|
||||||
public FixedPageHandler(@NotNull HumanEntity player) {
|
public FixedPageHandler(@NotNull Player 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) {
|
||||||
|
@@ -2,15 +2,17 @@ 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.page.PageManager;
|
import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils;
|
||||||
|
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.entity.HumanEntity;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
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;
|
||||||
@@ -20,21 +22,29 @@ 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;
|
||||||
private final HumanEntity player;
|
@NotNull
|
||||||
|
private final Player player;
|
||||||
protected Inventory inventory;
|
protected Inventory inventory;
|
||||||
|
|
||||||
public PageHandler(@NotNull HumanEntity player) {
|
public PageHandler(@NotNull Player player) {
|
||||||
pageConfig = PageManager.getPageConfig(getClass());
|
pageConfig = loadPageConfig();
|
||||||
this.player = player;
|
this.player = player;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PageHandler(@NotNull PageConfig pageConfig, @NotNull HumanEntity player) {
|
@NotNull
|
||||||
this.pageConfig = pageConfig;
|
public PageConfig loadPageConfig() {
|
||||||
this.player = player;
|
String simpleName = getClass().getSimpleName();
|
||||||
|
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) {
|
||||||
@@ -55,9 +65,6 @@ 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);
|
||||||
@@ -73,7 +80,7 @@ public abstract class PageHandler implements InventoryHolder {
|
|||||||
if (sound == null) {
|
if (sound == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
((Player) player).playSound(player.getLocation(), sound, 1, 1);
|
player.playSound(player.getLocation(), sound, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDrag(@NotNull InventoryDragEvent event) {
|
public void onDrag(@NotNull InventoryDragEvent event) {
|
||||||
@@ -94,21 +101,11 @@ public abstract class PageHandler implements InventoryHolder {
|
|||||||
if (init) {
|
if (init) {
|
||||||
initPage();
|
initPage();
|
||||||
}
|
}
|
||||||
Bukkit.getScheduler().runTask(pageConfig.getPlugin(), () -> player.openInventory(getInventory()));
|
Bukkit.getScheduler().runTask(getPlugin(), () -> player.openInventory(getInventory()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
Bukkit.getScheduler().runTask(pageConfig.getPlugin(), player::closeInventory);
|
Bukkit.getScheduler().runTask(getPlugin(), player::closeInventory);
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public PageConfig getPageConfig() {
|
|
||||||
return pageConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public HumanEntity getPlayer() {
|
|
||||||
return player;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -118,9 +115,9 @@ public abstract class PageHandler implements InventoryHolder {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
String title = pageConfig.getTitle();
|
String title = getPageConfig().getTitle();
|
||||||
if (player instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
||||||
title = PlaceholderAPI.setPlaceholders((Player) player, title);
|
title = PlaceholderAPI.setPlaceholders(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());
|
||||||
|
@@ -46,24 +46,18 @@ public abstract class PageableHandler<E> extends FixedPageHandler {
|
|||||||
@Getter
|
@Getter
|
||||||
private int page;
|
private int page;
|
||||||
|
|
||||||
public PageableHandler(@NotNull HumanEntity player) {
|
public PageableHandler(@NotNull Player player) {
|
||||||
super(player);
|
super(player);
|
||||||
this.page = 0;
|
this.page = 0;
|
||||||
elementSlot = new HashMap<>();
|
elementSlot = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public PageableHandler(@NotNull HumanEntity player, int page) {
|
public PageableHandler(@NotNull Player 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();
|
||||||
|
|
||||||
@@ -95,10 +89,7 @@ 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) {
|
||||||
HumanEntity player = getPlayer();
|
Player 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);
|
||||||
@@ -120,7 +111,7 @@ public abstract class PageableHandler<E> extends FixedPageHandler {
|
|||||||
if (sound == null) {
|
if (sound == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
((Player) player).playSound(player.getLocation(), sound, 1, 1);
|
player.playSound(player.getLocation(), sound, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -177,14 +168,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 (player instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
if (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 (player instanceof Player && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
if (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());
|
||||||
|
Reference in New Issue
Block a user