TIMAS_Bro

Untitled

May 16th, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.11 KB | None | 0 0
  1. package dev.ithundxr.createnumismatics.content.bank;
  2.  
  3. import com.simibubi.create.foundation.gui.menu.MenuBase;
  4. import dev.ithundxr.createnumismatics.Numismatics;
  5. import dev.ithundxr.createnumismatics.content.backend.BankAccount;
  6. import dev.ithundxr.createnumismatics.content.backend.Coin;
  7. import dev.ithundxr.createnumismatics.content.coins.CoinItem;
  8. import dev.ithundxr.createnumismatics.content.coins.SlotInputMergingCoinBag;
  9. import dev.ithundxr.createnumismatics.content.coins.SlotOutputMergingCoinBag;
  10. import dev.ithundxr.createnumismatics.registry.NumismaticsTags;
  11. import dev.ithundxr.createnumismatics.util.Utils;
  12. import net.minecraft.network.FriendlyByteBuf;
  13. import net.minecraft.server.level.ServerPlayer;
  14. import net.minecraft.world.Container;
  15. import net.minecraft.world.ContainerHelper;
  16. import net.minecraft.world.entity.player.Inventory;
  17. import net.minecraft.world.entity.player.Player;
  18. import net.minecraft.world.inventory.ContainerData;
  19. import net.minecraft.world.inventory.MenuType;
  20. import net.minecraft.world.inventory.Slot;
  21. import net.minecraft.world.item.ItemStack;
  22. import org.jetbrains.annotations.NotNull;
  23.  
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.UUID;
  27. import java.util.function.Consumer;
  28. import java.util.function.Function;
  29.  
  30. public class BankMenu extends MenuBase<BankAccount> {
  31.     public static final int COIN_SLOTS = Coin.values().length;
  32.     public static final int COIN_INPUT_SLOT_INDEX = COIN_SLOTS;
  33.     public static final int CARD_SLOT_INDEX = COIN_INPUT_SLOT_INDEX + 1;
  34.     public static final int PLAYER_INV_START_INDEX = CARD_SLOT_INDEX + 1;
  35.     public static final int PLAYER_HOTBAR_END_INDEX = PLAYER_INV_START_INDEX + 9;
  36.     public static final int PLAYER_INV_END_INDEX = PLAYER_INV_START_INDEX + 36;
  37.     protected ContainerData dataAccess;
  38.     private CardSwitchContainer cardSwitchContainer;
  39.     public BankMenu(MenuType<?> type, int id, Inventory inv, FriendlyByteBuf extraData) {
  40.         super(type, id, inv, extraData);
  41.     }
  42.  
  43.     public BankMenu(MenuType<?> type, int id, Inventory inv, BankAccount contentHolder, ContainerData dataAccess) {
  44.         super(type, id, inv, contentHolder);
  45.         this.dataAccess = dataAccess;
  46.         addDataSlots(dataAccess);
  47.     }
  48.  
  49.     @Override
  50.     protected BankAccount createOnClient(FriendlyByteBuf extraData) {
  51.         BankAccount account = BankAccount.clientSide(extraData);
  52.         this.dataAccess = account.dataAccess;
  53.         addDataSlots(dataAccess);
  54.         return account;
  55.     }
  56.  
  57.     @Override
  58.     protected void initAndReadInventory(BankAccount contentHolder) {}
  59.  
  60.     @Override
  61.     protected void addSlots() {
  62.         if (cardSwitchContainer == null)
  63.             cardSwitchContainer = new CardSwitchContainer(this::slotsChanged, this::switchTo);
  64.         int x = 13;
  65.         int y = 71;
  66.  
  67.         for (Coin coin : Coin.values()) {
  68.             addSlot(new SlotOutputMergingCoinBag(contentHolder.linkedCoinBag, coin, x, y));
  69.             x += 18;
  70.         }
  71.  
  72.         addSlot(new SlotInputMergingCoinBag(contentHolder.linkedCoinBag, null, 159, y));
  73.  
  74.         addSlot(new CardSlot.BoundCardSlot(cardSwitchContainer, 0, 8, 109));
  75.  
  76.         addPlayerSlots(40, 152);
  77.     }
  78.  
  79.     private boolean switchTo(UUID otherAccount) {
  80.         if (player instanceof ServerPlayer serverPlayer) {
  81.             BankAccount account = Numismatics.BANK.getAccount(otherAccount);
  82.             if (account != null) {
  83.                 if (account.isAuthorized(serverPlayer)) {
  84.                     Utils.openScreen(serverPlayer, account, account::sendToMenu);
  85.                 }
  86.                 return true;
  87.             } else {
  88.                 return false;
  89.             }
  90.         } else {
  91.             return true; // client
  92.         }
  93.     }
  94.  
  95.     @Override
  96.     protected void saveData(BankAccount contentHolder) {}
  97.  
  98.     @Override
  99.     public void removed(Player playerIn) {
  100.         super.removed(playerIn);
  101.         if (playerIn instanceof ServerPlayer) {
  102.             clearContainer(player, cardSwitchContainer);
  103.         }
  104.     }
  105.  
  106.     @Override
  107.     public @NotNull ItemStack quickMoveStack(@NotNull Player player, int index) { // index is slot that was clicked
  108.         Slot clickedSlot = this.slots.get(index);
  109.  
  110.         if (!clickedSlot.hasItem())
  111.             return ItemStack.EMPTY;
  112.  
  113.         ItemStack slotStack = CoinItem.clearDisplayedCount(clickedSlot.getItem());
  114.         ItemStack returnStack = slotStack.copy();
  115.  
  116.         if (index <= CARD_SLOT_INDEX) {
  117.             int count = slotStack.getCount();
  118.             if (!moveItemStackTo(slotStack, PLAYER_INV_START_INDEX, PLAYER_INV_END_INDEX, false))
  119.                 return ItemStack.EMPTY;
  120.  
  121.             returnStack = ItemStack.EMPTY;
  122.             clickedSlot.remove(count);
  123.         } else if (slotStack.getItem() instanceof CoinItem && !moveItemStackTo(slotStack, COIN_INPUT_SLOT_INDEX, COIN_INPUT_SLOT_INDEX + 1, false)) {
  124.             return ItemStack.EMPTY; // failed to move to coin input slot
  125.         } else if (NumismaticsTags.AllItemTags.CARDS.matches(slotStack) && !moveItemStackTo(slotStack, CARD_SLOT_INDEX, CARD_SLOT_INDEX + 1, false)) {
  126.             return ItemStack.EMPTY; // failed to move to card slot
  127.         } else if (index >= PLAYER_INV_START_INDEX && index < PLAYER_HOTBAR_END_INDEX && !moveItemStackTo(slotStack, PLAYER_HOTBAR_END_INDEX, PLAYER_INV_END_INDEX, false)) {
  128.             return ItemStack.EMPTY;
  129.         } else if (index >= PLAYER_HOTBAR_END_INDEX && index < PLAYER_INV_END_INDEX && !moveItemStackTo(slotStack, PLAYER_INV_START_INDEX, PLAYER_HOTBAR_END_INDEX, false)) {
  130.             return ItemStack.EMPTY;
  131.         }
  132.  
  133.         if (slotStack.isEmpty()) {
  134.             clickedSlot.set(ItemStack.EMPTY);
  135.         } else {
  136.             clickedSlot.setChanged();
  137.         }
  138.  
  139.         return returnStack;
  140.     }
  141.  
  142.     private class CardSwitchContainer implements Container {
  143.         private final Consumer<CardSwitchContainer> slotsChangedCallback;
  144.         private final Function<UUID, Boolean> uuidChangedCallback; // should return success
  145.  
  146.         @NotNull
  147.         protected final List<ItemStack> stacks = new ArrayList<>();
  148.  
  149.         public CardSwitchContainer(Consumer<CardSwitchContainer> slotsChangedCallback, Function<UUID, Boolean> uuidChangedCallback) {
  150.             this.slotsChangedCallback = slotsChangedCallback;
  151.             this.uuidChangedCallback = uuidChangedCallback;
  152.             stacks.add(ItemStack.EMPTY);
  153.         }
  154.  
  155.         @Override
  156.         public int getContainerSize() {
  157.             return 1;
  158.         }
  159.  
  160.         protected ItemStack getStack() {
  161.             return stacks.get(0);
  162.         }
  163.  
  164.         @Override
  165.         public boolean isEmpty() {
  166.             return getStack().isEmpty();
  167.         }
  168.  
  169.         @Override
  170.         public @NotNull ItemStack getItem(int slot) {
  171.             return getStack();
  172.         }
  173.  
  174.         @Override
  175.         public @NotNull ItemStack removeItem(int slot, int amount) {
  176.             ItemStack stack = ContainerHelper.removeItem(this.stacks, 0, amount);
  177.             if (!stack.isEmpty()) {
  178.                 this.slotsChangedCallback.accept(this);
  179.             }
  180.             return stack;
  181.         }
  182.  
  183.         @Override
  184.         public @NotNull ItemStack removeItemNoUpdate(int slot) {
  185.             return ContainerHelper.takeItem(this.stacks, 0);
  186.         }
  187.  
  188.         @Override
  189.         public void setItem(int slot, @NotNull ItemStack stack) {
  190.             this.stacks.set(0, stack);
  191.             if (CardItem.isBound(stack) && NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
  192.                 if (!this.uuidChangedCallback.apply(CardItem.get(stack))) {
  193.                     // Non-existent account
  194.                     stacks.set(0, CardItem.clear(stack));
  195.                     BankMenu.this.clearContainer(BankMenu.this.player, this);
  196.                 }
  197.             }
  198.             this.slotsChangedCallback.accept(this);
  199.         }
  200.  
  201.         @Override
  202.         public void setChanged() {}
  203.  
  204.         @Override
  205.         public boolean stillValid(@NotNull Player player) {
  206.             return true;
  207.         }
  208.  
  209.         @Override
  210.         public void clearContent() {
  211.             this.stacks.set(0, ItemStack.EMPTY);
  212.         }
  213.     }
  214. }
Add Comment
Please, Sign In to add comment