Advertisement
Exokem

Untitled

Jun 27th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.63 KB | None | 0 0
  1. package exokem.exkva.objects.blocks.ElectricalCable;
  2.  
  3. import com.google.common.collect.Maps;
  4. import exokem.exkva.common.Exkva;
  5. import exokem.exkva.objects.bases.mach.Machine;
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.SoundType;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.block.properties.PropertyBool;
  10. import net.minecraft.block.state.BlockStateContainer;
  11. import net.minecraft.block.state.IBlockState;
  12. import net.minecraft.client.renderer.block.model.ModelResourceLocation;
  13. import net.minecraft.entity.Entity;
  14. import net.minecraft.entity.EntityLivingBase;
  15. import net.minecraft.item.Item;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.tileentity.TileEntity;
  18. import net.minecraft.util.EnumFacing;
  19. import net.minecraft.util.ResourceLocation;
  20. import net.minecraft.util.math.AxisAlignedBB;
  21. import net.minecraft.util.math.BlockPos;
  22. import net.minecraft.util.math.Vec3d;
  23. import net.minecraft.world.IBlockAccess;
  24. import net.minecraft.world.World;
  25. import net.minecraftforge.client.model.ModelLoader;
  26.  
  27. import javax.annotation.Nullable;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.function.Function;
  31. import java.util.stream.Stream;
  32.  
  33. public class BlockCable extends Block {
  34.    
  35.  
  36.     private static final Map<EnumFacing, PropertyBool> SIDES = Stream.of(EnumFacing.VALUES)
  37.             .collect(Maps.toImmutableEnumMap(Function.identity(), f -> PropertyBool.create(f.getName())));
  38.  
  39.     private static final AxisAlignedBB BOUNDING_BOX = new AxisAlignedBB(0.3125D, 0, 0.3125D, 0.6875D, 0.1875D, 0.6875D);
  40.  
  41.     private static final Map<EnumFacing, AxisAlignedBB> BOUNDING_BOXES = Stream.of(EnumFacing.VALUES)
  42.             .collect(Maps.toImmutableEnumMap(Function.identity(), f -> {
  43.                 final Vec3d min = new Vec3d(0.3125D, 0, 0.000);
  44.                 final Vec3d max = new Vec3d(0.625, 0.1875D, 0.3125D);
  45.                 switch (f) {
  46.                     case DOWN: return BOUNDING_BOX;
  47.                     case UP: return BOUNDING_BOX;
  48.                     case NORTH: return new AxisAlignedBB(min.x, min.y, min.z, max.x, max.y, max.z);
  49.                     case SOUTH: return new AxisAlignedBB(1 - max.x, min.y, 1 - max.z, 1 - min.x, max.y, 1 - min.z);
  50.                     case WEST: return new AxisAlignedBB(min.z, min.y, 1 - max.x, max.z, max.y, 1 - min.x);
  51.                     case EAST: return new AxisAlignedBB(1 - max.z, min.y, min.x, 1 - min.z, max.y, max.x);
  52.                     default: throw new IllegalArgumentException("Not a recognized facing: " + f);
  53.                 }
  54.             }));
  55.  
  56.     public BlockCable() {
  57.         super(Material.CIRCUITS);
  58.         setUnlocalizedName(Exkva.MODID + ".electrical_cable");
  59.         setRegistryName(new ResourceLocation(Exkva.MODID, "electrical_cable"));
  60.         IBlockState defaultState = this.getDefaultState();
  61.         for (final PropertyBool property : BlockCable.SIDES.values()) {
  62.             defaultState = defaultState.withProperty(property, true);
  63.         }
  64.         this.setDefaultState(defaultState);
  65.         this.setSoundType(SoundType.METAL);
  66.         this.setHardness(0.25F);
  67.         this.setLightOpacity(0);
  68.     }
  69.  
  70.     @Override
  71.     public boolean isFullCube(IBlockState state) {
  72.         return false;
  73.     }
  74.  
  75.     @Override
  76.     public boolean isOpaqueCube(IBlockState state) {
  77.         return false;
  78.     }
  79.  
  80.     @Override
  81.     public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
  82.         final IBlockState actualState = state.getActualState(source, pos);
  83.         AxisAlignedBB box = BlockCable.BOUNDING_BOX;
  84.         for (final Map.Entry<EnumFacing, PropertyBool> e : BlockCable.SIDES.entrySet()) {
  85.             if (actualState.getValue(e.getValue())) {
  86.                 box = box.union(BlockCable.BOUNDING_BOXES.get(e.getKey()));
  87.             }
  88.         }
  89.         return box;
  90.     }
  91.  
  92.     @Override
  93.     public void addCollisionBoxToList(IBlockState state, World world, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> boxes, @Nullable Entity entityIn, boolean isActualState) {
  94.         final IBlockState actualState = isActualState ? state : state.getActualState(world, pos);
  95.         Block.addCollisionBoxToList(pos, entityBox, boxes, BlockCable.BOUNDING_BOX);
  96.         for (final Map.Entry<EnumFacing, PropertyBool> e : BlockCable.SIDES.entrySet()) {
  97.             if (actualState.getValue(e.getValue())) {
  98.                 final AxisAlignedBB box = BlockCable.BOUNDING_BOXES.get(e.getKey());
  99.                 Block.addCollisionBoxToList(pos, entityBox, boxes, box);
  100.             }
  101.         }
  102.     }
  103.  
  104.     @Override
  105.     protected BlockStateContainer createBlockState() {
  106.         final BlockStateContainer.Builder builder = new BlockStateContainer.Builder(this);
  107.         for (final PropertyBool property : BlockCable.SIDES.values()) {
  108.             builder.add(property);
  109.         }
  110.         return builder.build();
  111.     }
  112.  
  113.     @Override
  114.     public boolean doesSideBlockRendering(final IBlockState state, final IBlockAccess access, final BlockPos pos, final EnumFacing side) {
  115.         return this.canConnectTo(state, access, pos, side);
  116.     }
  117.  
  118.     private boolean canConnectTo(final IBlockState state, final IBlockAccess access, final BlockPos pos, final EnumFacing side) {
  119.         final BlockPos offset = pos.offset(side);
  120.         final IBlockState other = access.getBlockState(offset);
  121.         final Block block = other.getBlock();
  122.         if(side == EnumFacing.DOWN || side == EnumFacing.UP) {
  123.             return false;
  124.         }
  125.         return this == block || block instanceof Machine;
  126.     }
  127.  
  128.     @Override
  129.     @Deprecated
  130.     public IBlockState getStateFromMeta(final int meta) {
  131.         return this.getDefaultState();
  132.     }
  133.  
  134.     @Override
  135.     public int getMetaFromState(IBlockState state) {
  136.         return 0;
  137.     }
  138.  
  139.     @Override
  140.     @Deprecated
  141.     public IBlockState getActualState(final IBlockState state, final IBlockAccess access, final BlockPos pos) {
  142.         IBlockState actualState = state;
  143.         for (final Map.Entry<EnumFacing, PropertyBool> e : BlockCable.SIDES.entrySet()) {
  144.             actualState = actualState.withProperty(e.getValue(), this.canConnectTo(state, access, pos, e.getKey()));
  145.         }
  146.         return actualState;
  147.     }
  148.  
  149.     @Override
  150.     public boolean hasTileEntity(IBlockState state) {
  151.         return true;
  152.     }
  153.  
  154.     @Nullable
  155.     @Override
  156.     public TileEntity createTileEntity(World world, IBlockState state) {
  157.         return new TileCable();
  158.     }
  159.  
  160.     public void initModel() {
  161.         ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(this), 0, new ModelResourceLocation(getRegistryName(), "inventory"));
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement