Guest User

Untitled

a guest
Jul 1st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. package com.minecolonies.coremod.blocks;
  2.  
  3. import com.minecolonies.api.util.constant.Constants;
  4. import com.minecolonies.coremod.creativetab.ModCreativeTabs;
  5. import com.minecolonies.coremod.tileentities.TileEntityRack;
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.BlockHorizontal;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.block.properties.PropertyDirection;
  10. import net.minecraft.block.properties.PropertyEnum;
  11. import net.minecraft.block.state.BlockStateContainer;
  12. import net.minecraft.block.state.IBlockState;
  13. import net.minecraft.creativetab.CreativeTabs;
  14. import net.minecraft.entity.EntityLivingBase;
  15. import net.minecraft.entity.player.EntityPlayer;
  16. import net.minecraft.item.Item;
  17. import net.minecraft.item.ItemBlock;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.tileentity.TileEntity;
  20. import net.minecraft.util.*;
  21. import net.minecraft.util.math.BlockPos;
  22. import net.minecraft.util.math.RayTraceResult;
  23. import net.minecraft.world.IBlockAccess;
  24. import net.minecraft.world.World;
  25. import net.minecraftforge.fml.common.registry.GameRegistry;
  26. import net.minecraftforge.fml.relauncher.Side;
  27. import net.minecraftforge.fml.relauncher.SideOnly;
  28. import org.jetbrains.annotations.NotNull;
  29.  
  30. import java.util.ArrayList;
  31. import java.util.List;
  32.  
  33. /**
  34. * Block for the shelves of the warehouse.
  35. */
  36. public class BlockRack extends Block
  37. {
  38. public static final PropertyEnum<BlockRack.EnumType> VARIANT = PropertyEnum.<BlockRack.EnumType>create("variant", BlockRack.EnumType.class);
  39. public static final int DEFAULT_META = BlockRack.EnumType.DEFAULT.getMetadata();
  40. public static final int FULL_META = EnumType.FULL.getMetadata();
  41.  
  42. /**
  43. * The position it faces.
  44. */
  45. public static final PropertyDirection FACING = BlockHorizontal.FACING;
  46.  
  47. /**
  48. * The hardness this block has.
  49. */
  50. private static final float BLOCK_HARDNESS = 10.0F;
  51.  
  52. /**
  53. * This blocks name.
  54. */
  55. private static final String BLOCK_NAME = "blockRack";
  56.  
  57. /**
  58. * The resistance this block has.
  59. */
  60. private static final float RESISTANCE = Float.POSITIVE_INFINITY;
  61.  
  62. /**
  63. * How much light goes through the block.
  64. */
  65. private static final int LIGHT_OPACITY = 0;
  66.  
  67. public BlockRack()
  68. {
  69. super(Material.WOOD);
  70. initBlock();
  71. }
  72.  
  73. /**
  74. * initialize the block
  75. * sets the creative tab, as well as the resistance and the hardness.
  76. */
  77. private void initBlock()
  78. {
  79. setRegistryName(BLOCK_NAME);
  80. setUnlocalizedName(String.format("%s.%s", Constants.MOD_ID.toLowerCase(), BLOCK_NAME));
  81. setCreativeTab(ModCreativeTabs.MINECOLONIES);
  82. GameRegistry.register(this);
  83. GameRegistry.register((new ItemBlock(this)).setRegistryName(this.getRegistryName()));
  84. this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(VARIANT, EnumType.DEFAULT));
  85. setHardness(BLOCK_HARDNESS);
  86. setResistance(RESISTANCE);
  87. setLightOpacity(LIGHT_OPACITY);
  88. }
  89.  
  90. @Override
  91. public void getSubBlocks(final Item itemIn, final CreativeTabs tab, final List<ItemStack> list)
  92. {
  93. list.add(new ItemStack(this, 1, EnumType.DEFAULT.getMetadata()));
  94. list.add(new ItemStack(this, 1, EnumType.FULL.getMetadata()));
  95. }
  96.  
  97. @Override
  98. public void onBlockPlacedBy(
  99. final World worldIn, final BlockPos pos, final IBlockState state, final EntityLivingBase placer, final ItemStack stack)
  100. {
  101. IBlockState tempState = state;
  102. tempState = tempState.withProperty(VARIANT, EnumType.byMetadata(stack.getItemDamage()));
  103. tempState = tempState.withProperty(FACING, placer.getHorizontalFacing().getOpposite());
  104.  
  105. worldIn.setBlockState(pos, tempState, 2);
  106. }
  107.  
  108. /**
  109. * @Deprecated but we still need this because there is nothing better.
  110. * @param meta the metadata.
  111. * @return the state.
  112. */
  113. @Deprecated
  114. @Override
  115. public IBlockState getStateFromMeta(int meta)
  116. {
  117. EnumType type = EnumType.byMetadata(meta / 4);
  118. EnumFacing facing = EnumFacing.getHorizontal(meta - (type.getMetadata() * 4));
  119.  
  120. return this.getDefaultState().withProperty(VARIANT, type).withProperty(FACING, facing);
  121. }
  122.  
  123. @Override
  124. public int getMetaFromState(@NotNull IBlockState state)
  125. {
  126. return (state.getValue(VARIANT).getMetadata() * 4) + state.getValue(FACING).getHorizontalIndex();
  127. }
  128.  
  129. /**
  130. * This returns a complete list of items dropped from this block.
  131. *
  132. * @param world The current world
  133. * @param pos Block position in world
  134. * @param state Current state
  135. * @param fortune Breakers fortune level
  136. * @return A ArrayList containing all items this block drops
  137. */
  138. @Override
  139. public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
  140. {
  141. List<ItemStack> drops = new ArrayList<>();
  142. EnumType type = state.getValue(VARIANT);
  143.  
  144. drops.add(new ItemStack(this, 1, type.getMetadata()));
  145.  
  146. return drops;
  147. }
  148.  
  149. /**
  150. * Called when a user uses the creative pick block button on this block
  151. *
  152. * @param target The full target the player is looking at
  153. * @param player @return A ItemStack to add to the player's inventory, empty itemstack if nothing should be added.
  154. */
  155. @Override
  156. public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player)
  157. {
  158. EnumType type = state.getValue(VARIANT);
  159.  
  160. return new ItemStack(this, 1, type.getMetadata());
  161. }
  162.  
  163. @NotNull
  164. @Override
  165. protected BlockStateContainer createBlockState()
  166. {
  167. return new BlockStateContainer(this, FACING, VARIANT);
  168. }
  169.  
  170. @Override
  171. public boolean hasTileEntity(final IBlockState state)
  172. {
  173. return true;
  174. }
  175.  
  176. @Override
  177. public TileEntity createTileEntity(final World world, final IBlockState state)
  178. {
  179. return new TileEntityRack();
  180. }
  181.  
  182. /**
  183. * @deprecated (Remove this as soon as minecraft offers anything better).
  184. */
  185. @Override
  186. @Deprecated
  187. public boolean isFullBlock(final IBlockState state)
  188. {
  189. return false;
  190. }
  191.  
  192. /**
  193. * Convert the given metadata into a BlockState for this Block.
  194. *
  195. * @deprecated (Remove this as soon as minecraft offers anything better).
  196. */
  197.  
  198. /**
  199. * Convert the BlockState into the correct metadata value.
  200. *
  201. * @deprecated (Remove this as soon as minecraft offers anything better).
  202. */
  203. @NotNull
  204. @Override
  205. @Deprecated
  206. public IBlockState withRotation(@NotNull final IBlockState state, final Rotation rot)
  207. {
  208. return state.withProperty(FACING, rot.rotate(state.getValue(FACING)));
  209. }
  210.  
  211. /**
  212. * @deprecated (Remove this as soon as minecraft offers anything better).
  213. */
  214. @NotNull
  215. @Override
  216. @Deprecated
  217. public IBlockState withMirror(@NotNull final IBlockState state, final Mirror mirrorIn)
  218. {
  219. return state.withRotation(mirrorIn.toRotation(state.getValue(FACING)));
  220. }
  221.  
  222. /**
  223. * @deprecated (Remove this as soon as minecraft offers anything better).
  224. */
  225. @Override
  226. @Deprecated
  227. public boolean isFullCube(final IBlockState state)
  228. {
  229. return false;
  230. }
  231.  
  232. /**
  233. * @deprecated (Remove this as soon as minecraft offers anything better).
  234. */
  235. @Override
  236. @Deprecated
  237. public boolean isOpaqueCube(final IBlockState state)
  238. {
  239. return false;
  240. }
  241.  
  242. @NotNull
  243. @Override
  244. @SideOnly(Side.CLIENT)
  245. public BlockRenderLayer getBlockLayer()
  246. {
  247. return BlockRenderLayer.SOLID;
  248. }
  249.  
  250. public static enum EnumType implements IStringSerializable
  251. {
  252. DEFAULT(0, "blockrackempty", "Empty"),
  253. FULL(1, "blockrackfull", "Full");
  254.  
  255. private static final BlockRack.EnumType[] META_LOOKUP = new BlockRack.EnumType[values().length];
  256. private final int meta;
  257. private final String name;
  258. private final String unlocalizedName;
  259.  
  260. EnumType(int meta, String name, String unlocalizedName)
  261. {
  262. this.meta = meta;
  263. this.name = name;
  264. this.unlocalizedName = unlocalizedName;
  265. }
  266.  
  267. public int getMetadata()
  268. {
  269. return this.meta;
  270. }
  271.  
  272. @Override
  273. public String toString()
  274. {
  275. return this.name;
  276. }
  277.  
  278. public static BlockRack.EnumType byMetadata(int meta)
  279. {
  280. int tempMeta = meta;
  281. if (tempMeta < 0 || tempMeta >= META_LOOKUP.length)
  282. {
  283. tempMeta = 0;
  284. }
  285.  
  286. return META_LOOKUP[tempMeta];
  287. }
  288.  
  289. public String getName()
  290. {
  291. return this.name;
  292. }
  293.  
  294. public String getUnlocalizedName()
  295. {
  296. return this.unlocalizedName;
  297. }
  298.  
  299. static
  300. {
  301. for (BlockRack.EnumType blockRack : values())
  302. {
  303. META_LOOKUP[blockRack.getMetadata()] = blockRack;
  304. }
  305. }
  306. }
  307. }
Add Comment
Please, Sign In to add comment