diff --git a/pom.xml b/pom.xml index 326f28e..b65d4ab 100644 --- a/pom.xml +++ b/pom.xml @@ -26,14 +26,19 @@ spigot-api 1.12.2 + + cn.hamster3.cdapi + CDTimeAPI + 1.0 + me.Demon.DemonPlugin DemonAPI 1.2.0 - io.lumine.xikage.mythicmobs - MythicMobs + com.yaohun.nbtapi + NBT-API 1.12.2 diff --git a/src/main/java/com/yaohun/itemlibrary/api/ItemKuAPI.java b/src/main/java/com/yaohun/itemlibrary/api/ItemKuAPI.java new file mode 100644 index 0000000..bb2ddd9 --- /dev/null +++ b/src/main/java/com/yaohun/itemlibrary/api/ItemKuAPI.java @@ -0,0 +1,25 @@ +package com.yaohun.itemlibrary.api; + +import com.yaohun.itemlibrary.main; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; + +import java.io.File; + +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(); + } + return item; + } +} diff --git a/src/main/java/com/yaohun/itemlibrary/data/SortData.java b/src/main/java/com/yaohun/itemlibrary/data/SortData.java new file mode 100644 index 0000000..2203344 --- /dev/null +++ b/src/main/java/com/yaohun/itemlibrary/data/SortData.java @@ -0,0 +1,65 @@ +package com.yaohun.itemlibrary.data; + +import com.yaohun.itemlibrary.main; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; + +import java.io.File; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +public class SortData { + + private String sortName; + private ItemStack showStack; + private LinkedHashMap stackLinkedMap; + private File file; + private FileConfiguration yml; + + public SortData(String sortName, String showStack, ConfigurationSection section, File file,FileConfiguration configuration){ + 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); + } + this.stackLinkedMap = stackList; + this.file = file; + this.yml = configuration; + } + + public void SaveData(){ + yml.set("showStack",this.showStack.getType().name()); + yml.set("itemStack",null); + for (String itemKey : stackLinkedMap.keySet()){ + yml.set("itemStack."+itemKey,stackLinkedMap.get(itemKey).clone()); + } + main.sortManager.saveFile(file,yml); + } + + public String getSortName() { + return sortName; + } + + public ItemStack getShowStack() { + return showStack; + } + + public LinkedHashMap getStackLinkedMap() { + return stackLinkedMap; + } + + public void setStackMap(String itemKey,ItemStack stack){ + stackLinkedMap.put(itemKey,stack); + } + + public void deleteStackMap(String itemKey){ + if(stackLinkedMap.get(itemKey) != null) { + stackLinkedMap.remove(itemKey); + } + } +} diff --git a/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java b/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java new file mode 100644 index 0000000..904b0a4 --- /dev/null +++ b/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java @@ -0,0 +1,155 @@ +package com.yaohun.itemlibrary.gui; + +import com.yaohun.itemlibrary.api.ItemKuAPI; +import com.yaohun.itemlibrary.main; +import com.yaohun.itemlibrary.manage.SortManager; +import de.tr7zw.itemnbtapi.NBTItem; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class SortGui implements Listener { + + public static String invTitle = "我的世界物品库 "; + + @EventHandler + public void onClick(InventoryClickEvent e){ + int rawSlot = e.getRawSlot(); + Inventory inv = e.getInventory(); + Player p = (Player) e.getWhoClicked(); + if(inv != null && inv.getTitle().contains(invTitle)){ + e.setCancelled(true); + if(rawSlot == 45){ + p.playSound(p.getLocation(), Sound.BLOCK_COMPARATOR_CLICK, 1.6F, 1.8F); + NBTItem nbtItem = new NBTItem(e.getCurrentItem()); + String sortName = nbtItem.getString("sortName"); + int page = nbtItem.getInteger("page"); + if (page >= 2 && page <= 5) { + SortGui.OpenGui(p,sortName,page-1); + } else { + DemonAPI.sendMessage(p,"已经到第一页了!"); + } + } + if(rawSlot == 53){ + p.playSound(p.getLocation(), Sound.BLOCK_COMPARATOR_CLICK, 1.6F, 1.8F); + NBTItem nbtItem = new NBTItem(e.getCurrentItem()); + String sortName = nbtItem.getString("sortName"); + int page = nbtItem.getInteger("page"); + if (page >= 1 && page <= 4) { + SortGui.OpenGui(p,sortName,page+1); + } else { + DemonAPI.sendMessage(p,"已经是最后一页了!"); + } + } + if(rawSlot >= 0 && rawSlot < 45){ + ItemStack item = e.getCurrentItem(); + if(!DemonAPI.itemIsNull(item)){ + NBTItem nbtItem = new NBTItem(item); + if(nbtItem.hasKey("itemKey")){ + String sortName = nbtItem.getString("sortName"); + String itemKey = nbtItem.getString("itemKey"); + ItemStack stack = ItemKuAPI.getSortItemStack(sortName,itemKey); + if(e.getClick() == ClickType.LEFT) { + stack.setAmount(1); + p.getInventory().addItem(stack); + }else if(e.getClick() == ClickType.RIGHT){ + stack.setAmount(10); + p.getInventory().addItem(stack); + }else if(e.getClick() == ClickType.MIDDLE){ + stack.setAmount(64); + p.getInventory().addItem(stack); + } + p.playSound(p.getLocation(), Sound.ENTITY_CHICKEN_EGG,1,2); + } + } + } + } + } + + public static HashMap> playerListHashMap = new HashMap<>(); + public static void OpenGui(Player p,String sortName,int page){ + Inventory inv = Bukkit.createInventory(null,54,invTitle); + 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); // 将物品列表放入玩家映射 + } + } + itemsListID = playerListHashMap.get(p); // 获取玩家的物品列表 + if (itemsListID.size() >= 1) { // 如果物品列表不为空 + if (page == 1) { // 如果是第一页 + for (int i = 0; i < itemsListID.size(); i++) { // 遍历物品列表 + ItemStack item = itemsListID.get(i); + inv.addItem(item); // 添加物品到物品栏 + } + } else { // 如果是第二页或之后 + int startIndex = 45 * (page - 1); // 计算起始索引 + for (int i = startIndex; i < itemsListID.size(); i++) { + ItemStack item = itemsListID.get(i); + inv.addItem(item); // 添加物品到物品栏 + } + } + } + } + for (int i = 46;i<53;i++){ + inv.setItem(i, DemonAPI.glass(0,"§r")); + } + inv.setItem(45,Paper_Butt("§a上一页",sortName,page)); + inv.setItem(53,Paper_Butt("§a下一页",sortName,page)); + p.openInventory(inv); + } + + public static ItemStack ShowStack(String sortName,String itemKey,ItemStack stack){ + ItemStack item = stack.clone(); + item.setAmount(1); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(meta.getDisplayName()+" §7#"+itemKey); + item.setItemMeta(meta); + NBTItem nbt = new NBTItem(item); + nbt.setString("sortName",sortName); + nbt.setString("itemKey",itemKey); + return nbt.getItem(); + } + + public static ItemStack Paper_Butt(String name,String sortName,int page){ + ItemStack item = new ItemStack(Material.ARROW); + item.setAmount(page); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(name); + 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 ffa4f83..708f00a 100644 --- a/src/main/java/com/yaohun/itemlibrary/main.java +++ b/src/main/java/com/yaohun/itemlibrary/main.java @@ -1,16 +1,79 @@ package com.yaohun.itemlibrary; +import cn.hamster3.cdapi.CDTimeAPI; +import com.yaohun.itemlibrary.gui.SortGui; +import com.yaohun.itemlibrary.manage.SortManager; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Sound; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; public class main extends JavaPlugin { + public static SortManager sortManager; + public static main plugin; @Override public void onEnable() { - super.onEnable(); + plugin = this; + sortManager = new SortManager(); + getServer().getPluginManager().registerEvents(new SortGui(),this); } @Override public void onDisable() { - super.onDisable(); + sortManager.SaveSortData(); + } + + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if(!sender.isOp()){return true;} + if (args.length == 0) { + sender.sendMessage("/ditem sort [分类名] --- 打开分类/创建分类"); + sender.sendMessage("/ditem [分类名] [物品名] --- 保存物品"); + } + if(args.length == 1 && args[0].equalsIgnoreCase("reload")){ + SortGui.playerListHashMap.clear(); + DemonAPI.sendMessage(sender,"缓存数据和配置文件已重载."); + } + if(args.length == 2){ + if(args[0].equalsIgnoreCase("sort")) { + Player player = (Player) sender; + String sortName = args[1]; + if (sortManager.isSortFileExit(sortName)) { + SortGui.OpenGui(player, sortName, 1); + } else { + long createRequest = CDTimeAPI.getCD(player.getUniqueId(), "create_request"); + if (createRequest < 0) { + DemonAPI.sendMessage(sender, "分类不存在§e创建文件§a请再次输入此命令.", Sound.ENTITY_VILLAGER_NO); + CDTimeAPI.setPlayerCD(player.getUniqueId(), "create_request", 1000 * 5); + return true; + } + sortManager.createSortFile(sortName); + DemonAPI.sendMessage(sender, "分类文件§e" + sortName + "§a创建成功.", Sound.ENTITY_EXPERIENCE_ORB_PICKUP); + } + } else { + String sortName = args[0]; + Player player = (Player) sender; + ItemStack item = player.getInventory().getItemInMainHand(); + if (DemonAPI.itemIsNull(item) || DemonAPI.itemIsLore(item) || DemonAPI.itemIsName(item)) { + DemonAPI.sendMessage(player, "当前手持物品无法保存至物品库.",Sound.ENTITY_VILLAGER_NO); + return true; + } + if (!sortManager.isSortFileExit(sortName)) { + DemonAPI.sendMessage(player, "分类§e"+sortName+"§a并不存在.",Sound.ENTITY_VILLAGER_NO); + return true; + } + String itemKey = args[1]; + FileConfiguration yml = main.plugin.getConfig(); + yml.set("ItemStack." + itemKey, item); + main.plugin.saveConfig(); + } + } + return false; } } diff --git a/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java b/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java new file mode 100644 index 0000000..b3c8890 --- /dev/null +++ b/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java @@ -0,0 +1,98 @@ +package com.yaohun.itemlibrary.manage; + +import com.yaohun.itemlibrary.data.SortData; +import com.yaohun.itemlibrary.main; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.util.LinkedHashMap; + +public class SortManager { + + private LinkedHashMap sortDataLinkedHashMap = new LinkedHashMap<>(); + + public SortManager(){ + File folder = new File(main.plugin.getDataFolder(), "ItemType"); // 获取目录对象 + File[] files = folder.listFiles((dir, name) -> name.endsWith(".yml")); // 列出目录下所有以 .yml 结尾的文件 + if (files != null) { // 确保目录存在且不是空的 + for (File file : files) { // 遍历每个文件 + // 这里可以对每个文件进行操作 + String sortName = file.getName().replace(".yml",""); + FileConfiguration configuration = YamlConfiguration.loadConfiguration(file); + // 设置默认显示堆叠物品 + String showStack = configuration.getString("showStack", "DIAMOND"); + // 设置物品堆叠 + ConfigurationSection section = configuration.getConfigurationSection("itemStack"); + sortDataLinkedHashMap.put(sortName,new SortData(sortName,showStack,section,file,configuration)); + } + } + DemonAPI.sendConsoleMessage("§f[§a!§f] §f物品分类 §8> §6"+sortDataLinkedHashMap.size()+"种"); + } + + /* + * 创建这个分类文件 + * */ + public void createSortFile(String sortName){ + File file = new File(main.plugin.getDataFolder() + "/ItemType", sortName + ".yml"); // 创建文件对象 + if (!file.exists()) { // 检查文件是否存在 + try { + file.getParentFile().mkdirs(); // 创建文件夹 + file.createNewFile(); // 创建文件 + } catch (IOException e) { + e.printStackTrace(); // 捕获并打印IO异常 + } + // 加载文件配置 + FileConfiguration configuration = YamlConfiguration.loadConfiguration(file); + // 设置默认显示名称 + configuration.set("showName", "§6§l默认分类名"); + // 设置默认显示堆叠物品 + configuration.set("showStack", "DIAMOND"); + // 设置物品堆叠 + configuration.createSection("itemStack"); + // 保存文件 + saveFile(file, configuration); + } + } + + public void SaveSortData(){ + for (String sortName : sortDataLinkedHashMap.keySet()){ + SortData sortData = sortDataLinkedHashMap.get(sortName); + sortData.SaveData(); + } + } + + /* + * 获取分类数据 + * */ + public SortData getSortData(String sortName){ + if (sortDataLinkedHashMap.get(sortName) == null) { + return null; + } + return sortDataLinkedHashMap.get(sortName); + } + + /* + * 获取这个分类的文件是否存在 + * */ + public boolean isSortFileExit(String sortName){ + if (sortDataLinkedHashMap.get(sortName) == null) { // 检查文件是否存在 + return false; // 如果文件不存在,返回 false + } + return true; // 如果文件存在,返回 true + } + + /* + * 保存文件 + * */ + public void saveFile(File file,FileConfiguration fileConfiguration){ + try { + fileConfiguration.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/resources/ItemType/材料.yml b/src/main/resources/ItemType/材料.yml new file mode 100644 index 0000000..8f1c825 --- /dev/null +++ b/src/main/resources/ItemType/材料.yml @@ -0,0 +1,2 @@ +showName: "默认名" +showStack: DIAMOND \ No newline at end of file diff --git a/src/main/resources/ItemType/道具.yml b/src/main/resources/ItemType/道具.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..19bdcb6 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,19 @@ +ItemStack: + 自定义称号卡: + ==: org.bukkit.inventory.ItemStack + type: NAME_TAG + meta: + ==: ItemMeta + meta-type: UNSPECIFIC + display-name: §c§l自定义称号卡 + lore: + - §e用法:§a拿在手上右键使用 + - §e作用:§a修改前缀称号 + - §e说明: + - §7 -§a定制一款个性彩色称号 + - §7 -§a前缀字数最多六个字 + - §7 -§c禁止修改辱骂性称号 + enchants: + PROTECTION_ENVIRONMENTAL: 1 + ItemFlags: + - HIDE_ENCHANTS \ No newline at end of file