Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. package greatblitz.testmod.blocks;
  2.  
  3. import java.util.List;
  4.  
  5. import com.google.common.collect.Lists;
  6.  
  7. import greatblitz.testmod.TestMod;
  8. import greatblitz.testmod.common.GuiHandler;
  9. import greatblitz.testmod.tileentity.TileEntityTestBlock;
  10. import net.minecraft.block.BlockContainer;
  11. import net.minecraft.block.material.Material;
  12. import net.minecraft.block.state.IBlockState;
  13. import net.minecraft.entity.EntityLivingBase;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.inventory.InventoryHelper;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.util.EnumBlockRenderType;
  20. import net.minecraft.util.EnumFacing;
  21. import net.minecraft.util.EnumHand;
  22. import net.minecraft.util.math.BlockPos;
  23. import net.minecraft.world.IBlockAccess;
  24. import net.minecraft.world.World;
  25. import net.minecraftforge.items.CapabilityItemHandler;
  26. import net.minecraftforge.items.ItemStackHandler;
  27.  
  28. public class TestBlock extends BlockContainer {
  29.  
  30. public TestBlock() {
  31. super(Material.GLASS);
  32. setUnlocalizedName("testblock");
  33. setRegistryName("testblock");
  34. }
  35.  
  36. @Override
  37. public void breakBlock(World world, BlockPos pos, IBlockState blockstate) {
  38. TileEntityTestBlock te = (TileEntityTestBlock) world.getTileEntity(pos);
  39. if (te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
  40. ItemStackHandler cap = (ItemStackHandler) te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  41. for (int i = 0; i < cap.getSlots(); ++i)
  42. {
  43. ItemStack itemstack = cap.getStackInSlot(i);
  44. if (itemstack != null)
  45. {
  46. InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), itemstack);
  47. }
  48. }
  49. }
  50. super.breakBlock(world, pos, blockstate);
  51. }
  52.  
  53. @Override
  54. public EnumBlockRenderType getRenderType(IBlockState state) {
  55. return EnumBlockRenderType.MODEL;
  56. }
  57.  
  58. @Override
  59. public boolean isOpaqueCube(IBlockState state) {
  60. return false;
  61. }
  62.  
  63. @Override
  64. public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
  65. EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
  66. if (!worldIn.isRemote) {
  67. TileEntityTestBlock te = (TileEntityTestBlock) worldIn.getTileEntity(pos);
  68. if (te == null) System.out.println("not null");
  69. System.out.println("Hello!");
  70. System.out.println("Energy: " + te.getEnergyStored(null) + "/" + te.getMaxEnergyStored(null));
  71. playerIn.openGui(TestMod.testmod, GuiHandler.GUI_TILE_ENTITY_TEST_BLOCK, worldIn, pos.getX(), pos.getY(), pos.getZ());
  72. }
  73. return true;
  74. }
  75.  
  76. @Override
  77. public TileEntity createNewTileEntity(World worldIn, int meta) {
  78. return new TileEntityTestBlock();
  79. }
  80.  
  81. @Override
  82. public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
  83. {
  84. return willHarvest || super.removedByPlayer(state, world, pos, player, false);
  85. }
  86.  
  87. @Override
  88. public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
  89. {
  90. List<ItemStack> drops = Lists.newArrayList();
  91. TileEntityTestBlock te = (TileEntityTestBlock) world.getTileEntity(pos);
  92. if (te != null && te instanceof TileEntityTestBlock) {
  93. ItemStack i = new ItemStack(ModBlocks.testblock);
  94. NBTTagCompound compound = new NBTTagCompound();
  95. te.writeToNBT(compound);
  96. i.setTagCompound(compound);
  97. drops.add(i);
  98. }
  99.  
  100. return drops;
  101. }
  102.  
  103. @Override
  104. public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer,
  105. ItemStack stack) {
  106. TileEntity te = world.getTileEntity(pos);
  107. if (te != null && te instanceof TileEntityTestBlock) {
  108. if (!world.isRemote) {
  109. System.out.print("entered onBlockPlacedBy");
  110. TileEntityTestBlock ter = (TileEntityTestBlock) te;
  111. if (stack.hasTagCompound()) ter.readFromNBT(stack.getTagCompound());
  112. }
  113. }
  114.  
  115. }
  116.  
  117. @Override
  118. public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack tool)
  119. {
  120. super.harvestBlock(world, player, pos, state, te, tool);
  121. world.setBlockToAir(pos);
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement