use of net.robinfriedli.aiode.command.parser.ArgumentBuildingMode in project aiode by robinfriedli.
the class GuildPropertyInterceptor method updatePresets.
private void updatePresets(AbstractGuildProperty argumentPrefixProperty, Character oldArgumentPrefix, char newArgumentPrefix, Session session) {
List<Preset> presets = queryBuilderFactory.find(Preset.class).build(session).getResultList();
String defaultValue = argumentPrefixProperty.getDefaultValue();
char[] chars = defaultValue.toCharArray();
char defaultArgPrefix;
if (chars.length == 1) {
defaultArgPrefix = chars[0];
} else {
defaultArgPrefix = ArgumentPrefixProperty.DEFAULT_FALLBACK;
}
char argPrefix = oldArgumentPrefix != null ? oldArgumentPrefix : defaultArgPrefix;
ArgumentPrefixProperty.Config argumentPrefixConfig = new ArgumentPrefixProperty.Config(argPrefix, defaultArgPrefix);
for (Preset preset : presets) {
Aiode aiode = Aiode.get();
AbstractCommand command = preset.instantiateCommand(aiode.getCommandManager(), commandContext, preset.getName());
String oldPreset = preset.getPreset();
StringBuilder newPresetBuilder = new StringBuilder(oldPreset);
List<Integer> oldPrefixOccurrences = Lists.newArrayList();
CommandParser commandParser = new CommandParser(command, argumentPrefixConfig, new CommandParseListener() {
@Override
public void onModeSwitch(CommandParser.Mode previousMode, CommandParser.Mode newMode, int index, char character) {
// they are in fact argument prefixes
if (newMode instanceof ArgumentBuildingMode) {
oldPrefixOccurrences.add(index);
}
}
});
commandParser.parse(oldPreset);
for (int oldPrefixOccurrence : oldPrefixOccurrences) {
newPresetBuilder.setCharAt(oldPrefixOccurrence, newArgumentPrefix);
}
String newPreset = newPresetBuilder.toString();
if (!oldPreset.equals(newPreset)) {
preset.setPreset(newPreset);
}
}
}
Aggregations