Advertisement
Redsword

Untitled

Apr 24th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. package redsword.mub.blocks;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import javax.annotation.Nullable;
  8.  
  9. import redsword.mub.Reference;
  10. import redsword.mub.init.ModBlocks;
  11. import redsword.mub.init.ModColorBlocks;
  12. import redsword.mub.init.ModItems;
  13. import redsword.mub.tileentity.TileEntityCanvas;
  14. import net.minecraft.block.BlockContainer;
  15. import net.minecraft.block.ITileEntityProvider;
  16. import net.minecraft.block.SoundType;
  17. import net.minecraft.block.material.Material;
  18. import net.minecraft.block.state.IBlockState;
  19. import net.minecraft.client.Minecraft;
  20. import net.minecraft.client.resources.I18n;
  21. import net.minecraft.entity.EntityLivingBase;
  22. import net.minecraft.entity.player.EntityPlayer;
  23. import net.minecraft.item.ItemStack;
  24. import net.minecraft.nbt.NBTTagCompound;
  25. import net.minecraft.tileentity.TileEntity;
  26. import net.minecraft.util.BlockRenderLayer;
  27. import net.minecraft.util.EnumBlockRenderType;
  28. import net.minecraft.util.EnumFacing;
  29. import net.minecraft.util.EnumHand;
  30. import net.minecraft.util.ResourceLocation;
  31. import net.minecraft.util.math.BlockPos;
  32. import net.minecraft.util.math.RayTraceResult;
  33. import net.minecraft.util.text.TextFormatting;
  34. import net.minecraft.world.IBlockAccess;
  35. import net.minecraft.world.World;
  36. import net.minecraftforge.fml.relauncher.Side;
  37. import net.minecraftforge.fml.relauncher.SideOnly;
  38.  
  39. /**
  40. * A block which can be coloured when clicked on by a paint brush
  41. *
  42. * @author CJMinecraft
  43. *
  44. */
  45. public class BlockColoredGlass extends BlockContainer implements ITileEntityProvider {
  46.  
  47. /**
  48. * Default block constructor
  49. *
  50. * @param unlocalizedName
  51. * The unlocalised name of the block
  52. */
  53. public BlockColoredGlass(String unlocalizedName) {
  54. super(Material.GLASS);
  55. this.setSoundType(SoundType.GLASS);
  56. this.setUnlocalizedName(unlocalizedName);
  57. this.setRegistryName(new ResourceLocation(Reference.MODID, unlocalizedName));
  58. this.setHardness(1);
  59. this.setResistance(5);
  60. this.isBlockContainer = true; // Says it is a block container
  61. }
  62.  
  63. /**
  64. * This will change the blocks colour when clicked on by a paint brush
  65. */
  66. @Override
  67. public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
  68. EnumFacing facing, float hitX, float hitY, float hitZ) {
  69. if (player.getHeldItem(hand).getItem() == ModItems.paintBrush) {
  70. TileEntityCanvas canvas = (TileEntityCanvas) world.getTileEntity(pos);
  71. canvas.setColour(player.getHeldItem(hand).getTagCompound().getInteger("colour"));
  72. world.markBlockRangeForRenderUpdate(pos, pos);
  73. }
  74. return false;
  75. }
  76.  
  77. /**
  78. * Says what colour the block is
  79. */
  80. @Override
  81. public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced) {
  82. if (stack.hasTagCompound())
  83. if (stack.getTagCompound().hasKey("colour"))
  84. tooltip.add(TextFormatting.GRAY + I18n.format(getUnlocalizedName() + ".tooltip",
  85. String.format("#%06X", (0xFFFFFF & stack.getTagCompound().getInteger("colour")))));
  86. }
  87.  
  88. /**
  89. * Makes it so that when you pick block, you get the correct block
  90. */
  91. @Override
  92. public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos,
  93. EntityPlayer player) {
  94. TileEntityCanvas canvas = (TileEntityCanvas) world.getTileEntity(pos);
  95. ItemStack stack = new ItemStack(ModColorBlocks.coloredglass);
  96. NBTTagCompound nbt = new NBTTagCompound();
  97. if (canvas != null)
  98. nbt.setInteger("colour", canvas.getColour());
  99. else
  100. nbt.setInteger("colour", 0xFFFFFF);
  101. stack.setTagCompound(nbt);
  102. return stack;
  103. }
  104.  
  105. /**
  106. * Needed to make sure our block is rendered correctly
  107. */
  108. @Override
  109. public EnumBlockRenderType getRenderType(IBlockState state) {
  110. return EnumBlockRenderType.MODEL;
  111. }
  112.  
  113. /**
  114. * Create the tile entity
  115. */
  116. @Override
  117. public TileEntity createTileEntity(World world, IBlockState state) {
  118. return new TileEntityCanvas();
  119. }
  120.  
  121. /**
  122. * Also create the tile entity
  123. */
  124. @Override
  125. public TileEntity createNewTileEntity(World world, int meta) {
  126. return new TileEntityCanvas();
  127. }
  128.  
  129. /**
  130. * When placed it will update the colour to that of the tile entity which
  131. * the tile entity inherits from the item block
  132. */
  133. @Override
  134. public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer,
  135. ItemStack stack) {
  136. if (!world.isRemote) {
  137. TileEntityCanvas canvas = (TileEntityCanvas) world.getTileEntity(pos);
  138. if (canvas != null && stack.hasTagCompound() && stack.getTagCompound().hasKey("colour"))
  139. canvas.setColour(stack.getTagCompound().getInteger("colour"));
  140. }
  141. Minecraft.getMinecraft().renderGlobal.markBlockRangeForRenderUpdate(pos.getX(), pos.getY(), pos.getZ(), pos.getX(), pos.getY(), pos.getZ());
  142. }
  143.  
  144. //The following code is based off of BlockFlowerPot
  145.  
  146. /**
  147. * Will now drop the block the same colour as in the {@link TileEntityCanvas}
  148. */
  149. @Override
  150. public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
  151. List<ItemStack> drops = new ArrayList<ItemStack>();
  152. TileEntityCanvas te = world.getTileEntity(pos) instanceof TileEntityCanvas
  153. ? (TileEntityCanvas) world.getTileEntity(pos) : null;
  154. if (te != null) {
  155. ItemStack stack = new ItemStack(this);
  156. NBTTagCompound nbt = new NBTTagCompound();
  157. nbt.setInteger("colour", te.getColour());
  158. stack.setTagCompound(nbt);
  159. drops.add(stack);
  160. }
  161. return drops;
  162. }
  163.  
  164. /**
  165. * Allows the {@link TileEntity} to be deleted after get drops is called
  166. */
  167. @Override
  168. public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player,
  169. boolean willHarvest) {
  170. if (willHarvest)
  171. return true; // If it will harvest, delay deletion of the block
  172. // until after getDrops
  173. return super.removedByPlayer(state, world, pos, player, willHarvest);
  174. }
  175.  
  176. /**
  177. * Harvests the block correctly
  178. */
  179. @Override
  180. public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te,
  181. ItemStack tool) {
  182. super.harvestBlock(world, player, pos, state, te, tool);
  183. world.setBlockToAir(pos);
  184. }
  185.  
  186. public int quantityDropped(Random random)
  187. {
  188. return 0;
  189. }
  190.  
  191. @SideOnly(Side.CLIENT)
  192. public BlockRenderLayer getBlockLayer()
  193. {
  194. return BlockRenderLayer.CUTOUT;
  195. }
  196.  
  197. public boolean isFullCube(IBlockState state)
  198. {
  199. return false;
  200. }
  201.  
  202. protected boolean canSilkHarvest()
  203. {
  204. return true;
  205. }
  206.  
  207. public boolean isOpaqueCube()
  208. {
  209. return false;
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement