Guest User

ContainerItemInput

a guest
Oct 1st, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. package com.awesomesauce.minecraft.digitalcraft.core.common.gui;
  2.  
  3. import com.awesomesauce.minecraft.digitalcraft.core.common.tileentity.TileEntityItemInput;
  4.  
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.entity.player.InventoryPlayer;
  7. import net.minecraft.inventory.Container;
  8. import net.minecraft.inventory.Slot;
  9. import net.minecraft.item.ItemStack;
  10.  
  11. public class ContainerItemInput extends Container {
  12.  
  13.  
  14. public TileEntityItemInput tileEntity;
  15.  
  16. public ContainerItemInput (InventoryPlayer inventoryPlayer, TileEntityItemInput te){
  17. tileEntity = te;
  18.  
  19. //the Slot constructor takes the IInventory and the slot number in that it binds to
  20. //and the x-y coordinates it resides on-screen
  21. for (int i = 0; i < 3; i++) {
  22. for (int j = 0; j < 3; j++) {
  23. addSlotToContainer(new Slot(tileEntity, j + i * 3, 62 + j * 18, 17 + i * 18));
  24. }
  25. }
  26.  
  27. //commonly used vanilla code that adds the player's inventory
  28. bindPlayerInventory(inventoryPlayer);
  29. }
  30.  
  31. @Override
  32. public boolean canInteractWith(EntityPlayer player) {
  33. return tileEntity.isUseableByPlayer(player);
  34. }
  35.  
  36.  
  37. protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
  38. for (int i = 0; i < 3; i++) {
  39. for (int j = 0; j < 9; j++) {
  40. addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
  41. 8 + j * 18, 84 + i * 18));
  42. }
  43. }
  44.  
  45. for (int i = 0; i < 9; i++) {
  46. addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
  47. }
  48. }
  49.  
  50. @Override
  51. public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
  52. ItemStack stack = null;
  53. Slot slotObject = (Slot) inventorySlots.get(slot);
  54.  
  55. //null checks and checks if the item can be stacked (maxStackSize > 1)
  56. if (slotObject != null && slotObject.getHasStack()) {
  57. ItemStack stackInSlot = slotObject.getStack();
  58. stack = stackInSlot.copy();
  59.  
  60. //merges the item into player inventory since its in the tileEntity
  61. if (slot < 9) {
  62. if (!this.mergeItemStack(stackInSlot, 0, 35, true)) {
  63. return null;
  64. }
  65. }
  66. //places it into the tileEntity is possible since its in the player inventory
  67. else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) {
  68. return null;
  69. }
  70.  
  71. if (stackInSlot.stackSize == 0) {
  72. slotObject.putStack(null);
  73. } else {
  74. slotObject.onSlotChanged();
  75. }
  76.  
  77. if (stackInSlot.stackSize == stack.stackSize) {
  78. return null;
  79. }
  80. slotObject.onPickupFromSlot(player, stackInSlot);
  81. }
  82. return stack;
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment