Skip to content

Commit

Permalink
now determining the number of pages based on the highest position, as…
Browse files Browse the repository at this point in the history
… to allow for arbitrary icon-placement; substituted brute-force loops by position-trackers; added pagination-support to AvailableShops
  • Loading branch information
BlvckBytes committed Jan 16, 2025
1 parent e3e3c07 commit 999bc94
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 47 deletions.
63 changes: 37 additions & 26 deletions modules/API/src/main/java/de/Keyle/MyPet/api/gui/IconMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public class IconMenu implements Listener {
private int currentPageIndex;

private OptionClickEventHandler handler;

protected Map<Integer, IconMenuItem> options = new HashMap<>(54);
private int maximumOptionPosition;
private int nextVacantOptionPosition;

private final Plugin plugin;

Expand All @@ -73,12 +76,7 @@ public int getSize() {
if (pageSizeInSlots != null)
return pageSizeInSlots;

int size = 0;
for (int i : options.keySet()) {
if (++i > size) {
size = i;
}
}
int size = maximumOptionPosition + 1;
return (int) (Math.ceil(size / 9.) * 9);
}

Expand All @@ -97,21 +95,40 @@ public IconMenu setPaginationIdentifier(String identifier) {
return this;
}

public IconMenu setOption(int position, IconMenuItem icon) {
if (position >= 0 && (pageSizeInSlots != null || position < 54)) {
options.put(position, icon);
}
return this;
private void advanceNextVacantOptionPosition() {
do {
++nextVacantOptionPosition;
} while (options.containsKey(nextVacantOptionPosition));
}

public void setOption(int position, IconMenuItem icon) {
if (position < 0)
return;

if (position > maximumOptionPosition)
maximumOptionPosition = position;

if (position == nextVacantOptionPosition)
advanceNextVacantOptionPosition();

options.put(position, icon);
}

public int addOption(IconMenuItem icon) {
for (int i = 0; i < 54; i++) {
if (!options.containsKey(i)) {
options.put(i, icon);
return i;
}
}
return -1;
int position = nextVacantOptionPosition;

options.put(position, icon);
advanceNextVacantOptionPosition();

return position;
}

private int getNumberOfPages() {
if (pageSizeInSlots == null)
return 1;

int pageCapacity = pageSizeInSlots - 9;
return ((maximumOptionPosition + 1) + (pageCapacity - 1)) / pageCapacity;
}

private String substituteVariablesAndColors(String input) {
Expand All @@ -120,12 +137,9 @@ private String substituteVariablesAndColors(String input) {
if (pageSizeInSlots == null)
return colorizedInput;

int pageCapacity = pageSizeInSlots - 9;
int numberOfPages = (options.size() + (pageCapacity - 1)) / pageCapacity;

return colorizedInput
.replace("{currentPage}", String.valueOf(currentPageIndex + 1))
.replace("{numberOfPages}", String.valueOf(numberOfPages));
.replace("{numberOfPages}", String.valueOf(getNumberOfPages()));
}

private IconMenuItem makeConfigurableItem(String key) {
Expand Down Expand Up @@ -221,10 +235,7 @@ private void nextPage() {
if (pageSizeInSlots == null)
return;

int pageCapacity = pageSizeInSlots - 9;
int numberOfPages = (options.size() + (pageCapacity - 1)) / pageCapacity;

if (currentPageIndex == numberOfPages - 1)
if (currentPageIndex == getNumberOfPages() - 1)
return;

++currentPageIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package de.Keyle.MyPet.commands;

import de.Keyle.MyPet.MyPetApi;
import de.Keyle.MyPet.api.Util;
import de.Keyle.MyPet.api.WorldGroup;
import de.Keyle.MyPet.api.commands.CommandTabCompleter;
import de.Keyle.MyPet.api.gui.IconMenu;
Expand Down Expand Up @@ -120,30 +119,31 @@ public void run() {
event.setWillClose(true);
event.setWillDestroy(true);
}
}, MyPetApi.getPlugin());
}, MyPetApi.getPlugin()).setPaginationIdentifier("AvailableShops");

ItemDatabase itemDatabase = MyPetApi.getServiceManager().getService(ItemDatabase.class).get();

Queue<PetShop> filler = new ArrayDeque<>();

for (String shopname : availableShops) {
PetShop s = shopManager.get().getShop(shopname);
IconMenuItem icon = new IconMenuItem();
icon.setTitle(RESET + Colorizer.setColors(s.getDisplayName()));

SkilltreeIcon si = s.getIcon();
MaterialHolder material = itemDatabase.getByID(si.getMaterial());
if (material == null) {
material = itemDatabase.getByID("chest");
}
icon.setMaterial(material.getMaterial()).setGlowing(si.isGlowing());
if (material.isLegacy()) {
icon.setData(material.getLegacyId().getData());
}
if (Util.isBetween(0, 53, s.getPosition())) {
menu.setOption(s.getPosition(), icon);
shops.put(s.getPosition(), s.getName());
} else {
shops.put(menu.addOption(icon), s.getName());
int position = s.getPosition();

if (position < 0) {
filler.add(s);
continue;
}

menu.setOption(position, makeShopIcon(s, itemDatabase));
shops.put(position, s.getName());
}

while (!filler.isEmpty()) {
PetShop s = filler.poll();

int position = menu.addOption(makeShopIcon(s, itemDatabase));
shops.put(position, s.getName());
}

menu.open(player);
Expand All @@ -157,6 +157,23 @@ public void run() {
return true;
}

private IconMenuItem makeShopIcon(PetShop s, ItemDatabase itemDatabase) {
IconMenuItem icon = new IconMenuItem();
icon.setTitle(RESET + Colorizer.setColors(s.getDisplayName()));

SkilltreeIcon si = s.getIcon();
MaterialHolder material = itemDatabase.getByID(si.getMaterial());
if (material == null) {
material = itemDatabase.getByID("chest");
}
icon.setMaterial(material.getMaterial()).setGlowing(si.isGlowing());
if (material.isLegacy()) {
icon.setData(material.getLegacyId().getData());
}

return icon;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String s, String[] strings) {
Optional<ShopManager> shopManager = MyPetApi.getServiceManager().getService(ShopManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,12 @@ public void load(ConfigurationSection section) {
converter.convert(pet);
}

if (Util.isBetween(0, 53, pet.getPosition())) {
this.pets.put(pet.getPosition(), pet);
} else {
if (pet.getPosition() < 0) {
filler.add(pet);
return;
}

this.pets.put(pet.getPosition(), pet);
} catch (MyPetTypeNotFoundException ignored) {
}
});
Expand Down

0 comments on commit 999bc94

Please sign in to comment.