测试版

This commit is contained in:
yaohunya 2024-07-24 07:22:45 +08:00
parent c07efc4359
commit 29587f4538
5 changed files with 65 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package com.yaohun.itemlibrary.api; package com.yaohun.itemlibrary.api;
import com.yaohun.itemlibrary.data.SortData;
import com.yaohun.itemlibrary.main; import com.yaohun.itemlibrary.main;
import me.Demon.DemonPlugin.DemonAPI; import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -14,12 +15,10 @@ public abstract class ItemKuAPI {
* 根据分类名 + 物品代号获取物品 * 根据分类名 + 物品代号获取物品
* */ * */
public static ItemStack getSortItemStack(String sortName,String itemKey){ public static ItemStack getSortItemStack(String sortName,String itemKey){
File file = new File(main.plugin.getDataFolder() + "/ItemType", sortName + ".yml"); // 创建文件对象 SortData sortData = main.sortManager.getSortData(sortName);
FileConfiguration yml = YamlConfiguration.loadConfiguration(file); if(sortData.isStackExit(itemKey)){
ItemStack item = yml.getItemStack("itemStack."+itemKey).clone(); return sortData.getStackLinkedMap().get(itemKey).clone();
if(DemonAPI.itemIsNull(item)){ }
return DemonAPI.getErrItems(); return DemonAPI.getErrItems();
} }
return item;
}
} }

View File

@ -1,6 +1,7 @@
package com.yaohun.itemlibrary.data; package com.yaohun.itemlibrary.data;
import com.yaohun.itemlibrary.main; import com.yaohun.itemlibrary.main;
import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -23,13 +24,16 @@ public class SortData {
this.sortName = sortName; this.sortName = sortName;
this.showStack = new ItemStack(Material.valueOf(showStack)); this.showStack = new ItemStack(Material.valueOf(showStack));
LinkedHashMap<String,ItemStack> stackList = new LinkedHashMap<>(); LinkedHashMap<String,ItemStack> stackList = new LinkedHashMap<>();
for (String itemKey : section.getKeys(false)){ if(section != null) {
for (String itemKey : section.getKeys(false)) {
ItemStack stack = section.getItemStack(itemKey).clone(); ItemStack stack = section.getItemStack(itemKey).clone();
stackList.put(itemKey,stack); stackList.put(itemKey, stack);
}
} }
this.stackLinkedMap = stackList; this.stackLinkedMap = stackList;
this.file = file; this.file = file;
this.yml = configuration; this.yml = configuration;
DemonAPI.sendConsoleMessage("§f[§a!§f] §f分类 §8> §6"+sortName+" : "+stackList.size()+"");
} }
public void SaveData(){ public void SaveData(){
@ -53,13 +57,22 @@ public class SortData {
return stackLinkedMap; return stackLinkedMap;
} }
public boolean isStackExit(String itemKey){
if(this.stackLinkedMap.get(itemKey) == null){
return false;
}
return true;
}
public void setStackMap(String itemKey,ItemStack stack){ public void setStackMap(String itemKey,ItemStack stack){
stackLinkedMap.put(itemKey,stack); stackLinkedMap.put(itemKey,stack);
SaveData();
} }
public void deleteStackMap(String itemKey){ public void deleteStackMap(String itemKey){
if(stackLinkedMap.get(itemKey) != null) { if(stackLinkedMap.get(itemKey) != null) {
stackLinkedMap.remove(itemKey); stackLinkedMap.remove(itemKey);
SaveData();
} }
} }
} }

View File

@ -1,6 +1,8 @@
package com.yaohun.itemlibrary.gui; package com.yaohun.itemlibrary.gui;
import cn.hamster3.cdapi.CDTimeAPI;
import com.yaohun.itemlibrary.api.ItemKuAPI; import com.yaohun.itemlibrary.api.ItemKuAPI;
import com.yaohun.itemlibrary.data.SortData;
import com.yaohun.itemlibrary.main; import com.yaohun.itemlibrary.main;
import com.yaohun.itemlibrary.manage.SortManager; import com.yaohun.itemlibrary.manage.SortManager;
import de.tr7zw.itemnbtapi.NBTItem; import de.tr7zw.itemnbtapi.NBTItem;
@ -58,6 +60,16 @@ public class SortGui implements Listener {
DemonAPI.sendMessage(p,"已经是最后一页了!"); 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){ if(rawSlot >= 0 && rawSlot < 45){
ItemStack item = e.getCurrentItem(); ItemStack item = e.getCurrentItem();
if(!DemonAPI.itemIsNull(item)){ if(!DemonAPI.itemIsNull(item)){
@ -89,19 +101,15 @@ public class SortGui implements Listener {
SortManager sortManager = main.sortManager; SortManager sortManager = main.sortManager;
if (sortManager.isSortFileExit(sortName)) { // 检查排序文件是否存在 if (sortManager.isSortFileExit(sortName)) { // 检查排序文件是否存在
List<ItemStack> itemsListID = new ArrayList<>(); List<ItemStack> itemsListID = new ArrayList<>();
if (playerListHashMap.get(p) == null) { // 检查玩家的物品列表是否为空 if(playerListHashMap.get(p) == null) {
File file = new File(main.plugin.getDataFolder() + "/ItemType", sortName + ".yml"); // 创建文件对象 SortData sortData = sortManager.getSortData(sortName);
FileConfiguration yml = YamlConfiguration.loadConfiguration(file); for (String itemKey : sortData.getStackLinkedMap().keySet()) {
ConfigurationSection section = yml.getConfigurationSection("itemStack"); // 获取物品堆叠部分 ItemStack item = sortData.getStackLinkedMap().get(itemKey).clone();
if (section != null) { // 如果物品堆叠部分不为空 ItemStack stack = ShowStack(sortName, itemKey, item); // 获取每个物品堆叠
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); // 添加物品到列表 itemsListID.add(stack); // 添加物品到列表
} }
playerListHashMap.put(p, itemsListID); // 将物品列表放入玩家映射 playerListHashMap.put(p, itemsListID); // 将物品列表放入玩家映射
} }
}
itemsListID = playerListHashMap.get(p); // 获取玩家的物品列表 itemsListID = playerListHashMap.get(p); // 获取玩家的物品列表
if (itemsListID.size() >= 1) { // 如果物品列表不为空 if (itemsListID.size() >= 1) { // 如果物品列表不为空
if (page == 1) { // 如果是第一页 if (page == 1) { // 如果是第一页
@ -122,6 +130,7 @@ public class SortGui implements Listener {
inv.setItem(i, DemonAPI.glass(0,"§r")); inv.setItem(i, DemonAPI.glass(0,"§r"));
} }
inv.setItem(45,Paper_Butt("§a上一页",sortName,page)); inv.setItem(45,Paper_Butt("§a上一页",sortName,page));
inv.setItem(49,refreshData(sortName,page));
inv.setItem(53,Paper_Butt("§a下一页",sortName,page)); inv.setItem(53,Paper_Butt("§a下一页",sortName,page));
p.openInventory(inv); p.openInventory(inv);
} }
@ -152,4 +161,18 @@ public class SortGui implements Listener {
nbt.setInteger("page",page); nbt.setInteger("page",page);
return nbt.getItem(); 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<String> 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();
}
} }

View File

@ -1,6 +1,7 @@
package com.yaohun.itemlibrary; package com.yaohun.itemlibrary;
import cn.hamster3.cdapi.CDTimeAPI; import cn.hamster3.cdapi.CDTimeAPI;
import com.yaohun.itemlibrary.data.SortData;
import com.yaohun.itemlibrary.gui.SortGui; import com.yaohun.itemlibrary.gui.SortGui;
import com.yaohun.itemlibrary.manage.SortManager; import com.yaohun.itemlibrary.manage.SortManager;
import me.Demon.DemonPlugin.DemonAPI; import me.Demon.DemonPlugin.DemonAPI;
@ -37,6 +38,7 @@ public class main extends JavaPlugin {
sender.sendMessage("/ditem [分类名] [物品名] --- 保存物品"); sender.sendMessage("/ditem [分类名] [物品名] --- 保存物品");
} }
if(args.length == 1 && args[0].equalsIgnoreCase("reload")){ if(args.length == 1 && args[0].equalsIgnoreCase("reload")){
sortManager = new SortManager();
SortGui.playerListHashMap.clear(); SortGui.playerListHashMap.clear();
DemonAPI.sendMessage(sender,"缓存数据和配置文件已重载."); DemonAPI.sendMessage(sender,"缓存数据和配置文件已重载.");
} }
@ -68,10 +70,15 @@ public class main extends JavaPlugin {
DemonAPI.sendMessage(player, "分类§e"+sortName+"§a并不存在.",Sound.ENTITY_VILLAGER_NO); DemonAPI.sendMessage(player, "分类§e"+sortName+"§a并不存在.",Sound.ENTITY_VILLAGER_NO);
return true; return true;
} }
SortData sortData = sortManager.getSortData(sortName);
String itemKey = args[1]; String itemKey = args[1];
FileConfiguration yml = main.plugin.getConfig(); if(sortData.isStackExit(itemKey)){
yml.set("ItemStack." + itemKey, item); sortData.deleteStackMap(itemKey);
main.plugin.saveConfig(); 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; return false;

View File

@ -30,7 +30,6 @@ public class SortManager {
sortDataLinkedHashMap.put(sortName,new SortData(sortName,showStack,section,file,configuration)); sortDataLinkedHashMap.put(sortName,new SortData(sortName,showStack,section,file,configuration));
} }
} }
DemonAPI.sendConsoleMessage("§f[§a!§f] §f物品分类 §8> §6"+sortDataLinkedHashMap.size()+"");
} }
/* /*