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+ }
0 commit comments