Created
August 1, 2016 00:43
-
-
Save picknchew/2e9574d3e234ad0f49f194a293c4e3b4 to your computer and use it in GitHub Desktop.
A class for creating dynamically sizing inventories.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import org.bukkit.Bukkit; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.ItemStack; | |
import com.google.common.collect.Table; | |
public class PageInventory { | |
private final String title; | |
private final int size; | |
private final List<ItemStack> items; | |
private final List<Inventory> inventories = new ArrayList<Inventory>(); | |
private final Table<PageType, Integer, ItemStack> stationaryItems; | |
public PageInventory(String title, int size, List<ItemStack> items, Table<PageType, Integer, ItemStack> stationaryItems) { | |
this.title = title != null ? title : "Inventory"; | |
this.size = size; | |
this.items = items; | |
this.stationaryItems = stationaryItems; | |
init(); | |
} | |
public PageInventory(int size, List<ItemStack> items, Table<PageType, Integer, ItemStack> stationaryItems) { | |
this(null, size, items, stationaryItems); | |
} | |
public PageInventory(int size, List<ItemStack> items) { | |
this(null, size, items, null); | |
} | |
public PageInventory(List<ItemStack> items, Table<PageType, Integer, ItemStack> stationaryItems) { | |
this(54, items, stationaryItems); | |
} | |
public PageInventory(List<ItemStack> items) { | |
this(54, items); | |
} | |
public boolean containsInventory(Inventory inventory) { | |
return inventories.contains(inventory); | |
} | |
public Inventory getPage(int page) { | |
return inventories.get(page); | |
} | |
private void init() { | |
Map<PageType, Map<Integer, ItemStack>> stationaryItems = this.stationaryItems != null ? this.stationaryItems.rowMap() : null; | |
int itemAmount = items.size(); | |
if (stationaryItems == null) { | |
for (int fromIndex = 0, toIndex = size; fromIndex < itemAmount; fromIndex += size, toIndex += size) { | |
Inventory inventory = Bukkit.createInventory(null, size, title); | |
inventory.addItem(items.subList(fromIndex, toIndex).toArray(new ItemStack[size])); | |
} | |
return; | |
} | |
Map<Integer, ItemStack> normalStationaryItems = stationaryItems.get(PageType.NORMAL); | |
if (normalStationaryItems != null) { | |
if (size - normalStationaryItems.size() >= items.size()) { | |
final Inventory inventory = Bukkit.createInventory(null, size, title); | |
normalStationaryItems.entrySet().stream().forEach(entry -> inventory.setItem(entry.getKey(), entry.getValue())); | |
items.stream().forEach(item -> inventory.setItem(inventory.firstEmpty(), item)); | |
inventories.add(inventory); | |
return; | |
} | |
} | |
Inventory inventory = null; | |
for (int index = 0; index < itemAmount; index++) { | |
if (inventory != null) { | |
int firstEmpty = inventory.firstEmpty(); | |
if (firstEmpty > -1) { | |
inventory.setItem(firstEmpty, items.get(index)); | |
continue; | |
} | |
} | |
inventory = Bukkit.createInventory(null, size, title); | |
inventories.add(inventory); | |
PageType type = null; | |
int itemsRemaining = items.size() - index + 1; | |
if (inventories.size() == 1) { | |
type = PageType.NEXT; | |
} | |
if (type == null) { | |
Map<Integer, ItemStack> backStationaryItems = stationaryItems.get(PageType.BACK); | |
if (backStationaryItems != null) { | |
if (itemsRemaining <= size - backStationaryItems.size()) { | |
type = PageType.BACK; | |
} else { | |
type = PageType.BACK_AND_NEXT; | |
} | |
} | |
} | |
for (Entry<Integer, ItemStack> entry : stationaryItems.get(type).entrySet()) { | |
inventory.setItem(entry.getKey(), entry.getValue()); | |
} | |
inventory.setItem(inventory.firstEmpty(), items.get(index)); | |
} | |
} | |
public enum PageType { | |
NORMAL, NEXT, BACK_AND_NEXT, BACK | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment