Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.98 KB | None | 0 0
  1. package catwalks.block;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.EnumMap;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import catwalks.block.extended.EnumCubeEdge;
  10. import catwalks.block.extended.TileExtended;
  11. import catwalks.block.property.UPropertyBool;
  12. import catwalks.item.ItemBlockCatwalk;
  13. import catwalks.register.BlockRegister;
  14. import catwalks.shade.ccl.vec.Cuboid6;
  15. import catwalks.shade.ccl.vec.Matrix4;
  16. import catwalks.shade.ccl.vec.Vector3;
  17. import catwalks.util.AABBUtils;
  18. import catwalks.util.GeneralUtil;
  19. import catwalks.util.Logs;
  20. import net.minecraft.block.material.Material;
  21. import net.minecraft.block.state.IBlockState;
  22. import net.minecraft.entity.EntityLivingBase;
  23. import net.minecraft.entity.player.EntityPlayer;
  24. import net.minecraft.init.Blocks;
  25. import net.minecraft.item.ItemStack;
  26. import net.minecraft.util.AxisAlignedBB;
  27. import net.minecraft.util.BlockPos;
  28. import net.minecraft.util.EnumFacing;
  29. import net.minecraft.world.World;
  30. import net.minecraftforge.common.property.IExtendedBlockState;
  31. import net.minecraftforge.common.property.IUnlistedProperty;
  32.  
  33. public class BlockCatwalkStair extends BlockCatwalkBase {
  34.  
  35.     public static final UPropertyBool EAST_TOP = new UPropertyBool("EASTTOP");
  36.     public static final UPropertyBool WEST_TOP = new UPropertyBool("WESTTOP");
  37.    
  38.     public static final int I_EAST_TOP = I_BASE_LEN+1, I_WEST_TOP = I_BASE_LEN+2;
  39.    
  40.     public BlockCatwalkStair() {
  41.         super(Material.iron, "catwalkStair", ItemBlockCatwalk.class);
  42.         setHardness(1.5f);
  43.     }
  44.    
  45.     @Override
  46.     public void addAdditionalProperties(List<IUnlistedProperty> list) {
  47.         list.add(EAST_TOP);
  48.         list.add(WEST_TOP);
  49.     }
  50.    
  51.     @Override
  52.     public IExtendedBlockState addProperties(TileExtended tile, IExtendedBlockState state) {
  53.         return state
  54.                 .withProperty(EAST_TOP, tile.getBoolean(I_EAST_TOP))
  55.                 .withProperty(WEST_TOP, tile.getBoolean(I_WEST_TOP))
  56.         ;
  57.     }
  58.    
  59.     @Override
  60.     public boolean canPlaceBlockAt(World worldIn, BlockPos pos) {
  61.         return super.canPlaceBlockAt(worldIn, pos) && worldIn.getBlockState(pos.offset(EnumFacing.UP)).getBlock().isReplaceable(worldIn, pos.offset(EnumFacing.UP));
  62.     }
  63.    
  64.     @Override
  65.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
  66.         TileExtended tile = (TileExtended) worldIn.getTileEntity(pos);
  67.        
  68.         tile.setBoolean(I_EAST_TOP, true);
  69.         tile.setBoolean(I_WEST_TOP, true);
  70.        
  71.         super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
  72.        
  73.         worldIn.setBlockState(pos.offset(EnumFacing.UP), BlockRegister.multiblockPart.getDefaultState());
  74.         GeneralUtil.updateSurroundingCatwalkBlocks(worldIn, pos.offset(EnumFacing.UP));
  75.     }
  76.    
  77.     @Override
  78.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
  79.         worldIn.setBlockState(pos.offset(EnumFacing.UP), Blocks.air.getDefaultState());
  80.         super.breakBlock(worldIn, pos, state);
  81.     }
  82.    
  83.     @Override
  84.     public EnumFacing transformAffectedSide(World world, BlockPos pos, IBlockState state, EnumFacing side) {
  85.         IExtendedBlockState estate = (IExtendedBlockState)getExtendedState(state, world, pos);
  86.         return GeneralUtil.derotateFacing(GeneralUtil.getRotation(EnumFacing.NORTH, estate.getValue(BlockCatwalkBase.FACING)), side);
  87.     }
  88.    
  89.     public Map<EnumFacing, List<CollisionBox>> collisionBoxes;
  90.    
  91.     @Override
  92.     public void initColllisionBoxes() {
  93.         collisionBoxes = new EnumMap<>(EnumFacing.class);
  94.         List<CollisionBox> boxes = new ArrayList<>();
  95.        
  96.         AxisAlignedBB bounds = new AxisAlignedBB(0,0,0 , 1,1,1);
  97.         double thickness = Float.MIN_VALUE, stepCount = 4, stepLength = 1.0/stepCount;
  98.        
  99.         Cuboid6 cuboid = new Cuboid6(bounds);
  100.        
  101.         AABBUtils.offsetSide(cuboid, EnumFacing.UP, -(1-thickness));
  102.         AABBUtils.offsetSide(cuboid, EnumFacing.NORTH, -(1-stepLength));
  103.        
  104.         cuboid.offset(new Vector3(0, stepLength/2.0, 0));
  105.        
  106.         for (int i = 0; i < stepCount; i++) {
  107.             CollisionBox box = new CollisionBox();
  108.            
  109.             box.enableProperty = BOTTOM;
  110.            
  111.             box.normal = cuboid.copy();
  112.             box.sneak  = cuboid.copy();
  113.            
  114.             boxes.add(box);
  115.            
  116.             cuboid.offset(new Vector3(0, stepLength, -stepLength));
  117.         }
  118.        
  119.         cuboid = new Cuboid6(bounds);
  120.         Cuboid6 cuboid2 = new Cuboid6(bounds);
  121.        
  122.         AABBUtils.offsetSide(cuboid,  EnumFacing.NORTH, -(1-stepLength));
  123.         AABBUtils.offsetSide(cuboid2, EnumFacing.NORTH, -(1-stepLength));
  124.  
  125.         AABBUtils.offsetSide(cuboid,  EnumFacing.EAST,  -(1-thickness));
  126.         AABBUtils.offsetSide(cuboid2, EnumFacing.WEST,  -(1-thickness));
  127.        
  128.         for (int i = 0; i < stepCount; i++) {
  129.             CollisionBox box = new CollisionBox();
  130.            
  131.             box.enableProperty = EAST;
  132.            
  133.             box.normal = cuboid.copy();
  134.             box.normal.max.y += 0.5;
  135.             box.sneak  = cuboid.copy();
  136.            
  137.             boxes.add(box);
  138.            
  139.             cuboid.offset(new Vector3(0, stepLength, -stepLength));
  140.         }
  141.        
  142.         for (int i = 0; i < stepCount; i++) {
  143.             CollisionBox box = new CollisionBox();
  144.            
  145.             box.enableProperty = WEST;
  146.            
  147.             box.normal = cuboid2.copy();
  148.             box.normal.max.y += 0.5;
  149.             box.sneak  = cuboid2.copy();
  150.            
  151.             boxes.add(box);
  152.            
  153.             cuboid2.offset(new Vector3(0, stepLength, -stepLength));
  154.         }
  155.        
  156.         cuboid = new Cuboid6(bounds);
  157.         AABBUtils.offsetSide(cuboid, EnumFacing.NORTH, -(1-thickness));
  158.  
  159.         CollisionBox box = new CollisionBox();
  160.  
  161.         box.enableProperty = SOUTH;
  162.        
  163.         box.normal = cuboid.copy();
  164.         box.normal.max.y += 0.5;
  165.         box.sneak  = cuboid.copy();
  166.        
  167.         boxes.add(box);
  168.        
  169.         cuboid.offset(new Vector3(0, 1, -1));
  170.        
  171.         box = new CollisionBox();
  172.  
  173.         box.enableProperty = NORTH;
  174.        
  175.         box.normal = cuboid.copy();
  176.         box.normal.max.y += 0.5;
  177.         box.sneak  = cuboid.copy();
  178.        
  179.         boxes.add(box);
  180.        
  181.         double q = Math.toRadians(90);
  182.        
  183.         Matrix4 matrix = new Matrix4();
  184.        
  185.         for (EnumFacing dir : new EnumFacing[]{EnumFacing.NORTH, EnumFacing.EAST, EnumFacing.SOUTH, EnumFacing.WEST}) {
  186.            
  187.             List<CollisionBox> turnedBoxes = new ArrayList<>();
  188.            
  189.             for (CollisionBox rawBox : boxes) {
  190.                 CollisionBox turnedBox = rawBox.copy();
  191.                 turnedBox.apply(matrix);
  192.                 turnedBoxes.add(turnedBox);
  193.             }
  194.            
  195.             collisionBoxes.put(dir, turnedBoxes);
  196.            
  197.             matrix.translate(new Vector3(0.5, 0.5, 0.5)).rotate(-q, new Vector3(0, 1, 0)).translate(new Vector3(-0.5, -0.5, -0.5));
  198.         }
  199.     }
  200.    
  201.     @Override
  202.     public List<CollisionBox> getCollisionBoxes(IExtendedBlockState state) {
  203.         EnumFacing facing = state.getValue(FACING);
  204.         List<CollisionBox> list = collisionBoxes.get(facing);
  205.         if(list == null) {
  206.             Logs.error("ERROR: tried to get collision boxes for invalid facing value! %s", facing.toString());
  207.         }
  208.         return list;
  209.     }
  210.    
  211.     private Map<EnumFacing, List<LookSide>> sideLookBoxes;
  212.  
  213.     @Override
  214.     public void initSides() {
  215.         sideLookBoxes = new HashMap<>();
  216.        
  217.         List<LookSide> sides = new ArrayList<>();
  218.        
  219.         LookSide side = new LookSide();
  220.        
  221.         side.mainSide = new Quad(
  222.             new Vector3(0, 0, 1),
  223.             new Vector3(0, 1, 1),
  224.             new Vector3(1, 1, 1),
  225.             new Vector3(1, 0, 1)
  226.         );
  227.        
  228.         double h = 0.5;
  229.         side.wrenchSide = new Quad(
  230.             new Vector3(0, 0, 1),
  231.             new Vector3(0, h, 1),
  232.             new Vector3(1, h, 1),
  233.             new Vector3(1, 0, 1)
  234.         );
  235.        
  236.         side.showProperty = SOUTH;
  237.         side.side = EnumFacing.SOUTH;
  238.         sides.add(side.copy());
  239.        
  240.         side.showProperty = NORTH;
  241.         side.side = EnumFacing.NORTH;
  242.         side.mainSide  .apply(new Matrix4().translate(new Vector3(0, 0, -1)));
  243.         side.wrenchSide.apply(new Matrix4().translate(new Vector3(0, 0, -1)));
  244.         side.offset = new BlockPos(0, 1, 0);
  245.         sides.add(side.copy());
  246.         side.offset = new BlockPos(0, 0, 0);
  247.        
  248.         // bottom sides
  249.         side.mainSide = new Tri(
  250.             new Vector3(0, 0, 1),
  251.             new Vector3(0, 1, 1),
  252.             new Vector3(0, 1, 0)
  253.         );
  254.        
  255.         side.wrenchSide = new Quad(
  256.             new Vector3(0, 0, 1),
  257.             new Vector3(0, h, 1),
  258.             new Vector3(0, 1, h),
  259.             new Vector3(0, 1, 0)
  260.         );
  261.        
  262.         side.showProperty = WEST;
  263.         side.side = EnumFacing.WEST;
  264.         sides.add(side.copy());
  265.        
  266.         side.showProperty = EAST;
  267.         side.side = EnumFacing.EAST;
  268.         side.mainSide  .apply(new Matrix4().translate(new Vector3(1, 0, 0)));
  269.         side.wrenchSide.apply(new Matrix4().translate(new Vector3(1, 0, 0)));
  270.         sides.add(side.copy());
  271.        
  272.        
  273.         // top sides
  274.         side.mainSide = new Tri(
  275.             new Vector3(0, 0, 1),
  276.             new Vector3(0, 1, 0),
  277.             new Vector3(0, 0, 0)
  278.         );
  279.        
  280.         side.wrenchSide = new Quad(
  281.             new Vector3(0, 0, h),
  282.             new Vector3(0, h, 0),
  283.             new Vector3(0, 0, 0),
  284.             new Vector3(0, 0, 0)
  285.         );
  286.        
  287.         side.offset = new BlockPos(0, 1, 0);
  288.        
  289.         side.showProperty = WEST_TOP;
  290.         side.side = EnumFacing.WEST;
  291.         sides.add(side.copy());
  292.        
  293.         side.showProperty = EAST_TOP;
  294.         side.side = EnumFacing.EAST;
  295.         side.mainSide  .apply(new Matrix4().translate(new Vector3(1, 0, 0)));
  296.         side.wrenchSide.apply(new Matrix4().translate(new Vector3(1, 0, 0)));
  297.         sides.add(side.copy());
  298.        
  299.         double q = Math.toRadians(90);
  300.         Matrix4 matrix = new Matrix4();
  301.        
  302.         for (EnumFacing dir : new EnumFacing[]{EnumFacing.NORTH, EnumFacing.EAST, EnumFacing.SOUTH, EnumFacing.WEST}) {
  303.            
  304.             List<LookSide> turnedSides = new ArrayList<>();
  305.            
  306.             for (LookSide rawSide : sides) {
  307.                 LookSide turnedSide = rawSide.copy();
  308.                 turnedSide.apply(matrix);
  309.                 turnedSide.side = GeneralUtil.rotateFacing(GeneralUtil.getRotation(EnumFacing.NORTH, dir), turnedSide.side);
  310.                 turnedSides.add(turnedSide);
  311.             }
  312.            
  313.             sideLookBoxes.put(dir, turnedSides);
  314.            
  315.             matrix.translate(new Vector3(0.5, 0.5, 0.5)).rotate(-q, new Vector3(0, 1, 0)).translate(new Vector3(-0.5, -0.5, -0.5));
  316.         }
  317.     }
  318.  
  319.     @Override
  320.     public List<LookSide> lookSides(IExtendedBlockState state) {
  321.         return sideLookBoxes.get(state.getValue(FACING));
  322.     }
  323.    
  324.     { /* ICatwalkConnect */ }
  325.  
  326.     @Override
  327.     public boolean hasEdge(World world, BlockPos pos, EnumCubeEdge edge) {
  328.         IExtendedBlockState state = (IExtendedBlockState) getExtendedState(world.getBlockState(pos), world, pos);
  329.         if(state.getValue(BlockCatwalkBase.FACING) == edge.getDir1()) {
  330.             EnumFacing actualDir = GeneralUtil.derotateFacing(GeneralUtil.getRotation(EnumFacing.NORTH, state.getValue(BlockCatwalkBase.FACING)), edge.getDir2());
  331.             if(actualDir == EnumFacing.EAST && state.getValue(BlockCatwalkStair.EAST_TOP)) {
  332.                 return true;
  333.             }
  334.             if(actualDir == EnumFacing.WEST && state.getValue(BlockCatwalkStair.WEST_TOP)) {
  335.                 return true;
  336.             }
  337.         }
  338.         return false;
  339.     }
  340.    
  341.     @Override
  342.     public boolean hasSide(World world, BlockPos pos, EnumFacing side) {
  343.         IExtendedBlockState state = (IExtendedBlockState) getExtendedState(world.getBlockState(pos), world, pos);
  344.         EnumFacing actualDir = GeneralUtil.derotateFacing(GeneralUtil.getRotation(EnumFacing.NORTH, state.getValue(BlockCatwalkBase.FACING)), side);
  345.         if(actualDir == EnumFacing.EAST && state.getValue(BlockCatwalkStair.EAST_TOP)) {
  346.             return true;
  347.         }
  348.         if(actualDir == EnumFacing.WEST && state.getValue(BlockCatwalkStair.WEST_TOP)) {
  349.             return true;
  350.         }
  351.         if(side == state.getValue(BlockCatwalkBase.FACING).getOpposite()) {
  352.             return state.getValue(BlockCatwalkBase.SOUTH);
  353.         }
  354.         return false;
  355.     }
  356.    
  357.     @Override
  358.     public void setSide(World world, BlockPos pos, EnumFacing side, boolean value) {
  359.         IExtendedBlockState state = (IExtendedBlockState) getExtendedState(world.getBlockState(pos), world, pos);
  360.         TileExtended tile = (TileExtended) world.getTileEntity(pos);
  361.        
  362.         EnumFacing actualDir = GeneralUtil.derotateFacing(GeneralUtil.getRotation(EnumFacing.NORTH, state.getValue(BlockCatwalkBase.FACING)), side);
  363.         if(actualDir == EnumFacing.EAST && state.getValue(BlockCatwalkBase.EAST)) {
  364.             tile.setBoolean(BlockCatwalkBase.I_EAST, value);
  365.         }
  366.         if(actualDir == EnumFacing.WEST && state.getValue(BlockCatwalkBase.WEST)) {
  367.             tile.setBoolean(BlockCatwalkBase.I_WEST, value);
  368.         }
  369.         if(side == state.getValue(BlockCatwalkBase.FACING).getOpposite()) {
  370.             tile.setBoolean(BlockCatwalkBase.I_SOUTH, value);
  371.         }
  372.     }
  373.    
  374.     @Override
  375.     public Object sideData(World world, BlockPos pos, EnumFacing side) {
  376.         IExtendedBlockState state = (IExtendedBlockState) getExtendedState(world.getBlockState(pos), world, pos);
  377.         return state.getValue(BlockCatwalkBase.FACING);
  378.     }
  379.    
  380.     @Override
  381.     public EnumSideType sideType(World world, BlockPos pos, EnumFacing side) {
  382.         IExtendedBlockState state = (IExtendedBlockState) getExtendedState(world.getBlockState(pos), world, pos);
  383.         if(side == state.getValue(BlockCatwalkBase.FACING)) {
  384.             return null;
  385.         }
  386.         if(side == state.getValue(BlockCatwalkBase.FACING).getOpposite()) {
  387.             return EnumSideType.FULL;
  388.         }
  389.         return EnumSideType.SLOPE_BOTTOM;
  390.     }
  391.    
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement