测试版

This commit is contained in:
yaohunya 2024-07-24 07:04:45 +08:00
parent aeebd20585
commit c07efc4359
9 changed files with 436 additions and 4 deletions

View File

@ -26,14 +26,19 @@
<artifactId>spigot-api</artifactId>
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>cn.hamster3.cdapi</groupId>
<artifactId>CDTimeAPI</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>me.Demon.DemonPlugin</groupId>
<artifactId>DemonAPI</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>io.lumine.xikage.mythicmobs</groupId>
<artifactId>MythicMobs</artifactId>
<groupId>com.yaohun.nbtapi</groupId>
<artifactId>NBT-API</artifactId>
<version>1.12.2</version>
</dependency>
</dependencies>

View File

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

View File

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

View File

@ -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<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) { // 检查玩家的物品列表是否为空
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<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,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;
}
}

View File

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

View File

@ -0,0 +1,2 @@
showName: "默认名"
showStack: DIAMOND

View File

View File

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