perf: 优化 api
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
package cn.hamster3.mc.plugin.core.bukkit.command.core;
|
||||||
|
|
||||||
|
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
|
||||||
|
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 EnvCommand extends ChildCommand {
|
||||||
|
public static final EnvCommand INSTANCE = new EnvCommand();
|
||||||
|
|
||||||
|
private EnvCommand() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull String getName() {
|
||||||
|
return "env";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull String getUsage() {
|
||||||
|
return "env [变量名]";
|
||||||
|
}
|
||||||
|
|
||||||
|
@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) {
|
||||||
|
if (args.length < 1) {
|
||||||
|
sender.sendMessage(System.getenv().toString());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
sender.sendMessage(args[0] + "=" + System.getenv().getOrDefault(args[0], ""));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
@@ -8,6 +8,7 @@ public class ParentCoreCommand extends ParentCommand {
|
|||||||
|
|
||||||
private ParentCoreCommand() {
|
private ParentCoreCommand() {
|
||||||
super(HamsterCorePlugin.getInstance(), "core");
|
super(HamsterCorePlugin.getInstance(), "core");
|
||||||
|
addChildCommand(EnvCommand.INSTANCE);
|
||||||
addChildCommand(GCCommand.INSTANCE);
|
addChildCommand(GCCommand.INSTANCE);
|
||||||
addChildCommand(YamlCommand.INSTANCE);
|
addChildCommand(YamlCommand.INSTANCE);
|
||||||
addChildCommand(InfoModeCommand.INSTANCE);
|
addChildCommand(InfoModeCommand.INSTANCE);
|
||||||
|
@@ -1,133 +1,20 @@
|
|||||||
package cn.hamster3.mc.plugin.core.bukkit.page;
|
package cn.hamster3.mc.plugin.core.bukkit.page;
|
||||||
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public interface PageElement {
|
public interface PageElement {
|
||||||
|
@NotNull
|
||||||
/**
|
default ItemStack getDisplayItem(@NotNull HumanEntity player, @NotNull ItemStack defaultItem, @NotNull Map<String, String> pageVariables) {
|
||||||
* 获取展示物品
|
return defaultItem.clone();
|
||||||
* <p>
|
|
||||||
* 若返回 null 则使用 config 中的全局设置值
|
|
||||||
*
|
|
||||||
* @param player 占位符显示的目标玩家
|
|
||||||
* @return 展示物品
|
|
||||||
*/
|
|
||||||
default ItemStack getDisplayItem(HumanEntity player) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
default Map<String, String> getVariables(@NotNull HumanEntity player, @NotNull Map<String, String> pageVariables) {
|
||||||
* 获取展示物品的显示材质
|
return pageVariables;
|
||||||
* <p>
|
|
||||||
* 若返回 null 则使用 config 中的全局设置值
|
|
||||||
*
|
|
||||||
* @param player 占位符显示的目标玩家
|
|
||||||
* @return 展示物品的显示材质
|
|
||||||
*/
|
|
||||||
default Material getMaterial(HumanEntity player) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 替换物品的信息
|
|
||||||
*
|
|
||||||
* @param player 玩家
|
|
||||||
* @param stack 物品
|
|
||||||
* @param globalVariable 全局变量
|
|
||||||
*/
|
|
||||||
default void replaceItemInfo(HumanEntity player, ItemStack stack, HashMap<String, String> globalVariable) {
|
|
||||||
if (stack == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Material type = getMaterial(player);
|
|
||||||
if (type != null) {
|
|
||||||
stack.setType(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemMeta meta = stack.getItemMeta();
|
|
||||||
replaceMetaInfo(player, meta, globalVariable);
|
|
||||||
stack.setItemMeta(meta);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 替换物品的信息
|
|
||||||
*
|
|
||||||
* @param player 玩家
|
|
||||||
* @param meta 物品信息
|
|
||||||
* @param globalVariable 全局变量
|
|
||||||
*/
|
|
||||||
default void replaceMetaInfo(HumanEntity player, ItemMeta meta, HashMap<String, String> globalVariable) {
|
|
||||||
if (meta == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, String> replacer = getVariable(player, globalVariable);
|
|
||||||
|
|
||||||
if (meta.hasDisplayName()) {
|
|
||||||
String displayName = replaceDisplayName(player, meta.getDisplayName(), globalVariable);
|
|
||||||
meta.setDisplayName(displayName);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> lore = meta.getLore();
|
|
||||||
if (lore != null) {
|
|
||||||
lore = replaceLore(player, lore, globalVariable);
|
|
||||||
meta.setLore(lore);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 替换物品展示名称
|
|
||||||
*
|
|
||||||
* @param player 玩家
|
|
||||||
* @param displayName 物品原名称
|
|
||||||
* @param globalVariable 全局变量
|
|
||||||
* @return 替换后的物品展示名称
|
|
||||||
*/
|
|
||||||
default String replaceDisplayName(HumanEntity player, String displayName, HashMap<String, String> globalVariable) {
|
|
||||||
return replacePlaceholder(player, displayName, globalVariable);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 替换物品 lore 信息
|
|
||||||
*
|
|
||||||
* @param player 玩家
|
|
||||||
* @param lore 物品原 lore 信息
|
|
||||||
* @param globalReplacer 全局变量
|
|
||||||
* @return 替换后的物品 lore 信息
|
|
||||||
*/
|
|
||||||
default List<String> replaceLore(HumanEntity player, List<String> lore, HashMap<String, String> globalReplacer) {
|
|
||||||
if (lore == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Map<String, String> replacer = getVariable(player, globalReplacer);
|
|
||||||
for (int i = 0; i < lore.size(); i++) {
|
|
||||||
String s = lore.get(i);
|
|
||||||
for (Map.Entry<String, String> entry : replacer.entrySet()) {
|
|
||||||
s = s.replace(entry.getKey(), entry.getValue());
|
|
||||||
}
|
|
||||||
lore.set(i, s);
|
|
||||||
}
|
|
||||||
return lore;
|
|
||||||
}
|
|
||||||
|
|
||||||
default String replacePlaceholder(HumanEntity player, String string, HashMap<String, String> globalVariable) {
|
|
||||||
Map<String, String> replacer = getVariable(player, globalVariable);
|
|
||||||
for (Map.Entry<String, String> entry : replacer.entrySet()) {
|
|
||||||
string = string.replace(entry.getKey(), entry.getValue());
|
|
||||||
}
|
|
||||||
return string;
|
|
||||||
}
|
|
||||||
|
|
||||||
default Map<String, String> getVariable(HumanEntity player, HashMap<String, String> globalVariable) {
|
|
||||||
return globalVariable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -56,5 +56,4 @@ public class PageManager {
|
|||||||
PAGE_CONFIG.put(clazz.getName(), pageConfig);
|
PAGE_CONFIG.put(clazz.getName(), pageConfig);
|
||||||
return pageConfig;
|
return pageConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -2,8 +2,8 @@ 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 org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.inventory.Inventory;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -23,9 +23,11 @@ public class FixedPageHandler extends PageHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initPage() {
|
public void initPage() {
|
||||||
|
if (inventory == null) {
|
||||||
|
inventory = Bukkit.createInventory(this, getConfig().getInventory().getSize(), getTitle());
|
||||||
|
}
|
||||||
HumanEntity player = getPlayer();
|
HumanEntity player = getPlayer();
|
||||||
|
|
||||||
Inventory inventory = getInventory();
|
|
||||||
PageConfig config = getConfig();
|
PageConfig config = getConfig();
|
||||||
ButtonGroup group = getButtonGroup();
|
ButtonGroup group = getButtonGroup();
|
||||||
|
|
||||||
|
@@ -4,7 +4,6 @@ import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
|||||||
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.page.PageManager;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -20,7 +19,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
public abstract class PageHandler implements InventoryHolder {
|
public abstract class PageHandler implements InventoryHolder {
|
||||||
private final PageConfig config;
|
private final PageConfig config;
|
||||||
private final HumanEntity player;
|
private final HumanEntity player;
|
||||||
private final Inventory inventory;
|
protected Inventory inventory;
|
||||||
|
|
||||||
public PageHandler(@NotNull HumanEntity player) {
|
public PageHandler(@NotNull HumanEntity player) {
|
||||||
try {
|
try {
|
||||||
@@ -29,13 +28,11 @@ public abstract class PageHandler implements InventoryHolder {
|
|||||||
throw new IllegalArgumentException("加载界面配置时遇到了一个异常!", e);
|
throw new IllegalArgumentException("加载界面配置时遇到了一个异常!", e);
|
||||||
}
|
}
|
||||||
this.player = player;
|
this.player = player;
|
||||||
inventory = Bukkit.createInventory(this, config.getInventory().getSize(), getTitle());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public PageHandler(@NotNull PageConfig config, @NotNull HumanEntity player) {
|
public PageHandler(@NotNull PageConfig config, @NotNull HumanEntity player) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.player = player;
|
this.player = player;
|
||||||
inventory = Bukkit.createInventory(this, config.getInventory().getSize(), getTitle());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void initPage();
|
public abstract void initPage();
|
||||||
|
@@ -3,6 +3,7 @@ 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.PageElement;
|
import cn.hamster3.mc.plugin.core.bukkit.page.PageElement;
|
||||||
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.event.inventory.ClickType;
|
import org.bukkit.event.inventory.ClickType;
|
||||||
import org.bukkit.event.inventory.InventoryAction;
|
import org.bukkit.event.inventory.InventoryAction;
|
||||||
@@ -14,6 +15,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 支持翻页的 GUI
|
* 支持翻页的 GUI
|
||||||
@@ -22,6 +24,8 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public abstract class PageableHandler<E extends PageElement> extends FixedPageHandler {
|
public abstract class PageableHandler<E extends PageElement> extends FixedPageHandler {
|
||||||
|
private static final ItemStack EMPTY_STACK = new ItemStack(Material.AIR);
|
||||||
|
|
||||||
private String previewButtonName = "preview";
|
private String previewButtonName = "preview";
|
||||||
private String nextButtonName = "next";
|
private String nextButtonName = "next";
|
||||||
private String barrierButtonName = "barrier";
|
private String barrierButtonName = "barrier";
|
||||||
@@ -50,67 +54,6 @@ public abstract class PageableHandler<E extends PageElement> extends FixedPageHa
|
|||||||
return elementButtonName;
|
return elementButtonName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initElementButton(@NotNull E element, @NotNull ItemStack displayItem, HashMap<String, String> replacer) {
|
|
||||||
element.replaceItemInfo(getPlayer(), displayItem, replacer);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void initPage() {
|
|
||||||
super.initPage();
|
|
||||||
List<E> elements = getPageElements();
|
|
||||||
ButtonGroup group = getButtonGroup();
|
|
||||||
Inventory inventory = getInventory();
|
|
||||||
HumanEntity player = getPlayer();
|
|
||||||
|
|
||||||
ArrayList<Integer> buttonIndexes = group.getButtonAllIndex(elementButtonName);
|
|
||||||
int pageSize = buttonIndexes.size(); // 一页有多少个按钮
|
|
||||||
elementSlot = new HashMap<>();
|
|
||||||
|
|
||||||
HashMap<String, String> replacer = getReplacer();
|
|
||||||
|
|
||||||
for (int i = 0; i < pageSize; i++) {
|
|
||||||
// 元素在当前 page 中的索引位置
|
|
||||||
int elementIndex = page * pageSize + i;
|
|
||||||
// 按钮在 GUI 中的索引位置
|
|
||||||
int buttonIndex = buttonIndexes.get(i);
|
|
||||||
|
|
||||||
if (elementIndex >= elements.size()) {
|
|
||||||
inventory.setItem(buttonIndex, null);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
E element = elements.get(elementIndex);
|
|
||||||
elementSlot.put(buttonIndex, element);
|
|
||||||
|
|
||||||
ItemStack elementDisplayItem = element.getDisplayItem(player);
|
|
||||||
if (elementDisplayItem != null) {
|
|
||||||
elementDisplayItem = elementDisplayItem.clone();
|
|
||||||
element.replaceItemInfo(player, elementDisplayItem, replacer);
|
|
||||||
inventory.setItem(buttonIndex, elementDisplayItem);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack button = group.getButton(getElementButtonName(element));
|
|
||||||
if (button == null) {
|
|
||||||
inventory.setItem(buttonIndex, null);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ItemStack elementItem = button.clone();
|
|
||||||
initElementButton(element, elementItem, replacer);
|
|
||||||
inventory.setItem(buttonIndex, elementItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (page == 0) {
|
|
||||||
// 如果页面已在首页则撤掉上一页按钮
|
|
||||||
inventory.setItem(group.getButtonIndex(previewButtonName), group.getButton(barrierButtonName));
|
|
||||||
}
|
|
||||||
if (elements.size() <= (page + 1) * pageSize) {
|
|
||||||
// 如果页面显示超出已有元素数量则撤掉下一页按钮
|
|
||||||
inventory.setItem(group.getButtonIndex(nextButtonName), group.getButton(barrierButtonName));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClickInside(@NotNull InventoryClickEvent event) {
|
public void onClickInside(@NotNull InventoryClickEvent event) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
@@ -130,6 +73,53 @@ public abstract class PageableHandler<E extends PageElement> extends FixedPageHa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initPage() {
|
||||||
|
super.initPage();
|
||||||
|
List<E> elements = getPageElements();
|
||||||
|
ButtonGroup group = getButtonGroup();
|
||||||
|
Inventory inventory = getInventory();
|
||||||
|
HumanEntity player = getPlayer();
|
||||||
|
|
||||||
|
ArrayList<Integer> buttonIndexes = group.getButtonAllIndex(elementButtonName);
|
||||||
|
int pageSize = buttonIndexes.size(); // 一页有多少个按钮
|
||||||
|
elementSlot = new HashMap<>();
|
||||||
|
|
||||||
|
HashMap<String, String> variables = getVariables();
|
||||||
|
|
||||||
|
for (int i = 0; i < pageSize; i++) {
|
||||||
|
// 元素在当前 page 中的索引位置
|
||||||
|
int elementIndex = page * pageSize + i;
|
||||||
|
// 按钮在 GUI 中的索引位置
|
||||||
|
int buttonIndex = buttonIndexes.get(i);
|
||||||
|
|
||||||
|
if (elementIndex >= elements.size()) {
|
||||||
|
inventory.setItem(buttonIndex, null);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
E element = elements.get(elementIndex);
|
||||||
|
elementSlot.put(buttonIndex, element);
|
||||||
|
|
||||||
|
ItemStack button = group.getButton(getElementButtonName(element));
|
||||||
|
ItemStack displayItem = element.getDisplayItem(player, button == null ? EMPTY_STACK : button, variables);
|
||||||
|
initElementButton(element, displayItem, variables);
|
||||||
|
inventory.setItem(buttonIndex, displayItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (page == 0) {
|
||||||
|
// 如果页面已在首页则撤掉上一页按钮
|
||||||
|
inventory.setItem(group.getButtonIndex(previewButtonName), group.getButton(barrierButtonName));
|
||||||
|
}
|
||||||
|
if (elements.size() <= (page + 1) * pageSize) {
|
||||||
|
// 如果页面显示超出已有元素数量则撤掉下一页按钮
|
||||||
|
inventory.setItem(group.getButtonIndex(nextButtonName), group.getButton(barrierButtonName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initElementButton(@NotNull E element, @NotNull ItemStack displayItem, @NotNull Map<String, String> variables) {
|
||||||
|
}
|
||||||
|
|
||||||
public void showPreviewPage() {
|
public void showPreviewPage() {
|
||||||
page--;
|
page--;
|
||||||
show();
|
show();
|
||||||
@@ -169,7 +159,8 @@ public abstract class PageableHandler<E extends PageElement> extends FixedPageHa
|
|||||||
this.elementButtonName = elementButtonName;
|
this.elementButtonName = elementButtonName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<String, String> getReplacer() {
|
@NotNull
|
||||||
|
public HashMap<String, String> getVariables() {
|
||||||
return new HashMap<>();
|
return new HashMap<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user