Skip to content

Commit

Permalink
+ItemMaterialFiller (Only obtainable item Material is found in this f…
Browse files Browse the repository at this point in the history
…iller).
  • Loading branch information
anjoismysign committed Dec 1, 2022
1 parent 4b2ed2d commit 05839ee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/main/java/us/mytheria/bloblib/entities/BlobEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<VariableValue<T>> values = page(page, getItemsPerPage());
List<VariableValue<T>> values = this.page(page, getItemsPerPage());
for (int i = 0; i < values.size(); i++) {
setValue(i, values.get(i));
}
Expand Down Expand Up @@ -83,11 +85,15 @@ public List<VariableValue<T>> 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(), () -> {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Material> {
private final ArrayList<Material> 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<VariableValue<Material>> page(int page, int itemsPerPage) {
int start = (page - 1) * itemsPerPage;
int end = start + (itemsPerPage);
ArrayList<VariableValue<Material>> 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);
}
}

0 comments on commit 05839ee

Please sign in to comment.