Guest User

Untitled

a guest
Jul 7th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. public class CharmBagContainer extends Container {
  2.  
  3. public final SlotItemHandler handler;
  4. private final IWorldPosCallable canInteractWithCallable;
  5.  
  6. public CharmBagContainer(final int windowId, final PlayerInventory playerInventory,
  7. final SlotItemHandler handler) {
  8. super(ModContainerTypes.CHARM_BAG, windowId);
  9. this.handler = handler;
  10. this.canInteractWithCallable = IWorldPosCallable.of(tileEntity.getWorld(), tileEntity.getPos());
  11.  
  12. // Main Inventory
  13. int startX = 8;
  14. int startY = 18;
  15. int slotSizePlus2 = 18;
  16. for (int row = 0; row < 4; ++row) {
  17. for (int column = 0; column < 9; ++column) {
  18. this.addSlot(new Slot(tileEntity, (row * 9) + column, startX + (column * slotSizePlus2),
  19. startY + (row * slotSizePlus2)));
  20. }
  21. }
  22.  
  23. // Main Player Inventory
  24. int startPlayerInvY = startY * 5 + 12;
  25. for (int row = 0; row < 3; ++row) {
  26. for (int column = 0; column < 9; ++column) {
  27. this.addSlot(new Slot(playerInventory, 9 + (row * 9) + column, startX + (column * slotSizePlus2),
  28. startPlayerInvY + (row * slotSizePlus2)));
  29. }
  30. }
  31.  
  32. // Hotbar
  33. int hotbarY = startPlayerInvY + (startPlayerInvY / 2) + 7;
  34. for (int column = 0; column < 9; ++column) {
  35. this.addSlot(new Slot(playerInventory, column, startX + (column * slotSizePlus2), hotbarY));
  36. }
  37. }
  38.  
  39. private static CharmBagTileEntity getTileEntity(final PlayerInventory playerInventory,
  40. final PacketBuffer data) {
  41. Objects.requireNonNull(playerInventory, "playerInventory cannot be null");
  42. Objects.requireNonNull(data, "data cannot be null");
  43. final TileEntity tileAtPos = playerInventory.player.world.getTileEntity(data.readBlockPos());
  44. if (tileAtPos instanceof CharmBagTileEntity) {
  45. return (CharmBagTileEntity) tileAtPos;
  46. }
  47. throw new IllegalStateException("Tile entity is not correct! " + tileAtPos);
  48. }
  49.  
  50. public CharmBagContainer(final int windowId, final PlayerInventory playerInventory, final PacketBuffer data) {
  51. this(windowId, playerInventory, getTileEntity(playerInventory, data));
  52. }
  53.  
  54. @Override
  55. public boolean canInteractWith(PlayerEntity playerIn) {
  56. return isWithinUsableDistance(canInteractWithCallable, playerIn, BlockInit.EXAMPLE_CHEST.get());
  57. }
  58.  
  59. @Override
  60. public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
  61. ItemStack itemstack = ItemStack.EMPTY;
  62. Slot slot = this.inventorySlots.get(index);
  63. if (slot != null && slot.getHasStack()) {
  64. ItemStack itemstack1 = slot.getStack();
  65. itemstack = itemstack1.copy();
  66. if (index < 36) {
  67. if (!this.mergeItemStack(itemstack1, 36, this.inventorySlots.size(), true)) {
  68. return ItemStack.EMPTY;
  69. }
  70. } else if (!this.mergeItemStack(itemstack1, 0, 36, false)) {
  71. return ItemStack.EMPTY;
  72. }
  73.  
  74. if (itemstack1.isEmpty()) {
  75. slot.putStack(ItemStack.EMPTY);
  76. } else {
  77. slot.onSlotChanged();
  78. }
  79. }
  80. return itemstack;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment