Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. package panda.glassworks.gui;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import javax.annotation.Nullable;
  7.  
  8. import panda.glassworks.init.Recipes;
  9. import panda.glassworks.util.GlassBlowingRecipes;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.entity.player.InventoryPlayer;
  12. import net.minecraft.init.Blocks;
  13. import net.minecraft.inventory.ClickType;
  14. import net.minecraft.inventory.Container;
  15. import net.minecraft.inventory.IInventory;
  16. import net.minecraft.inventory.InventoryBasic;
  17. import net.minecraft.inventory.InventoryCraftResult;
  18. import net.minecraft.inventory.InventoryCrafting;
  19. import net.minecraft.inventory.ItemStackHelper;
  20. import net.minecraft.inventory.Slot;
  21. import net.minecraft.inventory.SlotCrafting;
  22. import net.minecraft.item.ItemStack;
  23. import net.minecraft.item.crafting.CraftingManager;
  24. import net.minecraft.util.math.BlockPos;
  25. import net.minecraft.world.World;
  26.  
  27. import javax.annotation.Nullable;
  28.  
  29. import net.minecraft.entity.player.EntityPlayer;
  30. import net.minecraft.item.ItemStack;
  31. import net.minecraft.util.text.ITextComponent;
  32. import net.minecraft.util.text.TextComponentString;
  33. import net.minecraft.util.text.TextComponentTranslation;
  34.  
  35. public class ContainerBlowpipe extends Container{
  36.  
  37. /** The crafting matrix inventory (3x3). */
  38. private final IInventory outputSlot;
  39. /** The 2slots where you put your items in that you want to merge and/or rename. */
  40. public final IInventory inputSlot;
  41. private final World worldObj;
  42. /** Position of the workbench */
  43. private final BlockPos pos;
  44.  
  45. private int selection;
  46.  
  47. public ContainerBlowpipe(InventoryPlayer playerInventory, World worldIn, BlockPos posIn)
  48. {
  49.  
  50. this.inputSlot = new InventoryBasic("Blowpipe", true, 1);
  51. this.outputSlot = new InventoryResult();
  52. this.selection = 0;
  53.  
  54. this.addSlotToContainer(new Slot(this.inputSlot, 0, 110, 22));
  55. this.addSlotToContainer(new Slot(this.outputSlot, 1, 138, 22));
  56.  
  57. this.worldObj = worldIn;
  58. this.pos = posIn;
  59.  
  60.  
  61. for (int i = 0; i < 3; ++i)
  62. {
  63. for (int j = 0; j < 9; ++j)
  64. {
  65. this.addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18-26));
  66. }
  67. }
  68.  
  69. for (int k = 0; k < 9; ++k)
  70. {
  71. this.addSlotToContainer(new Slot(playerInventory, k, 8 + k * 18, 142-26));
  72. }
  73.  
  74. this.onCraftMatrixChanged();
  75. }
  76.  
  77. @Override
  78. public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn,EntityPlayer player) {
  79. System.out.println("DOOT");
  80. this.onCraftMatrixChanged();
  81. return super.slotClick(slotId, dragType, clickTypeIn, player);
  82. }
  83.  
  84. @Override
  85. public void putStackInSlot(int slotID, ItemStack stack) {
  86. // TODO Auto-generated method stub
  87. super.putStackInSlot(slotID, stack);
  88. }
  89.  
  90. @Override
  91. public void setCanCraft(EntityPlayer player, boolean canCraft) {
  92. // TODO Auto-generated method stub
  93. super.setCanCraft(player, canCraft);
  94. }
  95.  
  96. public void onCraftMatrixChanged()
  97. {
  98. if(hasInput()){
  99. List<ItemStack> recipes = GlassBlowingRecipes.getGlassBlowingResultList(inputStack());
  100. if(!recipes.isEmpty()){
  101. if(selection < recipes.size()){
  102. if(recipes.get(selection) != null){
  103. System.out.println(selection);
  104. System.out.println(recipes.get(selection));
  105. this.putStackInSlot(1, recipes.get(selection));
  106. }else{
  107. this.outputSlot.clear();
  108. }
  109. }else{
  110. this.outputSlot.clear();
  111. this.selection = recipes.size()-1;
  112. }
  113.  
  114. }else{
  115. this.outputSlot.clear();
  116. }
  117. }else{
  118. this.outputSlot.clear();
  119. }
  120. }
  121.  
  122. boolean hasInput(){
  123. return (this.inputSlot.getStackInSlot(0) != null);
  124. }
  125.  
  126. ItemStack inputStack(){
  127. return (this.inputSlot.getStackInSlot(0));
  128. }
  129.  
  130. public void setCurrentRecipeIndex(int currentRecipeIndexIn)
  131. {
  132.  
  133. if (hasInput())
  134. {
  135. List<ItemStack> recipes = GlassBlowingRecipes.getGlassBlowingResultList(inputStack());
  136. if(!recipes.isEmpty()){
  137. if(recipes.get(selection) != null){
  138. if(currentRecipeIndexIn > GlassBlowingRecipes.getGlassBlowingResultList(inputStack()).size()-1){
  139. this.selection = GlassBlowingRecipes.getGlassBlowingResultList(inputStack()).size();
  140. }else{
  141. this.selection = currentRecipeIndexIn;
  142. }
  143. if (this.selection < 0)
  144. {
  145. this.selection = 0;
  146. }else{
  147. this.selection = currentRecipeIndexIn;
  148. }
  149. }else{
  150. this.outputSlot.clear();
  151. this.selection =0;
  152. }
  153. }else{
  154. this.outputSlot.clear();
  155. this.selection =0;
  156. }
  157. }else{
  158. this.outputSlot.clear();
  159. this.selection =0;
  160. }
  161.  
  162. onCraftMatrixChanged();
  163. }
  164.  
  165. @Override
  166. public void detectAndSendChanges() {
  167.  
  168. super.detectAndSendChanges();
  169. }
  170.  
  171. @Override
  172. protected void retrySlotClick(int slotId, int clickedButton, boolean mode,EntityPlayer playerIn) {
  173.  
  174.  
  175. super.retrySlotClick(slotId, clickedButton, mode, playerIn);
  176. }
  177.  
  178. /**
  179. * Called when the container is closed.
  180. */
  181. public void onContainerClosed(EntityPlayer playerIn)
  182. {
  183. super.onContainerClosed(playerIn);
  184.  
  185. if (!this.worldObj.isRemote)
  186. {
  187.  
  188. ItemStack itemstack = this.inputSlot.removeStackFromSlot(0);
  189.  
  190. if (itemstack != null)
  191. {
  192. playerIn.dropItem(itemstack, false);
  193. }
  194. }
  195. }
  196.  
  197. public boolean canInteractWith(EntityPlayer playerIn)
  198. {
  199. return true;
  200. }
  201.  
  202. /**
  203. * Take a stack from the specified inventory slot.
  204. */
  205. @Nullable
  206. public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
  207. {
  208. ItemStack itemstack = null;
  209. Slot slot = (Slot)this.inventorySlots.get(index);
  210.  
  211. if (slot != null && slot.getHasStack())
  212. {
  213. ItemStack itemstack1 = slot.getStack();
  214. itemstack = itemstack1.copy();
  215.  
  216. if (index == 0)
  217. {
  218. if (!this.mergeItemStack(itemstack1, 2, 38, true))
  219. {
  220. return null;
  221. }
  222.  
  223. slot.onSlotChange(itemstack1, itemstack);
  224. }
  225. else if (index >= 2 && index < 29)
  226. {
  227. if (!this.mergeItemStack(itemstack1, 29, 38, false))
  228. {
  229. return null;
  230. }
  231. }
  232. else if (index >= 29 && index < 38)
  233. {
  234. if (!this.mergeItemStack(itemstack1, 2, 29, false))
  235. {
  236. return null;
  237. }
  238. }
  239. else if (!this.mergeItemStack(itemstack1, 2, 38, false))
  240. {
  241. return null;
  242. }
  243.  
  244. if (itemstack1.stackSize == 0)
  245. {
  246. slot.putStack((ItemStack)null);
  247. }
  248. else
  249. {
  250. slot.onSlotChanged();
  251. }
  252.  
  253. if (itemstack1.stackSize == itemstack.stackSize)
  254. {
  255. return null;
  256. }
  257.  
  258. slot.onPickupFromSlot(playerIn, itemstack1);
  259. }
  260.  
  261. return itemstack;
  262. }
  263.  
  264. /**
  265. * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
  266. * is null for the initial slot that was double-clicked.
  267. */
  268. public boolean canMergeSlot(ItemStack stack, Slot slotIn)
  269. {
  270. return slotIn.inventory != this.outputSlot && super.canMergeSlot(stack, slotIn);
  271. }
  272.  
  273. static class InventoryResult implements IInventory
  274. {
  275. /** A list of one item containing the result of the crafting formula */
  276. private final ItemStack[] stackResult = new ItemStack[1];
  277.  
  278. /**
  279. * Returns the number of slots in the inventory.
  280. */
  281. public int getSizeInventory()
  282. {
  283. return 1;
  284. }
  285.  
  286. /**
  287. * Returns the stack in the given slot.
  288. */
  289. @Nullable
  290. public ItemStack getStackInSlot(int index)
  291. {
  292. return this.stackResult[0];
  293. }
  294.  
  295. /**
  296. * Get the name of this object. For players this returns their username
  297. */
  298. public String getName()
  299. {
  300. return "Result";
  301. }
  302.  
  303. /**
  304. * Returns true if this thing is named
  305. */
  306. public boolean hasCustomName()
  307. {
  308. return false;
  309. }
  310.  
  311. /**
  312. * Get the formatted ChatComponent that will be used for the sender's username in chat
  313. */
  314. public ITextComponent getDisplayName()
  315. {
  316. return (ITextComponent)(this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName(), new Object[0]));
  317. }
  318.  
  319. /**
  320. * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
  321. */
  322. @Nullable
  323. public ItemStack decrStackSize(int index, int count)
  324. {
  325. return ItemStackHelper.getAndRemove(this.stackResult, 0);
  326. }
  327.  
  328. /**
  329. * Removes a stack from the given slot and returns it.
  330. */
  331. @Nullable
  332. public ItemStack removeStackFromSlot(int index)
  333. {
  334. return ItemStackHelper.getAndRemove(this.stackResult, 0);
  335. }
  336.  
  337. /**
  338. * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
  339. */
  340. public void setInventorySlotContents(int index, @Nullable ItemStack stack)
  341. {
  342. this.stackResult[0] = stack;
  343. }
  344.  
  345. /**
  346. * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
  347. */
  348. public int getInventoryStackLimit()
  349. {
  350. return 64;
  351. }
  352.  
  353. /**
  354. * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
  355. * hasn't changed and skip it.
  356. */
  357. public void markDirty()
  358. {
  359. }
  360.  
  361. /**
  362. * Do not make give this method the name canInteractWith because it clashes with Container
  363. */
  364. public boolean isUseableByPlayer(EntityPlayer player)
  365. {
  366. return false;
  367. }
  368.  
  369. public void openInventory(EntityPlayer player)
  370. {
  371. }
  372.  
  373. public void closeInventory(EntityPlayer player)
  374. {
  375. }
  376.  
  377. /**
  378. * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  379. */
  380. public boolean isItemValidForSlot(int index, ItemStack stack)
  381. {
  382. return true;
  383. }
  384.  
  385. public int getField(int id)
  386. {
  387. return 0;
  388. }
  389.  
  390. public void setField(int id, int value)
  391. {
  392. }
  393.  
  394. public int getFieldCount()
  395. {
  396. return 0;
  397. }
  398.  
  399. public void clear()
  400. {
  401. for (int i = 0; i < this.stackResult.length; ++i)
  402. {
  403. this.stackResult[i] = null;
  404. }
  405. }
  406. }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement