diff --git a/src/lishid/openinv/OpenInv.java b/src/lishid/openinv/OpenInv.java index 214fe21..0738ac9 100644 --- a/src/lishid/openinv/OpenInv.java +++ b/src/lishid/openinv/OpenInv.java @@ -52,8 +52,7 @@ public class OpenInv extends JavaPlugin { pm.registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Event.Priority.Lowest, this); pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Event.Priority.Monitor, this); pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Event.Priority.Lowest, this); - //pm.registerEvent(Event.Type.CUSTOM_EVENT, inventoryListener, Event.Priority.Monitor, this); - pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Event.Priority.Monitor, this); + //pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Event.Priority.Monitor, this); setupPermissions(); PluginDescriptionFile pdfFile = this.getDescription(); @@ -62,6 +61,7 @@ public class OpenInv extends JavaPlugin { getCommand("openinv").setExecutor(new OpenInvPluginCommand(this)); getCommand("searchinv").setExecutor(new SearchInvPluginCommand(this)); getCommand("toggleopeninv").setExecutor(new OpenInvPluginCommand(this)); + getCommand("silentchest").setExecutor(new SilentChestPluginCommand(this)); } public static void ReplaceInv(CraftPlayer player) @@ -97,9 +97,24 @@ public class OpenInv extends JavaPlugin { mainPlugin.getConfig().set("ItemOpenInv." + name.toLowerCase() + ".toggle", status); mainPlugin.saveConfig(); } + + public static boolean GetPlayerSilentChestStatus(String name) + { + return mainPlugin.getConfig().getBoolean("SilentChest." + name.toLowerCase() + ".toggle", false); + } + + public static void SetPlayerSilentChestStatus(String name, boolean status) + { + mainPlugin.getConfig().set("SilentChest." + name.toLowerCase() + ".toggle", status); + mainPlugin.saveConfig(); + } public static int GetItemOpenInvItem() { + if(mainPlugin.getConfig().get("ItemOpenInvItemID") == null) + { + SaveToConfig("ItemOpenInvItemID", 280); + } return mainPlugin.getConfig().getInt("ItemOpenInvItemID", 280); } diff --git a/src/lishid/openinv/OpenInvPlayerListener.java b/src/lishid/openinv/OpenInvPlayerListener.java index b0b3944..e7e4b3e 100644 --- a/src/lishid/openinv/OpenInvPlayerListener.java +++ b/src/lishid/openinv/OpenInvPlayerListener.java @@ -1,11 +1,16 @@ package lishid.openinv; +import java.lang.reflect.Field; + import lishid.openinv.commands.OpenInvPluginCommand; import lishid.openinv.utils.PlayerInventoryChest; +import lishid.openinv.utils.SilentContainerChest; import net.minecraft.server.Block; import net.minecraft.server.EntityPlayer; +import net.minecraft.server.ICrafting; import net.minecraft.server.IInventory; import net.minecraft.server.InventoryLargeChest; +import net.minecraft.server.Packet100OpenWindow; import net.minecraft.server.Packet101CloseWindow; import net.minecraft.server.TileEntityChest; import net.minecraft.server.World; @@ -55,58 +60,44 @@ public class OpenInvPlayerListener extends PlayerListener{ @Override public void onPlayerInteract(PlayerInteractEvent event) { - if(event.useInteractedBlock() == Result.DENY) + if(event.useInteractedBlock() == Result.DENY || event.isCancelled()) return; + boolean silentchest = false; + boolean anychest = false; + int x = event.getClickedBlock().getX(); + int y = event.getClickedBlock().getY(); + int z = event.getClickedBlock().getZ(); + + if(event.getAction() == Action.RIGHT_CLICK_BLOCK && + event.getClickedBlock().getState() instanceof Chest && + PermissionRelay.hasPermission(event.getPlayer(), "OpenInv.silent") && + OpenInv.GetPlayerSilentChestStatus(event.getPlayer().getName())) + { + silentchest = true; + } + if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Chest && PermissionRelay.hasPermission(event.getPlayer(), "OpenInv.anychest")) { - EntityPlayer player = ((CraftPlayer)event.getPlayer()).getHandle(); - World world = player.world; - int x = event.getClickedBlock().getX(); - int y = event.getClickedBlock().getY(); - int z = event.getClickedBlock().getZ(); try { - boolean override = false; - + EntityPlayer player = ((CraftPlayer)event.getPlayer()).getHandle(); + World world = player.world; //If block on top if(world.e(x, y + 1, z)) - override = true; + anychest = true; //If block next to chest is chest and has a block on top if ((world.getTypeId(x - 1, y, z) == Block.CHEST.id) && (world.e(x - 1, y + 1, z))) - override = true; + anychest = true; if ((world.getTypeId(x + 1, y, z) == Block.CHEST.id) && (world.e(x + 1, y + 1, z))) - override = true; + anychest = true; if ((world.getTypeId(x, y, z - 1) == Block.CHEST.id) && (world.e(x, y + 1, z - 1))) - override = true; + anychest = true; if ((world.getTypeId(x, y, z + 1) == Block.CHEST.id) && (world.e(x, y + 1, z + 1))) - override = true; - - //If the chest is blocked - if(override) - { - //Create chest - Object inventory = (TileEntityChest)player.world.getTileEntity(x, y, z); - - //Link chest - if (world.getTypeId(x - 1, y, z) == Block.CHEST.id) inventory = new InventoryLargeChest("Large chest", (TileEntityChest)world.getTileEntity(x - 1, y, z), (IInventory)inventory); - if (world.getTypeId(x + 1, y, z) == Block.CHEST.id) inventory = new InventoryLargeChest("Large chest", (IInventory)inventory, (TileEntityChest)world.getTileEntity(x + 1, y, z)); - if (world.getTypeId(x, y, z - 1) == Block.CHEST.id) inventory = new InventoryLargeChest("Large chest", (TileEntityChest)world.getTileEntity(x, y, z - 1), (IInventory)inventory); - if (world.getTypeId(x, y, z + 1) == Block.CHEST.id) inventory = new InventoryLargeChest("Large chest", (IInventory)inventory, (TileEntityChest)world.getTileEntity(x, y, z + 1)); - - //Open chest - player.a((IInventory)inventory); - - //Send a notification - event.getPlayer().sendMessage("You are opening a blocked chest."); - } - /* - Chest chest = (Chest)event.getClickedBlock().getState(); - player.a(((CraftInventory)chest.getInventory()).getInventory());*/ - return; + anychest = true; } catch(Exception e) //Incompatible CraftBukkit? { @@ -114,6 +105,59 @@ public class OpenInvPlayerListener extends PlayerListener{ event.getPlayer().sendMessage(ChatColor.RED + "Error while executing openinv. Unsupported CraftBukkit."); } } + + + //If the chest is blocked + if(anychest || silentchest) + { + EntityPlayer player = ((CraftPlayer)event.getPlayer()).getHandle(); + World world = player.world; + Object chest = (TileEntityChest)world.getTileEntity(x, y, z); + if (chest == null) return; + + if(!anychest) + { + if (world.e(x, y + 1, z)) return; + if ((world.getTypeId(x - 1, y, z) == Block.CHEST.id) && (world.e(x - 1, y + 1, z))) return; + if ((world.getTypeId(x + 1, y, z) == Block.CHEST.id) && (world.e(x + 1, y + 1, z))) return; + if ((world.getTypeId(x, y, z - 1) == Block.CHEST.id) && (world.e(x, y + 1, z - 1))) return; + if ((world.getTypeId(x, y, z + 1) == Block.CHEST.id) && (world.e(x, y + 1, z + 1))) return; + } + + if (world.getTypeId(x - 1, y, z) == Block.CHEST.id) chest = new InventoryLargeChest("Large chest", (TileEntityChest)world.getTileEntity(x - 1, y, z), (IInventory)chest); + if (world.getTypeId(x + 1, y, z) == Block.CHEST.id) chest = new InventoryLargeChest("Large chest", (IInventory)chest, (TileEntityChest)world.getTileEntity(x + 1, y, z)); + if (world.getTypeId(x, y, z - 1) == Block.CHEST.id) chest = new InventoryLargeChest("Large chest", (TileEntityChest)world.getTileEntity(x, y, z - 1), (IInventory)chest); + if (world.getTypeId(x, y, z + 1) == Block.CHEST.id) chest = new InventoryLargeChest("Large chest", (IInventory)chest, (TileEntityChest)world.getTileEntity(x, y, z + 1)); + + if(!silentchest) + { + player.a((IInventory)chest); + } + else + { + try{ + Field ciField = player.getClass().getDeclaredField("ci"); + ciField.setAccessible(true); + int ci = ciField.getInt(player); + ci = ci % 100 + 1; + ciField.setInt(player, ci); + player.netServerHandler.sendPacket(new Packet100OpenWindow(ci, 0, ((IInventory)chest).getName(), ((IInventory)chest).getSize())); + player.activeContainer = new SilentContainerChest(player.inventory, ((IInventory)chest)); + System.out.println(player.activeContainer.toString()); + player.activeContainer.windowId = ci; + player.activeContainer.a((ICrafting)player); + event.getPlayer().sendMessage("You are opening a silent chest."); + } + catch(Exception e) + { + e.printStackTrace(); + event.getPlayer().sendMessage(ChatColor.RED + "Error while sending silent chest."); + } + } + + if(anychest) + event.getPlayer().sendMessage("You are opening a blocked chest."); + } if(event.getAction() == Action.RIGHT_CLICK_BLOCK && (event.getClickedBlock() == Block.CHEST || @@ -142,12 +186,12 @@ public class OpenInvPlayerListener extends PlayerListener{ Player player = event.getPlayer(); try{ if (PermissionRelay.hasPermission(player, "openinv") && - ((Sign)event.getClickedBlock().getState()).getLine(1).equalsIgnoreCase("[openinv]")) + ((Sign)event.getClickedBlock().getState()).getLine(0).equalsIgnoreCase("[openinv]")) { - if(plugin.getServer().getPlayer(((Sign)event.getClickedBlock().getState()).getLine(2)) != null) + if(plugin.getServer().getPlayer(((Sign)event.getClickedBlock().getState()).getLine(1)) != null) { Sign sign = ((Sign)event.getClickedBlock().getState()); - String text = sign.getLine(2).trim() + sign.getLine(3).trim() + sign.getLine(4).trim(); + String text = sign.getLine(1).trim() + sign.getLine(2).trim() + sign.getLine(2).trim(); player.performCommand("openinv " + text); } else @@ -159,6 +203,7 @@ public class OpenInvPlayerListener extends PlayerListener{ catch(Exception ex) { player.sendMessage("Internal Error."); + ex.printStackTrace(); } } } diff --git a/src/lishid/openinv/commands/OpenInvPluginCommand.java b/src/lishid/openinv/commands/OpenInvPluginCommand.java index 1c404ee..1b45aa9 100644 --- a/src/lishid/openinv/commands/OpenInvPluginCommand.java +++ b/src/lishid/openinv/commands/OpenInvPluginCommand.java @@ -100,6 +100,11 @@ public class OpenInvPluginCommand implements CommandExecutor { { //Offline inv here... try{ + if(!this.plugin.getServer().getOfflinePlayer(name).hasPlayedBefore()) + { + sender.sendMessage(ChatColor.RED + "Player not found!"); + return true; + } MinecraftServer server = ((CraftServer)this.plugin.getServer()).getServer(); EntityPlayer entity = new EntityPlayer(server, server.getWorldServer(0), name, new ItemInWorldManager(server.getWorldServer(0))); target = (entity == null) ? null : (Player) entity.getBukkitEntity(); diff --git a/src/lishid/openinv/commands/SearchInvPluginCommand.java b/src/lishid/openinv/commands/SearchInvPluginCommand.java index ba4648b..e2fa2e3 100644 --- a/src/lishid/openinv/commands/SearchInvPluginCommand.java +++ b/src/lishid/openinv/commands/SearchInvPluginCommand.java @@ -21,8 +21,6 @@ public class SearchInvPluginCommand implements CommandExecutor { sender.sendMessage(ChatColor.RED + "You do not have permission to access player inventories"); return true; } - - ; String PlayerList = ""; diff --git a/src/plugin.yml b/src/plugin.yml index fa10c67..a998e1f 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: OpenInv main: lishid.openinv.OpenInv -version: 1.6.1 +version: 1.6.2 author: lishid description: > This plugin allows you to open another player's inventory as a chest @@ -16,4 +16,8 @@ commands: toggleopeninv: description: Toggle the stick openinv usage: | - / [check] - Checks whether stick openinv is enabled \ No newline at end of file + / [check] - Checks whether stick openinv is enabled + silentchest: + description: Toggle silent chest, which does not animate a chest when opened or closed, and suppresses the sound. + usage: | + / [check] - Checks whether silent chest is enabled \ No newline at end of file