Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package carpentersblocks.block;
- import java.util.List;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.item.ItemStack;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.AxisAlignedBB;
- import net.minecraft.util.MathHelper;
- import net.minecraft.world.IBlockAccess;
- import net.minecraft.world.World;
- import carpentersblocks.data.Barrier;
- import carpentersblocks.data.Gate;
- import carpentersblocks.tileentity.TEBase;
- import carpentersblocks.util.registry.BlockRegistry;
- import cpw.mods.fml.relauncher.Side;
- import cpw.mods.fml.relauncher.SideOnly;
- public class BlockCarpentersGate extends BlockCoverable {
- public BlockCarpentersGate(Material material)
- {
- super(material);
- }
- @Override
- /**
- * Alters gate type or sub-type and returns result.
- */
- protected boolean onHammerRightClick(TEBase TE, EntityPlayer entityPlayer)
- {
- int type = Gate.getType(TE);
- if (entityPlayer.isSneaking()) {
- /*
- * Cycle through sub-types
- */
- if (type <= Gate.TYPE_VANILLA_X3) {
- if (++type > Gate.TYPE_VANILLA_X3) {
- type = Gate.TYPE_VANILLA;
- }
- }
- } else {
- /*
- * Cycle through barrier types
- */
- if (type <= Gate.TYPE_VANILLA_X3) {
- type = Gate.TYPE_PICKET;
- } else if (++type > Gate.TYPE_WALL) {
- type = Gate.TYPE_VANILLA;
- }
- }
- Gate.setType(TE, type);
- return true;
- }
- @Override
- /**
- * Opens or closes gate on right click.
- */
- protected void postOnBlockActivated(TEBase TE, EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ, List<Boolean> altered, List<Boolean> decInv)
- {
- if (Gate.getState(TE) == Gate.STATE_OPEN) {
- Gate.setState(TE, Gate.STATE_CLOSED, true);
- cycleNeighborGate(TE, TE.getWorldObj(), TE.xCoord, TE.yCoord, TE.zCoord);
- } else {
- int facing = (MathHelper.floor_double(entityPlayer.rotationYaw * 4.0F / 360.0F + 0.5D) & 3) % 4;
- Gate.setState(TE, Gate.STATE_OPEN, true);
- if (Gate.getFacing(TE) == Gate.FACING_ON_X) {
- Gate.setDirOpen(TE, facing == 0 ? Gate.DIR_POS : Gate.DIR_NEG);
- } else {
- Gate.setDirOpen(TE, facing == 3 ? Gate.DIR_POS : Gate.DIR_NEG);
- }
- cycleNeighborGate(TE, TE.getWorldObj(), TE.xCoord, TE.yCoord, TE.zCoord);
- }
- altered.add(true);
- }
- @Override
- /**
- * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
- */
- public boolean canPlaceBlockAt(World world, int x, int y, int z)
- {
- return !world.getBlock(x, y - 1, z).getMaterial().isSolid() ? false : super.canPlaceBlockAt(world, x, y, z);
- }
- @Override
- /**
- * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
- * cleared to be reused)
- */
- public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z)
- {
- TileEntity TE = world.getTileEntity(x, y, z);
- if (TE instanceof TEBase) {
- if (Gate.getState((TEBase) TE) == Gate.STATE_OPEN) {
- return null;
- } else if (Gate.getFacing((TEBase) TE) == Gate.FACING_ON_Z) {
- if (Gate.getType((TEBase) TE) == Gate.TYPE_VANILLA || Gate.getType((TEBase) TE) == Gate.TYPE_WALL) {
- return AxisAlignedBB.getAABBPool().getAABB(x + 0.4375F, y, z, x + 0.5625F, y + 1.5F, z + 1.0F);
- } else {
- return AxisAlignedBB.getAABBPool().getAABB(x + 0.375F, y, z, x + 0.625F, y + 1.5F, z + 1.0F);
- }
- } else {
- if (Gate.getType((TEBase) TE) == Gate.TYPE_VANILLA || Gate.getType((TEBase) TE) == Gate.TYPE_WALL) {
- return AxisAlignedBB.getAABBPool().getAABB(x, y, z + 0.4375F, x + 1.0F, y + 1.5F, z + 0.5625F);
- } else {
- return AxisAlignedBB.getAABBPool().getAABB(x, y, z + 0.375F, x + 1.0F, y + 1.5F, z + 0.625F);
- }
- }
- }
- return null;
- }
- @Override
- /**
- * Updates the blocks bounds based on its current state. Args: world, x, y, z
- */
- public void setBlockBoundsBasedOnState(IBlockAccess world, int x, int y, int z)
- {
- TileEntity TE = world.getTileEntity(x, y, z);
- if (TE instanceof TEBase) {
- if (Gate.getFacing((TEBase) TE) == Gate.FACING_ON_Z) {
- if (Gate.getType((TEBase) TE) == Gate.TYPE_VANILLA || Gate.getType((TEBase) TE) == Gate.TYPE_WALL) {
- setBlockBounds(0.4375F, 0.0F, 0.0F, 0.5625F, 1.0F, 1.0F);
- } else {
- setBlockBounds(0.375F, 0.0F, 0.0F, 0.625F, 1.0F, 1.0F);
- }
- } else {
- if (Gate.getType((TEBase) TE) == Gate.TYPE_VANILLA || Gate.getType((TEBase) TE) == Gate.TYPE_WALL) {
- setBlockBounds(0.0F, 0.0F, 0.4375F, 1.0F, 1.0F, 0.5625F);
- } else {
- setBlockBounds(0.0F, 0.0F, 0.375F, 1.0F, 1.0F, 0.625F);
- }
- }
- }
- }
- /**
- * Opens or closes one neighboring gate above or below block.
- */
- private void cycleNeighborGate(TEBase TE, World world, int x, int y, int z)
- {
- boolean isGateBelow = world.getBlock(x, y - 1, z).equals(this) ? true : false;
- boolean isGateAbove = world.getBlock(x, y + 1, z).equals(this) ? true : false;
- /*
- * Will only check for gate above or below, and limit to only activating a single stacked gate.
- * It is done this way intentionally.
- */
- if (isGateBelow) {
- TileEntity TE_YN = (TEBase) world.getTileEntity(x, y - 1, z);
- if (Gate.getFacing((TEBase) TE_YN) == Gate.getFacing(TE)) {
- Gate.setDirOpen((TEBase) TE_YN, Gate.getDirOpen(TE));
- Gate.setState((TEBase) TE_YN, Gate.getState(TE), false);
- }
- } else if (isGateAbove) {
- TileEntity TE_YP = (TEBase) world.getTileEntity(x, y + 1, z);
- if (Gate.getFacing((TEBase) TE_YP) == Gate.getFacing(TE)) {
- Gate.setDirOpen((TEBase) TE_YP, Gate.getDirOpen(TE));
- Gate.setState((TEBase) TE_YP, Gate.getState(TE), false);
- }
- }
- }
- @Override
- /**
- * Called when the block is placed in the world.
- */
- public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemStack)
- {
- TileEntity TE = world.getTileEntity(x, y, z);
- if (TE instanceof TEBase) {
- int facing = (MathHelper.floor_double(entityLiving.rotationYaw * 4.0F / 360.0F + 0.5D) & 3) % 4;
- Gate.setFacing((TEBase) TE, facing == 3 || facing == 1 ? Gate.FACING_ON_Z : Gate.FACING_ON_X);
- /* Match block type with adjacent type if possible. */
- TEBase[] TE_list = getAdjacentTileEntities(world, x, y, z);
- for (TEBase TE_current : TE_list) {
- if (TE_current != null) {
- Block block = TE_current.getBlockType();
- if (block.equals(this)) {
- Gate.setType((TEBase) TE, Gate.getType(TE_current));
- } else if (block.equals(BlockRegistry.blockCarpentersGate)) {
- Gate.setType((TEBase) TE, Barrier.getType(TE_current));
- }
- }
- }
- }
- super.onBlockPlacedBy(world, x, y, z, entityLiving, itemStack);
- }
- @Override
- /**
- * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
- * their own) Args: x, y, z, neighbor blockID
- */
- public void onNeighborBlockChange(World world, int x, int y, int z, Block block)
- {
- if (!world.isRemote) {
- TileEntity TE = world.getTileEntity(x, y, z);
- if (TE != null) {
- boolean isPowered = world.isBlockIndirectlyGettingPowered(x, y, z);
- if (isPowered || block != null && block.canProvidePower())
- {
- int state = Gate.getState((TEBase) TE);
- if (isPowered && state == Gate.STATE_CLOSED) {
- Gate.setState((TEBase) TE, Gate.STATE_OPEN, true);
- cycleNeighborGate((TEBase) TE, world, x, y, z);
- } else if (!isPowered && state == Gate.STATE_OPEN) {
- Gate.setState((TEBase) TE, Gate.STATE_CLOSED, true);
- cycleNeighborGate((TEBase) TE, world, x, y, z);
- }
- }
- }
- }
- super.onNeighborBlockChange(world, x, y, z, block);
- }
- @Override
- @SideOnly(Side.CLIENT)
- /**
- * Returns true if the given side of this block type should be rendered, if the adjacent block is at the given
- * coordinates. Args: world, x, y, z, side
- */
- public boolean shouldSideBeRendered(IBlockAccess world, int x, int y, int z, int side)
- {
- return true;
- }
- @Override
- /**
- * The type of render function that is called for this block
- */
- public int getRenderType()
- {
- return BlockRegistry.carpentersGateRenderID;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement