PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/thaumic/tinkerer/common/item/kami/ItemBlockTalisman.java

https://gitlab.com/MineYourMind/ThaumicTinkerer
Java | 315 lines | 254 code | 47 blank | 14 comment | 76 complexity | 344ac2b7df44be04273af969967f6548 MD5 | raw file
  1. /**
  2. * This class was created by <Vazkii>. It's distributed as
  3. * part of the ThaumicTinkerer Mod.
  4. *
  5. * ThaumicTinkerer is Open Source and distributed under a
  6. * Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
  7. * (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
  8. *
  9. * ThaumicTinkerer is a Derivative Work on Thaumcraft 4.
  10. * Thaumcraft 4 (c) Azanor 2012
  11. * (http://www.minecraftforum.net/topic/1585216-)
  12. *
  13. * File Created @ [Dec 30, 2013, 12:46:22 AM (GMT)]
  14. */
  15. package thaumic.tinkerer.common.item.kami;
  16. import baubles.api.BaubleType;
  17. import baubles.api.IBauble;
  18. import cpw.mods.fml.relauncher.Side;
  19. import cpw.mods.fml.relauncher.SideOnly;
  20. import net.minecraft.block.Block;
  21. import net.minecraft.client.renderer.texture.IIconRegister;
  22. import net.minecraft.entity.EntityLivingBase;
  23. import net.minecraft.entity.player.EntityPlayer;
  24. import net.minecraft.init.Blocks;
  25. import net.minecraft.init.Items;
  26. import net.minecraft.inventory.IInventory;
  27. import net.minecraft.inventory.ISidedInventory;
  28. import net.minecraft.item.EnumRarity;
  29. import net.minecraft.item.Item;
  30. import net.minecraft.item.ItemStack;
  31. import net.minecraft.tileentity.TileEntity;
  32. import net.minecraft.util.IIcon;
  33. import net.minecraft.util.StatCollector;
  34. import net.minecraft.world.World;
  35. import thaumcraft.api.aspects.Aspect;
  36. import thaumcraft.api.aspects.AspectList;
  37. import thaumcraft.api.research.ResearchPage;
  38. import thaumcraft.common.config.ConfigBlocks;
  39. import thaumcraft.common.config.ConfigItems;
  40. import thaumic.tinkerer.client.core.helper.IconHelper;
  41. import thaumic.tinkerer.common.ThaumicTinkerer;
  42. import thaumic.tinkerer.common.block.tile.transvector.TileTransvectorInterface;
  43. import thaumic.tinkerer.common.core.helper.ItemNBTHelper;
  44. import thaumic.tinkerer.common.core.proxy.TTCommonProxy;
  45. import thaumic.tinkerer.common.lib.LibItemNames;
  46. import thaumic.tinkerer.common.lib.LibResearch;
  47. import thaumic.tinkerer.common.registry.ItemKamiBase;
  48. import thaumic.tinkerer.common.registry.ThaumicTinkererInfusionRecipe;
  49. import thaumic.tinkerer.common.registry.ThaumicTinkererRecipe;
  50. import thaumic.tinkerer.common.research.IRegisterableResearch;
  51. import thaumic.tinkerer.common.research.KamiResearchItem;
  52. import thaumic.tinkerer.common.research.ResearchHelper;
  53. import java.util.Arrays;
  54. import java.util.List;
  55. public class ItemBlockTalisman extends ItemKamiBase implements IBauble {
  56. @Deprecated
  57. private static final String TAG_BLOCK_ID = "blockID";
  58. private static final String TAG_BLOCK_NAME = "blockName";
  59. private static final String TAG_BLOCK_META = "blockMeta";
  60. private static final String TAG_BLOCK_COUNT = "blockCount";
  61. IIcon enabledIcon;
  62. public ItemBlockTalisman() {
  63. super();
  64. setMaxStackSize(1);
  65. setHasSubtypes(true);
  66. }
  67. private static void setCount(ItemStack stack, int count) {
  68. ItemNBTHelper.setInt(stack, TAG_BLOCK_COUNT, count);
  69. }
  70. public static int remove(ItemStack stack, int count) {
  71. int current = getBlockCount(stack);
  72. setCount(stack, Math.max(current - count, 0));
  73. return Math.min(current, count);
  74. }
  75. @Deprecated
  76. public static int getBlockID(ItemStack stack) {
  77. return ItemNBTHelper.getInt(stack, TAG_BLOCK_ID, 0);
  78. }
  79. public static String getBlockName(ItemStack stack) {
  80. return ItemNBTHelper.getString(stack, TAG_BLOCK_NAME, "");
  81. }
  82. public static Block getBlock(ItemStack stack) {
  83. Block block = Block.getBlockFromName(getBlockName(stack));
  84. if (block == Blocks.air)
  85. block = Block.getBlockById(getBlockID(stack));
  86. return block;
  87. }
  88. public static int getBlockMeta(ItemStack stack) {
  89. return ItemNBTHelper.getInt(stack, TAG_BLOCK_META, 0);
  90. }
  91. public static int getBlockCount(ItemStack stack) {
  92. return ItemNBTHelper.getInt(stack, TAG_BLOCK_COUNT, 0);
  93. }
  94. @Override
  95. public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) {
  96. if ((getBlock(par1ItemStack) != Blocks.air || getBlockID(par1ItemStack) != 0) && par3EntityPlayer.isSneaking()) {
  97. int dmg = par1ItemStack.getItemDamage();
  98. par1ItemStack.setItemDamage(~dmg & 1);
  99. par2World.playSoundAtEntity(par3EntityPlayer, "random.orb", 0.3F, 0.1F);
  100. }
  101. return par1ItemStack;
  102. }
  103. @Override
  104. public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10) {
  105. Block block = par3World.getBlock(par4, par5, par6);
  106. int meta = par3World.getBlockMetadata(par4, par5, par6);
  107. boolean set = setBlock(par1ItemStack, block, meta);
  108. if (!set) {
  109. Block bBlock = getBlock(par1ItemStack);
  110. int bmeta = getBlockMeta(par1ItemStack);
  111. TileEntity tile = par3World.getTileEntity(par4, par5, par6);
  112. if (tile != null && tile instanceof IInventory) {
  113. IInventory inv = (IInventory) tile;
  114. int[] slots = inv instanceof ISidedInventory ? ((ISidedInventory) inv).getAccessibleSlotsFromSide(par7) : TileTransvectorInterface.buildSlotsForLinearInventory(inv);
  115. for (int slot : slots) {
  116. ItemStack stackInSlot = inv.getStackInSlot(slot);
  117. if (stackInSlot == null) {
  118. ItemStack stack = new ItemStack(bBlock, 1, bmeta);
  119. int maxSize = stack.getMaxStackSize();
  120. stack.stackSize = remove(par1ItemStack, maxSize);
  121. if (stack.stackSize != 0) {
  122. if (inv.isItemValidForSlot(slot, stack) && (!(inv instanceof ISidedInventory) || ((ISidedInventory) inv).canInsertItem(slot, stack, par7))) {
  123. inv.setInventorySlotContents(slot, stack);
  124. inv.markDirty();
  125. set = true;
  126. }
  127. }
  128. } else if (stackInSlot.getItem() == Item.getItemFromBlock(bBlock) && stackInSlot.getItemDamage() == bmeta) {
  129. int maxSize = stackInSlot.getMaxStackSize();
  130. int missing = maxSize - stackInSlot.stackSize;
  131. if (inv.isItemValidForSlot(slot, stackInSlot) && (!(inv instanceof ISidedInventory) || ((ISidedInventory) inv).canInsertItem(slot, stackInSlot, par7))) {
  132. stackInSlot.stackSize += remove(par1ItemStack, missing);
  133. inv.markDirty();
  134. set = true;
  135. }
  136. }
  137. }
  138. } else {
  139. int remove = remove(par1ItemStack, 1);
  140. if (remove > 0) {
  141. Item.getItemFromBlock(bBlock).onItemUse(new ItemStack(bBlock, 1, bmeta), par2EntityPlayer, par3World, par4, par5, par6, par7, par8, par9, par10);
  142. set = true;
  143. }
  144. }
  145. }
  146. par2EntityPlayer.setCurrentItemOrArmor(0, par1ItemStack);
  147. return set;
  148. }
  149. private boolean setBlock(ItemStack stack, Block block, int meta) {
  150. if (getBlock(stack) == Blocks.air || getBlockCount(stack) == 0) {
  151. ItemNBTHelper.setString(stack, TAG_BLOCK_NAME, Block.blockRegistry.getNameForObject(block));
  152. ItemNBTHelper.setInt(stack, TAG_BLOCK_META, meta);
  153. return true;
  154. }
  155. return false;
  156. }
  157. private void add(ItemStack stack, int count) {
  158. int current = getBlockCount(stack);
  159. setCount(stack, current + count);
  160. }
  161. @Override
  162. @SideOnly(Side.CLIENT)
  163. public void registerIcons(IIconRegister par1IconRegister) {
  164. itemIcon = IconHelper.forItem(par1IconRegister, this, 0);
  165. enabledIcon = IconHelper.forItem(par1IconRegister, this, 1);
  166. }
  167. @Override
  168. @SideOnly(Side.CLIENT)
  169. public IIcon getIconFromDamage(int par1) {
  170. return par1 == 1 ? enabledIcon : itemIcon;
  171. }
  172. @Override
  173. @SideOnly(Side.CLIENT)
  174. public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4) {
  175. Block block = getBlock(par1ItemStack);
  176. if (block != null && block != Blocks.air) {
  177. int count = getBlockCount(par1ItemStack);
  178. par3List.add(StatCollector.translateToLocal(new ItemStack(block, 1, getBlockMeta(par1ItemStack)).getUnlocalizedName() + ".name") + " (x" + count + ")");
  179. }
  180. if (par1ItemStack.getItemDamage() == 1)
  181. par3List.add(StatCollector.translateToLocal("ttmisc.active"));
  182. else par3List.add(StatCollector.translateToLocal("ttmisc.inactive"));
  183. }
  184. @Override
  185. public EnumRarity getRarity(ItemStack par1ItemStack) {
  186. return TTCommonProxy.kamiRarity;
  187. }
  188. @Override
  189. public BaubleType getBaubleType(ItemStack itemstack) {
  190. return BaubleType.RING;
  191. }
  192. @Override
  193. public void onWornTick(ItemStack itemstack, EntityLivingBase entity) {
  194. Block block = getBlock(itemstack);
  195. if (!entity.worldObj.isRemote && itemstack.getItemDamage() == 1 && block != Blocks.air && entity instanceof EntityPlayer) {
  196. EntityPlayer player = (EntityPlayer) entity;
  197. int meta = getBlockMeta(itemstack);
  198. int highest = -1;
  199. boolean hasFreeSlot = false;
  200. int[] counts = new int[player.inventory.getSizeInventory() - player.inventory.armorInventory.length];
  201. Arrays.fill(counts, 0);
  202. for (int i = 0; i < counts.length; i++) {
  203. ItemStack stack = player.inventory.getStackInSlot(i);
  204. if (stack == null) {
  205. hasFreeSlot = true;
  206. continue;
  207. }
  208. if (Item.getItemFromBlock(block) == stack.getItem() && stack.getItemDamage() == meta) {
  209. counts[i] = stack.stackSize;
  210. if (highest == -1)
  211. highest = i;
  212. else highest = counts[i] > counts[highest] && highest > 8 ? i : highest;
  213. }
  214. }
  215. if (highest == -1) {
  216. ItemStack heldItem = player.inventory.getItemStack();
  217. if (hasFreeSlot && (heldItem == null || Item.getItemFromBlock(block) == heldItem.getItem() || heldItem.getItemDamage() != meta)) {
  218. ItemStack stack = new ItemStack(block, remove(itemstack, 64), meta);
  219. if (stack.stackSize != 0)
  220. player.inventory.addItemStackToInventory(stack);
  221. }
  222. } else {
  223. for (int i = 0; i < counts.length; i++) {
  224. int count = counts[i];
  225. if (i == highest || count == 0)
  226. continue;
  227. add(itemstack, count);
  228. player.inventory.setInventorySlotContents(i, null);
  229. }
  230. int countInHighest = counts[highest];
  231. int maxSize = new ItemStack(block, 1, meta).getMaxStackSize();
  232. if (countInHighest < maxSize) {
  233. int missing = maxSize - countInHighest;
  234. ItemStack stackInHighest = player.inventory.getStackInSlot(highest);
  235. stackInHighest.stackSize += remove(itemstack, missing);
  236. }
  237. }
  238. }
  239. }
  240. @Override
  241. public void onEquipped(ItemStack itemstack, EntityLivingBase player) {
  242. }
  243. @Override
  244. public void onUnequipped(ItemStack itemstack, EntityLivingBase player) {
  245. }
  246. @Override
  247. public boolean canEquip(ItemStack itemstack, EntityLivingBase player) {
  248. return true;
  249. }
  250. @Override
  251. public boolean canUnequip(ItemStack itemstack, EntityLivingBase player) {
  252. return true;
  253. }
  254. @Override
  255. public String getItemName() {
  256. return LibItemNames.BLOCK_TALISMAN;
  257. }
  258. @Override
  259. public IRegisterableResearch getResearchItem() {
  260. return (IRegisterableResearch) new KamiResearchItem(LibResearch.KEY_BLOCK_TALISMAN, new AspectList().add(Aspect.VOID, 2).add(Aspect.DARKNESS, 1).add(Aspect.ELDRITCH, 1).add(Aspect.MAGIC, 1), 14, 17, 5, new ItemStack(this)).setParents(LibResearch.KEY_ICHOR_PICK_GEM, LibResearch.KEY_ICHOR_SHOVEL_GEM)
  261. .setPages(new ResearchPage("0"), ResearchHelper.infusionPage(LibResearch.KEY_BLOCK_TALISMAN));
  262. }
  263. @Override
  264. public ThaumicTinkererRecipe getRecipeItem() {
  265. return new ThaumicTinkererInfusionRecipe(LibResearch.KEY_BLOCK_TALISMAN, new ItemStack(this), 9, new AspectList().add(Aspect.VOID, 65).add(Aspect.DARKNESS, 32).add(Aspect.MAGIC, 50).add(Aspect.ELDRITCH, 32), new ItemStack(ConfigItems.itemFocusPortableHole),
  266. new ItemStack(ThaumicTinkerer.registry.getFirstItemFromClass(ItemKamiResource.class)), new ItemStack(Blocks.ender_chest), new ItemStack(Items.diamond), new ItemStack(ThaumicTinkerer.registry.getFirstItemFromClass(ItemKamiResource.class)), new ItemStack(ConfigItems.itemResource, 1, 11), new ItemStack(ConfigBlocks.blockJar, 1, 3));
  267. }
  268. }