Advertisement
MrCyberdragon

Untitled

Dec 22nd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. public class Snowglobe extends BlockContainer {
  2.  
  3. public static final PropertyDirection FACING = BlockHorizontal.FACING;
  4. public static final PropertyBool HAS_RECORD = PropertyBool.create("has_record");
  5.  
  6. public static void registerFixesJukebox(DataFixer fixer)
  7. {
  8. fixer.registerWalker(FixTypes.BLOCK_ENTITY, new ItemStackData(Snowglobe.TileEntityJukebox.class, new String[] {"RecordItem"}));
  9. }
  10.  
  11. public Snowglobe(String name, Material material){
  12. super(material, MapColor.SNOW);
  13. setUnlocalizedName(name);
  14. setRegistryName(name);
  15. this.setHardness(0.3F);
  16. this.setCreativeTab(CreativeTabs.DECORATIONS);
  17. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(HAS_RECORD, Boolean.valueOf(false)));
  18.  
  19. ModBlocks.BLOCKS.add(this);
  20. ModItems.ITEMS.add(new ItemBlock(this).setRegistryName(this.getRegistryName()));
  21. }
  22.  
  23. @Override
  24. public BlockRenderLayer getBlockLayer() {
  25. return BlockRenderLayer.CUTOUT;
  26. }
  27.  
  28. @Override
  29. public boolean isFullCube(IBlockState state) {
  30. return false;
  31. }
  32.  
  33. @Override
  34. public boolean isOpaqueCube(IBlockState state) {
  35. return false;
  36. }
  37.  
  38. public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
  39. {
  40. return worldIn.getBlockState(pos).getBlock().isReplaceable(worldIn, pos) && worldIn.isSideSolid(pos.down(), EnumFacing.UP);
  41. }
  42.  
  43. public IBlockState withRotation(IBlockState state, Rotation rot)
  44. {
  45. return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
  46. }
  47.  
  48. public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
  49. {
  50. return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
  51. }
  52.  
  53. public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  54. {
  55. return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
  56. }
  57.  
  58. public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  59. {
  60. if (((Boolean)state.getValue(HAS_RECORD)).booleanValue())
  61. {
  62. this.dropRecord(worldIn, pos, state);
  63. state = state.withProperty(HAS_RECORD, Boolean.valueOf(false)).withProperty(FACING, state.getValue(FACING));
  64. worldIn.setBlockState(pos, state, 2);
  65. return true;
  66. }
  67. else
  68. {
  69. return false;
  70. }
  71. }
  72.  
  73. public void insertRecord(World worldIn, BlockPos pos, IBlockState state, ItemStack recordStack)
  74. {
  75. TileEntity tileentity = worldIn.getTileEntity(pos);
  76.  
  77. if (tileentity instanceof Snowglobe.TileEntityJukebox)
  78. {
  79. ((Snowglobe.TileEntityJukebox)tileentity).setRecord(recordStack.copy());
  80. worldIn.setBlockState(pos, state.withProperty(HAS_RECORD, Boolean.valueOf(true)).withProperty(FACING, state.getValue(FACING)), 2);
  81. }
  82. }
  83.  
  84. private void dropRecord(World worldIn, BlockPos pos, IBlockState state)
  85. {
  86. if (!worldIn.isRemote)
  87. {
  88. TileEntity tileentity = worldIn.getTileEntity(pos);
  89.  
  90. if (tileentity instanceof Snowglobe.TileEntityJukebox)
  91. {
  92. Snowglobe.TileEntityJukebox snowglobe$tileentitysnowglobe = (Snowglobe.TileEntityJukebox)tileentity;
  93. ItemStack itemstack = snowglobe$tileentitysnowglobe.getRecord();
  94.  
  95. if (!itemstack.isEmpty())
  96. {
  97. worldIn.playEvent(1010, pos, 0);
  98. worldIn.playRecord(pos, (SoundEvent)null);
  99. snowglobe$tileentitysnowglobe.setRecord(ItemStack.EMPTY);
  100. float f = 0.7F;
  101. double d0 = (double)(worldIn.rand.nextFloat() * 0.7F) + 0.15000000596046448D;
  102. double d1 = (double)(worldIn.rand.nextFloat() * 0.7F) + 0.06000000238418579D + 0.6D;
  103. double d2 = (double)(worldIn.rand.nextFloat() * 0.7F) + 0.15000000596046448D;
  104. ItemStack itemstack1 = itemstack.copy();
  105. EntityItem entityitem = new EntityItem(worldIn, (double)pos.getX() + d0, (double)pos.getY() + d1, (double)pos.getZ() + d2, itemstack1);
  106. entityitem.setDefaultPickupDelay();
  107. worldIn.spawnEntity(entityitem);
  108. }
  109. }
  110. }
  111. }
  112.  
  113. public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  114. {
  115. this.dropRecord(worldIn, pos, state);
  116. super.breakBlock(worldIn, pos, state);
  117. }
  118.  
  119. public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
  120. {
  121. if (!worldIn.isRemote)
  122. {
  123. super.dropBlockAsItemWithChance(worldIn, pos, state, chance, 0);
  124. }
  125. }
  126.  
  127. public TileEntity createNewTileEntity(World worldIn, int meta)
  128. {
  129. return new Snowglobe.TileEntityJukebox();
  130. }
  131.  
  132. public boolean hasComparatorInputOverride(IBlockState state)
  133. {
  134. return true;
  135. }
  136.  
  137. public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos)
  138. {
  139. TileEntity tileentity = worldIn.getTileEntity(pos);
  140.  
  141. if (tileentity instanceof Snowglobe.TileEntityJukebox)
  142. {
  143. ItemStack itemstack = ((Snowglobe.TileEntityJukebox)tileentity).getRecord();
  144.  
  145. if (!itemstack.isEmpty())
  146. {
  147. return Item.getIdFromItem(itemstack.getItem()) + 1 - Item.getIdFromItem(Items.RECORD_13);
  148. }
  149. }
  150.  
  151. return 0;
  152. }
  153.  
  154. public EnumBlockRenderType getRenderType(IBlockState state)
  155. {
  156. return EnumBlockRenderType.MODEL;
  157. }
  158.  
  159.  
  160. public IBlockState getStateFromMeta(int meta)
  161. {
  162. return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(HAS_RECORD, Boolean.valueOf(meta > 0));
  163. }
  164.  
  165. public int getMetaFromState(IBlockState state)
  166. {
  167. int index = ((EnumFacing)state.getValue(FACING)).getHorizontalIndex() >> 1;
  168. int bool = ((Boolean)state.getValue(HAS_RECORD)).booleanValue() ? 1 : 0 & 1;
  169. int i = index << 1|bool;
  170. return i;
  171. }
  172.  
  173. protected BlockStateContainer createBlockState()
  174. {
  175. return new BlockStateContainer(this, new IProperty[] {FACING, HAS_RECORD});
  176. }
  177.  
  178. public static class TileEntityJukebox extends TileEntity
  179. {
  180. private ItemStack record = ItemStack.EMPTY;
  181.  
  182. public void readFromNBT(NBTTagCompound compound)
  183. {
  184. super.readFromNBT(compound);
  185.  
  186. if (compound.hasKey("RecordItem", 10))
  187. {
  188. this.setRecord(new ItemStack(compound.getCompoundTag("RecordItem")));
  189. }
  190. else if (compound.getInteger("Record") > 0)
  191. {
  192. this.setRecord(new ItemStack(Item.getItemById(compound.getInteger("Record"))));
  193. }
  194. }
  195.  
  196. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  197. {
  198. super.writeToNBT(compound);
  199.  
  200. if (!this.getRecord().isEmpty())
  201. {
  202. compound.setTag("RecordItem", this.getRecord().writeToNBT(new NBTTagCompound()));
  203. }
  204.  
  205. return compound;
  206. }
  207.  
  208. public ItemStack getRecord()
  209. {
  210. return this.record;
  211. }
  212.  
  213. public void setRecord(ItemStack recordStack)
  214. {
  215. this.record = recordStack;
  216. this.markDirty();
  217. }
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement