Advertisement
Guest User

CabinetBlock

a guest
Aug 5th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.46 KB | None | 0 0
  1. package net.fusionlord.cabinetsreloaded.block;
  2.  
  3. import net.fusionlord.cabinetsreloaded.CabinetsReloaded;
  4. import net.fusionlord.cabinetsreloaded.Reference;
  5. import net.fusionlord.cabinetsreloaded.item.CabinetItem;
  6. import net.fusionlord.cabinetsreloaded.tileentity.CabinetTileEntity;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.BlockContainer;
  9. import net.minecraft.block.BlockWorkbench;
  10. import net.minecraft.block.material.Material;
  11. import net.minecraft.block.properties.IProperty;
  12. import net.minecraft.block.properties.PropertyEnum;
  13. import net.minecraft.block.state.BlockState;
  14. import net.minecraft.block.state.IBlockState;
  15. import net.minecraft.client.particle.EffectRenderer;
  16. import net.minecraft.client.particle.EntityDiggingFX;
  17. import net.minecraft.creativetab.CreativeTabs;
  18. import net.minecraft.enchantment.EnchantmentHelper;
  19. import net.minecraft.entity.Entity;
  20. import net.minecraft.entity.EntityLivingBase;
  21. import net.minecraft.entity.player.EntityPlayer;
  22. import net.minecraft.init.Blocks;
  23. import net.minecraft.inventory.Container;
  24. import net.minecraft.item.Item;
  25. import net.minecraft.item.ItemAxe;
  26. import net.minecraft.item.ItemPickaxe;
  27. import net.minecraft.item.ItemStack;
  28. import net.minecraft.nbt.NBTTagCompound;
  29. import net.minecraft.tileentity.TileEntity;
  30. import net.minecraft.util.*;
  31. import net.minecraft.world.Explosion;
  32. import net.minecraft.world.IBlockAccess;
  33. import net.minecraft.world.World;
  34. import net.minecraftforge.fml.common.registry.GameRegistry;
  35. import net.minecraftforge.fml.relauncher.Side;
  36. import net.minecraftforge.fml.relauncher.SideOnly;
  37.  
  38. import java.util.ArrayList;
  39.  
  40. public class CabinetBlock extends BlockContainer
  41. {
  42.     public static final PropertyEnum TYPE = PropertyEnum.create("CabinetType", Types.class);
  43.  
  44.     protected enum Types implements IStringSerializable
  45.     {
  46.         LEFT,
  47.         RIGHT,
  48.         DOUBLE
  49.         ;
  50.  
  51.         @Override
  52.         public String getName()
  53.         {
  54.             return name().toLowerCase();
  55.         }
  56.  
  57.         public int getID()
  58.         {
  59.             return ordinal();
  60.         }
  61.  
  62.         @Override
  63.         public String toString()
  64.         {
  65.             return name().toLowerCase();
  66.         }
  67.     }
  68.  
  69.     public CabinetBlock()
  70.     {
  71.         super(Material.wood);
  72.         setHardness(1.5f);
  73.         setCreativeTab(CreativeTabs.tabDecorations);
  74.         setDefaultState(this.blockState.getBaseState().withProperty(TYPE, Types.LEFT));
  75.         GameRegistry.registerBlock(this, CabinetItem.class, "Cabinet");
  76.     }
  77.  
  78.     @Override
  79.     protected BlockState createBlockState()
  80.     {
  81.         return new BlockState(this, new IProperty[] {TYPE});
  82.     }
  83.  
  84.     @Override
  85.     public IBlockState getStateFromMeta(int meta)
  86.     {
  87.         if (meta < 0 || meta > 2) meta = 0;
  88.         return getDefaultState().withProperty(TYPE, Types.values()[meta]);
  89.     }
  90.  
  91.     @Override
  92.     public int getMetaFromState(IBlockState state)
  93.     {
  94.         return ((Types) state.getValue(TYPE)).getID();
  95.     }
  96.  
  97.     @Override
  98.     public ItemStack getPickBlock(MovingObjectPosition target, World world, BlockPos pos)
  99.     {
  100.         return new ItemStack(Item.getItemFromBlock(this), 1, getMetaFromState(world.getBlockState(pos)));
  101.     }
  102.  
  103.     public static boolean getBlocked(World world, BlockPos pos, int facing)
  104.     {
  105.         int x = pos.getX();
  106.         int y = pos.getY();
  107.         int z = pos.getZ();
  108.         boolean blocked = false;
  109.         switch (facing)
  110.         {
  111.             case 0:
  112.                 blocked = !world.isAirBlock(new BlockPos(x, y, z - 1));
  113.                 break;
  114.             case 1:
  115.                 blocked = !world.isAirBlock(new BlockPos(x + 1, y, z));
  116.                 break;
  117.             case 2:
  118.                 blocked = !world.isAirBlock(new BlockPos(x, y, z + 1));
  119.                 break;
  120.             case 3:
  121.                 blocked = !world.isAirBlock(new BlockPos(x - 1, y, z));
  122.                 break;
  123.         }
  124.         return blocked;
  125.     }
  126.  
  127.  
  128.     @Override
  129.     public boolean removedByPlayer(World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
  130.     {
  131.         TileEntity te = world.getTileEntity(pos);
  132.         if (te != null && te instanceof CabinetTileEntity)
  133.         {
  134.             CabinetTileEntity cabinet = (CabinetTileEntity) te;
  135.             if (cabinet.isOwner(player))
  136.             {
  137.                 IBlockState state = world.getBlockState(pos);
  138.                 int meta = state.getBlock().getMetaFromState(state) << 12;
  139.                 boolean silky = false;
  140.                 ItemStack tool = player.getCurrentEquippedItem();
  141.                 if (tool != null && (tool.getItem() instanceof ItemAxe || tool.getItem() instanceof ItemPickaxe))
  142.                 {
  143.                     silky = EnchantmentHelper.getSilkTouchModifier(player);
  144.                 }
  145.                 if (player.capabilities.isCreativeMode || silky)
  146.                 {
  147.                     ItemStack stack = new ItemStack(this, 1, meta);
  148.                     boolean storeData = false;
  149.                     for (ItemStack itemStack : cabinet.getContents())
  150.                     {
  151.                         if (storeData)
  152.                         {
  153.                             break;
  154.                         }
  155.                         if (itemStack != null)
  156.                         {
  157.                             storeData = true;
  158.                         }
  159.                     }
  160.                     if (storeData || cabinet.getDisplayStack() != null)
  161.                     {
  162.                         NBTTagCompound cabinetTag = new NBTTagCompound();
  163.                         cabinet.writeExtraNBT(cabinetTag);
  164.                         stack.setTagCompound(new NBTTagCompound());
  165.                         stack.setTagInfo("silktouch", cabinetTag);
  166.                     }
  167.                     dropBlockAsItem(world, pos, world.getBlockState(pos), 0);
  168.                 }
  169.                 else
  170.                 {
  171.                     getDrops(world, pos, state, 0).stream().filter(stack -> stack != null).forEach(stack -> Block.spawnAsEntity(world, pos, stack));
  172.                 }
  173.                 world.setBlockToAir(pos);
  174.                 world.removeTileEntity(pos);
  175.             }
  176.         }
  177.         return false;
  178.     }
  179.  
  180.     @Override
  181.     public ArrayList<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
  182.     {
  183.         ArrayList<ItemStack> ret = new ArrayList<>();
  184.  
  185.         TileEntity te = world.getTileEntity(pos);
  186.         if (te instanceof CabinetTileEntity)
  187.         {
  188.             CabinetTileEntity cabinet = (CabinetTileEntity) te;
  189.             ret.add(new ItemStack(Reference.cabinet, 1, ((Types)state.getValue(TYPE)).getID()));
  190.             for (ItemStack stack : cabinet.getContents())
  191.             {
  192.                 if (stack != null)
  193.                 {
  194.                     ret.add(stack);
  195.                 }
  196.             }
  197.             if (cabinet.getDisplayStack() != null)
  198.             {
  199.                 ret.add(cabinet.getDisplayStack());
  200.             }
  201.         }
  202.         return ret;
  203.     }
  204.  
  205.     @Override
  206.     public float getExplosionResistance(World world, BlockPos pos, Entity entity, Explosion explosion)
  207.     {
  208.         TileEntity tileEntity = world.getTileEntity(pos);
  209.         if (tileEntity instanceof CabinetTileEntity)
  210.         {
  211.             CabinetTileEntity cabinet = (CabinetTileEntity) tileEntity;
  212.             if (!cabinet.isLocked())
  213.             {
  214.                 return Blocks.stone.getExplosionResistance(entity);
  215.             }
  216.         }
  217.         return Blocks.bedrock.getExplosionResistance(entity);
  218.     }
  219.  
  220.     @Override
  221.     @SideOnly(Side.CLIENT)
  222.     public boolean addHitEffects(World worldObj, MovingObjectPosition target, EffectRenderer effectRenderer)
  223.     {
  224.         Block block = worldObj.getBlockState(target.getBlockPos()).getBlock();
  225.  
  226.         if (worldObj.isAirBlock(target.getBlockPos()))
  227.         {
  228.             float f = 0.1F;
  229.  
  230.             int side = target.sideHit.getIndex();
  231.  
  232.             double d1 = target.getBlockPos().getY() + block.getBlockBoundsMinY() - (double) f * side == 0 ? 1 : -1;
  233.             double d2 = target.getBlockPos().getZ() + block.getBlockBoundsMinZ() - (double) f * side == 2 ? 1 : -1;
  234.             double d0 = target.getBlockPos().getX() + block.getBlockBoundsMinX() - (double) f * side == 4 ? 1 : -1;
  235.  
  236.             TileEntity te = worldObj.getTileEntity(target.getBlockPos());
  237.             if (te instanceof CabinetTileEntity)
  238.             {
  239.                 CabinetTileEntity cabinet = (CabinetTileEntity) te;
  240.                 block = cabinet.getDisplayStack() != null ? Block.getBlockFromItem(cabinet.getDisplayStack().getItem()) : Blocks.planks;
  241.             }
  242.             effectRenderer.addEffect(new EntityDiggingFX.Factory().getEntityFX(0,
  243.                                              worldObj,
  244.                                              d0,
  245.                                              d1,
  246.                                              d2,
  247.                                              0.0D,
  248.                                              0.0D,
  249.                                              0.0D,
  250.                                              Block.getIdFromBlock(block)
  251.                                      )
  252.                     /*.applyColourMultiplier(target.getBlockPos().getX(), target.getBlockPos().getY(), target.getBlockPos().getZ()).multiplyVelocity(0.2F)*/
  253.                     .multipleParticleScaleBy(0.6F));
  254.         }
  255.         return true;
  256.     }
  257.  
  258.     @Override
  259.     @SideOnly(Side.CLIENT)
  260.     public boolean addDestroyEffects(World world, BlockPos pos, EffectRenderer effectRenderer)
  261.     {
  262.         int x = pos.getX();
  263.         int y = pos.getY();
  264.         int z = pos.getZ();
  265.         Block block = world.getBlockState(pos).getBlock();
  266.  
  267.         TileEntity te = world.getTileEntity(pos);
  268.         if (te instanceof CabinetTileEntity)
  269.         {
  270.             CabinetTileEntity cabinet = (CabinetTileEntity) te;
  271.             if (cabinet.getDisplayStack() != null)
  272.             {
  273.                 block = Block.getBlockFromItem(cabinet.getDisplayStack().getItem());
  274.             }
  275.             else
  276.             {
  277.                 block = Blocks.planks;
  278.             }
  279.         }
  280.  
  281.         byte b0 = 4;
  282.  
  283.         for (int i1 = 0; i1 < b0; ++i1)
  284.         {
  285.             for (int j1 = 0; j1 < b0; ++j1)
  286.             {
  287.                 for (int k1 = 0; k1 < b0; ++k1)
  288.                 {
  289.                     double d0 = (double) x + ((double) i1 + 0.5D) / (double) b0;
  290.                     double d1 = (double) y + ((double) j1 + 0.5D) / (double) b0;
  291.                     double d2 = (double) z + ((double) k1 + 0.5D) / (double) b0;
  292.  
  293.                     effectRenderer.addEffect(new EntityDiggingFX.Factory().getEntityFX(0,
  294.                                                                                        world,
  295.                                                                                        d0,
  296.                                                                                        d1,
  297.                                                                                        d2,
  298.                                                                                        0.0D,
  299.                                                                                        0.0D,
  300.                                                                                        0.0D,
  301.                                                                                        Block.getIdFromBlock(block)
  302.                                              )
  303.                     /*.applyColourMultiplier(target.getBlockPos().getX(), target.getBlockPos().getY(), target.getBlockPos().getZ()).multiplyVelocity(0.2F)*/
  304.                                                      .multipleParticleScaleBy(0.6F));
  305.                 }
  306.             }
  307.         }
  308.         return true;
  309.     }
  310.  
  311.     @Override
  312.     public TileEntity createNewTileEntity(World world, int metadata)
  313.     {
  314.         return new CabinetTileEntity();
  315.     }
  316.  
  317.     @Override
  318.     public int getComparatorInputOverride(World world, BlockPos pos)
  319.     {
  320.         return Container.calcRedstoneFromInventory((CabinetTileEntity) world.getTileEntity(pos));
  321.     }
  322.  
  323.     @Override
  324.     public boolean hasComparatorInputOverride()
  325.     {
  326.         return true;
  327.     }
  328.  
  329.     @Override
  330.     public int getRenderType()
  331.     {
  332.         return -1;
  333.     }
  334.  
  335.     @Override
  336.     public boolean isSideSolid(IBlockAccess world, BlockPos pos, EnumFacing side)
  337.     {
  338.         EnumFacing face = EnumFacing.DOWN;
  339.         TileEntity te = world.getTileEntity(pos);
  340.         if (te != null && te instanceof CabinetTileEntity)
  341.         {
  342.             switch (((CabinetTileEntity) te).getFacing())
  343.             {
  344.                 case 0:
  345.                     face = EnumFacing.NORTH;
  346.                     break;
  347.                 case 1:
  348.                     face = EnumFacing.EAST;
  349.                     break;
  350.                 case 2:
  351.                     face = EnumFacing.SOUTH;
  352.                     break;
  353.                 case 3:
  354.                     face = EnumFacing.WEST;
  355.                     break;
  356.             }
  357.         }
  358.         return side != face;
  359.     }
  360.  
  361.     @Override
  362.     public boolean isOpaqueCube()
  363.     {
  364.         return false;
  365.     }
  366.  
  367.     @Override
  368.     public void onNeighborBlockChange(World world, BlockPos pos, IBlockState state, Block block)
  369.     {
  370.         int powered = world.isBlockIndirectlyGettingPowered(pos);
  371.         CabinetTileEntity cabinet = (CabinetTileEntity) world.getTileEntity(pos);
  372.         cabinet.setPowered(powered != 0);
  373.         super.onNeighborBlockChange(world, pos, state, block);
  374.     }
  375.  
  376.     @Override
  377.     public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
  378.     {
  379.         int x = pos.getX();
  380.         int y = pos.getY();
  381.         int z = pos.getZ();
  382.         TileEntity te = world.getTileEntity(pos);
  383.         if (te == null || !(te instanceof CabinetTileEntity) || world.isRemote)
  384.         {
  385.             return true;
  386.         }
  387.         CabinetTileEntity cabinet = (CabinetTileEntity) te;
  388.         if (cabinet.getDisplayStack() != null)
  389.         {
  390.             Block baseBlock = Block.getBlockFromItem(cabinet.getDisplayStack().getItem());
  391.             if (baseBlock != null && baseBlock instanceof BlockWorkbench && side == EnumFacing.UP)
  392.             {
  393.                 player.openGui(CabinetsReloaded.instance, 1, world, x, y, z);
  394.                 return true;
  395.             }
  396.         }
  397.  
  398.         if (cabinet.getOwner() == null)
  399.         {
  400.             cabinet.setOwner(player);
  401.         }
  402.  
  403.         boolean blocked = getBlocked(world, pos, cabinet.getFacing());
  404.         if (cabinet.isUseableByPlayer(player) && !blocked)
  405.         {
  406.             player.openGui(CabinetsReloaded.instance, 0, world, x, y, z);
  407.             return true;
  408.         }
  409.         return true;
  410.     }
  411.  
  412.     @Override
  413.     public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack stack)
  414.     {
  415.         if (world.isRemote)
  416.         {
  417.             return;
  418.         }
  419.         int facing = MathHelper.floor_double(entity.rotationYaw * 4F / 360F + 0.5D) & 3;
  420.         TileEntity te = world.getTileEntity(pos);
  421.         if (te != null && te instanceof CabinetTileEntity && entity instanceof EntityPlayer)
  422.         {
  423.             CabinetTileEntity cabinet = (CabinetTileEntity) te;
  424.             EntityPlayer player = (EntityPlayer) entity;
  425.             if (!canPlace(world, pos, player, cabinet))
  426.             {
  427.                 world.setBlockToAir(pos);
  428.                 world.removeTileEntity(pos);
  429.                 ItemStack newStack = stack.copy();
  430.                 newStack.stackSize = 1;
  431.                 player.inventory.addItemStackToInventory(newStack);
  432.                 return;
  433.             }
  434.             if (!stack.hasTagCompound())
  435.             {
  436.                 cabinet.setOwner(player);
  437.                 cabinet.setFacing(facing);
  438.             }
  439.             else
  440.             {
  441.                 cabinet.readExtraNBT(stack.getTagCompound().getCompoundTag("silktouch"));
  442.                 cabinet.setFacing(facing);
  443.                 cabinet.sync();
  444.             }
  445.         }
  446.     }
  447.  
  448.     private boolean canPlace(World world, BlockPos pos, EntityPlayer player, CabinetTileEntity cabinet)
  449.     {
  450.         int x = pos.getX();
  451.         int y = pos.getY();
  452.         int z = pos.getZ();
  453.         for (int x2 = -1; x2 < 2; x2++)
  454.         {
  455.             for (int y2 = -1; y2 < 2; y2++)
  456.             {
  457.                 for (int z2 = -1; z2 < 2; z2++)
  458.                 {
  459.                     TileEntity tileEntity = world.getTileEntity(new BlockPos(x + x2, y + y2, z + z2));
  460.                     if (tileEntity != null && tileEntity instanceof CabinetTileEntity)
  461.                     {
  462.                         CabinetTileEntity cabinet2 = (CabinetTileEntity) tileEntity;
  463.                         if (cabinet != cabinet2)
  464.                         {
  465.                             if (!cabinet2.isOwner(player))
  466.                             {
  467.                                 return false;
  468.                             }
  469.                         }
  470.                     }
  471.                 }
  472.             }
  473.         }
  474.         return true;
  475.     }
  476.  
  477.     @Override
  478.     public int getLightValue(IBlockAccess world, BlockPos pos)
  479.     {
  480.         TileEntity te = world.getTileEntity(pos);
  481.         if (te instanceof CabinetTileEntity)
  482.         {
  483.             CabinetTileEntity cabinet = (CabinetTileEntity) te;
  484.             if (cabinet.getDisplayStack() != null)
  485.             {
  486.                 Block baseBlock = Block.getBlockFromItem(cabinet.getDisplayStack().getItem());
  487.                 if (baseBlock != null)
  488.                 {
  489.                     return baseBlock.getLightValue();
  490.                 }
  491.             }
  492.         }
  493.         return getLightValue();
  494.     }
  495.  
  496.     @Override
  497.     public float getBlockHardness(World world, BlockPos pos)
  498.     {
  499.         TileEntity te = world.getTileEntity(pos);
  500.         if (te instanceof CabinetTileEntity)
  501.         {
  502.             CabinetTileEntity cabinet = (CabinetTileEntity) te;
  503.             if (cabinet.getDisplayStack() != null)
  504.             {
  505.                 Block baseBlock = Block.getBlockFromItem(cabinet.getDisplayStack().getItem());
  506.                 if (baseBlock != null)
  507.                 {
  508.                     return baseBlock.getBlockHardness(world, pos);
  509.                 }
  510.             }
  511.         }
  512.         return blockHardness;
  513.     }
  514.  
  515.     @Override
  516.     public int damageDropped(IBlockState state)
  517.     {
  518.         return ((Types)state.getValue(TYPE)).getID();
  519.     }
  520.  
  521.     @Override
  522.     public boolean shouldSideBeRendered(IBlockAccess worldIn, BlockPos pos, EnumFacing side)
  523.     {
  524.         return false;
  525.     }
  526. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement