Advertisement
Guest User

Untitled

a guest
Jul 1st, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. import cpw.mods.fml.common.Optional;
  2. import li.cil.oc.api.network.Arguments;
  3. import li.cil.oc.api.network.Callback;
  4. import li.cil.oc.api.network.Context;
  5. import li.cil.oc.api.network.SimpleComponent;
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.inventory.IInventory;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.nbt.NBTTagList;
  11. import net.minecraft.tileentity.TileEntity;
  12.  
  13. @Optional.Interface(iface = "li.cil.oc.api.network.SimpleComponent", modid = "OpenComputers")
  14. public class InventoryTileEntity extends TileEntity implements IInventory,SimpleComponent {
  15. private ItemStack[] inv;
  16.  
  17. public InventoryTileEntity(){
  18. inv = new ItemStack[9];
  19. }
  20.  
  21. @Override
  22. public int getSizeInventory() {
  23. return inv.length;
  24. }
  25.  
  26. @Override
  27. public ItemStack getStackInSlot(int slot) {
  28. return inv[slot];
  29. }
  30.  
  31. @Override
  32. public void setInventorySlotContents(int slot, ItemStack stack) {
  33. inv[slot] = stack;
  34. if (stack != null && stack.stackSize > getInventoryStackLimit()) {
  35. stack.stackSize = getInventoryStackLimit();
  36. }
  37. }
  38.  
  39. @Override
  40. public ItemStack decrStackSize(int slot, int amt) {
  41. ItemStack stack = getStackInSlot(slot);
  42. if (stack != null) {
  43. if (stack.stackSize <= amt) {
  44. setInventorySlotContents(slot, null);
  45. } else {
  46. ItemStack retstack = stack.splitStack(amt);
  47. if (stack.stackSize == 0) {
  48. setInventorySlotContents(slot, null);
  49. }
  50. stack = retstack;
  51. }
  52. }
  53. return stack;
  54. }
  55.  
  56. @Override
  57. public ItemStack getStackInSlotOnClosing(int slot) {
  58. ItemStack stack = getStackInSlot(slot);
  59. if (stack != null) {
  60. setInventorySlotContents(slot, null);
  61. }
  62. return stack;
  63. }
  64.  
  65. @Override
  66. public int getInventoryStackLimit() {
  67. return 64;
  68. }
  69.  
  70. @Override
  71. public boolean isUseableByPlayer(EntityPlayer player) {
  72. return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
  73. player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
  74. }
  75.  
  76. @Override
  77. public void openChest() {}
  78.  
  79. @Override
  80. public void closeChest() {}
  81.  
  82. @Override
  83. public void readFromNBT(NBTTagCompound tagCompound) {
  84. super.readFromNBT(tagCompound);
  85.  
  86. NBTTagList tagList = tagCompound.getTagList("Inventory");
  87. for (int i = 0; i < tagList.tagCount(); i++) {
  88. NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
  89. byte slot = tag.getByte("Slot");
  90. if (slot >= 0 && slot < inv.length) {
  91. inv[slot] = ItemStack.loadItemStackFromNBT(tag);
  92. }
  93. }
  94. }
  95.  
  96. @Override
  97. public void writeToNBT(NBTTagCompound tagCompound) {
  98. super.writeToNBT(tagCompound);
  99.  
  100. NBTTagList itemList = new NBTTagList();
  101. for (int i = 0; i < inv.length; i++) {
  102. ItemStack stack = inv[i];
  103. if (stack != null) {
  104. NBTTagCompound tag = new NBTTagCompound();
  105. tag.setByte("Slot", (byte) i);
  106. stack.writeToNBT(tag);
  107. itemList.appendTag(tag);
  108. }
  109. }
  110. tagCompound.setTag("Inventory", itemList);
  111. }
  112.  
  113. @Override
  114. public String getInvName() {
  115. return "openinventory.inventorytileentity";
  116. }
  117.  
  118. @Override
  119. public boolean isInvNameLocalized() {
  120. // TODO Auto-generated method stub
  121. return false;
  122. }
  123.  
  124. @Override
  125. public boolean isItemValidForSlot(int i, ItemStack itemstack) {
  126. // TODO Auto-generated method stub
  127. return true;
  128. }
  129.  
  130. @Override
  131. public String getComponentName() {
  132. return "computerizedInventory";
  133. }
  134.  
  135. @Callback
  136. @Optional.Method(modid = "OpenComputers")
  137. public Object[] greet(Context context, Arguments args) {
  138. return new Object[]{String.format("Hello, %s!", args.checkString(0))};
  139. }
  140.  
  141. @Callback
  142. @Optional.Method(modid = "OpenComputers")
  143. public Object[] GetInventory(Context context, Arguments args) {
  144. return null;
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement