Core/src/main/java/eu/univento/core/api/items/PageLayout.java

58 lines
1.9 KiB
Java

package eu.univento.core.api.items;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
/**
* @author joethei
* @version 0.1
*/
public class PageLayout {
private static String empty = "X";
private static String full = "O";
private int invSize;
private ArrayList<Integer> size;
public static void setStringFormat(final String noItem, final String aItem) {
PageLayout.empty = noItem;
PageLayout.full = aItem;
}
public PageLayout(final String... strings) {
this.invSize = 0;
this.size = new ArrayList<Integer>();
this.invSize = strings.length * 9;
for (int slot = 0; slot < strings.length * 9; ++slot) {
final String string = strings[(int)Math.floor(slot / 9.0)];
if (string.length() != 9) {
throw new RuntimeException("String is not a length of 9. String is a length of " + string.length() + ". " + string);
}
final String letter = string.substring(slot % 9, slot % 9 + 1);
if (!letter.equalsIgnoreCase(PageLayout.empty)) {
if (!letter.equalsIgnoreCase(PageLayout.full)) {
throw new RuntimeException("Unrecognised value " + letter);
}
this.size.add(slot);
}
}
}
public ItemStack[] generate(final ArrayList<ItemStack> items) {
return this.generate((ItemStack[])items.toArray(new ItemStack[items.size()]));
}
public ItemStack[] generate(final ItemStack... items) {
final ItemStack[] itemArray = new ItemStack[this.invSize];
for (int i = 0; i < this.size.size(); ++i) {
if (i < items.length) {
final ItemStack itemToInsert = items[i];
if (itemToInsert != null) {
itemArray[this.size.get(i)] = itemToInsert.clone();
}
}
}
return itemArray;
}
}