Advertisement
Superloup10

WIP

Jun 19th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.76 KB | None | 0 0
  1. package carpentersblocks.block;
  2.  
  3. import java.util.List;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.material.Material;
  7. import net.minecraft.entity.EntityLivingBase;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.tileentity.TileEntity;
  11. import net.minecraft.util.AxisAlignedBB;
  12. import net.minecraft.util.MathHelper;
  13. import net.minecraft.world.IBlockAccess;
  14. import net.minecraft.world.World;
  15. import carpentersblocks.data.Barrier;
  16. import carpentersblocks.data.Gate;
  17. import carpentersblocks.tileentity.TEBase;
  18. import carpentersblocks.util.registry.BlockRegistry;
  19. import cpw.mods.fml.relauncher.Side;
  20. import cpw.mods.fml.relauncher.SideOnly;
  21.  
  22. public class BlockCarpentersGate extends BlockCoverable {
  23.  
  24.     public BlockCarpentersGate(Material material)
  25.     {
  26.         super(material);
  27.     }
  28.  
  29.     @Override
  30.     /**
  31.      * Alters gate type or sub-type and returns result.
  32.      */
  33.     protected boolean onHammerRightClick(TEBase TE, EntityPlayer entityPlayer)
  34.     {
  35.         int type = Gate.getType(TE);
  36.  
  37.         if (entityPlayer.isSneaking()) {
  38.  
  39.             /*
  40.              * Cycle through sub-types
  41.              */
  42.             if (type <= Gate.TYPE_VANILLA_X3) {
  43.                 if (++type > Gate.TYPE_VANILLA_X3) {
  44.                     type = Gate.TYPE_VANILLA;
  45.                 }
  46.             }
  47.  
  48.         } else {
  49.  
  50.             /*
  51.              * Cycle through barrier types
  52.              */
  53.             if (type <= Gate.TYPE_VANILLA_X3) {
  54.                 type = Gate.TYPE_PICKET;
  55.             } else if (++type > Gate.TYPE_WALL) {
  56.                 type = Gate.TYPE_VANILLA;
  57.             }
  58.  
  59.         }
  60.  
  61.         Gate.setType(TE, type);
  62.  
  63.         return true;
  64.     }
  65.  
  66.     @Override
  67.     /**
  68.      * Opens or closes gate on right click.
  69.      */
  70.     protected void postOnBlockActivated(TEBase TE, EntityPlayer entityPlayer, int side, float hitX, float hitY, float hitZ, List<Boolean> altered, List<Boolean> decInv)
  71.     {
  72.         if (Gate.getState(TE) == Gate.STATE_OPEN) {
  73.  
  74.             Gate.setState(TE, Gate.STATE_CLOSED, true);
  75.             cycleNeighborGate(TE, TE.getWorldObj(), TE.xCoord, TE.yCoord, TE.zCoord);
  76.  
  77.         } else {
  78.  
  79.             int facing = (MathHelper.floor_double(entityPlayer.rotationYaw * 4.0F / 360.0F + 0.5D) & 3) % 4;
  80.             Gate.setState(TE, Gate.STATE_OPEN, true);
  81.  
  82.             if (Gate.getFacing(TE) == Gate.FACING_ON_X) {
  83.                 Gate.setDirOpen(TE, facing == 0 ? Gate.DIR_POS : Gate.DIR_NEG);
  84.             } else {
  85.                 Gate.setDirOpen(TE, facing == 3 ? Gate.DIR_POS : Gate.DIR_NEG);
  86.             }
  87.  
  88.             cycleNeighborGate(TE, TE.getWorldObj(), TE.xCoord, TE.yCoord, TE.zCoord);
  89.  
  90.         }
  91.  
  92.         altered.add(true);
  93.     }
  94.  
  95.     @Override
  96.     /**
  97.      * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
  98.      */
  99.     public boolean canPlaceBlockAt(World world, int x, int y, int z)
  100.     {
  101.         return !world.getBlock(x, y - 1, z).getMaterial().isSolid() ? false : super.canPlaceBlockAt(world, x, y, z);
  102.     }
  103.  
  104.     @Override
  105.     /**
  106.      * Returns a bounding box from the pool of bounding boxes (this means this box can change after the pool has been
  107.      * cleared to be reused)
  108.      */
  109.     public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z)
  110.     {
  111.         TileEntity TE = world.getTileEntity(x, y, z);
  112.  
  113.         if (TE instanceof TEBase) {
  114.  
  115.             if (Gate.getState((TEBase) TE) == Gate.STATE_OPEN) {
  116.                 return null;
  117.             } else if (Gate.getFacing((TEBase) TE) == Gate.FACING_ON_Z) {
  118.                 if (Gate.getType((TEBase) TE) == Gate.TYPE_VANILLA || Gate.getType((TEBase) TE) == Gate.TYPE_WALL) {
  119.                     return AxisAlignedBB.getAABBPool().getAABB(x + 0.4375F, y, z, x + 0.5625F, y + 1.5F, z + 1.0F);
  120.                 } else {
  121.                     return AxisAlignedBB.getAABBPool().getAABB(x + 0.375F, y, z, x + 0.625F, y + 1.5F, z + 1.0F);
  122.                 }
  123.             } else {
  124.                 if (Gate.getType((TEBase) TE) == Gate.TYPE_VANILLA || Gate.getType((TEBase) TE) == Gate.TYPE_WALL) {
  125.                     return AxisAlignedBB.getAABBPool().getAABB(x, y, z + 0.4375F, x + 1.0F, y + 1.5F, z + 0.5625F);
  126.                 } else {
  127.                     return AxisAlignedBB.getAABBPool().getAABB(x, y, z + 0.375F, x + 1.0F, y + 1.5F, z + 0.625F);
  128.                 }
  129.             }
  130.  
  131.         }
  132.  
  133.         return null;
  134.     }
  135.  
  136.     @Override
  137.     /**
  138.      * Updates the blocks bounds based on its current state. Args: world, x, y, z
  139.      */
  140.     public void setBlockBoundsBasedOnState(IBlockAccess world, int x, int y, int z)
  141.     {
  142.         TileEntity TE = world.getTileEntity(x, y, z);
  143.  
  144.         if (TE instanceof TEBase) {
  145.  
  146.             if (Gate.getFacing((TEBase) TE) == Gate.FACING_ON_Z) {
  147.                 if (Gate.getType((TEBase) TE) == Gate.TYPE_VANILLA || Gate.getType((TEBase) TE) == Gate.TYPE_WALL) {
  148.                     setBlockBounds(0.4375F, 0.0F, 0.0F, 0.5625F, 1.0F, 1.0F);
  149.                 } else {
  150.                     setBlockBounds(0.375F, 0.0F, 0.0F, 0.625F, 1.0F, 1.0F);
  151.                 }
  152.             } else {
  153.                 if (Gate.getType((TEBase) TE) == Gate.TYPE_VANILLA || Gate.getType((TEBase) TE) == Gate.TYPE_WALL) {
  154.                     setBlockBounds(0.0F, 0.0F, 0.4375F, 1.0F, 1.0F, 0.5625F);
  155.                 } else {
  156.                     setBlockBounds(0.0F, 0.0F, 0.375F, 1.0F, 1.0F, 0.625F);
  157.                 }
  158.             }
  159.  
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * Opens or closes one neighboring gate above or below block.
  165.      */
  166.     private void cycleNeighborGate(TEBase TE, World world, int x, int y, int z)
  167.     {
  168.         boolean isGateBelow = world.getBlock(x, y - 1, z).equals(this) ? true : false;
  169.         boolean isGateAbove = world.getBlock(x, y + 1, z).equals(this) ? true : false;
  170.  
  171.         /*
  172.          * Will only check for gate above or below, and limit to only activating a single stacked gate.
  173.          * It is done this way intentionally.
  174.          */
  175.         if (isGateBelow) {
  176.  
  177.             TileEntity TE_YN = (TEBase) world.getTileEntity(x, y - 1, z);
  178.             if (Gate.getFacing((TEBase) TE_YN) == Gate.getFacing(TE)) {
  179.                 Gate.setDirOpen((TEBase) TE_YN, Gate.getDirOpen(TE));
  180.                 Gate.setState((TEBase) TE_YN, Gate.getState(TE), false);
  181.             }
  182.  
  183.         } else if (isGateAbove) {
  184.  
  185.             TileEntity TE_YP = (TEBase) world.getTileEntity(x, y + 1, z);
  186.             if (Gate.getFacing((TEBase) TE_YP) == Gate.getFacing(TE)) {
  187.                 Gate.setDirOpen((TEBase) TE_YP, Gate.getDirOpen(TE));
  188.                 Gate.setState((TEBase) TE_YP, Gate.getState(TE), false);
  189.             }
  190.  
  191.         }
  192.     }
  193.  
  194.     @Override
  195.     /**
  196.      * Called when the block is placed in the world.
  197.      */
  198.     public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemStack)
  199.     {
  200.         TileEntity TE = world.getTileEntity(x, y, z);
  201.  
  202.         if (TE instanceof TEBase) {
  203.  
  204.             int facing = (MathHelper.floor_double(entityLiving.rotationYaw * 4.0F / 360.0F + 0.5D) & 3) % 4;
  205.  
  206.             Gate.setFacing((TEBase) TE, facing == 3 || facing == 1 ? Gate.FACING_ON_Z : Gate.FACING_ON_X);
  207.  
  208.             /* Match block type with adjacent type if possible. */
  209.  
  210.             TEBase[] TE_list = getAdjacentTileEntities(world, x, y, z);
  211.  
  212.             for (TEBase TE_current : TE_list) {
  213.  
  214.                 if (TE_current != null) {
  215.  
  216.                     Block block = TE_current.getBlockType();
  217.  
  218.                     if (block.equals(this)) {
  219.                         Gate.setType((TEBase) TE, Gate.getType(TE_current));
  220.                     } else if (block.equals(BlockRegistry.blockCarpentersGate)) {
  221.                         Gate.setType((TEBase) TE, Barrier.getType(TE_current));
  222.                     }
  223.  
  224.                 }
  225.  
  226.             }
  227.  
  228.         }
  229.  
  230.         super.onBlockPlacedBy(world, x, y, z, entityLiving, itemStack);
  231.     }
  232.  
  233.     @Override
  234.     /**
  235.      * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
  236.      * their own) Args: x, y, z, neighbor blockID
  237.      */
  238.     public void onNeighborBlockChange(World world, int x, int y, int z, Block block)
  239.     {
  240.         if (!world.isRemote) {
  241.  
  242.             TileEntity TE = world.getTileEntity(x, y, z);
  243.  
  244.             if (TE != null) {
  245.  
  246.                 boolean isPowered = world.isBlockIndirectlyGettingPowered(x, y, z);
  247.  
  248.                 if (isPowered || block != null && block.canProvidePower())
  249.                 {
  250.                     int state = Gate.getState((TEBase) TE);
  251.  
  252.                     if (isPowered && state == Gate.STATE_CLOSED) {
  253.                         Gate.setState((TEBase) TE, Gate.STATE_OPEN, true);
  254.                         cycleNeighborGate((TEBase) TE, world, x, y, z);
  255.                     } else if (!isPowered && state == Gate.STATE_OPEN) {
  256.                         Gate.setState((TEBase) TE, Gate.STATE_CLOSED, true);
  257.                         cycleNeighborGate((TEBase) TE, world, x, y, z);
  258.                     }
  259.                 }
  260.  
  261.             }
  262.  
  263.         }
  264.  
  265.         super.onNeighborBlockChange(world, x, y, z, block);
  266.     }
  267.  
  268.     @Override
  269.     @SideOnly(Side.CLIENT)
  270.     /**
  271.      * Returns true if the given side of this block type should be rendered, if the adjacent block is at the given
  272.      * coordinates.  Args: world, x, y, z, side
  273.      */
  274.     public boolean shouldSideBeRendered(IBlockAccess world, int x, int y, int z, int side)
  275.     {
  276.         return true;
  277.     }
  278.  
  279.     @Override
  280.     /**
  281.      * The type of render function that is called for this block
  282.      */
  283.     public int getRenderType()
  284.     {
  285.         return BlockRegistry.carpentersGateRenderID;
  286.     }
  287.  
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement