|
| 1 | +package gg.nextforge.ui.builder; |
| 2 | + |
| 3 | +import gg.nextforge.ui.component.UIComponent; |
| 4 | +import org.bukkit.Bukkit; |
| 5 | +import org.bukkit.entity.Player; |
| 6 | +import org.bukkit.inventory.Inventory; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.function.Function; |
| 11 | + |
| 12 | +public class InventoryBuilder { |
| 13 | + |
| 14 | + private final int size; |
| 15 | + private String title = "Inventory"; |
| 16 | + private final Map<Integer, UIComponent> components = new HashMap<>(); |
| 17 | + private Function<Player, String> dynamicTitle = null; |
| 18 | + |
| 19 | + public InventoryBuilder(int size) { |
| 20 | + this.size = size; |
| 21 | + } |
| 22 | + |
| 23 | + public InventoryBuilder title(String title) { |
| 24 | + this.title = title; |
| 25 | + return this; |
| 26 | + } |
| 27 | + |
| 28 | + public InventoryBuilder title(Function<Player, String> titleFunction) { |
| 29 | + this.dynamicTitle = titleFunction; |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + public InventoryBuilder set(int slot, UIComponent component) { |
| 34 | + components.put(slot, component); |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + public Inventory build(Player player) { |
| 39 | + String finalTitle = dynamicTitle != null ? dynamicTitle.apply(player) : title; |
| 40 | + Inventory inventory = Bukkit.createInventory(null, size, finalTitle); |
| 41 | + |
| 42 | + for (Map.Entry<Integer, UIComponent> entry : components.entrySet()) { |
| 43 | + inventory.setItem(entry.getKey(), entry.getValue().render(player)); |
| 44 | + } |
| 45 | + |
| 46 | + return inventory; |
| 47 | + } |
| 48 | + |
| 49 | + public void open(Player player) { |
| 50 | + player.openInventory(build(player)); |
| 51 | + } |
| 52 | + |
| 53 | + public Map<Integer, UIComponent> getComponents() { |
| 54 | + return components; |
| 55 | + } |
| 56 | +} |
0 commit comments