From 7aa79c220caa04c31337b7343fbbca9f2734b98a Mon Sep 17 00:00:00 2001 From: j502647092 Date: Sun, 23 Aug 2015 00:04:01 +0800 Subject: [PATCH] add Inventory Control... Signed-off-by: j502647092 --- .../SimpleEssential/SimpleEssential.java | 15 +++++ .../command/CommandEnchantBench.java | 57 ++-------------- .../inventory/InventoryControl.java | 66 +++++++++++++++++++ .../listen/PlayerInventoryViewListen.java | 39 +++++++++++ 4 files changed, 124 insertions(+), 53 deletions(-) create mode 100644 src/cn/citycraft/SimpleEssential/inventory/InventoryControl.java create mode 100644 src/cn/citycraft/SimpleEssential/listen/PlayerInventoryViewListen.java diff --git a/src/cn/citycraft/SimpleEssential/SimpleEssential.java b/src/cn/citycraft/SimpleEssential/SimpleEssential.java index 7ed06db..aeef58d 100644 --- a/src/cn/citycraft/SimpleEssential/SimpleEssential.java +++ b/src/cn/citycraft/SimpleEssential/SimpleEssential.java @@ -30,6 +30,8 @@ import cn.citycraft.SimpleEssential.command.CommandTphere; import cn.citycraft.SimpleEssential.command.CommandWorkBench; import cn.citycraft.SimpleEssential.config.Config; import cn.citycraft.SimpleEssential.config.Language; +import cn.citycraft.SimpleEssential.inventory.InventoryControl; +import cn.citycraft.SimpleEssential.listen.PlayerInventoryViewListen; import cn.citycraft.SimpleEssential.listen.PlayerLocationListen; import cn.citycraft.SimpleEssential.teleport.TeleportControl; import cn.citycraft.SimpleEssential.utils.VersionChecker; @@ -45,11 +47,22 @@ public class SimpleEssential extends JavaPlugin { * 传送控制 */ public TeleportControl tpcontrol; + /** + * 容器界面控制 + */ + public InventoryControl invcontrol; /** * 命令监听列表 */ private List commandlist; + /** + * + */ + private void initInventoryControl() { + invcontrol = new InventoryControl(this); + } + private void initTeleportControl() { int tpdelay = Config.getInstance().getInt("Teleport.delay", 3); String tpcontorlname = Config.getMessage("Teleport.name"); @@ -88,6 +101,7 @@ public class SimpleEssential extends JavaPlugin { @Override public void onEnable() { this.initTeleportControl(); + this.initInventoryControl(); this.registerCommands(); this.registerEvents(); new VersionChecker(this); @@ -145,5 +159,6 @@ public class SimpleEssential extends JavaPlugin { */ private void registerEvents() { registerEvent(new PlayerLocationListen(this)); + registerEvent(new PlayerInventoryViewListen(this)); } } diff --git a/src/cn/citycraft/SimpleEssential/command/CommandEnchantBench.java b/src/cn/citycraft/SimpleEssential/command/CommandEnchantBench.java index be0c9e5..01aa3af 100644 --- a/src/cn/citycraft/SimpleEssential/command/CommandEnchantBench.java +++ b/src/cn/citycraft/SimpleEssential/command/CommandEnchantBench.java @@ -5,7 +5,6 @@ package cn.citycraft.SimpleEssential.command; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.command.CommandException; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -26,27 +25,20 @@ public class CommandEnchantBench extends BaseCommand { this.plugin = main; } - protected void clearEnchatRange(Location loc) { - setRange(loc, Material.AIR); - loc.getBlock().setType(Material.AIR); - }; - @Override public void execute(CommandSender sender, String label, String[] args) throws CommandException { Player p = (Player) sender; final Location loc = p.getLocation(); switch (args.length) { case 0: - setEnchatRange(loc); - p.openEnchanting(loc.clone().add(0, 255, 0), true); - Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { + loc.setY(250); + Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { @Override public void run() { - clearEnchatRange(loc); - } - }, 200); + } + }, 600); break; case 1: switch (args[0]) { @@ -77,45 +69,4 @@ public class CommandEnchantBench extends BaseCommand { public boolean isOnlyPlayerExecutable() { return true; } - - protected void setBlock(Location loc, int x, int y, int z, Material ma) { - new Location(loc.getWorld(), loc.getBlockX() + x, loc.getBlockY() + y + 255, loc.getBlockZ() + z).getBlock().setType(ma); - } - - protected void setEnchatRange(Location loc) { - setRange(loc, Material.BOOKSHELF); - loc.getBlock().setType(Material.ENCHANTMENT_TABLE); - } - - protected void setRange(Location loc, Material ma) { - for (int i = -2; i < 3; i++) { - for (int j = 0; j < 3; j++) { - for (int k = -2; k < 3; k++) { - if (i != 0 && j != 0) { - setBlock(loc, i, j, k, ma); - } - } - } - } - - // setBlock(loc, -2, 0, -2, ma); - // setBlock(loc, -2, 1, -1, ma); - // setBlock(loc, -2, 0, 1, ma); - // setBlock(loc, -1, 0, 2, ma); - // - // setBlock(loc, 2, 0, 2, ma); - // setBlock(loc, 2, 1, 2, ma); - // setBlock(loc, 1, 0, 2, ma); - // setBlock(loc, 2, 0, 1, ma); - // - // setBlock(loc, -2, 0, -2, ma); - // setBlock(loc, -2, 1, -2, ma); - // setBlock(loc, -1, 0, -2, ma); - // setBlock(loc, -2, 0, -1, ma); - // - // setBlock(loc, 2, 0, -2, ma); - // setBlock(loc, 2, 1, -2, ma); - // setBlock(loc, 1, 0, -2, ma); - // setBlock(loc, 2, 0, -1, ma); - } } diff --git a/src/cn/citycraft/SimpleEssential/inventory/InventoryControl.java b/src/cn/citycraft/SimpleEssential/inventory/InventoryControl.java new file mode 100644 index 0000000..49cfbf1 --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/inventory/InventoryControl.java @@ -0,0 +1,66 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.inventory; + +import java.util.HashMap; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import cn.citycraft.SimpleEssential.SimpleEssential; + +/** + * @author 蒋天蓓 + * + */ +public class InventoryControl { + SimpleEssential plugin; + HashMap enchantbench = new HashMap(); + + public InventoryControl(SimpleEssential main) { + this.plugin = main; + } + + public void clearEnchatRange(Player player) { + if (!isOpen(player)) + return; + Location loc = enchantbench.get(player.getName()); + setRange(loc, Material.AIR); + loc.getBlock().setType(Material.AIR); + } + + public boolean isOpen(Player player) { + return enchantbench.containsKey(player.getName()); + } + + public void open(Player player) { + Location loc = player.getLocation(); + loc.setY(-5); + setEnchatRange(loc); + player.openEnchanting(loc, true); + enchantbench.put(player.getName(), loc); + }; + + protected void setBlock(Location loc, int x, int y, int z, Material ma) { + new Location(loc.getWorld(), loc.getBlockX() + x, loc.getBlockY() + y, loc.getBlockZ() + z).getBlock().setType(ma); + } + + protected void setEnchatRange(Location loc) { + setRange(loc, Material.BOOKSHELF); + loc.getBlock().setType(Material.ENCHANTMENT_TABLE); + } + + protected void setRange(Location loc, Material ma) { + for (int i = -2; i < 3; i++) { + for (int j = 0; j < 2; j++) { + for (int k = -2; k < 3; k++) { + if (!((i * k == 0) || (i * k * i * k == 1))) { + setBlock(loc, i, j, k, ma); + } + } + } + } + } +} diff --git a/src/cn/citycraft/SimpleEssential/listen/PlayerInventoryViewListen.java b/src/cn/citycraft/SimpleEssential/listen/PlayerInventoryViewListen.java new file mode 100644 index 0000000..806d933 --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/listen/PlayerInventoryViewListen.java @@ -0,0 +1,39 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.listen; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.inventory.Inventory; + +import cn.citycraft.SimpleEssential.SimpleEssential; + +/** + * 玩家随身附魔台记录监听 + * + * @author 蒋天蓓 2015年8月12日下午8:19:33 + * + */ +public class PlayerInventoryViewListen implements Listener { + SimpleEssential plugin; + + public PlayerInventoryViewListen(SimpleEssential main) { + this.plugin = main; + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onInventoryClose(InventoryCloseEvent e) { + if (!(e.getPlayer() instanceof Player)) + return; + Player player = (Player) e.getPlayer(); + Inventory inv = e.getInventory(); + if (inv.getType() == InventoryType.ENCHANTING) { + plugin.invcontrol.clearEnchatRange(player); + } + } +}