feat: 完善GUI工具
This commit is contained in:
@@ -9,15 +9,13 @@ import cn.hamster3.mc.plugin.core.bukkit.hook.VaultAPI;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.DebugListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.page.listener.PageListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.BukkitSerializeUtils;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.ItemStackAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.constant.CoreConstantObjects;
|
||||
import com.google.gson.*;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class HamsterCorePlugin extends JavaPlugin {
|
||||
@@ -94,26 +92,3 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
class ItemStackAdapter implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
||||
public static final ItemStackAdapter INSTANCE = new ItemStackAdapter();
|
||||
|
||||
private ItemStackAdapter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
if (json.isJsonNull()) {
|
||||
return null;
|
||||
}
|
||||
return BukkitSerializeUtils.deserializeItemStack(json.getAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
String s = BukkitSerializeUtils.serializeItemStack(src);
|
||||
if (s == null) {
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
return new JsonPrimitive(s);
|
||||
}
|
||||
}
|
@@ -1,20 +1,5 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.page;
|
||||
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public interface PageElement {
|
||||
@NotNull
|
||||
default ItemStack getDisplayItem(@NotNull HumanEntity player, @NotNull ItemStack defaultItem, @NotNull Map<String, String> pageVariables) {
|
||||
return defaultItem.clone();
|
||||
}
|
||||
|
||||
default Map<String, String> getVariables(@NotNull HumanEntity player, @NotNull Map<String, String> pageVariables) {
|
||||
return pageVariables;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ import org.bukkit.event.inventory.InventoryAction;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -106,10 +107,9 @@ public abstract class PageableHandler<E extends PageElement> extends FixedPageHa
|
||||
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);
|
||||
String buttonName = getElementButtonName(element);
|
||||
ItemStack buttonItem = group.getButton(buttonName);
|
||||
initElementButton(buttonIndex, element, buttonItem, variables);
|
||||
}
|
||||
|
||||
if (page == 0) {
|
||||
@@ -122,7 +122,31 @@ public abstract class PageableHandler<E extends PageElement> extends FixedPageHa
|
||||
}
|
||||
}
|
||||
|
||||
public void initElementButton(@NotNull E element, @NotNull ItemStack displayItem, @NotNull Map<String, String> variables) {
|
||||
public void initElementButton(int index, @NotNull E element, @NotNull ItemStack stack, @NotNull Map<String, String> variables) {
|
||||
ItemMeta meta = stack.getItemMeta();
|
||||
if (meta == null) {
|
||||
return;
|
||||
}
|
||||
if (meta.hasDisplayName()) {
|
||||
String displayName = meta.getDisplayName();
|
||||
for (Map.Entry<String, String> entry : variables.entrySet()) {
|
||||
displayName = displayName.replace(entry.getKey(), entry.getValue());
|
||||
}
|
||||
meta.setDisplayName(displayName);
|
||||
}
|
||||
List<String> lore = meta.getLore();
|
||||
if (lore != null) {
|
||||
for (int i = 0; i < lore.size(); i++) {
|
||||
String s = lore.get(i);
|
||||
for (Map.Entry<String, String> entry : variables.entrySet()) {
|
||||
s = s.replace(entry.getKey(), entry.getValue());
|
||||
}
|
||||
lore.set(i, s);
|
||||
}
|
||||
meta.setLore(lore);
|
||||
}
|
||||
stack.setItemMeta(meta);
|
||||
inventory.setItem(index, stack);
|
||||
}
|
||||
|
||||
public void showPreviewPage() {
|
||||
|
@@ -0,0 +1,30 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||
|
||||
import com.google.gson.*;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ItemStackAdapter implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
||||
public static final ItemStackAdapter INSTANCE = new ItemStackAdapter();
|
||||
|
||||
private ItemStackAdapter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
if (json.isJsonNull()) {
|
||||
return null;
|
||||
}
|
||||
return BukkitSerializeUtils.deserializeItemStack(json.getAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
String s = BukkitSerializeUtils.serializeItemStack(src);
|
||||
if (s == null) {
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
return new JsonPrimitive(s);
|
||||
}
|
||||
}
|
57
hamster-core-bukkit/src/main/resources/FixedPage.yml
Normal file
57
hamster-core-bukkit/src/main/resources/FixedPage.yml
Normal file
@@ -0,0 +1,57 @@
|
||||
title: "&c固定页面"
|
||||
|
||||
graphic:
|
||||
- "####!####"
|
||||
- "# 1 2 3 #"
|
||||
- "#########"
|
||||
|
||||
groups:
|
||||
default:
|
||||
'#': "barrier"
|
||||
'1': "accept"
|
||||
'2': "deny"
|
||||
'3': "close"
|
||||
'!': "info"
|
||||
|
||||
sounds:
|
||||
"default": "UI_BUTTON_CLICK"
|
||||
"accept": "ENTITY_PLAYER_LEVELUP"
|
||||
"deny": "BLOCK_ANVIL_HIT"
|
||||
|
||||
buttons:
|
||||
'barrier':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: STAINED_GLASS_PANE
|
||||
damage: 4
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§c屏障"
|
||||
'accept':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: ARROW
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§a同意"
|
||||
'deny':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: BARRIER
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§c拒绝"
|
||||
'close':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: REDSTONE
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§c关闭"
|
||||
'info':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: REDSTONE
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§c页面信息展示"
|
48
hamster-core-bukkit/src/main/resources/PageablePage.yml
Normal file
48
hamster-core-bukkit/src/main/resources/PageablePage.yml
Normal file
@@ -0,0 +1,48 @@
|
||||
title: "&c分页页面"
|
||||
|
||||
graphic:
|
||||
- "#########"
|
||||
- "# #"
|
||||
- "# 11111 #"
|
||||
- "# 11111 #"
|
||||
- "# #"
|
||||
- "<#######>"
|
||||
|
||||
groups:
|
||||
default:
|
||||
'#': "barrier"
|
||||
'1': "element"
|
||||
|
||||
sounds:
|
||||
"default": "UI_BUTTON_CLICK"
|
||||
|
||||
buttons:
|
||||
'barrier':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: STAINED_GLASS_PANE
|
||||
damage: 4
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§c屏障"
|
||||
'preview':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: REDSTONE
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§c上一页"
|
||||
'next':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: ARROW
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§a下一页"
|
||||
'element':
|
||||
==: org.bukkit.inventory.ItemStack
|
||||
type: ARROW
|
||||
meta:
|
||||
==: ItemMeta
|
||||
meta-type: UNSPECIFIC
|
||||
display-name: "§a玩家信息"
|
Reference in New Issue
Block a user