From 777d2554c41847ef982f8b9e2506a113065f0720 Mon Sep 17 00:00:00 2001 From: yaohunya <31456652@qq.com> Date: Wed, 24 Jul 2024 19:23:39 +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 --- README.md | 6 ++ .../yaohun/itemlibrary/data/RandomData.java | 43 ++++++++ .../yaohun/itemlibrary/gui/SortAllGui.java | 68 +++++++++++++ .../com/yaohun/itemlibrary/gui/SortGui.java | 21 ++-- .../java/com/yaohun/itemlibrary/main.java | 99 ++++++++++++++++++- .../itemlibrary/manage/RandomGroup.java | 39 ++++++++ .../itemlibrary/manage/SortManager.java | 36 +++++++ src/main/resources/config.yml | 20 +--- 8 files changed, 298 insertions(+), 34 deletions(-) create mode 100644 README.md create mode 100644 src/main/java/com/yaohun/itemlibrary/data/RandomData.java create mode 100644 src/main/java/com/yaohun/itemlibrary/gui/SortAllGui.java create mode 100644 src/main/java/com/yaohun/itemlibrary/manage/RandomGroup.java diff --git a/README.md b/README.md new file mode 100644 index 0000000..46ce57c --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# AuItemStackLibrary +一个带分类的物品库 +- 随机物品组 OK +- 分类物品库 OK +- give物品对接 OK +- \ No newline at end of file diff --git a/src/main/java/com/yaohun/itemlibrary/data/RandomData.java b/src/main/java/com/yaohun/itemlibrary/data/RandomData.java new file mode 100644 index 0000000..5e9220d --- /dev/null +++ b/src/main/java/com/yaohun/itemlibrary/data/RandomData.java @@ -0,0 +1,43 @@ +package com.yaohun.itemlibrary.data; + +import com.yaohun.itemlibrary.main; +import org.bukkit.inventory.ItemStack; + +import java.util.*; + +public class RandomData { + + private String groupName; + private HashMap stackMap = new HashMap<>(); + public RandomData(String groupName,List stringList){ + this.groupName = groupName; + for (String itemKey : stringList){ + if(itemKey.contains("#")){ + String[] strings = itemKey.split("#"); + String NewitemKey = strings[0]; + int amount = Integer.parseInt(strings[1]); + stackMap.put(NewitemKey,amount); + }else{ + stackMap.put(itemKey,1); + } + } + } + + public String getGroupName() { + return groupName; + } + + public ItemStack getRandomStack(){ + List stackList = new ArrayList<>(); + for (String itemKey : stackMap.keySet()){ + stackList.add(getItemStack(itemKey)); + } + Collections.shuffle(stackList); + Random random = new Random(); + return stackList.get(random.nextInt(stackList.size())); + } + + public ItemStack getItemStack(String itemKey) { + return main.sortManager.getItemStack(itemKey); + } +} diff --git a/src/main/java/com/yaohun/itemlibrary/gui/SortAllGui.java b/src/main/java/com/yaohun/itemlibrary/gui/SortAllGui.java new file mode 100644 index 0000000..1b965b7 --- /dev/null +++ b/src/main/java/com/yaohun/itemlibrary/gui/SortAllGui.java @@ -0,0 +1,68 @@ +package com.yaohun.itemlibrary.gui; + +import com.yaohun.itemlibrary.data.SortData; +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.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; + +public class SortAllGui 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); + ItemStack item = e.getCurrentItem(); + if(!DemonAPI.itemIsNull(item)){ + NBTItem nbt = new NBTItem(item); + if(nbt.hasKey("sortName")){ + String sortName = nbt.getString("sortName"); + SortGui.OpenGui(p,sortName,1); + p.playSound(p.getLocation(), Sound.BLOCK_COMPARATOR_CLICK,1.6F,1.8F); + } + } + } + } + + public static void OpenGui(Player p) { + Inventory inv = Bukkit.createInventory(null, 36, invTitle); + SortManager sortManager = main.sortManager; + for (String sortName : sortManager.getSortDataLinkedHashMap().keySet()){ + SortData sortData = sortManager.getSortData(sortName); + inv.addItem(showTypeStack(sortData)); + } + p.openInventory(inv); + } + + + public static ItemStack showTypeStack(SortData sortData){ + ItemStack item = sortData.getShowStack(); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName("§6§l"+sortData.getSortName()); + 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",sortData.getSortName()); + return nbt.getItem(); + } +} diff --git a/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java b/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java index 4f688b3..6a726f7 100644 --- a/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java +++ b/src/main/java/com/yaohun/itemlibrary/gui/SortGui.java @@ -46,7 +46,7 @@ public class SortGui implements Listener { if (page >= 2 && page <= 5) { SortGui.OpenGui(p,sortName,page-1); } else { - DemonAPI.sendMessage(p,"已经到第一页了!"); + SortAllGui.OpenGui(p); } } if(rawSlot == 53){ @@ -62,8 +62,6 @@ public class SortGui implements Listener { } 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"); @@ -95,22 +93,18 @@ public class SortGui implements Listener { } } - 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) { - 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); // 将物品列表放入玩家映射 + SortData sortData = sortManager.getSortData(sortName); + inv = Bukkit.createInventory(null,54,invTitle+"[§6§l"+sortName+"§r]"); + for (String itemKey : sortData.getStackLinkedMap().keySet()) { + ItemStack item = sortData.getStackLinkedMap().get(itemKey).clone(); + ItemStack stack = ShowStack(sortName, itemKey, item); // 获取每个物品堆叠 + itemsListID.add(stack); // 添加物品到列表 } - itemsListID = playerListHashMap.get(p); // 获取玩家的物品列表 if (itemsListID.size() >= 1) { // 如果物品列表不为空 if (page == 1) { // 如果是第一页 for (int i = 0; i < itemsListID.size(); i++) { // 遍历物品列表 @@ -163,7 +157,6 @@ public class SortGui implements Listener { } 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<>(); diff --git a/src/main/java/com/yaohun/itemlibrary/main.java b/src/main/java/com/yaohun/itemlibrary/main.java index ceadc4b..d158c6d 100644 --- a/src/main/java/com/yaohun/itemlibrary/main.java +++ b/src/main/java/com/yaohun/itemlibrary/main.java @@ -1,10 +1,15 @@ package com.yaohun.itemlibrary; import cn.hamster3.cdapi.CDTimeAPI; +import com.yaohun.itemlibrary.data.RandomData; import com.yaohun.itemlibrary.data.SortData; +import com.yaohun.itemlibrary.gui.SortAllGui; import com.yaohun.itemlibrary.gui.SortGui; +import com.yaohun.itemlibrary.manage.RandomGroup; import com.yaohun.itemlibrary.manage.SortManager; import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -16,17 +21,25 @@ import org.bukkit.plugin.java.JavaPlugin; public class main extends JavaPlugin { public static SortManager sortManager; + public static RandomGroup randomGroup; public static main plugin; @Override public void onEnable() { plugin = this; + saveDefaultConfig(); + DemonAPI.sendConsoleMessage("§f[§6!§f] §aAuItemStackLibrary (1.0) §f开始加载"); sortManager = new SortManager(); + randomGroup = new RandomGroup(); getServer().getPluginManager().registerEvents(new SortGui(),this); + getServer().getPluginManager().registerEvents(new SortAllGui(),this); + DemonAPI.sendConsoleMessage("§f[§6!§f] §aAuItemStackLibrary §f加载完成,祝你使用愉快!"); + DemonAPI.sendConsoleMessage("§f[§6!§f] §b极光像素工作室出品"); } @Override public void onDisable() { sortManager.SaveSortData(); + DemonAPI.sendConsoleMessage("§f[§c!§f] §aAuItemStackLibrary §f卸载完成,欢迎下次使用!"); } @@ -36,12 +49,96 @@ public class main extends JavaPlugin { if (args.length == 0) { sender.sendMessage("/ditem sort [分类名] --- 打开分类/创建分类"); sender.sendMessage("/ditem [分类名] [物品名] --- 保存物品"); + sender.sendMessage("/ditem give [物品名] [数量] <玩家名> --- 给予玩家物品"); + sender.sendMessage("/ditem type [物品id] [数量] <玩家名> --- 给予玩家物品"); + sender.sendMessage("/ditem group [物品组] <玩家名> --- 给予随机物品组"); + } + if(args.length == 1 && args[0].equalsIgnoreCase("open")){ + SortAllGui.OpenGui((Player) sender); } if(args.length == 1 && args[0].equalsIgnoreCase("reload")){ sortManager = new SortManager(); - SortGui.playerListHashMap.clear(); DemonAPI.sendMessage(sender,"缓存数据和配置文件已重载."); } + if(args.length >= 2){ + if(args[0].equalsIgnoreCase("give")) { + String name = sender.getName(); + String itemKey = args[1]; + int amount = 1; + if(args.length >= 3){ + amount = Integer.parseInt(args[2]); + } + if(args.length >= 4){ + name = args[3]; + } + if(!sortManager.isStackExit(itemKey)){ + DemonAPI.sendMessage(sender, "物品 §6"+itemKey+" §a并未找到."); + return true; + } + ItemStack item = sortManager.getItemStack(itemKey); + item.setAmount(amount); + Player player = Bukkit.getPlayer(name); + if(player == null){ + DemonAPI.sendMessage(sender, "给予目标玩家 §6"+name+" §a并未找到."); + return true; + } + player.getInventory().addItem(item); + DemonAPI.sendMessage(sender, "玩家: §e"+name+" §a物品: §6"+itemKey+" §a数量: §b"+amount); + return true; + } + if(args[0].equalsIgnoreCase("type")) { + String name = sender.getName(); + String itemId = args[1]; + int amount = 1; + if(args.length >= 3){ + amount = Integer.parseInt(args[2]); + } + if(args.length >= 4){ + name = args[3]; + } + int itemType = 1; + int itemData = 0; + if(itemId.contains(":")){ + String[] strings = itemId.split(":"); + itemType = Integer.parseInt(strings[0]); + itemData = Integer.parseInt(strings[1]); + }else{ + itemType = Integer.parseInt(itemId); + } + ItemStack item = new ItemStack(itemType,amount,(short) itemData); + Player player = Bukkit.getPlayer(name); + if(player == null){ + DemonAPI.sendMessage(sender, "给予目标玩家 §6"+name+" §a并未找到."); + return true; + } + player.getInventory().addItem(item); + DemonAPI.sendMessage(sender, "玩家: §e"+name+" §a物品: §6"+DemonAPI.getItemName(item)+" §a数量: §b"+amount); + return true; + } + } + if(args.length >= 2){ + if(args[0].equalsIgnoreCase("group")){ + String name = sender.getName(); + String groupName = args[1]; + if(args.length >= 4){ + name = args[2]; + } + if(!randomGroup.isRandomDataExit(groupName)){ + DemonAPI.sendMessage(sender,"随机组 §6"+groupName+" §a不存在."); + return true; + } + Player player = Bukkit.getPlayer(name); + if(player == null){ + DemonAPI.sendMessage(sender, "给予目标玩家 §6"+name+" §a并未找到."); + return true; + } + RandomData randomData = randomGroup.getRandomData(groupName); + ItemStack stack = randomData.getRandomStack(); + player.getInventory().addItem(stack); + DemonAPI.sendMessage(sender, "玩家: §e"+name+" §a随机组: §6"+groupName+" §a物品: §b"+DemonAPI.getItemName(stack)); + return true; + } + } if(args.length == 2){ if(args[0].equalsIgnoreCase("sort")) { Player player = (Player) sender; diff --git a/src/main/java/com/yaohun/itemlibrary/manage/RandomGroup.java b/src/main/java/com/yaohun/itemlibrary/manage/RandomGroup.java new file mode 100644 index 0000000..a661be1 --- /dev/null +++ b/src/main/java/com/yaohun/itemlibrary/manage/RandomGroup.java @@ -0,0 +1,39 @@ +package com.yaohun.itemlibrary.manage; + +import com.yaohun.itemlibrary.data.RandomData; +import com.yaohun.itemlibrary.main; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.HashMap; +import java.util.List; + +public class RandomGroup { + + private HashMap randomDataHashMap = new HashMap<>(); + + public RandomGroup(){ + FileConfiguration yml = main.plugin.getConfig(); + ConfigurationSection section = yml.getConfigurationSection("ItemsRandGroup"); + if(section == null){ + return; + } + for (String groupName : section.getKeys(false)){ + List stringList = section.getStringList(groupName); + randomDataHashMap.put(groupName,new RandomData(groupName,stringList)); + } + DemonAPI.sendConsoleMessage("§f[§a!§f] §f随机物品组 §8> §6"+randomDataHashMap.size()+" 个"); + } + + public boolean isRandomDataExit(String groupName){ + if(randomDataHashMap.get(groupName) == null){ + return false; + } + return true; + } + + public RandomData getRandomData(String groupName){ + return randomDataHashMap.get(groupName); + } +} diff --git a/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java b/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java index 7017adf..bf4adaa 100644 --- a/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java +++ b/src/main/java/com/yaohun/itemlibrary/manage/SortManager.java @@ -6,13 +6,16 @@ import me.Demon.DemonPlugin.DemonAPI; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.inventory.ItemStack; import java.io.File; import java.io.IOException; +import java.util.HashMap; import java.util.LinkedHashMap; public class SortManager { + private LinkedHashMap stackHashMap = new LinkedHashMap<>(); private LinkedHashMap sortDataLinkedHashMap = new LinkedHashMap<>(); public SortManager(){ @@ -29,6 +32,13 @@ public class SortManager { ConfigurationSection section = configuration.getConfigurationSection("itemStack"); sortDataLinkedHashMap.put(sortName,new SortData(sortName,showStack,section,file,configuration)); } + for (String sortName : sortDataLinkedHashMap.keySet()){ + SortData sortData = sortDataLinkedHashMap.get(sortName); + for (String itemKey : sortData.getStackLinkedMap().keySet()){ + stackHashMap.put(itemKey,sortData.getStackLinkedMap().get(itemKey)); + } + } + DemonAPI.sendConsoleMessage("§f[§a!§f] §f物品存储 §8> §6"+stackHashMap.size()+" 件"); } } @@ -84,6 +94,32 @@ public class SortManager { return true; // 如果文件存在,返回 true } + public LinkedHashMap getSortDataLinkedHashMap() { + return sortDataLinkedHashMap; + } + + public HashMap getStackHashMap() { + return stackHashMap; + } + + public void putStackHashMap(String itemKey,ItemStack stack) { + this.stackHashMap.put(itemKey,stack); + } + + public boolean isStackExit(String itemKey){ + if(stackHashMap.get(itemKey) == null){ + return false; + } + return true; + } + + public ItemStack getItemStack(String itemKey){ + if(stackHashMap.get(itemKey) != null){ + return stackHashMap.get(itemKey); + } + return DemonAPI.getErrItems(); + } + /* * 保存文件 * */ diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 19bdcb6..2291592 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,19 +1 @@ -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 +ItemsRandGroup: {} \ No newline at end of file