|
@@ -0,0 +1,258 @@
|
|
|
|
|
+package me.lethunderhawk.profile.data.sacks.gui;
|
|
|
|
|
+
|
|
|
|
|
+import me.lethunderhawk.fluxapi.util.gui.InventoryGUI;
|
|
|
|
|
+import me.lethunderhawk.fluxapi.util.itemdesign.ItemOptions;
|
|
|
|
|
+import me.lethunderhawk.fluxapi.util.itemdesign.LoreDesigner;
|
|
|
|
|
+import me.lethunderhawk.profile.data.sacks.Sack;
|
|
|
|
|
+import me.lethunderhawk.util.StringUtil;
|
|
|
|
|
+import net.kyori.adventure.text.Component;
|
|
|
|
|
+import net.kyori.adventure.text.format.NamedTextColor;
|
|
|
|
|
+import org.bukkit.Material;
|
|
|
|
|
+import org.bukkit.Sound;
|
|
|
|
|
+import org.bukkit.entity.Player;
|
|
|
|
|
+import org.bukkit.event.inventory.ClickType;
|
|
|
|
|
+import org.bukkit.inventory.ItemStack;
|
|
|
|
|
+import org.bukkit.inventory.PlayerInventory;
|
|
|
|
|
+import org.bukkit.inventory.meta.ItemMeta;
|
|
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+public class SackGUI extends InventoryGUI {
|
|
|
|
|
+
|
|
|
|
|
+ private final Sack sack;
|
|
|
|
|
+ private Player player;
|
|
|
|
|
+
|
|
|
|
|
+ private int currentPage = 0;
|
|
|
|
|
+ int maxPage;
|
|
|
|
|
+ private List<Material> materials;
|
|
|
|
|
+
|
|
|
|
|
+ private List<Integer> contentSlots;
|
|
|
|
|
+
|
|
|
|
|
+ public SackGUI(@NotNull Player p, @NotNull Sack sack) {
|
|
|
|
|
+ super("Your " + sack.getType().name().toLowerCase() + " Sack", 36, p);
|
|
|
|
|
+ this.sack = sack;
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void performAdditionalComputationOnPlayer(Player player) {
|
|
|
|
|
+ this.player = player;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void buildContent() {
|
|
|
|
|
+
|
|
|
|
|
+ fillGlassPaneBackground();
|
|
|
|
|
+
|
|
|
|
|
+ materials = new ArrayList<>(sack.getAssociatedSackMap().keySet());
|
|
|
|
|
+
|
|
|
|
|
+ contentSlots = computeContentSlots();
|
|
|
|
|
+ int itemsPerPage = contentSlots.size();
|
|
|
|
|
+ maxPage = (materials.size() - 1) / itemsPerPage;
|
|
|
|
|
+
|
|
|
|
|
+ renderPage();
|
|
|
|
|
+ addNavigationButtons();
|
|
|
|
|
+ setCloseButton(getInventory().getSize() - 5);
|
|
|
|
|
+ setAddAllItemsItem();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Computes all interior slots dynamically (1 slot border).
|
|
|
|
|
+ */
|
|
|
|
|
+ private List<Integer> computeContentSlots() {
|
|
|
|
|
+
|
|
|
|
|
+ List<Integer> slots = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ int rows = getInventory().getSize() / 9;
|
|
|
|
|
+
|
|
|
|
|
+ for (int row = 1; row < rows - 1; row++) {
|
|
|
|
|
+ for (int col = 1; col < 8; col++) {
|
|
|
|
|
+
|
|
|
|
|
+ int slot = row * 9 + col;
|
|
|
|
|
+ slots.add(slot);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return slots;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void renderPage() {
|
|
|
|
|
+
|
|
|
|
|
+ int itemsPerPage = contentSlots.size();
|
|
|
|
|
+ int start = currentPage * itemsPerPage;
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < itemsPerPage; i++) {
|
|
|
|
|
+
|
|
|
|
|
+ int materialIndex = start + i;
|
|
|
|
|
+
|
|
|
|
|
+ if (materialIndex >= materials.size()) continue;
|
|
|
|
|
+
|
|
|
|
|
+ Material material = materials.get(materialIndex);
|
|
|
|
|
+
|
|
|
|
|
+ addSackItem(material, contentSlots.get(i));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void addSackItem(Material material, int slot) {
|
|
|
|
|
+
|
|
|
|
|
+ String materialName = StringUtil.toCamelCase(material.name());
|
|
|
|
|
+
|
|
|
|
|
+ List<Component> lore = LoreDesigner.createLore(
|
|
|
|
|
+ "<dark_gray>" + sack.getType().name().toUpperCase() + " SACK </dark_gray>" +
|
|
|
|
|
+ "<br><br>Stored: " +
|
|
|
|
|
+ (sack.getMaterialAmount(material) == 0
|
|
|
|
|
+ ? "<dark_gray>0</dark_gray>"
|
|
|
|
|
+ : "<yellow>" + sack.getMaterialAmount(material) + "</yellow>")
|
|
|
|
|
+ + "/" + sack.getMaxMaterialAmount(material) +
|
|
|
|
|
+ "<br><br><aqua>Right-Click for stack!</aqua>" +
|
|
|
|
|
+ "<br><yellow>Click to pickup!</yellow>",
|
|
|
|
|
+ "This is an example width reference",
|
|
|
|
|
+ NamedTextColor.GRAY
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ setItemWithClickAction(slot,
|
|
|
|
|
+ new ItemOptions(material)
|
|
|
|
|
+ .setName(Component.text(materialName, NamedTextColor.GREEN))
|
|
|
|
|
+ .setLore(lore)
|
|
|
|
|
+ .buildItemStack(),
|
|
|
|
|
+ (p, type) -> doSackAction(type, material)
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void addNavigationButtons() {
|
|
|
|
|
+ if (currentPage > 0) {
|
|
|
|
|
+ setItemWithClickAction(getInventory().getSize() - 9,
|
|
|
|
|
+ new ItemOptions(Material.ARROW)
|
|
|
|
|
+ .setName(Component.text("Previous Page", NamedTextColor.YELLOW))
|
|
|
|
|
+ .setLore(List.of(Component.text("Current Page: " + (currentPage + 1) + "/" + (maxPage + 1), NamedTextColor.GRAY)))
|
|
|
|
|
+ .buildItemStack(),
|
|
|
|
|
+ (p, type) -> {
|
|
|
|
|
+ previousPage();
|
|
|
|
|
+ });
|
|
|
|
|
+ }else{
|
|
|
|
|
+ setItem(getInventory().getSize() - 9, getGlassPaneStack());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (currentPage < maxPage) {
|
|
|
|
|
+ setItemWithClickAction(getInventory().getSize() -1,
|
|
|
|
|
+ new ItemOptions(Material.ARROW)
|
|
|
|
|
+ .setName(Component.text("Next Page", NamedTextColor.YELLOW))
|
|
|
|
|
+ .setLore(List.of(Component.text("Current Page " + (currentPage + 1) + "/" + (maxPage + 1), NamedTextColor.GRAY)))
|
|
|
|
|
+ .buildItemStack(),
|
|
|
|
|
+ (p, type) -> {
|
|
|
|
|
+ nextPage();
|
|
|
|
|
+ });
|
|
|
|
|
+ }else{
|
|
|
|
|
+ setItem(getInventory().getSize() -1, getGlassPaneStack());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ private ItemStack getGlassPaneStack(){
|
|
|
|
|
+ ItemStack background = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
|
|
|
|
|
+ ItemMeta meta = background.getItemMeta();
|
|
|
|
|
+ meta.displayName(Component.text(" "));
|
|
|
|
|
+ meta.setHideTooltip(true);
|
|
|
|
|
+ background.setItemMeta(meta);
|
|
|
|
|
+ return background;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void previousPage() {
|
|
|
|
|
+ if(currentPage > 0) {
|
|
|
|
|
+ currentPage--;
|
|
|
|
|
+ update();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void nextPage() {
|
|
|
|
|
+ if(currentPage < maxPage) {
|
|
|
|
|
+ currentPage++;
|
|
|
|
|
+ update();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void doSackAction(ClickType type, Material material) {
|
|
|
|
|
+
|
|
|
|
|
+ int amountToTake = 1;
|
|
|
|
|
+
|
|
|
|
|
+ if (type == ClickType.RIGHT || type == ClickType.SHIFT_RIGHT) {
|
|
|
|
|
+ amountToTake = 64;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int removed = sack.takeOutItem(material, amountToTake);
|
|
|
|
|
+
|
|
|
|
|
+ if (removed > 0) {
|
|
|
|
|
+ player.playSound(player.getLocation(), Sound.ITEM_BUNDLE_REMOVE_ONE, 1f, 1f);
|
|
|
|
|
+ ItemStack stack = new ItemStack(material, removed);
|
|
|
|
|
+
|
|
|
|
|
+ HashMap<Integer, ItemStack> leftover = player.getInventory().addItem(stack);
|
|
|
|
|
+
|
|
|
|
|
+ if (!leftover.isEmpty()) {
|
|
|
|
|
+
|
|
|
|
|
+ int notStored = leftover.values().stream()
|
|
|
|
|
+ .mapToInt(ItemStack::getAmount)
|
|
|
|
|
+ .sum();
|
|
|
|
|
+
|
|
|
|
|
+ sack.addItem(material, notStored);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ player.updateInventory();
|
|
|
|
|
+ update();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void setAddAllItemsItem() {
|
|
|
|
|
+
|
|
|
|
|
+ setItemWithClickAction(getInventory().getSize() - 4,
|
|
|
|
|
+ new ItemOptions(Material.CAULDRON)
|
|
|
|
|
+ .setName(Component.text("Add all items into the Sack", NamedTextColor.GREEN))
|
|
|
|
|
+ .setLore(LoreDesigner.createLore(
|
|
|
|
|
+ "<br>Allows you to put all items from your inventory into the sack",
|
|
|
|
|
+ "This is another example width reference",
|
|
|
|
|
+ NamedTextColor.DARK_GRAY))
|
|
|
|
|
+ .buildItemStack(),
|
|
|
|
|
+ this::putAllItemsIn);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void putAllItemsIn(Player player, ClickType type) {
|
|
|
|
|
+
|
|
|
|
|
+ PlayerInventory inv = player.getInventory();
|
|
|
|
|
+
|
|
|
|
|
+ for (ItemStack stack : inv.getContents()) {
|
|
|
|
|
+
|
|
|
|
|
+ if (stack == null || stack.getType() == Material.AIR) continue;
|
|
|
|
|
+
|
|
|
|
|
+ Material material = stack.getType();
|
|
|
|
|
+ int amount = stack.getAmount();
|
|
|
|
|
+
|
|
|
|
|
+ int leftover = sack.addItem(material, amount);
|
|
|
|
|
+
|
|
|
|
|
+ int successfullyAdded = amount - leftover;
|
|
|
|
|
+
|
|
|
|
|
+ if (successfullyAdded > 0) {
|
|
|
|
|
+ stack.setAmount(leftover);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ player.playSound(player.getLocation(), Sound.ITEM_BUNDLE_INSERT, 1f, 1f);
|
|
|
|
|
+ player.updateInventory();
|
|
|
|
|
+ update();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void update() {
|
|
|
|
|
+ clearInventory();
|
|
|
|
|
+ fillGlassPaneBackground();
|
|
|
|
|
+ renderPage();
|
|
|
|
|
+ addNavigationButtons();
|
|
|
|
|
+ setCloseButton(getInventory().getSize() - 5);
|
|
|
|
|
+ setAddAllItemsItem();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void clearInventory() {
|
|
|
|
|
+ for(int i = 0; i < getInventory().getSize(); ++i) {
|
|
|
|
|
+ this.getInventory().setItem(i,null);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|