Finished GUIs

This commit is contained in:
Auxilor
2021-04-16 15:32:42 +01:00
parent e543be7a13
commit 7d4262e0ef
5 changed files with 113 additions and 33 deletions

View File

@@ -1,50 +1,51 @@
package com.willfp.eco.core.gui.menu;
import com.willfp.eco.core.gui.slot.Slot;
import com.willfp.eco.internal.gui.FillerSlot;
import com.willfp.eco.util.ListUtils;
import lombok.Getter;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class FillerMask {
@Getter
private final FillerSlot[][] mask;
private final List<List<Slot>> mask;
public FillerMask(@NotNull final Material material,
@NotNull final Menu menu,
@NotNull final String... pattern) {
if (pattern.length != menu.getRows()) {
throw new IllegalArgumentException("Invalid amount of rows specified in pattern!");
}
if (material == Material.AIR) {
throw new IllegalArgumentException("Material cannot be air!");
}
mask = new FillerSlot[menu.getRows()][9];
mask = ListUtils.create2DList(6, 9);
ItemStack itemStack = new ItemStack(material);
ItemMeta meta = itemStack.getItemMeta();
assert meta != null;
meta.setDisplayName("");
meta.setDisplayName("§r");
itemStack.setItemMeta(meta);
int row = 0;
for (String patternRow : pattern) {
int column = 0;
if (pattern.length != 9) {
if (patternRow.length() != 9) {
throw new IllegalArgumentException("Invalid amount of columns in pattern!");
}
for (char c : patternRow.toCharArray()) {
if (c == '0') {
mask[row][column] = null;
mask.get(row).set(column, null);
} else if (c == '1') {
mask[row][column] = new FillerSlot(itemStack);
mask.get(row).set(column, new FillerSlot(itemStack));
} else {
throw new IllegalArgumentException("Invalid character in pattern! (Must only be 0 and 1)");
}
column++;
}
row++;
}

View File

@@ -3,6 +3,8 @@ package com.willfp.eco.core.gui.menu;
import com.willfp.eco.core.gui.slot.Slot;
import com.willfp.eco.internal.gui.EcoMenu;
import com.willfp.eco.internal.gui.FillerSlot;
import com.willfp.eco.util.ListUtils;
import com.willfp.eco.util.StringUtils;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryCloseEvent;
@@ -10,6 +12,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.function.Consumer;
public interface Menu {
@@ -29,34 +32,34 @@ public interface Menu {
class Builder {
private final int rows;
private String title = "Menu";
private Slot[][] maskSlots;
private final Slot[][] slots;
private List<List<Slot>> maskSlots;
private final List<List<Slot>> slots;
private Consumer<InventoryCloseEvent> onClose = (event) -> {
};
Builder(final int rows) {
this.rows = rows;
this.slots = new Slot[rows][9];
this.maskSlots = new Slot[rows][9];
this.slots = ListUtils.create2DList(rows, 9);
this.maskSlots = ListUtils.create2DList(rows, 9);
}
public Builder setTitle(@NotNull final String title) {
this.title = title;
this.title = StringUtils.translate(title);
return this;
}
public Builder setSlot(final int row,
final int column,
@NotNull final Slot slot) {
if (row < 0 || row > this.rows - 1) {
if (row < 1 || row > this.rows) {
throw new IllegalArgumentException("Invalid row number!");
}
if (column < 0 || column > 8) {
if (column < 1 || column > 9) {
throw new IllegalArgumentException("Invalid column number!");
}
slots[row][column] = slot;
slots.get(row - 1).set(column - 1, slot);
return this;
}
@@ -71,20 +74,20 @@ public interface Menu {
}
public Menu build() {
Slot[][] finalSlots = maskSlots;
for (int i = 0; i < slots.length; i++) {
for (int j = 0; j < slots[i].length; j++) {
Slot slot = slots[i][j];
List<List<Slot>> finalSlots = maskSlots;
for (int i = 0; i < slots.size(); i++) {
for (int j = 0; j < slots.get(i).size(); j++) {
Slot slot = slots.get(i).get(j);
if (slot != null) {
finalSlots[i][j] = slot;
finalSlots.get(i).set(j, slot);
}
}
}
for (int i = 0; i < finalSlots.length; i++) {
for (int j = 0; j < finalSlots[i].length; j++) {
if (finalSlots[i][j] == null) {
finalSlots[i][j] = new FillerSlot(new ItemStack(Material.AIR));
for (List<Slot> finalSlot : finalSlots) {
for (int j = 0; j < finalSlot.size(); j++) {
if (finalSlot.get(j) == null) {
finalSlot.set(j, new FillerSlot(new ItemStack(Material.AIR)));
}
}
}

View File

@@ -9,13 +9,14 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.function.Consumer;
public class EcoMenu implements Menu {
@Getter
private final int rows;
private final Slot[][] slots;
private final List<List<Slot>> slots;
@Getter
private final String title;
@@ -23,7 +24,7 @@ public class EcoMenu implements Menu {
private final Consumer<InventoryCloseEvent> onClose;
public EcoMenu(final int rows,
@NotNull final Slot[][] slots,
@NotNull final List<List<Slot>> slots,
@NotNull final String title,
@NotNull final Consumer<InventoryCloseEvent> onClose) {
this.rows = rows;
@@ -35,20 +36,32 @@ public class EcoMenu implements Menu {
@Override
public Slot getSlot(final int row,
final int column) {
if (row < 0 || row > this.rows - 1) {
if (row < 1 || row > this.rows) {
throw new IllegalArgumentException("Invalid row number!");
}
if (column < 0 || column > 8) {
if (column < 1 || column > 9) {
throw new IllegalArgumentException("Invalid column number!");
}
return slots[row][column];
return slots.get(row - 1).get(column - 1);
}
@Override
public Inventory open(@NotNull final Player player) {
Inventory inventory = Bukkit.createInventory(null, rows * 9, title);
int i = 0;
for (List<Slot> row : slots) {
for (Slot item : row) {
if (i == rows * 9) {
break;
}
inventory.setItem(i, item.getItemStack());
i++;
}
}
player.openInventory(inventory);
MenuHandler.registerMenu(inventory, this);
return inventory;

View File

@@ -0,0 +1,31 @@
package com.willfp.eco.util;
import lombok.experimental.UtilityClass;
import java.util.ArrayList;
import java.util.List;
@UtilityClass
public class ListUtils {
/**
* Initialize 2D list of a given size.
*
* @param rows The amount of rows.
* @param columns The amount of columns.
* @param <T> The type of the object stored in the list.
* @return The list, filled will null objects.
*/
public <T> List<List<T>> create2DList(final int rows,
final int columns) {
List<List<T>> list = new ArrayList<>(rows);
while (list.size() < rows) {
List<T> row = new ArrayList<>(columns);
while (row.size() < columns) {
row.add(null);
}
list.add(row);
}
return list;
}
}