OpenInv/src/main/java/com/lishid/openinv/internal/AnySilentChest.java

170 lines
6 KiB
Java
Raw Normal View History

2015-05-27 02:13:20 +02:00
/*
2016-03-02 20:11:45 +11:00
* Copyright (C) 2011-2016 lishid. All rights reserved.
2015-05-27 02:13:20 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2015-06-22 12:03:03 +10:00
package com.lishid.openinv.internal;
2015-05-27 02:13:20 +02:00
import java.util.Iterator;
2016-03-10 13:37:28 +11:00
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer;
2015-05-27 02:13:20 +02:00
import org.bukkit.entity.Player;
import com.lishid.openinv.OpenInv;
2016-03-10 13:37:28 +11:00
import net.minecraft.server.v1_9_R1.AxisAlignedBB;
import net.minecraft.server.v1_9_R1.Block;
import net.minecraft.server.v1_9_R1.BlockChest;
2016-03-02 13:44:16 +11:00
import net.minecraft.server.v1_9_R1.BlockChest.Type;
2016-03-10 13:37:28 +11:00
import net.minecraft.server.v1_9_R1.BlockPosition;
import net.minecraft.server.v1_9_R1.Entity;
import net.minecraft.server.v1_9_R1.EntityOcelot;
import net.minecraft.server.v1_9_R1.EntityPlayer;
import net.minecraft.server.v1_9_R1.EnumDirection;
import net.minecraft.server.v1_9_R1.ITileInventory;
import net.minecraft.server.v1_9_R1.InventoryLargeChest;
import net.minecraft.server.v1_9_R1.TileEntity;
import net.minecraft.server.v1_9_R1.TileEntityChest;
import net.minecraft.server.v1_9_R1.World;
2015-05-27 02:13:20 +02:00
2015-06-22 12:03:03 +10:00
public class AnySilentChest {
2016-03-04 13:45:54 +11:00
2016-03-10 13:37:28 +11:00
private final OpenInv plugin;
public AnySilentChest(OpenInv plugin) {
this.plugin = plugin;
}
2015-06-23 13:31:26 +10:00
public boolean isAnyChestNeeded(Player p, int x, int y, int z) {
2015-05-27 02:13:20 +02:00
// FOR REFERENCE, LOOK AT net.minecraft.server.BlockChest
BlockPosition position = new BlockPosition(x, y, z);
EntityPlayer player = ((CraftPlayer) p).getHandle();
World world = player.world;
2016-03-02 13:44:16 +11:00
BlockChest chest = (BlockChest) (((BlockChest) world.getType(position).getBlock()).g == Type.TRAP ?
2015-05-27 02:13:20 +02:00
Block.getByName("trapped_chest") : Block.getByName("chest"));
2016-03-04 13:45:54 +11:00
// If a block is on top
2015-05-27 02:13:20 +02:00
if (topBlocking(world, position)) {
return true;
}
2016-03-04 13:45:54 +11:00
// If the block next to the chest is chest and has a block on top
2015-05-27 02:13:20 +02:00
for (EnumDirection direction : EnumDirectionList.HORIZONTAL) {
BlockPosition sidePosition = position.shift(direction);
2016-03-04 13:45:54 +11:00
Block block = world.getType(sidePosition).getBlock();
if (block == chest) {
2015-05-27 02:13:20 +02:00
if (this.topBlocking(world, sidePosition)) {
return true;
}
}
}
return false;
}
2016-03-04 13:45:54 +11:00
2015-05-27 02:13:20 +02:00
private boolean topBlocking(World world, BlockPosition position) {
return this.blockOnTop(world, position) || this.ocelotOnTop(world, position);
}
private boolean blockOnTop(World world, BlockPosition position) {
2016-03-02 13:44:16 +11:00
Block block = world.getType(position.up()).getBlock();
return block.isOccluding(block.getBlockData());
2015-05-27 02:13:20 +02:00
}
private boolean ocelotOnTop(World world, BlockPosition position) {
2016-03-04 13:45:54 +11:00
Iterator iterator = world.a(EntityOcelot.class,
2015-05-27 02:13:20 +02:00
new AxisAlignedBB((double) position.getX(), (double) (position.getY() + 1),
(double) position.getZ(), (double) (position.getX() + 1),
(double) (position.getY() + 2), (double) (position.getZ() + 1))).iterator();
2016-03-04 13:45:54 +11:00
EntityOcelot entityOcelot;
2015-05-27 02:13:20 +02:00
do {
2016-03-04 13:45:54 +11:00
if (!iterator.hasNext()) {
2015-05-27 02:13:20 +02:00
return false;
}
2016-03-04 13:45:54 +11:00
Entity entity = (Entity) iterator.next();
entityOcelot = (EntityOcelot) entity;
} while (!entityOcelot.isSitting());
2015-05-27 02:13:20 +02:00
return true;
}
2015-06-23 13:31:26 +10:00
public boolean activateChest(Player p, boolean anyChest, boolean silentChest, int x, int y, int z) {
2015-05-27 02:13:20 +02:00
BlockPosition position = new BlockPosition(x, y, z);
EntityPlayer player = ((CraftPlayer) p).getHandle();
World world = player.world;
if (world.isClientSide) {
return true;
}
2016-03-02 13:44:16 +11:00
BlockChest chest = (BlockChest) (((BlockChest) world.getType(position).getBlock()).g == Type.TRAP ?
2015-05-27 02:13:20 +02:00
Block.getByName("trapped_chest") : Block.getByName("chest"));
TileEntity tileEntity = world.getTileEntity(position);
if (!(tileEntity instanceof TileEntityChest)) {
return true;
}
ITileInventory tileInventory = (ITileInventory) tileEntity;
2015-06-23 13:31:26 +10:00
if (!anyChest && this.topBlocking(world, position)) {
2015-05-27 02:13:20 +02:00
return true;
}
for (EnumDirection direction : EnumDirectionList.HORIZONTAL) {
BlockPosition side = position.shift(direction);
Block block = world.getType(side).getBlock();
2016-03-04 13:45:54 +11:00
2015-05-27 02:13:20 +02:00
if (block == chest) {
2015-06-23 13:31:26 +10:00
if (!anyChest && this.topBlocking(world, side)) {
2015-05-27 02:13:20 +02:00
return true;
}
TileEntity sideTileEntity = world.getTileEntity(side);
2016-03-04 13:45:54 +11:00
2015-05-27 02:13:20 +02:00
if (sideTileEntity instanceof TileEntityChest) {
if (direction != EnumDirection.WEST && direction != EnumDirection.NORTH) {
tileInventory = new InventoryLargeChest("container.chestDouble", tileInventory, (TileEntityChest) sideTileEntity);
} else {
tileInventory = new InventoryLargeChest("container.chestDouble", (TileEntityChest) sideTileEntity, tileInventory);
}
}
}
}
boolean returnValue = true;
2016-03-04 13:45:54 +11:00
2015-06-23 13:31:26 +10:00
if (silentChest) {
2015-05-27 02:13:20 +02:00
tileInventory = new SilentInventory(tileInventory);
2016-03-04 13:45:54 +11:00
2016-03-10 13:37:28 +11:00
if (plugin.getConfiguration().notifySilentChest()) {
plugin.sendMessage(p, "You are opening a chest silently.");
2015-05-27 02:13:20 +02:00
}
2016-03-04 13:45:54 +11:00
2015-05-27 02:13:20 +02:00
returnValue = false;
}
player.openContainer(tileInventory);
2016-03-10 13:37:28 +11:00
if (anyChest && plugin.getConfiguration().notifyAnyChest()) {
plugin.sendMessage(p, "You are opening a blocked chest.");
2015-05-27 02:13:20 +02:00
}
return returnValue;
}
}