Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions core/src/main/java/com/nisovin/magicspells/spells/MenuSpell.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public MenuSpell(MagicConfig config, String spellName) {
option.spellSwapName = getConfigString(path + "spell-swap", "");
option.power = getConfigFloat(path + "power", 1);
option.modifierList = getConfigStringList(path + "modifiers", null);
option.sortMode = SortMode.fromConfigValue(getConfigString(path + "sort-mode", "none"));
option.stayOpen = getConfigBoolean(path + "stay-open", false);
Comment thread
DragonsAscent marked this conversation as resolved.
options.put(optionName, option);
}
Expand Down Expand Up @@ -293,9 +294,28 @@ private void applyOptionsToInventory(Player opener, MenuInventory menu) {
} else quantity = (int) Math.round(variable.getValue(opener));
item.setAmount(quantity);

// Set item for all defined slots.
for (int slot : option.slots) {
if (inv.getItem(slot) == null) inv.setItem(slot, item);
// Set item for the defined slots based on the configured fill behavior.
switch (option.sortMode) {
case FIRST -> {
for (int slot : option.slots) {
if (inv.getItem(slot) != null) continue;
inv.setItem(slot, item);
break;
}
}
case LAST -> {
for (int index = option.slots.size() - 1; index >= 0; index--) {
int slot = option.slots.get(index);
if (inv.getItem(slot) != null) continue;
inv.setItem(slot, item);
break;
}
}
case NONE -> {
for (int slot : option.slots) {
if (inv.getItem(slot) == null) inv.setItem(slot, item);
}
}
}
}
// Fill inventory.
Expand Down Expand Up @@ -434,6 +454,22 @@ private enum PostClickState {
IGNORE
}

private enum SortMode {
NONE,
FIRST,
LAST;

private static SortMode fromConfigValue(String value) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably get rid of this, and use a normal Util#enumValueSafe. Unfortunately, you still have to uppercase, but feel free to change that.

This is not just to reduce redundancy, and localise the "default/fallback" value, but because it simplifies it too. Ignore the AI suggestion because that's even more of a roundabout way. Even a simple try/catch to parse the enum would've been enough.

Keep the config default, pass it to enumValueSafe, check if null was returned, then report that it was invalid and have it fall back to none.

if (value == null) return NONE;

return switch (value.toLowerCase()) {
case "first" -> FIRST;
case "last" -> LAST;
default -> NONE;
};
}
}

private static class MenuOption {

private String menuOptionName;
Expand All @@ -456,6 +492,7 @@ private static class MenuOption {
private float power;
private List<String> modifierList;
private ModifierSet menuOptionModifiers;
private SortMode sortMode;
private boolean stayOpen;

}
Expand Down
Loading