Guest User

Untitled

a guest
Feb 19th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. public class TeleporterMenu extends AbstractContainerMenu {
  2.  
  3. public final WarpPipeBlockEntity blockEntity;
  4. private final Level level;
  5.  
  6. // Client constructor
  7. public TeleporterMenu(int pContainerId, Inventory inv, FriendlyByteBuf extraData) {
  8. this(pContainerId, inv, inv.player.level.getBlockEntity(extraData.readBlockPos()));
  9. }
  10.  
  11. // Server constructor
  12. public TeleporterMenu(int pContainerId, Inventory inv, BlockEntity entity) {
  13. super(ModMenuTypes.TELEPORTER_MENU.get(), pContainerId);
  14. checkContainerSize(inv, Constants.TELEPORTER_TOTALSLOTS);
  15. blockEntity = ((WarpPipeBlockEntity) entity);
  16. this.level = inv.player.level;
  17.  
  18. addPlayerInventory(inv);
  19. addPlayerHotbar(inv);
  20.  
  21. this.blockEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handler -> {
  22. //The third and fourth numbers are the position of the slots
  23. // (this.addSlot(new SlotItemHandler(handler, 0, THIRD, FOURTH));)
  24. //in an editing program, put your mouse above one of the slots at the top left part
  25. //(the mouse goes starting from the lightest grey pixel)
  26. this.addSlot(new TeleporterSlot(handler, 0, 152, 8));
  27. });
  28. }
  29.  
  30. private static final int HOTBAR_SLOT_COUNT = 9;
  31. private static final int PLAYER_INVENTORY_ROW_COUNT = 3;
  32. private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
  33. private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
  34. private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
  35. private static final int VANILLA_FIRST_SLOT_INDEX = 0;
  36. private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
  37.  
  38. // THIS YOU HAVE TO DEFINE!
  39. private static final int TE_INVENTORY_SLOT_COUNT = Constants.TELEPORTER_TOTALSLOTS; // must be the number of slots you have!
  40.  
  41. @Override
  42. public ItemStack quickMoveStack(Player playerIn, int index) {
  43. Slot sourceSlot = slots.get(index);
  44. if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY; //EMPTY_ITEM
  45. ItemStack sourceStack = sourceSlot.getItem();
  46. ItemStack copyOfSourceStack = sourceStack.copy();
  47.  
  48. // Check if the slot clicked is one of the vanilla container slots
  49. if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
  50. // This is a vanilla container slot so merge the stack into the tile inventory
  51. if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX
  52. + TE_INVENTORY_SLOT_COUNT, false)) {
  53. return ItemStack.EMPTY; // EMPTY_ITEM
  54. }
  55. } else if (index < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) {
  56. // This is a TE slot so merge the stack into the players inventory
  57. if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
  58. return ItemStack.EMPTY;
  59. }
  60. } else {
  61. System.out.println("Invalid slotIndex:" + index);
  62. return ItemStack.EMPTY;
  63. }
  64. // If stack size == 0 (the entire stack was moved) set slot contents to null
  65. if (sourceStack.getCount() == 0) {
  66. sourceSlot.set(ItemStack.EMPTY);
  67. } else {
  68. sourceSlot.setChanged();
  69. }
  70. sourceSlot.onTake(playerIn, sourceStack);
  71. return copyOfSourceStack;
  72. }
  73.  
  74. @Override
  75. public boolean stillValid(Player pPlayer) {
  76. /*return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()),
  77. pPlayer, ModBlocks.TELEPORTER_WHITE.get()) ||
  78. stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()),
  79. pPlayer, ModBlocks.TELEPORTER_RED.get());*/
  80. return true;
  81. }
  82.  
  83. private final void addPlayerInventory(Inventory playerInventory) {
  84. for (int i = 0; i < 3; ++i) {
  85. for (int l = 0; l < 9; ++l) {
  86. this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 86 + i * 18)); //default: 122
  87. }
  88. }
  89. }
  90. private final void addPlayerHotbar(Inventory playerInventory) {
  91. for (int i = 0; i < 9; ++i) {
  92. this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 144)); //default: 180
  93. }
  94. }
  95.  
  96. public WarpPipeBlockEntity getBlockEntity() {
  97. return this.blockEntity;
  98. }
  99. }
Add Comment
Please, Sign In to add comment