From 29587f45386ce10decb8af7f65791fc5d9a5eb45 Mon Sep 17 00:00:00 2001 From: yaohunya <31456652@qq.com> Date: Wed, 24 Jul 2024 07:22:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yaohun/itemlibrary/api/ItemKuAPI.java | 11 +++-- .../com/yaohun/itemlibrary/data/SortData.java | 19 ++++++-- .../com/yaohun/itemlibrary/gui/SortGui.java | 45 ++++++++++++++----- .../java/com/yaohun/itemlibrary/main.java | 13 ++++-- .../itemlibrary/manage/SortManager.java | 1 - 5 files changed, 65 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/yaohun/itemlibrary/api/ItemKuAPI.java b/src/main/java/com/yaohun/itemlibrary/api/ItemKuAPI.java index bb2ddd9..d4256b1 100644 --- a/src/main/java/com/yaohun/itemlibrary/api/ItemKuAPI.java +++ b/src/main/java/com/yaohun/itemlibrary/api/ItemKuAPI.java @@ -1,5 +1,6 @@ package com.yaohun.itemlibrary.api; +import com.yaohun.itemlibrary.data.SortData; import com.yaohun.itemlibrary.main; import me.Demon.DemonPlugin.DemonAPI; import org.bukkit.configuration.file.FileConfiguration; @@ -14,12 +15,10 @@ public abstract class ItemKuAPI { * 根据分类名 + 物品代号获取物品 * */ public static ItemStack getSortItemStack(String sortName,String itemKey){ - File file = new File(main.plugin.getDataFolder() + "/ItemType", sortName + ".yml"); // 创建文件对象 - FileConfiguration yml = YamlConfiguration.loadConfiguration(file); - ItemStack item = yml.getItemStack("itemStack."+itemKey).clone(); - if(DemonAPI.itemIsNull(item)){ - return DemonAPI.getErrItems(); + SortData sortData = main.sortManager.getSortData(sortName); + if(sortData.isStackExit(itemKey)){ + return sortData.getStackLinkedMap().get(itemKey).clone(); } - return item; + return DemonAPI.getErrItems(); } } diff --git a/src/main/java/com/yaohun/itemlibrary/data/SortData.java b/src/main/java/com/yaohun/itemlibrary/data/SortData.java index 2203344..a85dab2 100644 --- a/src/main/java/com/yaohun/itemlibrary/data/SortData.java +++ b/src/main/java/com/yaohun/itemlibrary/data/SortData.java @@ -1,6 +1,7 @@ package com.yaohun.itemlibrary.data; import com.yaohun.itemlibrary.main; +import me.Demon.DemonPlugin.DemonAPI; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; @@ -23,13 +24,16 @@ public class SortData { this.sortName = sortName; this.showStack = new ItemStack(Material.valueOf(showStack)); LinkedHashMap stackList = new LinkedHashMap<>(); - for (String itemKey : section.getKeys(false)){ - ItemStack stack = section.getItemStack(itemKey).clone(); - stackList.put(itemKey,stack); + if(section != null) { + for (String itemKey : section.getKeys(false)) { + ItemStack stack = section.getItemStack(itemKey).clone(); + stackList.put(itemKey, stack); + } } this.stackLinkedMap = stackList; this.file = file; this.yml = configuration; + DemonAPI.sendConsoleMessage("§f[§a!§f] §f分类 §8> §6"+sortName+" : "+stackList.size()+" 件"); } public void SaveData(){ @@ -53,13 +57,22 @@ public class SortData { return stackLinkedMap; } + public boolean isStackExit(String itemKey){ + if(this.stackLinkedMap.get(itemKey) == null){ + return false; + } + return true; + } + public void setStackMap(String itemKey,ItemStack stack){ stackLinkedMap.put(itemKey,stack); + SaveData(); } public void deleteStackMap(String itemKey){ if(stackLinkedMap.get(itemKey) != null) { stackLinkedMap.remove(itemKey); + SaveData(); } } } diff --git a/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java b/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java index 904b0a4..4f688b3 100644 --- a/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java +++ b/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java @@ -1,6 +1,8 @@ package com.yaohun.itemlibrary.gui; +import cn.hamster3.cdapi.CDTimeAPI; import com.yaohun.itemlibrary.api.ItemKuAPI; +import com.yaohun.itemlibrary.data.SortData; import com.yaohun.itemlibrary.main; import com.yaohun.itemlibrary.manage.SortManager; import de.tr7zw.itemnbtapi.NBTItem; @@ -58,6 +60,16 @@ public class SortGui implements Listener { DemonAPI.sendMessage(p,"已经是最后一页了!"); } } + if(rawSlot == 49){ + p.playSound(p.getLocation(), Sound.BLOCK_COMPARATOR_CLICK, 1.6F, 1.8F); + playerListHashMap.clear(); + p.closeInventory(); + DemonAPI.sendMessage(p,"刷新成功!"); + NBTItem nbtItem = new NBTItem(e.getCurrentItem()); + String sortName = nbtItem.getString("sortName"); + int page = nbtItem.getInteger("page"); + OpenGui(p,sortName,page); + } if(rawSlot >= 0 && rawSlot < 45){ ItemStack item = e.getCurrentItem(); if(!DemonAPI.itemIsNull(item)){ @@ -89,18 +101,14 @@ public class SortGui implements Listener { SortManager sortManager = main.sortManager; if (sortManager.isSortFileExit(sortName)) { // 检查排序文件是否存在 List itemsListID = new ArrayList<>(); - if (playerListHashMap.get(p) == null) { // 检查玩家的物品列表是否为空 - File file = new File(main.plugin.getDataFolder() + "/ItemType", sortName + ".yml"); // 创建文件对象 - FileConfiguration yml = YamlConfiguration.loadConfiguration(file); - ConfigurationSection section = yml.getConfigurationSection("itemStack"); // 获取物品堆叠部分 - if (section != null) { // 如果物品堆叠部分不为空 - inv = Bukkit.createInventory(null, 54, invTitle+"[§6§l"+sortName+"§r]"); // 创建一个新的物品栏 - for (String itemKey : section.getKeys(false)) { // 遍历所有物品堆叠的键 - ItemStack stack = ShowStack(sortName, itemKey, section.getItemStack(itemKey)); // 获取每个物品堆叠 - itemsListID.add(stack); // 添加物品到列表 - } - playerListHashMap.put(p, itemsListID); // 将物品列表放入玩家映射 + if(playerListHashMap.get(p) == null) { + SortData sortData = sortManager.getSortData(sortName); + for (String itemKey : sortData.getStackLinkedMap().keySet()) { + ItemStack item = sortData.getStackLinkedMap().get(itemKey).clone(); + ItemStack stack = ShowStack(sortName, itemKey, item); // 获取每个物品堆叠 + itemsListID.add(stack); // 添加物品到列表 } + playerListHashMap.put(p, itemsListID); // 将物品列表放入玩家映射 } itemsListID = playerListHashMap.get(p); // 获取玩家的物品列表 if (itemsListID.size() >= 1) { // 如果物品列表不为空 @@ -122,6 +130,7 @@ public class SortGui implements Listener { inv.setItem(i, DemonAPI.glass(0,"§r")); } inv.setItem(45,Paper_Butt("§a上一页",sortName,page)); + inv.setItem(49,refreshData(sortName,page)); inv.setItem(53,Paper_Butt("§a下一页",sortName,page)); p.openInventory(inv); } @@ -152,4 +161,18 @@ public class SortGui implements Listener { nbt.setInteger("page",page); return nbt.getItem(); } + public static ItemStack refreshData(String sortName,int page){ + ItemStack item = new ItemStack(Material.TOTEM); + item.setAmount(page); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName("§c刷新缓存数据"); + List lore = new ArrayList<>(); + lore.add("§a§l★ §7点击执行 §a§l★"); + meta.setLore(lore); + item.setItemMeta(meta); + NBTItem nbt = new NBTItem(item); + nbt.setString("sortName",sortName); + nbt.setInteger("page",page); + return nbt.getItem(); + } } diff --git a/src/main/java/com/yaohun/itemlibrary/main.java b/src/main/java/com/yaohun/itemlibrary/main.java index 708f00a..ceadc4b 100644 --- a/src/main/java/com/yaohun/itemlibrary/main.java +++ b/src/main/java/com/yaohun/itemlibrary/main.java @@ -1,6 +1,7 @@ package com.yaohun.itemlibrary; import cn.hamster3.cdapi.CDTimeAPI; +import com.yaohun.itemlibrary.data.SortData; import com.yaohun.itemlibrary.gui.SortGui; import com.yaohun.itemlibrary.manage.SortManager; import me.Demon.DemonPlugin.DemonAPI; @@ -37,6 +38,7 @@ public class main extends JavaPlugin { sender.sendMessage("/ditem [分类名] [物品名] --- 保存物品"); } if(args.length == 1 && args[0].equalsIgnoreCase("reload")){ + sortManager = new SortManager(); SortGui.playerListHashMap.clear(); DemonAPI.sendMessage(sender,"缓存数据和配置文件已重载."); } @@ -68,10 +70,15 @@ public class main extends JavaPlugin { DemonAPI.sendMessage(player, "分类§e"+sortName+"§a并不存在.",Sound.ENTITY_VILLAGER_NO); return true; } + SortData sortData = sortManager.getSortData(sortName); String itemKey = args[1]; - FileConfiguration yml = main.plugin.getConfig(); - yml.set("ItemStack." + itemKey, item); - main.plugin.saveConfig(); + if(sortData.isStackExit(itemKey)){ + sortData.deleteStackMap(itemKey); + DemonAPI.sendMessage(player, "删除物品: §c" + itemKey + " §a分类: §6"+sortName, Sound.ENTITY_EXPERIENCE_ORB_PICKUP); + }else { + sortData.setStackMap(itemKey, item); + DemonAPI.sendMessage(player, "创建物品: §e" + itemKey + " §a分类: §6"+sortName, Sound.ENTITY_EXPERIENCE_ORB_PICKUP); + } } } return false; diff --git a/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java b/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java index b3c8890..7017adf 100644 --- a/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java +++ b/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java @@ -30,7 +30,6 @@ public class SortManager { sortDataLinkedHashMap.put(sortName,new SortData(sortName,showStack,section,file,configuration)); } } - DemonAPI.sendConsoleMessage("§f[§a!§f] §f物品分类 §8> §6"+sortDataLinkedHashMap.size()+"种"); } /*