Advertisement
Guest User

CardPrinter.java

a guest
Apr 1st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.08 KB | None | 0 0
  1. package com.mekelaina.duelcraft.blocks;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import javax.annotation.Nullable;
  7.  
  8. import com.google.common.collect.Lists;
  9. import com.mekelaina.duelcraft.Duelcraft;
  10. import com.mekelaina.duelcraft.util.IHasModel;
  11.  
  12. import net.minecraft.block.BlockHorizontal;
  13. import net.minecraft.block.material.Material;
  14. import net.minecraft.block.properties.IProperty;
  15. import net.minecraft.block.properties.PropertyDirection;
  16. import net.minecraft.block.state.BlockStateContainer;
  17. import net.minecraft.block.state.IBlockState;
  18. import net.minecraft.entity.Entity;
  19. import net.minecraft.entity.EntityLivingBase;
  20. import net.minecraft.item.Item;
  21. import net.minecraft.util.BlockRenderLayer;
  22. import net.minecraft.util.EnumFacing;
  23. import net.minecraft.util.Rotation;
  24. import net.minecraft.util.math.AxisAlignedBB;
  25. import net.minecraft.util.math.BlockPos;
  26. import net.minecraft.util.math.RayTraceResult;
  27. import net.minecraft.util.math.Vec3d;
  28. import net.minecraft.world.IBlockAccess;
  29. import net.minecraft.world.World;
  30. import net.minecraftforge.fml.relauncher.Side;
  31. import net.minecraftforge.fml.relauncher.SideOnly;
  32.  
  33. public class BlockCardPrinter extends BlockBase implements IHasModel
  34. {
  35.  
  36.     private static final AxisAlignedBB BASE = new AxisAlignedBB(0.312, 0, 0.25, 0.688, 0.062, 0.625);
  37.     private static final AxisAlignedBB SOUTHWEST1 = new AxisAlignedBB(0.312, 0.062, 0.562, 0.562, 0.125, 0.625);
  38.     private static final AxisAlignedBB SOUTHEAST1 = new AxisAlignedBB(0.562, 0.062, 0.562, 0.688, 0.125, 0.625);
  39.     private static final AxisAlignedBB MID = new AxisAlignedBB(0.312, 0.125, 0.25, 0.688, 0.188, 0.625);
  40.     private static final AxisAlignedBB NORTHEAST1 = new AxisAlignedBB(0.594, 0.062, 0.25, 0.688, 0.125, 0.312);
  41.     private static final AxisAlignedBB WEST1 = new AxisAlignedBB(0.312, 0.062, 0.312, 0.375, 0.125, 0.562);
  42.     private static final AxisAlignedBB EAST1 = new AxisAlignedBB(0.625, 0.062, 0.312, 0.688, 0.125, 0.562);
  43.     private static final AxisAlignedBB NORTHRIDGE = new AxisAlignedBB(0.312, 0.188, 0.25, 0.688, 0.25, 0.312);
  44.     private static final AxisAlignedBB TOP1 = new AxisAlignedBB(0.312, 0.25, 0.312, 0.688, 0.312, 0.438);
  45.     private static final AxisAlignedBB SOUTHWEST2 = new AxisAlignedBB(0.312, 0.188, 0.562, 0.438, 0.25, 0.625);
  46.     private static final AxisAlignedBB SOUTHEAST2 = new AxisAlignedBB(0.562, 0.188, 0.562, 0.688, 0.25, 0.625);
  47.     private static final AxisAlignedBB EAST2 = new AxisAlignedBB(0.625, 0.188, 0.312, 0.688, 0.25, 0.562);
  48.     private static final AxisAlignedBB SOUTHWEST3 = new AxisAlignedBB(0.312, 0.062, 0.25, 0.406, 0.125, 0.312);
  49.     private static final AxisAlignedBB WEST2 = new AxisAlignedBB(0.312, 0.188, 0.312, 0.375, 0.25, 0.562);
  50.     private static final AxisAlignedBB TOP2 = new AxisAlignedBB(0.312, 0.25, 0.5, 0.688, 0.312, 0.625);
  51.     private static final AxisAlignedBB TOP3 = new AxisAlignedBB(0.312, 0.25, 0.438, 0.406, 0.312, 0.5);
  52.     private static final AxisAlignedBB TOP4 = new AxisAlignedBB(0.594, 0.25, 0.438, 0.688, 0.312, 0.5);
  53.     // Skipped 'Sheet', as it has roatation
  54.     private static final AxisAlignedBB INKM = new AxisAlignedBB(0.438, 0.188, 0.575, 0.469, 0.25, 0.637);
  55.     private static final AxisAlignedBB INKC = new AxisAlignedBB(0.469, 0.188, 0.575, 0.5, 0.25, 0.637);
  56.     private static final AxisAlignedBB INKY = new AxisAlignedBB(0.5, 0.188, 0.575, 0.531, 0.25, 0.637);
  57.     private static final AxisAlignedBB INKB = new AxisAlignedBB(0.531, 0.188, 0.575, 0.562, 0.25, 0.637);
  58.     private static final AxisAlignedBB CASE = new AxisAlignedBB(0.438, 0.188, 0.562, 0.562, 0.25, 0.569);
  59.     private static final AxisAlignedBB TRAY = new AxisAlignedBB(0.406, 0, 0.125, 0.594, 0.062, 0.25);
  60.     private static final AxisAlignedBB TRAYLIPEAST = new AxisAlignedBB(0.594, 0.062, 0.138, 0.625, 0.094, 0.25);
  61.     private static final AxisAlignedBB SHEET2 = new AxisAlignedBB(0.406, 0.062, 0.125, 0.594, 0.069, 0.375);
  62.     private static final AxisAlignedBB TRAYLIPWEST = new AxisAlignedBB(0.375, 0.062, 0.138, 0.406, 0.094, 0.25);
  63.     private static final AxisAlignedBB MECHSOUTH1 = new AxisAlignedBB(0.406, 0.087, 0.4, 0.594, 0.125, 0.406);
  64.     private static final AxisAlignedBB MECHSOUTH2 = new AxisAlignedBB(0.406, 0.063, 0.374, 0.594, 0.094, 0.4);
  65.     private static final AxisAlignedBB MECHWEST = new AxisAlignedBB(0.404, 0.063, 0.312, 0.406, 0.125, 0.4);
  66.     private static final AxisAlignedBB MECHEAST = new AxisAlignedBB(0.594, 0.063, 0.312, 0.596, 0.125, 0.4);
  67.     private static final AxisAlignedBB LIGHT = new AxisAlignedBB(0.656, 0.275, 0.306, 0.669, 0.287, 0.318);
  68.     /**
  69.     * AxisAlignedBBs and methods getBoundingBox, collisionRayTrace, and collisionRayTrace generated using MrCrayfish's Model Creator <a href="https://mrcrayfish.com/tools?id=mc">https://mrcrayfish.com/tools?id=mc</a>
  70.     */
  71.     private static final List<AxisAlignedBB> COLLISION_BOXES = Lists.newArrayList(BASE, SOUTHWEST3, SOUTHEAST1, MID, NORTHEAST1, WEST1, EAST1, NORTHRIDGE, TOP1, SOUTHWEST2, SOUTHEAST2, EAST2, SOUTHWEST3, WEST2, TOP2, TOP3, TOP4, INKM, INKC, INKY, INKB, CASE, TRAY, TRAYLIPEAST, SHEET2, TRAYLIPWEST, MECHSOUTH1, MECHSOUTH2, MECHWEST, MECHEAST, LIGHT);
  72.     private static final AxisAlignedBB BOUNDING_BOX = new AxisAlignedBB(0.312, 0, 0.125, 0.688, 0.312, 0.637);
  73.     public static final PropertyDirection FACING = BlockHorizontal.FACING; 
  74.    
  75.    
  76.     public BlockCardPrinter(String name, Material materialIn)
  77.     {
  78.         super(name, materialIn);
  79.        
  80.     }
  81.  
  82.     @Override
  83.     public void registerModels()
  84.     {
  85.         Duelcraft.proxy.registerItemRenderer(Item.getItemFromBlock(this), 0, "inventory");
  86.        
  87.     }
  88.    
  89.      public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  90.         {
  91.             this.setDefaultFacing(worldIn, pos, state);
  92.         }
  93.  
  94.         private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
  95.         {
  96.             if (!worldIn.isRemote)
  97.             {
  98.                 IBlockState iblockstate = worldIn.getBlockState(pos.north());
  99.                 IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
  100.                 IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
  101.                 IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
  102.                 EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  103.  
  104.                 if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock())
  105.                 {
  106.                     enumfacing = EnumFacing.SOUTH;
  107.                 }
  108.                 else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock())
  109.                 {
  110.                     enumfacing = EnumFacing.NORTH;
  111.                 }
  112.                 else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock())
  113.                 {
  114.                     enumfacing = EnumFacing.EAST;
  115.                 }
  116.                 else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock())
  117.                 {
  118.                     enumfacing = EnumFacing.WEST;
  119.                 }
  120.  
  121.                 worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
  122.             }
  123.         }
  124.    
  125.     @Override
  126.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  127.     {
  128.         return Item.getItemFromBlock(this);
  129.     }
  130.        
  131.     @Override
  132.     public boolean isOpaqueCube(IBlockState state)
  133.     {
  134.        
  135.         return false;
  136.     }
  137.    
  138.     @Override
  139.     public boolean isFullCube(IBlockState state)
  140.     {
  141.         return false;
  142.     }
  143.  
  144.     @Override
  145.     protected BlockStateContainer createBlockState()
  146.     {
  147.         return new BlockStateContainer(this, new IProperty[] {FACING/*, GLASS_TYPES*/});
  148.     }
  149.    
  150.     @Override
  151.     public IBlockState withRotation(IBlockState state, Rotation rot)
  152.     {
  153.         return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
  154.     }
  155.    
  156.     @Override
  157.     public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  158.     {
  159.         return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
  160.     }
  161.  
  162.     @Override
  163.     public IBlockState getStateFromMeta(int meta)
  164.     {
  165.         EnumFacing enumfacing = EnumFacing.getFront(meta);
  166.        
  167.         if (enumfacing.getAxis() == EnumFacing.Axis.Y)
  168.         {
  169.             enumfacing = EnumFacing.NORTH;
  170.         }
  171.        
  172.         return this.getDefaultState().withProperty(FACING, enumfacing);
  173.     }
  174.    
  175.  
  176.         /**
  177.          * Convert the BlockState into the correct metadata value
  178.          */
  179.         public int getMetaFromState(IBlockState state)
  180.         {
  181.             return ((EnumFacing)state.getValue(FACING)).getIndex();
  182.         }
  183.    
  184.     @SideOnly(Side.CLIENT)
  185.     public BlockRenderLayer getBlockLayer()
  186.     {
  187.         return BlockRenderLayer.TRANSLUCENT;
  188.     }
  189.    
  190.  
  191.     @Override
  192.     public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
  193.     {
  194.         return BOUNDING_BOX;
  195.     }
  196.  
  197.     @Override
  198.     public void addCollisionBoxToList(IBlockState state, World world, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, @Nullable Entity entity, boolean isActualState)
  199.     {
  200.         entityBox = entityBox.offset(-pos.getX(), -pos.getY(), -pos.getZ());
  201.         for (AxisAlignedBB box : COLLISION_BOXES)
  202.         {
  203.             if (entityBox.intersects(box))
  204.                 collidingBoxes.add(box.offset(pos));
  205.         }
  206.     }
  207.  
  208.     @Override
  209.     @Nullable
  210.     public RayTraceResult collisionRayTrace(IBlockState state, World world, BlockPos pos, Vec3d start, Vec3d end)
  211.     {
  212.         double distanceSq;
  213.         double distanceSqShortest = Double.POSITIVE_INFINITY;
  214.         RayTraceResult resultClosest = null;
  215.         RayTraceResult result;
  216.         start = start.subtract(pos.getX(), pos.getY(), pos.getZ());
  217.         end = end.subtract(pos.getX(), pos.getY(), pos.getZ());
  218.         for (AxisAlignedBB box : COLLISION_BOXES)
  219.         {
  220.             result = box.calculateIntercept(start, end);
  221.             if (result == null)
  222.                 continue;
  223.  
  224.             distanceSq = result.hitVec.squareDistanceTo(start);
  225.             if (distanceSq < distanceSqShortest)
  226.             {
  227.                 distanceSqShortest = distanceSq;
  228.                 resultClosest = result;
  229.             }
  230.         }
  231.         return resultClosest == null ? null : new RayTraceResult(RayTraceResult.Type.BLOCK, resultClosest.hitVec.addVector(pos.getX(), pos.getY(), pos.getZ()), resultClosest.sideHit, pos);
  232.     }
  233.    
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement