Guest User

ModContainer

a guest
Oct 30th, 2020
8
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ModContainer extends Container {
  2. private IInventory lowerChestInventory;
  3. private int rows;
  4. private final ModTileEntity tileEntity;
  5. public ModTileEntity getTileEntity() { return this.tileEntity; }
  6. private BlockPos blockPos;
  7. public BlockPos getBlockPos() {
  8. return this.blockPos;
  9. }
  10. private int containerInd;
  11. public int getContainerInd() {
  12. return this.containerInd;
  13. }
  14.  
  15. public void setBlockPos(ModTileEntity modTileEntity) {
  16. BlockPos blockPos1 = modTileEntity.getBlockPos();
  17. if (blockPos1 != null) {
  18. System.out.println("Working?");
  19. int containerInd1 = modTileEntity.getCapability(ModTeIDProvider.MOD_TE_ID).orElseThrow(
  20. () -> new IllegalArgumentException("LazyOptional cannot be empty!")).get()[3];
  21. modTileEntity.getCapability(ModTeIDProvider.MOD_TE_ID).ifPresent(iModTag -> iModTag.set(new int[]{blockPos1.getX(), blockPos1.getY(), blockPos1.getZ(), containerInd1}));
  22. }
  23. }
  24.  
  25. public ModContainer(int id, PlayerInventory playerInventory) {
  26. this(ModContainerTypes2.MULTI_9X3, id, playerInventory, 3);
  27. }
  28.  
  29. private ModContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, int rows) {
  30. this(type, id, playerInventory, new Inventory(9 * rows), rows);
  31. }
  32.  
  33. public ModContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, IInventory iinventory, int rows) {
  34. super(type, id);
  35. this.tileEntity = new ModTileEntity();
  36. createInventory(playerInventory, iinventory, this.tileEntity, rows);
  37. }
  38.  
  39.  
  40. public ModContainer(int id, PlayerInventory playerInventory, ModTileEntity modTileEntity) {
  41. this(ModContainerTypes2.MULTI_9X3, id, playerInventory, modTileEntity, 3);
  42. }
  43.  
  44. private ModContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, ModTileEntity modTileEntity, int rows) {
  45. this(type, id, playerInventory, new Inventory(9 * rows), modTileEntity, rows);
  46. }
  47.  
  48. public ModContainer(ContainerType<?> type, int id, PlayerInventory playerInventory, IInventory iinventory, ModTileEntity modTileEntity, int rows) {
  49. super(type, id);
  50. this.tileEntity = modTileEntity;
  51. System.out.println("TileEntity: " + modTileEntity);
  52. createInventory(playerInventory, iinventory, this.tileEntity, rows);
  53. }
  54.  
  55.  
  56. public void createInventory(PlayerInventory playerInventory, IInventory iinventory, ModTileEntity modTileEntity, int rows) {
  57. assertInventorySize(iinventory, rows * 9);
  58. this.lowerChestInventory = iinventory;
  59. iinventory.openInventory(playerInventory.player);
  60.  
  61. this.containerInd = modTileEntity.getContainerInd();
  62. this.blockPos = modTileEntity.getBlockPos();
  63. System.out.println("BlockPos container: " + this.blockPos);
  64. setBlockPos(modTileEntity);
  65.  
  66. this.rows = rows;
  67. int i = (this.rows - 4) * 18;
  68.  
  69. for(int j = 0; j < this.rows; ++j) {
  70. for(int k = 0; k < 9; ++k) {
  71. this.addSlot(new Slot(iinventory, k + j * 9, 8 + k * 18, 19 + j * 18));
  72. }
  73. }
  74. for(int l = 0; l < 3; ++l) {
  75. for(int j1 = 0; j1 < 9; ++j1) {
  76. this.addSlot(new Slot(playerInventory, j1 + l * 9 + 9, 8 + j1 * 18, 122 + l * 18 + i));
  77. }
  78. }
  79. for(int i1 = 0; i1 < 9; ++i1) {
  80. this.addSlot(new Slot(playerInventory, i1, 8 + i1 * 18, 180 + i));
  81. }
  82. }
  83.  
  84. public boolean canInteractWith(PlayerEntity playerIn) {
  85. return this.lowerChestInventory.isUsableByPlayer(playerIn);
  86. }
  87.  
  88. public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
  89. ItemStack itemstack = ItemStack.EMPTY;
  90. Slot slot = this.inventorySlots.get(index);
  91.  
  92. if (slot != null && slot.getHasStack()) {
  93. ItemStack itemstack1 = slot.getStack();
  94. itemstack = itemstack1.copy();
  95.  
  96. if (index < this.rows * 9) {
  97. if (!this.mergeItemStack(itemstack1, this.rows * 9, this.inventorySlots.size(), true)) {
  98. return ItemStack.EMPTY;
  99. }
  100. } else if (!this.mergeItemStack(itemstack1, 0, this.rows * 9, false)) {
  101. return ItemStack.EMPTY;
  102. }
  103. if (itemstack1.isEmpty()) {
  104. slot.putStack(ItemStack.EMPTY);
  105. } else {
  106. slot.onSlotChanged();
  107. }
  108. }
  109. return itemstack;
  110. }
  111.  
  112. public void onContainerClosed(PlayerEntity playerIn) {
  113. super.onContainerClosed(playerIn);
  114. this.lowerChestInventory.closeInventory(playerIn);
  115. }
  116.  
  117. public IInventory getLowerChestInventory() {
  118. return this.lowerChestInventory;
  119. }
  120. }
RAW Paste Data