Skip to content
Merged
Show file tree
Hide file tree
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
40 changes: 37 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 @@ -133,6 +133,14 @@ public MenuSpell(MagicConfig config, String spellName) {
option.power = getConfigFloat(path + "power", 1);
option.modifierList = getConfigStringList(path + "modifiers", null);
option.stayOpen = getConfigBoolean(path + "stay-open", false);

String modeString = getConfigString(path + "sort-mode", "none");
option.sortMode = Util.enumValueSafe(SortMode.class, modeString);
if (option.sortMode == null) {
MagicSpells.error("MenuSpell '" + internalName + "' has an invalid 'sort-mode' defined for '" + optionName + "': '" + modeString + "'. Falling back to 'none'.");
option.sortMode = SortMode.NONE;
}

options.put(optionName, option);
}
size = (int) Math.ceil((maxSlot + 1) / 9.0) * 9;
Expand Down Expand Up @@ -293,9 +301,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 +461,12 @@ private enum PostClickState {
IGNORE
}

private enum SortMode {
NONE,
FIRST,
LAST
}

private static class MenuOption {

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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MobGoalEditSpell(MagicConfig config, String spellName) {
List<String> removeTypeStrings = getConfigStringList("remove-types", null);
if (removeTypeStrings != null) {
for (String string : removeTypeStrings) {
GoalType type = Util.enumValueSafe(GoalType.class, string.toUpperCase());
GoalType type = Util.enumValueSafe(GoalType.class, string);
if (type == null) {
MagicSpells.error("MobGoalEditSpell '" + internalName + "' lists an invalid value in 'remove-types': " + string);
continue;
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/nisovin/magicspells/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ public static <C extends Collection<Material>> C getMaterialList(List<String> st

public static <E extends Enum<E>> E enumValueSafe(Class<E> clazz, String name) {
try {
return Enum.valueOf(clazz, name);
return Enum.valueOf(clazz, name.toUpperCase());
} catch (Exception e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void load(ConfigurationSection recipesSection) {
}

String typeName = config.getString("type", "none");
RecipeType type = Util.enumValueSafe(RecipeType.class, typeName.toUpperCase());
RecipeType type = Util.enumValueSafe(RecipeType.class, typeName);
if (type == null) {
MagicSpells.error("Recipe '%s' has an invalid 'type' defined: %s".formatted(recipeKey, typeName));
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected final <E extends Enum<E>> E resolveEnum(ConfigurationSection config, S
String received = config.getString(path);
if (received == null) return null;

E value = Util.enumValueSafe(enumClass, received.toUpperCase());
E value = Util.enumValueSafe(enumClass, received);
if (value != null) return value;

MagicSpells.error("Invalid %s '%s' for option '%s' on custom recipe '%s'.".formatted(enumClass.getSimpleName(), received, path, config.getName()));
Expand Down
Loading