测试版

This commit is contained in:
yaohunya 2024-07-24 19:23:39 +08:00
parent 29587f4538
commit 777d2554c4
8 changed files with 298 additions and 34 deletions

6
README.md Normal file
View File

@ -0,0 +1,6 @@
# AuItemStackLibrary
一个带分类的物品库
- 随机物品组 OK
- 分类物品库 OK
- give物品对接 OK
-

View File

@ -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<String,Integer> stackMap = new HashMap<>();
public RandomData(String groupName,List<String> 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<ItemStack> 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);
}
}

View File

@ -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<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",sortData.getSortName());
return nbt.getItem();
}
}

View File

@ -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<Player,List<ItemStack>> 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<ItemStack> 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<String> lore = new ArrayList<>();

View File

@ -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;

View File

@ -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<String, RandomData> 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<String> 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);
}
}

View File

@ -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<String, ItemStack> stackHashMap = new LinkedHashMap<>();
private LinkedHashMap<String, SortData> 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<String, SortData> getSortDataLinkedHashMap() {
return sortDataLinkedHashMap;
}
public HashMap<String, ItemStack> 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();
}
/*
* 保存文件
* */

View File

@ -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
ItemsRandGroup: {}