From 4c9015bacaab1fb7b7d7030f00ae273b8e7b6392 Mon Sep 17 00:00:00 2001 From: tianyu <32282861@qq.com> Date: Wed, 24 Jul 2024 06:14:33 +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 --- .gitignore | 38 ++++ pom.xml | 37 ++++ .../java/me/Demon/DemonTrash/AdminGui.java | 163 ++++++++++++++++++ src/main/java/me/Demon/DemonTrash/Main.java | 96 +++++++++++ .../java/me/Demon/DemonTrash/MainGui.java | 51 ++++++ .../java/me/Demon/DemonTrash/TrashAPI.java | 32 ++++ src/main/resources/Data.yml | 18 ++ src/main/resources/config.yml | 2 + src/main/resources/plugin.yml | 6 + 9 files changed, 443 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/me/Demon/DemonTrash/AdminGui.java create mode 100644 src/main/java/me/Demon/DemonTrash/Main.java create mode 100644 src/main/java/me/Demon/DemonTrash/MainGui.java create mode 100644 src/main/java/me/Demon/DemonTrash/TrashAPI.java create mode 100644 src/main/resources/Data.yml create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e3d8624 --- /dev/null +++ b/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + org.example + DemonTrash + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + public-rpg + https://repo.aurora-pixels.com/repository/public-rpg/ + + + + + + org.spigotmc + spigot-api + 1.12.2 + + + me.Demon.DemonPlugin + DemonAPI + 1.2.0 + + + + \ No newline at end of file diff --git a/src/main/java/me/Demon/DemonTrash/AdminGui.java b/src/main/java/me/Demon/DemonTrash/AdminGui.java new file mode 100644 index 0000000..c110f9d --- /dev/null +++ b/src/main/java/me/Demon/DemonTrash/AdminGui.java @@ -0,0 +1,163 @@ +package me.Demon.DemonTrash; + +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.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; + +public class AdminGui implements Listener { + + public static String inv_Title = "管理员垃圾桶(右键物品即可取出)#"; + + @EventHandler + public void onclick(InventoryClickEvent e) { + Player p = (Player) e.getWhoClicked(); + if (e.getView().getTitle().contains(inv_Title)) { + e.setCancelled(true); + int Pages = Integer.parseInt(e.getView().getTitle().replace(inv_Title, "")); + if (e.getRawSlot() == 45) { + p.playSound(p.getLocation(), Sound.BLOCK_LEVER_CLICK, 1, 2); + if (Pages >= 2 && Pages <= 15) { + AdminGui.OpenGui(p, Pages - 1); + } else { + p.sendMessage(Main.prefix + "§c已经是第一页了!"); + } + } + if (e.getRawSlot() == 53) { + p.playSound(p.getLocation(), Sound.BLOCK_LEVER_CLICK, 1, 2); + if (Pages >= 1 && Pages <= 14) { + AdminGui.OpenGui(p, Pages + 1); + } else { + p.sendMessage(Main.prefix + "§c已经是最后一页了!"); + } + } + if (e.getRawSlot() >= 0 && e.getRawSlot() <= 44) { + if (!DemonAPI.itemIsNull(e.getCurrentItem()) && !DemonAPI.itemIsLore(e.getCurrentItem())) { + String playName = null; + String keyId = null; + ItemStack item = e.getCurrentItem(); + ItemMeta meta = item.getItemMeta(); + for (String str : meta.getLore()) { + if (str.startsWith("§a§l★§7物品主人: §f")) { + playName = str.replace("§a§l★§7物品主人: §f", ""); + } + if (str.startsWith("§a§l★§7物品序号: §f")) { + keyId = str.replace("§a§l★§7物品序号: §f", ""); + } + } + String key = playName + "#" + keyId; + if (e.getClick() == ClickType.LEFT) { + ItemStack items = Main.DataG.getItemStack("TrashItems." + key + ".Items"); + p.getInventory().addItem(items); + p.playSound(p.getLocation(), Sound.BLOCK_LEVER_CLICK, 1, 2); + Main.DataG.set("TrashItems." + key, null); + Main.KeyList.remove(key); + Main.saveDataStatsConfig(); + AdminGui.OpenGui(p, Pages); + } + if (e.getClick() == ClickType.SHIFT_RIGHT) { + p.playSound(p.getLocation(), Sound.BLOCK_LEVER_CLICK, 1, 2); + Main.DataG.set("TrashItems." + key, null); + Main.KeyList.remove(key); + Main.saveDataStatsConfig(); + AdminGui.OpenGui(p, Pages); + } + } + } + } + } + + public static void OpenGui(Player p, int Paper) { + String title = inv_Title + Paper; + Inventory inv = Bukkit.createInventory(null, 54, title); + for (int i = 46; i < 53; i++) { + inv.setItem(i, glass(15, "§7[[§a▧§c▧§a▧§7]]")); + } + inv.setItem(45, Paper_Butt("§a上一页")); + inv.setItem(53, Paper_Butt("§a下一页")); + List New_KeyList = Main.KeyList; + if (New_KeyList.size() >= 1) { + int amount = New_KeyList.size() - 1; + int addamount = Paper * 36; + if (Paper == 1) { + for (int i = amount; i > -1; i--) { + if (New_KeyList.get(i) == null) { + break; + } + String key = Main.KeyList.get(i); + String str = "TrashItems." + key + "."; + String playName = key.split("#")[0]; + String keyID = key.split("#")[1]; + String time = Main.DataG.getString(str + "DropTime"); + ItemStack items = Main.DataG.getItemStack(str + "Items"); + inv.addItem(ItemsShow(playName, items, keyID, time)); + } + } else { + for (int i = amount - (addamount - 9); i > (amount - addamount); i--) { + if (i >= 0) { + String key = New_KeyList.get(i); + String str = "TrashItems." + key + "."; + String playName = key.split("#")[0]; + String keyID = key.split("#")[1]; + String time = Main.DataG.getString(str + "DropTime"); + ItemStack items = Main.DataG.getItemStack(str + "Items"); + inv.addItem(ItemsShow(playName, items, keyID, time)); + } + } + } + } + p.openInventory(inv); + } + + + public static ItemStack ItemsShow(String playName, ItemStack itemStack, String keyID, String time) { + ItemStack item = itemStack.clone(); + ItemMeta meta = item.getItemMeta(); + List lore = new ArrayList<>(itemStack.getItemMeta().getLore()); + lore.add("§7-------------------------"); + lore.add("§a§l★§7物品主人: §f" + playName); + lore.add("§a§l★§7物品序号: §f" + keyID); + lore.add("§a§l★§7丢弃时间: §f" + time); + lore.add(" "); + lore.add("§e§l★§7左键点击取出此物品"); + lore.add("§e§l★§7SHIFT+右键点击删除此物品"); + meta.setLore(lore); + meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); + meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE); + meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS); + item.setItemMeta(meta); + return item; + } + + + public static ItemStack Paper_Butt(String name) { + ItemStack item = new ItemStack(Material.ARROW); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(name); + List lore = new ArrayList<>(); + lore.add("§a§l★ §7点击翻页 §a§l★"); + meta.setLore(lore); + item.setItemMeta(meta); + return item; + } + + public static ItemStack glass(int data, String name) { + ItemStack item = new ItemStack(160, 1, (short) data); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(name); + item.setItemMeta(meta); + return item; + } +} diff --git a/src/main/java/me/Demon/DemonTrash/Main.java b/src/main/java/me/Demon/DemonTrash/Main.java new file mode 100644 index 0000000..fa4d966 --- /dev/null +++ b/src/main/java/me/Demon/DemonTrash/Main.java @@ -0,0 +1,96 @@ +package me.Demon.DemonTrash; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +public class Main extends JavaPlugin implements Listener { + + public static Main plugin; + public static String prefix = "§7[§6废品处理§7] §f"; + //获取序列号 + public static List KeyList = new ArrayList<>(); + public static List BlackNoItemList = new ArrayList<>(); + public static File Datafile = new File("plugins/DemonTrash", "Data.yml"); + public static FileConfiguration DataG = YamlConfiguration.loadConfiguration(Datafile); + public static void saveDataStatsConfig(){ try { Main.DataG.save(Datafile); }catch (IOException e){e.printStackTrace();} } + + public void onEnable() { + plugin = this; + saveDefaultConfig(); + BlackNoItemList.addAll(getConfig().getStringList("AntiItemsLore")); + // 获取今天是否是15号 + int time = Integer.parseInt(getTime("dd")); + if(time == 15){ + int a = 0; + // 清理非本月的所有垃圾 + FileConfiguration yml = Main.DataG; + for (String key : yml.getConfigurationSection("TrashItems").getKeys(false)){ + String trash_time = yml.getString("TrashItems."+key + ".DropTime"); + if(!trash_time.split("-")[0].equalsIgnoreCase(getTime("MM"))){ + yml.set("TrashItems."+key,null); + a++; + } + } + Main.saveDataStatsConfig(); + Bukkit.getConsoleSender().sendMessage("§3[魂式垃圾箱] §e总计清理历史垃圾 §a"+a+"件"); + } + if(Main.DataG.getConfigurationSection("TrashItems") != null) { + KeyList.addAll(Main.DataG.getConfigurationSection("TrashItems").getKeys(false)); + } + Bukkit.getPluginManager().registerEvents(this, this); + Bukkit.getServer().getPluginManager().registerEvents(new MainGui(), plugin); + Bukkit.getServer().getPluginManager().registerEvents(new AdminGui(), plugin); + Bukkit.getConsoleSender().sendMessage("§3[魂式垃圾箱] §a插件成功载入Server!"); + Bukkit.getConsoleSender().sendMessage("§3[魂式垃圾箱] §a妖魂QQ:1763917516"); + } + + public void onDisable() { + Bukkit.getConsoleSender().sendMessage("§3[魂式垃圾箱] §c插件已正常关闭!"); + } + + public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) { + if (CommandLabel.equalsIgnoreCase("ljt")) { + Player p = (Player) sender; + MainGui.OpenGui(p); + } + if (CommandLabel.equalsIgnoreCase("trash")) { + if (!sender.isOp()) { + sender.sendMessage("§7[§6帝國戰爭§7] §f指令無效."); + return true; + } + if (args.length == 1 && args[0].equalsIgnoreCase("qingli")) { + if (Main.DataG.getConfigurationSection("TrashItems") != null) { + for (String key : Main.DataG.getConfigurationSection("TrashItems").getKeys(false)) { + Main.DataG.set("TrashItems." + key, null); + } + Main.saveDataStatsConfig(); + } + sender.sendMessage(prefix + "总计清理 §e" + KeyList.size() + " §f件。"); + KeyList.clear(); + } else{ + AdminGui.OpenGui((Player) sender, 1); + } + } + return false; + } + + public static String getTime(String format){ + Date date = Calendar.getInstance().getTime(); + SimpleDateFormat datatime = new SimpleDateFormat(format); + return datatime.format(date); + } +} diff --git a/src/main/java/me/Demon/DemonTrash/MainGui.java b/src/main/java/me/Demon/DemonTrash/MainGui.java new file mode 100644 index 0000000..ed055ee --- /dev/null +++ b/src/main/java/me/Demon/DemonTrash/MainGui.java @@ -0,0 +1,51 @@ +package me.Demon.DemonTrash; + +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Bukkit; +import org.bukkit.Sound; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +public class MainGui implements Listener { + + public static void OpenGui(Player p){ + String title = "公共垃圾桶(请把不要的物品放进来)"; + Inventory inv = Bukkit.createInventory(null,18,title); + p.openInventory(inv); + p.playSound(p.getLocation(), Sound.BLOCK_LEVER_CLICK,1,2); + } + + @EventHandler + public void onclose(InventoryCloseEvent e){ + Player p = (Player) e.getPlayer(); + if(e.getView().getTitle().equalsIgnoreCase("公共垃圾桶(请把不要的物品放进来)")){ + Inventory inv = e.getInventory(); + Inventory newInv = Bukkit.createInventory(null,18,"公共垃圾桶(请再次确认你要丢弃的物品)"); + for (int i = 0;i< 18;i++){ + ItemStack item = inv.getItem(i); + if(!DemonAPI.itemIsNull(item)){ + newInv.addItem(item); + } + } + int amount = 0; + for (int i = 0; i < 18; i++) { + ItemStack item = newInv.getItem(i); + if(!DemonAPI.itemIsNull(item)) { + if(!DemonAPI.itemIsLore(item)) { + TrashAPI.addItemsTrash(p.getName(), item); + } + amount = amount + item.getAmount(); + } + } + if(amount >= 1) { + p.sendMessage(Main.prefix + "本次总计处理§e" + amount + "样§f你丢弃的废品。"); + p.playSound(p.getLocation(), Sound.BLOCK_LEVER_CLICK, 1, 2); + } + } + } + +} diff --git a/src/main/java/me/Demon/DemonTrash/TrashAPI.java b/src/main/java/me/Demon/DemonTrash/TrashAPI.java new file mode 100644 index 0000000..9a22941 --- /dev/null +++ b/src/main/java/me/Demon/DemonTrash/TrashAPI.java @@ -0,0 +1,32 @@ +package me.Demon.DemonTrash; + +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.Objects; + +public abstract class TrashAPI { + //将物品加入垃圾桶 + public static void addItemsTrash(String playName,ItemStack item){ + if(!itemIsBind(Objects.requireNonNull(item.getItemMeta()))) { + String key = playName + "#" + DemonAPI.getRandomInt(90000, 10000); + String time = Main.getTime("MM-dd"); + Main.DataG.set("TrashItems." + key + ".DropTime", time); + Main.DataG.set("TrashItems." + key + ".Items", item); + Main.KeyList.add(key); + Main.saveDataStatsConfig(); + } + } + + + public static boolean itemIsBind(ItemMeta meta){ + String itemName = meta.getDisplayName(); + for (int i = 0; i < Main.BlackNoItemList.size(); i++) { + if (meta.getLore().contains(Main.BlackNoItemList.get(i))) { + return true; + } + } + return false; + } +} diff --git a/src/main/resources/Data.yml b/src/main/resources/Data.yml new file mode 100644 index 0000000..9b1e097 --- /dev/null +++ b/src/main/resources/Data.yml @@ -0,0 +1,18 @@ +TrashItems: + Yao_Hun_Ya#10086: + DropTime: '2021-01-19' + Items: + ==: org.bukkit.inventory.ItemStack + type: DIAMOND_CHESTPLATE + meta: + ==: ItemMeta + meta-type: UNSPECIFIC + display-name: §b§l钻石甲 + lore: + - '§c§m§l §6§m§l §e§m§l §a§m§l §b§m§l §e§l装备淬炼§b§m§l §a§m§l §e§m§l §6§m§l §c§m§l ' + - §3§l❂六星淬炼 + - §e§l淬炼属性:§a附加防御+§610.0 + - §e§l淬炼属性:§a闪避率+§66.0% + enchants: + PROTECTION_ENVIRONMENTAL: 4 + THORNS: 2 \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..3f7e9f1 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,2 @@ +AntiItemsLore: + - "§7§o食物" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..f8ffd57 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,6 @@ +name: DemonTrash +main: me.Demon.DemonTrash.Main +version: 1.0 +commands: + trash: + ljt: \ No newline at end of file