- public class BlockLogLite extends org.bukkit.plugin.java.JavaPlugin {
- ...
- public boolean onChestOpen(Player player, Integer[] xyz) {
- String player_name = player.getName();
- if (chestInteractions.containsKey(player_name)) {
- onChestClose(player, chestInteractions.get(player_name));
- chestInteractions.remove(player_name);
- }
- log("onChestOpen(" + player_name + ", {" + xyz[0] + ", " + xyz[1] + ", " + xyz[2] + "});");
- chestInteractions.put(player_name, xyz);
- return false;
- }
- public boolean onChestClose(Player player, Integer[] xyz) {
- log("onChestClose(" + player.getName() + ", {" + xyz[0] + ", " + xyz[1] + ", " + xyz[2] + "});");
- Block chest_block = player.getWorld().getBlockAt(xyz[0], xyz[1], xyz[2]);
- Block block = chest_block;
- BlockFace[] faces = {BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST};
- for (int i = 0; i < 5; i++) {
- if (i > 0) {
- block = chest_block.getFace(faces[i - 1]);
- }
- if (block.getTypeId() != 54) {
- continue;
- }
- Chest chest = (Chest) block.getState();
- ItemStack[] items = chest.getInventory().getContents();
- for (int i2 = 0; i2 < 27; i2++) {
- if (items[i2].getAmount() > 0) {
- log("[" + i2 + "," + items[i2].getTypeId() + "," + items[i2].getDurability() + "," + items[i2].getAmount() + "]");
- }
- }
- }
- return false;
- }
- ...
- }
- public class BlockListener extends org.bukkit.event.block.BlockListener {
- ...
- @Override
- public void onBlockInteract(BlockInteractEvent event) {
- if (event.isCancelled()) {
- return;
- }
- if (event.isPlayer()) {
- Block block = event.getBlock();
- switch (block.getTypeId()) {
- case 54:
- plugin.onChestOpen((Player) event.getEntity(), new Integer[] {block.getX(), block.getY(), block.getZ()});
- break;
- }
- }
- }
- ...
- }
- public class PlayerListener extends org.bukkit.event.player.PlayerListener {
- ...
- @Override
- public void onPlayerQuit(PlayerEvent event) {
- Player player = event.getPlayer();
- String player_name = player.getName();
- plugin.onPlayerEvent(player_name, 2);
- if (plugin.chestInteractions.containsKey(player_name)) {
- plugin.onChestClose(player, plugin.chestInteractions.get(player_name));
- plugin.chestInteractions.remove(player_name);
- }
- }
- @Override
- public void onPlayerMove(PlayerMoveEvent event) {
- if (event.isCancelled()) {
- return;
- }
- Player player = event.getPlayer();
- String player_name = player.getName();
- if (plugin.chestInteractions.containsKey(player_name)) {
- Integer[] xyz = plugin.chestInteractions.get(player_name);
- int x = player.getLocation().getBlockX() - xyz[0];
- int y = player.getLocation().getBlockY() - xyz[1];
- int z = player.getLocation().getBlockZ() - xyz[2];
- if (x > 5 || x < -5 || y > 5 || y < -5 || z > 5 || z < -5) {
- plugin.onChestClose(player, xyz);
- plugin.chestInteractions.remove(player_name);
- }
- }
- }
- ...
- }