forked from SteamWar/SteamWar
Toggle two-option particle settings on click
Choices with exactly two options switch on click like on/off settings, and their lore lists both options with the current one highlighted.
This commit is contained in:
@@ -154,9 +154,13 @@ class EntryEditor {
|
||||
Player player = context.player();
|
||||
Object value = context.entry().getValues().get(parameter);
|
||||
List<String> lore = new ArrayList<>();
|
||||
lore.add(msg("PARTICLE_VALUE", player, valueText(parameter, value, player)));
|
||||
if (parameter instanceof ChoiceParameter<?> choice && isToggle(choice)) {
|
||||
lore.addAll(optionLines((ChoiceParameter<Object>) choice, value, player));
|
||||
} else {
|
||||
lore.add(msg("PARTICLE_VALUE", player, valueText(parameter, value, player)));
|
||||
}
|
||||
lore.add("");
|
||||
lore.add(msg(parameter instanceof BooleanParameter ? "PARTICLE_VALUE_TOGGLE" : "PARTICLE_VALUE_CLICK", player));
|
||||
lore.add(msg(isToggle(parameter) ? "PARTICLE_VALUE_TOGGLE" : "PARTICLE_VALUE_CLICK", player));
|
||||
|
||||
Material icon = ICONS.getOrDefault(parameter, Material.PAPER);
|
||||
if (parameter instanceof BooleanParameter) icon = (Boolean) value ? Material.LIME_DYE : Material.GRAY_DYE;
|
||||
@@ -180,11 +184,36 @@ class EntryEditor {
|
||||
ValueEditors.editColors(player, () -> context.entry().getValues().get(gradient), gradient.getMaxColors(),
|
||||
colors -> apply(context, gradient, colors), context::reopen);
|
||||
} else if (parameter instanceof ChoiceParameter<?> choice) {
|
||||
pick(context, (ChoiceParameter<Object>) choice, value);
|
||||
if (isToggle(choice)) {
|
||||
toggle(context, (ChoiceParameter<Object>) choice, value);
|
||||
} else {
|
||||
pick(context, (ChoiceParameter<Object>) choice, value);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Booleans and choices with exactly two options switch on click instead of opening a screen
|
||||
private boolean isToggle(Parameter<?> parameter) {
|
||||
return parameter instanceof BooleanParameter || (parameter instanceof ChoiceParameter<?> choice && choice.getBuildableValues().size() == 2);
|
||||
}
|
||||
|
||||
// Both options of a toggle, the current one highlighted
|
||||
private <E> List<String> optionLines(ChoiceParameter<E> parameter, E current, Player player) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
for (E option : parameter.getBuildableValues()) {
|
||||
String name = choiceName(parameter, option, player).replaceAll("§.", "");
|
||||
lines.add(option == current ? "§a» " + name : "§8» §7" + name);
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
private <E> void toggle(Context context, ChoiceParameter<E> parameter, E current) {
|
||||
List<E> values = parameter.getBuildableValues();
|
||||
apply(context, parameter, values.get(values.indexOf(current) == 0 ? 1 : 0));
|
||||
context.reopen();
|
||||
}
|
||||
|
||||
private <E> void pick(Context context, ChoiceParameter<E> parameter, E current) {
|
||||
Pickers.choice(context.player(), parameter, current, EntryEditor::choiceIcon, picked -> {
|
||||
apply(context, parameter, picked);
|
||||
|
||||
Reference in New Issue
Block a user