From 05839ee55f7640dfd0c800bef2569b98aaa8edcd Mon Sep 17 00:00:00 2001 From: lbenav8095 Date: Wed, 30 Nov 2022 18:46:51 -0600 Subject: [PATCH] +ItemMaterialFiller (Only obtainable item Material is found in this filler). --- .../mytheria/bloblib/entities/BlobEditor.java | 22 ++++++-- .../managers/fillermanager/FillerManager.java | 5 ++ .../fillermanager/ItemMaterialFiller.java | 54 +++++++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/main/java/us/mytheria/bloblib/managers/fillermanager/ItemMaterialFiller.java diff --git a/src/main/java/us/mytheria/bloblib/entities/BlobEditor.java b/src/main/java/us/mytheria/bloblib/entities/BlobEditor.java index 17d24123..08ca3495 100644 --- a/src/main/java/us/mytheria/bloblib/entities/BlobEditor.java +++ b/src/main/java/us/mytheria/bloblib/entities/BlobEditor.java @@ -3,6 +3,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; +import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -46,15 +47,16 @@ public void loadInConstructor() { @Override public void loadPage(int page, boolean refill) { - if (page < 1) + if (page < 1) { return; + } if (getTotalPages() < page) { return; } if (refill) refillButton("White-Background"); clearValues(); - List> values = page(page, getItemsPerPage()); + List> values = this.page(page, getItemsPerPage()); for (int i = 0; i < values.size(); i++) { setValue(i, values.get(i)); } @@ -83,11 +85,15 @@ public List> page(int page, int itemsPerPage) { @SuppressWarnings("unchecked") public void removeElement(Player player, Runnable onRemove) { - loadPage(getPage(), false); + loadPage(getPage(), true); selectorManager.addSelectorListener(player, BlobSelectorListener.build(player, () -> { - if (selectorManager.get(player).getInput() == null) + if (selectorManager.get(player).getInput() == null) { + selectorManager.removeSelectorListener(player); + onRemove.run(); return; + } + player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1); T input = (T) selectorManager.get(player).getInput(); selectorManager.removeSelectorListener(player); Bukkit.getScheduler().runTask(BlobLib.getInstance(), () -> { @@ -110,6 +116,14 @@ public int totalPages(int itemsPerPage) { return (int) Math.ceil((double) list.size() / (double) itemsPerPage); } + @Override + public int getTotalPages() { + int totalPages = totalPages(getItemsPerPage()); + if (totalPages == 0) + totalPages = 1; + return totalPages; + } + @Override public void add(T t) { list.add(t); diff --git a/src/main/java/us/mytheria/bloblib/managers/fillermanager/FillerManager.java b/src/main/java/us/mytheria/bloblib/managers/fillermanager/FillerManager.java index 0e4a1896..89d9dfa9 100644 --- a/src/main/java/us/mytheria/bloblib/managers/fillermanager/FillerManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/fillermanager/FillerManager.java @@ -10,6 +10,7 @@ public class FillerManager { public FillerManager() { variableFillers = new HashMap<>(); variableFillers.put("Material", new MaterialFiller()); + variableFillers.put("ItemMaterial", new ItemMaterialFiller()); variableFillers.put("BlockMaterial", new BlockMaterialFiller()); variableFillers.put("SpawnableEntityType", new SpawnableEntityTypeFiller()); } @@ -18,6 +19,10 @@ public MaterialFiller getMaterialFiller() { return (MaterialFiller) variableFillers.get("Material"); } + public ItemMaterialFiller getItemMaterialFiller() { + return (ItemMaterialFiller) variableFillers.get("ItemMaterial"); + } + public BlockMaterialFiller getBlockMaterialFiller() { return (BlockMaterialFiller) variableFillers.get("BlockMaterial"); } diff --git a/src/main/java/us/mytheria/bloblib/managers/fillermanager/ItemMaterialFiller.java b/src/main/java/us/mytheria/bloblib/managers/fillermanager/ItemMaterialFiller.java new file mode 100644 index 00000000..23b34605 --- /dev/null +++ b/src/main/java/us/mytheria/bloblib/managers/fillermanager/ItemMaterialFiller.java @@ -0,0 +1,54 @@ +package us.mytheria.bloblib.managers.fillermanager; + +import net.md_5.bungee.api.ChatColor; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import us.mytheria.bloblib.entities.VariableFiller; +import us.mytheria.bloblib.entities.VariableValue; + +import java.util.ArrayList; +import java.util.List; + +public class ItemMaterialFiller implements VariableFiller { + private final ArrayList materials; + + public ItemMaterialFiller() { + materials = new ArrayList<>(); + for (Material material : Material.values()) { + if (material.name().contains("LEGACY")) + continue; + if (!material.isItem()) + continue; + if (new ItemStack(material).getItemMeta() == null) + continue; + materials.add(material); + } + } + + @Override + public List> page(int page, int itemsPerPage) { + int start = (page - 1) * itemsPerPage; + int end = start + (itemsPerPage); + ArrayList> values = new ArrayList<>(); + for (int i = start; i < end; i++) { + Material material; + try { + material = materials.get(i); + ItemStack itemStack = new ItemStack(material); + ItemMeta itemMeta = itemStack.getItemMeta(); + itemMeta.setDisplayName(ChatColor.GOLD + material.name()); + itemStack.setItemMeta(itemMeta); + values.add(new VariableValue<>(itemStack, material)); + } catch (IndexOutOfBoundsException e) { + break; + } + } + return values; + } + + @Override + public int totalPages(int itemsPerPage) { + return (int) Math.ceil((double) materials.size() / (double) itemsPerPage); + } +}