Skip to content

Commit e99e5dd

Browse files
committed
1.7.7 增加 gui 管理物品库数据
1 parent e10e7b4 commit e99e5dd

9 files changed

Lines changed: 330 additions & 2 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>cn.handyplus.menu</groupId>
88
<name>PlayerMenu</name>
99
<artifactId>PlayerMenu</artifactId>
10-
<version>1.7.6</version>
10+
<version>1.7.7</version>
1111
<description>一个有点好用的玩家菜单插件</description>
1212

1313
<properties>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.handyplus.menu.command.admin;
2+
3+
import cn.handyplus.lib.command.IHandyCommandEvent;
4+
import cn.handyplus.lib.expand.adapter.HandySchedulerUtil;
5+
import cn.handyplus.lib.util.AssertUtil;
6+
import cn.handyplus.lib.util.BaseUtil;
7+
import cn.handyplus.menu.inventory.ItemGui;
8+
import org.bukkit.command.Command;
9+
import org.bukkit.command.CommandSender;
10+
import org.bukkit.entity.Player;
11+
import org.bukkit.inventory.Inventory;
12+
13+
/**
14+
* 打开物品库菜单
15+
*
16+
* @author handy
17+
* @since 1.7.7
18+
*/
19+
public class ItemCommand implements IHandyCommandEvent {
20+
21+
@Override
22+
public String command() {
23+
return "item";
24+
}
25+
26+
@Override
27+
public String permission() {
28+
return "playerMenu.item";
29+
}
30+
31+
@Override
32+
public boolean isAsync() {
33+
return true;
34+
}
35+
36+
@Override
37+
public void onCommand(CommandSender sender, Command cmd, String label, String[] args) {
38+
// 是否为玩家
39+
Player player = AssertUtil.notPlayer(sender, BaseUtil.getLangMsg("noPlayerFailureMsg"));
40+
// 创建gui
41+
Inventory inventory = ItemGui.getInstance().createGui(player);
42+
HandySchedulerUtil.runTask(() -> player.openInventory(inventory));
43+
}
44+
45+
}

src/main/java/cn/handyplus/menu/constants/GuiTypeEnum.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public enum GuiTypeEnum {
1717
*/
1818
CREATE("create", BaseUtil.replaceChatColor("创建菜单")),
1919
MENU("menu", BaseUtil.replaceChatColor("查看菜单")),
20+
ITEM("item", BaseUtil.replaceChatColor("物品库")),
2021
;
2122

2223
private final String type;
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package cn.handyplus.menu.inventory;
2+
3+
import cn.handyplus.lib.constants.BaseConstants;
4+
import cn.handyplus.lib.core.CollUtil;
5+
import cn.handyplus.lib.core.MapUtil;
6+
import cn.handyplus.lib.core.StrUtil;
7+
import cn.handyplus.lib.db.Page;
8+
import cn.handyplus.lib.inventory.HandyInventory;
9+
import cn.handyplus.lib.inventory.HandyInventoryUtil;
10+
import cn.handyplus.lib.util.BaseUtil;
11+
import cn.handyplus.lib.util.ItemStackUtil;
12+
import cn.handyplus.menu.constants.GuiTypeEnum;
13+
import cn.handyplus.menu.enter.MenuItem;
14+
import cn.handyplus.menu.hook.PlaceholderApiUtil;
15+
import cn.handyplus.menu.service.MenuItemService;
16+
import cn.handyplus.menu.util.ConfigUtil;
17+
import org.bukkit.entity.Player;
18+
import org.bukkit.inventory.Inventory;
19+
import org.bukkit.inventory.ItemStack;
20+
import org.bukkit.inventory.meta.ItemMeta;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
/**
27+
* 物品库
28+
*
29+
* @author handy
30+
* @since 1.7.7
31+
*/
32+
public class ItemGui {
33+
34+
private ItemGui() {
35+
}
36+
37+
private final static ItemGui INSTANCE = new ItemGui();
38+
39+
public static ItemGui getInstance() {
40+
return INSTANCE;
41+
}
42+
43+
/**
44+
* 创建gui
45+
*
46+
* @param player 玩家
47+
* @return gui
48+
*/
49+
public Inventory createGui(Player player) {
50+
String title = ConfigUtil.ITEM_CONFIG.getString("title");
51+
title = PlaceholderApiUtil.set(player, title);
52+
int size = ConfigUtil.ITEM_CONFIG.getInt("size", BaseConstants.GUI_SIZE_54);
53+
String sound = ConfigUtil.ITEM_CONFIG.getString("sound");
54+
HandyInventory handyInventory = new HandyInventory(GuiTypeEnum.ITEM.getType(), title, size, sound);
55+
handyInventory.setPlayer(player);
56+
this.setInventoryDate(handyInventory);
57+
return handyInventory.getInventory();
58+
}
59+
60+
/**
61+
* 设置数据
62+
*
63+
* @param handyInventory gui
64+
*/
65+
public void setInventoryDate(HandyInventory handyInventory) {
66+
// 基础设置
67+
handyInventory.setGuiType(GuiTypeEnum.ITEM.getType());
68+
// 1.刷新
69+
HandyInventoryUtil.refreshInventory(handyInventory.getInventory());
70+
// 2.设置数据
71+
this.setDate(handyInventory);
72+
// 3.设置功能性菜单
73+
this.setFunctionMenu(handyInventory);
74+
}
75+
76+
/**
77+
* 设置数据
78+
*
79+
* @param handyInventory gui
80+
*/
81+
private void setDate(HandyInventory handyInventory) {
82+
Inventory inventory = handyInventory.getInventory();
83+
Map<Integer, Integer> map = handyInventory.getIntMap();
84+
String guiIndexStr = ConfigUtil.ITEM_CONFIG.getString("item.index");
85+
List<Integer> guiIndexList = StrUtil.strToIntList(guiIndexStr);
86+
handyInventory.setPageSize(guiIndexList.size());
87+
Page<MenuItem> page = MenuItemService.getInstance().page(handyInventory.getPageNum(), guiIndexList.size());
88+
handyInventory.setPageCount(page.getTotal());
89+
List<MenuItem> records = page.getRecords();
90+
if (CollUtil.isEmpty(records)) {
91+
return;
92+
}
93+
int i = 0;
94+
// 生成显示物品
95+
List<String> loreList = ConfigUtil.ITEM_CONFIG.getStringList("item.lore");
96+
for (MenuItem record : records) {
97+
Integer index = guiIndexList.get(i++);
98+
ItemStack itemStack = ItemStackUtil.itemStackDeserialize(record.getItemStack());
99+
ItemMeta itemMeta = ItemStackUtil.getItemMeta(itemStack);
100+
List<String> newLoreList = CollUtil.isNotEmpty(itemMeta.getLore()) ? itemMeta.getLore() : new ArrayList<>();
101+
newLoreList.addAll(loreList);
102+
newLoreList = ItemStackUtil.loreReplaceMap(newLoreList, this.replaceMap(record));
103+
itemMeta.setLore(BaseUtil.replaceChatColor(newLoreList));
104+
itemStack.setItemMeta(itemMeta);
105+
inventory.setItem(index, itemStack);
106+
map.put(index, record.getId());
107+
}
108+
}
109+
110+
/**
111+
* 设置功能性菜单
112+
*
113+
* @param handyInventory GUI
114+
*/
115+
private void setFunctionMenu(HandyInventory handyInventory) {
116+
// 设置翻页按钮
117+
Map<String, String> replacePageMap = HandyInventoryUtil.replacePageMap(handyInventory);
118+
HandyInventoryUtil.setButton(ConfigUtil.ITEM_CONFIG, handyInventory, "nextPage", replacePageMap);
119+
HandyInventoryUtil.setButton(ConfigUtil.ITEM_CONFIG, handyInventory, "previousPage", replacePageMap);
120+
// 自定义按钮
121+
HandyInventoryUtil.setCustomButton(ConfigUtil.ITEM_CONFIG, handyInventory, "custom");
122+
}
123+
124+
/**
125+
* 变量map
126+
*
127+
* @param menuItem lore
128+
* @return map
129+
*/
130+
private Map<String, String> replaceMap(MenuItem menuItem) {
131+
Map<String, String> map = MapUtil.newHashMapWithExpectedSize(4);
132+
map.put("id", menuItem.getId().toString());
133+
return map;
134+
}
135+
136+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cn.handyplus.menu.listener.gui;
2+
3+
import cn.handyplus.lib.inventory.HandyInventory;
4+
import cn.handyplus.lib.inventory.HandyInventoryUtil;
5+
import cn.handyplus.lib.inventory.IHandyClickEvent;
6+
import cn.handyplus.menu.constants.GuiTypeEnum;
7+
import cn.handyplus.menu.inventory.ItemGui;
8+
import cn.handyplus.menu.service.MenuItemService;
9+
import cn.handyplus.menu.util.ConfigUtil;
10+
import org.bukkit.event.inventory.InventoryClickEvent;
11+
12+
import java.util.Map;
13+
14+
/**
15+
* 物品库
16+
*
17+
* @author handy
18+
* @since 1.7.7
19+
*/
20+
public class ItemClickEventListener implements IHandyClickEvent {
21+
22+
@Override
23+
public String guiType() {
24+
return GuiTypeEnum.ITEM.getType();
25+
}
26+
27+
@Override
28+
public boolean isAsync() {
29+
return true;
30+
}
31+
32+
@Override
33+
public void rawSlotClick(HandyInventory handyInventory, InventoryClickEvent event) {
34+
int rawSlot = event.getRawSlot();
35+
Integer pageNum = handyInventory.getPageNum();
36+
Integer pageCount = handyInventory.getPageCount();
37+
Map<Integer, Integer> map = handyInventory.getIntMap();
38+
39+
// 上一页
40+
if (HandyInventoryUtil.isIndex(rawSlot, ConfigUtil.ITEM_CONFIG, "previousPage")) {
41+
if (pageNum > 1) {
42+
handyInventory.setPageNum(handyInventory.getPageNum() - 1);
43+
ItemGui.getInstance().setInventoryDate(handyInventory);
44+
}
45+
return;
46+
}
47+
// 下一页
48+
if (HandyInventoryUtil.isIndex(rawSlot, ConfigUtil.ITEM_CONFIG, "nextPage")) {
49+
if (pageNum + 1 <= pageCount) {
50+
handyInventory.setPageNum(handyInventory.getPageNum() + 1);
51+
ItemGui.getInstance().setInventoryDate(handyInventory);
52+
}
53+
return;
54+
}
55+
56+
// 点击删除
57+
Integer id = map.get(rawSlot);
58+
if (id != null) {
59+
MenuItemService.getInstance().delById(id);
60+
ItemGui.getInstance().setInventoryDate(handyInventory);
61+
}
62+
}
63+
64+
}

src/main/java/cn/handyplus/menu/service/MenuItemService.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.handyplus.lib.core.SecureUtil;
44
import cn.handyplus.lib.db.Db;
5+
import cn.handyplus.lib.db.Page;
56
import cn.handyplus.menu.enter.MenuItem;
67

78
import java.util.List;
@@ -88,4 +89,28 @@ public void updateItemStack(String itemStack, Integer id) {
8889
use.execution().updateById(id);
8990
}
9091

92+
/**
93+
* 分页查询
94+
*
95+
* @param pageNum 页数
96+
* @param pageSize 条数
97+
* @return MenuItem
98+
* @since 1.7.7
99+
*/
100+
public Page<MenuItem> page(Integer pageNum, Integer pageSize) {
101+
Db<MenuItem> db = Db.use(MenuItem.class);
102+
db.where().limit(pageNum, pageSize).orderByDesc(MenuItem::getId);
103+
return db.execution().page();
104+
}
105+
106+
/**
107+
* 根据id删除
108+
*
109+
* @param id ID
110+
* @since 1.7.7
111+
*/
112+
public void delById(Integer id) {
113+
Db.use(MenuItem.class).execution().deleteById(id);
114+
}
115+
91116
}

src/main/java/cn/handyplus/menu/util/ConfigUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class ConfigUtil {
2323
public static Map<String, String> COMMAND_MAP;
2424
public static Map<String, String> ITEM_MAP;
2525
public static Map<String, Boolean> PERMISSION_MAP;
26+
public static FileConfiguration ITEM_CONFIG;
2627

2728
/**
2829
* 加载全部配置
@@ -62,6 +63,8 @@ public static void init() {
6263
if (MapUtil.isNotEmpty(COMMAND_MAP)) {
6364
HandyCommandWrapper.injectCommand(new ArrayList<>(COMMAND_MAP.keySet()));
6465
}
66+
// 物品库
67+
ITEM_CONFIG = HandyConfigUtil.load("gui/item.yml");
6568
// 升级节点处理
6669
upConfig();
6770
}

src/main/resources/gui/item.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
title: "&0物品库管理"
2+
size: 54
3+
sound: ""
4+
5+
# 介绍信息
6+
item:
7+
index: 20,21,22,23,24,29,30,31,32,33
8+
lore:
9+
- ''
10+
- '&8▪ &7ID: &a${id}'
11+
- '&8[&c✔&8] &7点击删除此物品'
12+
13+
# 上一页
14+
previousPage:
15+
enable: true
16+
index: 47
17+
name: " &8[&a上一页&8]"
18+
lore:
19+
- '&8▪ &7点击打开上一页'
20+
- '&8▪ &7当前页码 &a${pageNum}'
21+
- '&8▪ &7总页码数 &a${count}'
22+
material: PAPER
23+
isEnchant: false
24+
custom-model-data: 0
25+
26+
# 下一页
27+
nextPage:
28+
enable: true
29+
index: 51
30+
name: " &8[&a下一页&8]"
31+
lore:
32+
- '&8▪ &7点击打开下一页'
33+
- '&8▪ &7当前页码 &a${pageNum}'
34+
- '&8▪ &7总页码数 &a${count}'
35+
material: PAPER
36+
isEnchant: false
37+
custom-model-data: 0
38+
39+
# 自定义按钮
40+
custom:
41+
# 分隔板
42+
pane:
43+
enable: true
44+
index: 0,1,2,3,4,5,6,7,8,9,18,27,36,44,17,26,35,46,48,49,50,52
45+
material: black_stained_glass_pane
46+
isEnchant: false
47+
name: " &8[&7分割板&8]"
48+
lore:
49+
- "&7哎呀,不要随便戳人家啦"
50+
custom-model-data: 0

src/main/resources/plugin.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: PlayerMenu
22
main: cn.handyplus.menu.PlayerMenu
3-
version: 1.7.6
3+
version: 1.7.7
44
author: handy
55
api-version: 1.13
66
website: https://ricedoc.handyplus.cn/wiki/PlayerMenu/log/
@@ -27,6 +27,7 @@ permissions:
2727
playerMenu.getItem: true
2828
playerMenu.addItem: true
2929
playerMenu.convert: true
30+
playerMenu.item: true
3031
playerMenu.reload:
3132
description: 重新加载配置
3233
default: op
@@ -62,4 +63,7 @@ permissions:
6263
default: op
6364
playerMenu.convert:
6465
description: 转化数据库
66+
default: op
67+
playerMenu.item:
68+
description: 查看物品库
6569
default: op

0 commit comments

Comments
 (0)