Skip to content

Commit

Permalink
Complexity reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Aug 8, 2019
1 parent 1e04e48 commit e6fd6e0
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/main/java/world/bentobox/bentobox/api/panels/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,18 @@ protected void makePanel(String name, Map<Integer, PanelItem> items, int size, U
PanelListener listener) {
this.name = name;
this.items = items;
// If size is undefined (0) then use the number of items
if (size == 0) {
size = items.keySet().size();
}
size = fixSize(size);
// Create panel
if (size > 0) {
// Make sure size is a multiple of 9 and is 54 max.
size = size + 8;
size -= (size % 9);
if (size > 54) size = 54;

inventory = Bukkit.createInventory(null, size, name);
// Fill the inventory and return
for (Map.Entry<Integer, PanelItem> en: items.entrySet()) {
if (en.getKey() < 54) {
inventory.setItem(en.getKey(), en.getValue().getItem());
// Get player head async
if (en.getValue().isPlayerHead()) {
HeadGetter.getHead(en.getValue(), this);
}
inventory = Bukkit.createInventory(null, size, name);
// Fill the inventory and return
for (Map.Entry<Integer, PanelItem> en: items.entrySet()) {
if (en.getKey() < 54) {
inventory.setItem(en.getKey(), en.getValue().getItem());
// Get player head async
if (en.getValue().isPlayerHead()) {
HeadGetter.getHead(en.getValue(), this);
}
}
} else {
inventory = Bukkit.createInventory(null, 9, name);
}
this.listener = listener;
// If the listener is defined, then run setup
Expand All @@ -75,6 +63,22 @@ protected void makePanel(String name, Map<Integer, PanelItem> items, int size, U
if (user != null) this.open(user);
}

private int fixSize(int size) {
// If size is undefined (0) then use the number of items
if (size == 0) {
size = items.keySet().size();
}
if (size > 0) {
// Make sure size is a multiple of 9 and is 54 max.
size = size + 8;
size -= (size % 9);
if (size > 54) size = 54;
} else {
return 9;
}
return size;
}

@NonNull
@Override
public Inventory getInventory() {
Expand Down

0 comments on commit e6fd6e0

Please sign in to comment.