TIMAS_Bro

Untitled

May 10th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.16 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.  
  68.  
  69.  
  70.         for (Coin coin : Coin.values()) {
  71.             addSlot(new SlotOutputMergingCoinBag(contentHolder.linkedCoinBag, coin, x, y));
  72.             x += 18;
  73.         }
  74.  
  75.         addSlot(new SlotInputMergingCoinBag(contentHolder.linkedCoinBag, null, 159, y));
  76.  
  77.         addSlot(new CardSlot.BoundCardSlot(cardSwitchContainer, 0, 8, 109));
  78.  
  79.         addPlayerSlots(40, 152);
  80.     }
  81.  
  82.     private boolean switchTo(UUID otherAccount) {
  83.         if (player instanceof ServerPlayer serverPlayer) {
  84.             BankAccount account = Numismatics.BANK.getAccount(otherAccount);
  85.             if (account != null && otherAccount != player.getUUID()) {
  86.                 if (account.isAuthorized(serverPlayer)) {
  87.                     Utils.openScreen(serverPlayer, account, account::sendToMenu);
  88.                 }
  89.                 return true;
  90.             } else {
  91.                 return false;
  92.             }
  93.         } else {
  94.             return true; // client
  95.         }
  96.     }
  97.  
  98.     @Override
  99.     protected void saveData(BankAccount contentHolder) {}
  100.  
  101.     @Override
  102.     public void removed(Player playerIn) {
  103.         super.removed(playerIn);
  104.         if (playerIn instanceof ServerPlayer) {
  105.             clearContainer(player, cardSwitchContainer);
  106.         }
  107.     }
  108.  
  109.     @Override
  110.     public @NotNull ItemStack quickMoveStack(@NotNull Player player, int index) { // index is slot that was clicked
  111.         Slot clickedSlot = this.slots.get(index);
  112.  
  113.         if (!clickedSlot.hasItem())
  114.             return ItemStack.EMPTY;
  115.  
  116.         ItemStack slotStack = CoinItem.clearDisplayedCount(clickedSlot.getItem());
  117.         ItemStack returnStack = slotStack.copy();
  118.  
  119.         if (index <= CARD_SLOT_INDEX) {
  120.             int count = slotStack.getCount();
  121.             if (!moveItemStackTo(slotStack, PLAYER_INV_START_INDEX, PLAYER_INV_END_INDEX, false))
  122.                 return ItemStack.EMPTY;
  123.  
  124.             returnStack = ItemStack.EMPTY;
  125.             clickedSlot.remove(count);
  126.         } else if (slotStack.getItem() instanceof CoinItem && !moveItemStackTo(slotStack, COIN_INPUT_SLOT_INDEX, COIN_INPUT_SLOT_INDEX + 1, false)) {
  127.             return ItemStack.EMPTY; // failed to move to coin input slot
  128.         } else if (NumismaticsTags.AllItemTags.CARDS.matches(slotStack) && !moveItemStackTo(slotStack, CARD_SLOT_INDEX, CARD_SLOT_INDEX + 1, false)) {
  129.             return ItemStack.EMPTY; // failed to move to card slot
  130.         } else if (index >= PLAYER_INV_START_INDEX && index < PLAYER_HOTBAR_END_INDEX && !moveItemStackTo(slotStack, PLAYER_HOTBAR_END_INDEX, PLAYER_INV_END_INDEX, false)) {
  131.             return ItemStack.EMPTY;
  132.         } else if (index >= PLAYER_HOTBAR_END_INDEX && index < PLAYER_INV_END_INDEX && !moveItemStackTo(slotStack, PLAYER_INV_START_INDEX, PLAYER_HOTBAR_END_INDEX, false)) {
  133.             return ItemStack.EMPTY;
  134.         }
  135.  
  136.         if (slotStack.isEmpty()) {
  137.             clickedSlot.set(ItemStack.EMPTY);
  138.         } else {
  139.             clickedSlot.setChanged();
  140.         }
  141.  
  142.         return returnStack;
  143.     }
  144.  
  145.     private class CardSwitchContainer implements Container {
  146.         private final Consumer<CardSwitchContainer> slotsChangedCallback;
  147.         private final Function<UUID, Boolean> uuidChangedCallback; // should return success
  148.  
  149.         @NotNull
  150.         protected final List<ItemStack> stacks = new ArrayList<>();
  151.  
  152.         public CardSwitchContainer(Consumer<CardSwitchContainer> slotsChangedCallback, Function<UUID, Boolean> uuidChangedCallback) {
  153.             this.slotsChangedCallback = slotsChangedCallback;
  154.             this.uuidChangedCallback = uuidChangedCallback;
  155.             stacks.add(ItemStack.EMPTY);
  156.         }
  157.  
  158.         @Override
  159.         public int getContainerSize() {
  160.             return 1;
  161.         }
  162.  
  163.         protected ItemStack getStack() {
  164.             return stacks.get(0);
  165.         }
  166.  
  167.         @Override
  168.         public boolean isEmpty() {
  169.             return getStack().isEmpty();
  170.         }
  171.  
  172.         @Override
  173.         public @NotNull ItemStack getItem(int slot) {
  174.             return getStack();
  175.         }
  176.  
  177.         @Override
  178.         public @NotNull ItemStack removeItem(int slot, int amount) {
  179.             ItemStack stack = ContainerHelper.removeItem(this.stacks, 0, amount);
  180.             if (!stack.isEmpty()) {
  181.                 this.slotsChangedCallback.accept(this);
  182.             }
  183.             return stack;
  184.         }
  185.  
  186.         @Override
  187.         public @NotNull ItemStack removeItemNoUpdate(int slot) {
  188.             return ContainerHelper.takeItem(this.stacks, 0);
  189.         }
  190.  
  191.         @Override
  192.         public void setItem(int slot, @NotNull ItemStack stack) {
  193.             this.stacks.set(0, stack);
  194.             if (CardItem.isBound(stack) && NumismaticsTags.AllItemTags.CARDS.matches(stack)) {
  195.                 if (!this.uuidChangedCallback.apply(CardItem.get(stack))) {
  196.                     // Non-existent account
  197.                     stacks.set(0, CardItem.clear(stack));
  198.                     BankMenu.this.clearContainer(BankMenu.this.player, this);
  199.                 }
  200.             }
  201.             this.slotsChangedCallback.accept(this);
  202.         }
  203.  
  204.         @Override
  205.         public void setChanged() {}
  206.  
  207.         @Override
  208.         public boolean stillValid(@NotNull Player player) {
  209.             return true;
  210.         }
  211.  
  212.         @Override
  213.         public void clearContent() {
  214.             this.stacks.set(0, ItemStack.EMPTY);
  215.         }
  216.     }
  217. }
  218.  
Add Comment
Please, Sign In to add comment