commit f23d4d4ae1a678701d9c45c16739e28b39086cf0 Author: tianyu <32282861@qq.com> Date: Wed Jul 24 20:45:58 2024 +0800 测试版 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c037f46 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +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 +/.idea/ +/out/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..be13174 --- /dev/null +++ b/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + org.example + DemonChestPreview + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + public-rpg + https://repo.aurora-pixels.com/repository/public-rpg/ + + + + + + org.spigotmc + spigot-api + 1.12.2 + + + + \ No newline at end of file diff --git a/src/main/java/me/Demon/DemonChestPreview/Command/ChestCommand.java b/src/main/java/me/Demon/DemonChestPreview/Command/ChestCommand.java new file mode 100644 index 0000000..45c1d63 --- /dev/null +++ b/src/main/java/me/Demon/DemonChestPreview/Command/ChestCommand.java @@ -0,0 +1,79 @@ +package me.Demon.DemonChestPreview.Command; + +import me.Demon.DemonChestPreview.Main; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class ChestCommand implements CommandExecutor, TabExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + Player p = (Player) sender; + if (args.length == 1) { + if (args[0].equalsIgnoreCase("add") && sender.isOp()) { + Block targetBlock = p.getTargetBlock(null, 5); + if (targetBlock != null && targetBlock.getType() == Material.CHEST) { + String loc = getLoc(targetBlock); + if (!Main.previewList.contains(loc)) { + List list = Main.previewList; + list.add(loc); + Main.plugin.getConfig().set("ChestLoc", list); + Main.plugin.saveConfig(); + p.sendMessage(Main.prefix + "已添加箱子展示箱 §7#" + loc); + } else { + p.sendMessage(Main.prefix + "此箱子已存在展示坐标内."); + } + } else { + p.sendMessage(Main.prefix + "添加失败,指向的物品类型不是箱子."); + } + } else if (args[0].equalsIgnoreCase("del") && sender.isOp()) { + Block targetBlock = p.getTargetBlock(null, 5); + if (targetBlock != null && targetBlock.getType() == Material.CHEST) { + String loc = getLoc(targetBlock); + if (Main.previewList.contains(loc)) { + List list = Main.previewList; + list.remove(loc); + Main.plugin.getConfig().set("ChestLoc", list); + Main.plugin.saveConfig(); + p.sendMessage(Main.prefix + "已删除箱子展示箱 §7#" + loc); + } else { + p.sendMessage(Main.prefix + "此箱子不存在展示坐标内."); + } + } else { + p.sendMessage(Main.prefix + "删除失败,指向的物品类型不是箱子."); + } + } + } + return false; + } + + private static String getLoc(Block targetBlock) { + Location chestLocation = targetBlock.getLocation(); + String world = chestLocation.getWorld().getName(); + int x = chestLocation.getBlockX(); + int y = chestLocation.getBlockY(); + int z = chestLocation.getBlockZ(); + return world + "," + x + "," + y + "," + z; + } + + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + List list = new ArrayList<>(); + if (args.length == 1) { + if (sender.isOp()) { + list.add("add"); + list.add("del"); + } + return list; + } + return null; + } +} \ No newline at end of file diff --git a/src/main/java/me/Demon/DemonChestPreview/Listener/PreviewEvent.java b/src/main/java/me/Demon/DemonChestPreview/Listener/PreviewEvent.java new file mode 100644 index 0000000..1527ae3 --- /dev/null +++ b/src/main/java/me/Demon/DemonChestPreview/Listener/PreviewEvent.java @@ -0,0 +1,43 @@ +package me.Demon.DemonChestPreview.Listener; + +import me.Demon.DemonChestPreview.Main; +import org.bukkit.Location; +import org.bukkit.Sound; +import org.bukkit.block.BlockState; +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.event.inventory.InventoryType; +import org.bukkit.inventory.Inventory; + +public class PreviewEvent implements Listener { + + @EventHandler + public void onChestClick(InventoryClickEvent e) { + Player p = (Player) e.getWhoClicked(); + Inventory inv = e.getInventory(); + if (inv.getHolder() instanceof BlockState) { + BlockState blockState = (BlockState) inv.getHolder(); + Location chestLocation = blockState.getLocation(); + String world = chestLocation.getWorld().getName(); + int x = chestLocation.getBlockX(); + int y = chestLocation.getBlockY(); + int z = chestLocation.getBlockZ(); + String loc = world + "," + x + "," + y + "," + z; + if (Main.previewList.contains(loc)) { + if (inv.getType() == InventoryType.CHEST && !p.isOp()) { + if (e.getClick() == ClickType.DOUBLE_CLICK || e.getClick() == ClickType.SHIFT_LEFT || e.getClick() == ClickType.SHIFT_RIGHT) { + e.setCancelled(true); + p.closeInventory(); + p.sendMessage("§7点击速度过快,请慢一点。"); + p.playSound(p.getLocation(), Sound.BLOCK_COMPARATOR_CLICK, 1.0F, 2.0F); + return; + } + e.setCancelled(true); + } + } + } + } +} \ No newline at end of file diff --git a/src/main/java/me/Demon/DemonChestPreview/Main.java b/src/main/java/me/Demon/DemonChestPreview/Main.java new file mode 100644 index 0000000..70ac7cc --- /dev/null +++ b/src/main/java/me/Demon/DemonChestPreview/Main.java @@ -0,0 +1,33 @@ +package me.Demon.DemonChestPreview; + +import me.Demon.DemonChestPreview.Command.ChestCommand; +import me.Demon.DemonChestPreview.Listener.PreviewEvent; +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.ArrayList; +import java.util.List; + +public class Main extends JavaPlugin { + public static Main plugin; + public static String prefix = "§7[§6箱子展示§7] §f"; + public static List previewList = new ArrayList<>(); + + public void onEnable() { + plugin = this; + saveDefaultConfig(); + Bukkit.getServer().getPluginManager().registerEvents(new PreviewEvent(), plugin); + getCommand("dcpreview").setExecutor(new ChestCommand()); + getCommand("dcpreview").setTabCompleter(new ChestCommand()); + previewList.addAll(getConfig().getStringList("ChestLoc")); + Bukkit.getConsoleSender().sendMessage("§f[§6!§f] §aDemonChestPreview (" + this.getDescription().getVersion() + ") §f开始加载"); + Bukkit.getConsoleSender().sendMessage("§f[§6!§f] §b已加载展示箱: §f" + previewList.size() + " §b个"); + Bukkit.getConsoleSender().sendMessage("§f[§6!§f] §aDemonChestPreview §f加载完成,祝你使用愉快!"); + Bukkit.getConsoleSender().sendMessage("§f[§6!§f] §f作者QQ号: §c32282861"); + } + + + public void onDisable() { + Bukkit.getConsoleSender().sendMessage("§f[§6!§f] §eDemonChestPreview §c卸载完成,下次再见!"); + } +} \ 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..18746b7 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1 @@ +ChestLoc: {} \ 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..4bd4bc5 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +name: DemonChestPreview +main: me.Demon.DemonChestPreview.Main +version: 1.0 +commands: + dcpreview: \ No newline at end of file