From 1f3239d3262127f33c0a99fe8853c07391b863ce Mon Sep 17 00:00:00 2001 From: ThatGamerBlue Date: Tue, 5 May 2020 01:38:43 +0100 Subject: [PATCH] config: add buttons --- .../runelite/client/config/ConfigManager.java | 180 ++++++++---------- .../client/plugins/config/ConfigPanel.java | 15 ++ 2 files changed, 90 insertions(+), 105 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/config/ConfigManager.java b/runelite-client/src/main/java/net/runelite/client/config/ConfigManager.java index bd4471c544..8c8c2ec704 100644 --- a/runelite-client/src/main/java/net/runelite/client/config/ConfigManager.java +++ b/runelite-client/src/main/java/net/runelite/client/config/ConfigManager.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2017, Adam + * Copyright (c) 2020, ThatGamerBlue * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -56,6 +57,7 @@ import java.util.Objects; import java.util.Properties; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; import java.util.stream.Collectors; import javax.inject.Inject; import javax.inject.Named; @@ -83,6 +85,7 @@ public class ConfigManager private final ConfigInvocationHandler handler = new ConfigInvocationHandler(this); private final Properties properties = new Properties(); private final Map pendingChanges = new HashMap<>(); + private final Map> consumers = new HashMap<>(); private final File settingsFileInput; private final Groups groups; @@ -425,6 +428,7 @@ public class ConfigManager { handler.invalidate(); properties.clear(); + consumers.clear(); try (FileInputStream in = new FileInputStream(settingsFileInput)) { @@ -539,6 +543,12 @@ public class ConfigManager public void setConfiguration(String groupName, String key, Object value) { + // do not save consumers for buttons, they cannot be changed anyway + if (value instanceof Consumer) + { + return; + } + setConfiguration(groupName, key, objectToString(value)); } @@ -688,55 +698,74 @@ public class ConfigManager continue; } - if (!method.isDefault()) + if (method.getReturnType().isAssignableFrom(Consumer.class)) { - if (override) + Object defaultValue; + try { - String current = getConfiguration(group.value(), item.keyName()); - // only unset if already set + defaultValue = ConfigInvocationHandler.callDefaultMethod(proxy, method, null); + } + catch (Throwable ex) + { + log.warn(null, ex); + continue; + } + + log.debug("Registered consumer: {}.{}", group.value(), item.keyName()); + consumers.put(group.value() + "." + item.keyName(), (Consumer) defaultValue); + } + else + { + if (!method.isDefault()) + { + if (override) + { + String current = getConfiguration(group.value(), item.keyName()); + // only unset if already set + if (current != null) + { + unsetConfiguration(group.value(), item.keyName()); + } + } + continue; + } + + if (!override) + { + // This checks if it is set and is also unmarshallable to the correct type; so + // we will overwrite invalid config values with the default + Object current = getConfiguration(group.value(), item.keyName(), method.getReturnType()); if (current != null) { - unsetConfiguration(group.value(), item.keyName()); + continue; // something else is already set } } - continue; - } - if (!override) - { - // This checks if it is set and is also unmarshallable to the correct type; so - // we will overwrite invalid config values with the default - Object current = getConfiguration(group.value(), item.keyName(), method.getReturnType()); - if (current != null) + Object defaultValue; + try { - continue; // something else is already set + defaultValue = ConfigInvocationHandler.callDefaultMethod(proxy, method, null); + } + catch (Throwable ex) + { + log.warn(null, ex); + continue; } - } - Object defaultValue; - try - { - defaultValue = ConfigInvocationHandler.callDefaultMethod(proxy, method, null); - } - catch (Throwable ex) - { - log.warn(null, ex); - continue; - } + String current = getConfiguration(group.value(), item.keyName()); + String valueString = objectToString(defaultValue); + // null and the empty string are treated identically in sendConfig and treated as an unset + // If a config value defaults to "" and the current value is null, it will cause an extra + // unset to be sent, so treat them as equal + if (Objects.equals(current, valueString) || (Strings.isNullOrEmpty(current) && Strings.isNullOrEmpty(valueString))) + { + continue; // already set to the default value + } - String current = getConfiguration(group.value(), item.keyName()); - String valueString = objectToString(defaultValue); - // null and the empty string are treated identically in sendConfig and treated as an unset - // If a config value defaults to "" and the current value is null, it will cause an extra - // unset to be sent, so treat them as equal - if (Objects.equals(current, valueString) || (Strings.isNullOrEmpty(current) && Strings.isNullOrEmpty(valueString))) - { - continue; // already set to the default value + log.debug("Setting default configuration value for {}.{} to {}", group.value(), item.keyName(), defaultValue); + + setConfiguration(group.value(), item.keyName(), valueString); } - - log.debug("Setting default configuration value for {}.{} to {}", group.value(), item.keyName(), defaultValue); - - setConfiguration(group.value(), item.keyName(), valueString); } } @@ -747,74 +776,7 @@ public class ConfigManager */ public void setDefaultConfiguration(Object proxy, boolean override, Plugin plugin) { - Class clazz = proxy.getClass().getInterfaces()[0]; - ConfigGroup group = clazz.getAnnotation(ConfigGroup.class); - - if (group == null) - { - return; - } - - for (Method method : clazz.getDeclaredMethods()) - { - ConfigItem item = method.getAnnotation(ConfigItem.class); - - // only apply default configuration for methods which read configuration (0 args) - if (item == null || method.getParameterCount() != 0) - { - continue; - } - - if (!method.isDefault()) - { - if (override) - { - String current = getConfiguration(group.value(), item.keyName()); - // only unset if already set - if (current != null) - { - unsetConfiguration(group.value(), item.keyName()); - } - } - continue; - } - - if (!override) - { - // This checks if it is set and is also unmarshallable to the correct type; so - // we will overwrite invalid config values with the default - Object current = getConfiguration(group.value(), item.keyName(), method.getReturnType()); - if (current != null) - { - continue; // something else is already set - } - } - - Object defaultValue; - try - { - defaultValue = ConfigInvocationHandler.callDefaultMethod(proxy, method, null); - } - catch (Throwable ex) - { - log.warn(null, ex); - continue; - } - - String current = getConfiguration(group.value(), item.keyName()); - String valueString = objectToString(defaultValue); - // null and the empty string are treated identically in sendConfig and treated as an unset - // If a config value defaults to "" and the current value is null, it will cause an extra - // unset to be sent, so treat them as equal - if (Objects.equals(current, valueString) || (Strings.isNullOrEmpty(current) && Strings.isNullOrEmpty(valueString))) - { - continue; // already set to the default value - } - - log.debug("Setting default configuration value for {}.{} to {}", group.value(), item.keyName(), defaultValue); - - setConfiguration(group.value(), item.keyName(), valueString); - } + setDefaultConfiguration(proxy, override); } public void sendConfig() @@ -914,4 +876,12 @@ public class ConfigManager e.printStackTrace(); } } + + /** + * Retrieves a consumer from config group and key name + */ + public Consumer getConsumer(final String configGroup, final String keyName) + { + return consumers.getOrDefault(configGroup + "." + keyName, (p) -> log.error("Failed to retrieve consumer with name {}.{}", configGroup, keyName)); + } } \ No newline at end of file diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java index 41506de9e8..92139785af 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java @@ -1,5 +1,6 @@ /* * Copyright (c) 2017, Adam + * Copyright (c) 2020, ThatGamerBlue * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -46,6 +47,7 @@ import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Consumer; import javax.inject.Inject; import javax.swing.BorderFactory; import javax.swing.BoxLayout; @@ -587,6 +589,19 @@ class ConfigPanel extends PluginPanel item.add(checkbox, BorderLayout.EAST); } + if (cid.getType().isAssignableFrom(Consumer.class)) + { + item.remove(configEntryName); + + JButton button = new JButton(cid.getItem().name()); + button.addActionListener((e) -> { + log.debug("Running consumer: {}.{}", cd.getGroup().value(), cid.getItem().keyName()); + configManager.getConsumer(cd.getGroup().value(), cid.getItem().keyName()).accept(pluginConfig.getPlugin()); + }); + + item.add(button, BorderLayout.CENTER); + } + if (cid.getType() == int.class) { int value;