From ff0424b7f0c4e01bc665f114e76ea7ec0b64df1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= Date: Thu, 9 May 2024 22:19:58 +0200 Subject: [PATCH 1/9] feat: Added the possibility to use all the keys --- .../config/actions/values/StatTypeValue.java | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index 9c6d3165e..2b0707751 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -14,31 +14,50 @@ @ValueData(name = "stat-type", description = "Gets a certain Stat type from a bot", example = "stat-type(experience, earned)") public class StatTypeValue implements Value, Parser { - private Stats.General key; + private StatsAPI.Key key; private StatsAPI.Stat stat; private StatData dataType; @Override public @Nullable Number get(Main main) { - if (stat == null && key != null) stat = main.statsManager.getStat(key); + if (stat == null && key != null) + stat = main.statsManager.getStat(key); if (dataType == null || key == null || stat == null) { return null; } switch (dataType) { - case INITIAL: return stat.getInitial(); - case CURRENT: return stat.getCurrent(); - case EARNED: return stat.getEarned(); - case SPENT: return stat.getSpent(); - case DIFFERENCE: return stat.getEarned() - stat.getSpent(); - default: throw new IllegalStateException("Undefined operation " + dataType); + case INITIAL: + return stat.getInitial(); + case CURRENT: + return stat.getCurrent(); + case EARNED: + return stat.getEarned(); + case SPENT: + return stat.getSpent(); + case DIFFERENCE: + return stat.getEarned() - stat.getSpent(); + default: + throw new IllegalStateException("Undefined operation " + dataType); } } - private Stats.General getKeyFromString(String key) { - for (Stats.General stat : Stats.General.values()) { - if (stat.name().equalsIgnoreCase(key)) return stat; + private StatsAPI.Key getKeyFromString(String key) { + for (Stats.General statGeneral : Stats.General.values()) { + if (statGeneral.name().equalsIgnoreCase(key)) + return statGeneral; } + + for (Stats.Bot statBot : Stats.Bot.values()) { + if (statBot.name().equalsIgnoreCase(key)) + return statBot; + } + + for (Stats.BootyKey statKey : Stats.BootyKey.values()) { + if (statKey.name().equalsIgnoreCase(key)) + return statKey; + } + return null; } @@ -56,7 +75,8 @@ public String toString() { public static StatData of(String sd) { for (StatData statData : StatData.values()) { - if (statData.toString().equalsIgnoreCase(sd)) return statData; + if (statData.toString().equalsIgnoreCase(sd)) + return statData; } return null; } From ff32bd4f543c2d2554a35799322c0fc4b4437182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= Date: Thu, 9 May 2024 22:22:16 +0200 Subject: [PATCH 2/9] updated the format --- .../config/actions/values/StatTypeValue.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index 2b0707751..33829a792 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -20,25 +20,18 @@ public class StatTypeValue implements Value, Parser { @Override public @Nullable Number get(Main main) { - if (stat == null && key != null) - stat = main.statsManager.getStat(key); + if (stat == null && key != null) stat = main.statsManager.getStat(key); if (dataType == null || key == null || stat == null) { return null; } switch (dataType) { - case INITIAL: - return stat.getInitial(); - case CURRENT: - return stat.getCurrent(); - case EARNED: - return stat.getEarned(); - case SPENT: - return stat.getSpent(); - case DIFFERENCE: - return stat.getEarned() - stat.getSpent(); - default: - throw new IllegalStateException("Undefined operation " + dataType); + case INITIAL: return stat.getInitial(); + case CURRENT: return stat.getCurrent(); + case EARNED: return stat.getEarned(); + case SPENT: return stat.getSpent(); + case DIFFERENCE: return stat.getEarned() - stat.getSpent(); + default: throw new IllegalStateException("Undefined operation " + dataType); } } @@ -75,8 +68,7 @@ public String toString() { public static StatData of(String sd) { for (StatData statData : StatData.values()) { - if (statData.toString().equalsIgnoreCase(sd)) - return statData; + if (statData.toString().equalsIgnoreCase(sd)) return statData; } return null; } From 66ab53709357e44a8d8d828082cacdb4950b34be Mon Sep 17 00:00:00 2001 From: dm94 Date: Mon, 20 May 2024 21:58:38 +0200 Subject: [PATCH 3/9] feat: Now returns the full list of stats --- .../config/actions/values/StatTypeValue.java | 52 +++++++++---------- .../darkbot/core/manager/StatsManager.java | 13 +++++ 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index 33829a792..95eae14e5 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -6,8 +6,12 @@ import com.github.manolo8.darkbot.config.actions.Value; import com.github.manolo8.darkbot.config.actions.ValueData; import com.github.manolo8.darkbot.config.actions.parser.ParseUtil; -import eu.darkbot.api.game.stats.Stats; +import com.github.manolo8.darkbot.config.actions.parser.Values; +import com.github.manolo8.darkbot.core.manager.StatsManager; + import eu.darkbot.api.managers.StatsAPI; +import eu.darkbot.api.managers.StatsAPI.Key; + import org.jetbrains.annotations.Nullable; import java.util.Locale; @@ -20,38 +24,30 @@ public class StatTypeValue implements Value, Parser { @Override public @Nullable Number get(Main main) { - if (stat == null && key != null) stat = main.statsManager.getStat(key); + if (stat == null && key != null) + stat = main.statsManager.getStat(key); if (dataType == null || key == null || stat == null) { return null; } switch (dataType) { - case INITIAL: return stat.getInitial(); - case CURRENT: return stat.getCurrent(); - case EARNED: return stat.getEarned(); - case SPENT: return stat.getSpent(); - case DIFFERENCE: return stat.getEarned() - stat.getSpent(); - default: throw new IllegalStateException("Undefined operation " + dataType); + case INITIAL: + return stat.getInitial(); + case CURRENT: + return stat.getCurrent(); + case EARNED: + return stat.getEarned(); + case SPENT: + return stat.getSpent(); + case DIFFERENCE: + return stat.getEarned() - stat.getSpent(); + default: + throw new IllegalStateException("Undefined operation " + dataType); } } private StatsAPI.Key getKeyFromString(String key) { - for (Stats.General statGeneral : Stats.General.values()) { - if (statGeneral.name().equalsIgnoreCase(key)) - return statGeneral; - } - - for (Stats.Bot statBot : Stats.Bot.values()) { - if (statBot.name().equalsIgnoreCase(key)) - return statBot; - } - - for (Stats.BootyKey statKey : Stats.BootyKey.values()) { - if (statKey.name().equalsIgnoreCase(key)) - return statKey; - } - - return null; + return StatsManager.getStatKeys().stream().filter(s -> s.name().equals(key)).findFirst().orElse(null); } public enum StatData { @@ -68,7 +64,9 @@ public String toString() { public static StatData of(String sd) { for (StatData statData : StatData.values()) { - if (statData.toString().equalsIgnoreCase(sd)) return statData; + if (statData.toString().equalsIgnoreCase(sd)) { + return statData; + } } return null; } @@ -87,7 +85,9 @@ public String parse(String str) throws SyntaxException { stat = null; if (key == null) { - throw new SyntaxException("Unknown stat-type: '" + params[0] + "'", str, Stats.General.class); + throw new SyntaxException("Unknown stat-type: '" + params[0] + "'", str, Values.getMeta(getClass()), + StatsManager.getStatKeys().stream().map(Key::name) + .toArray(String[]::new)); } params = ParseUtil.separate(params, getClass(), ",").split("\\)", 2); diff --git a/src/main/java/com/github/manolo8/darkbot/core/manager/StatsManager.java b/src/main/java/com/github/manolo8/darkbot/core/manager/StatsManager.java index 6a21306ee..03137980a 100644 --- a/src/main/java/com/github/manolo8/darkbot/core/manager/StatsManager.java +++ b/src/main/java/com/github/manolo8/darkbot/core/manager/StatsManager.java @@ -18,6 +18,8 @@ import org.jetbrains.annotations.Nullable; import java.time.Duration; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -31,6 +33,8 @@ public class StatsManager implements Manager, StatsAPI, NativeUpdatable { private final Main main; private final EventBrokerAPI eventBroker; + private static StatsManager INSTANCE; + private long address; public int userId; @@ -49,6 +53,7 @@ public class StatsManager implements Manager, StatsAPI, NativeUpdatable { public StatsManager(Main main, EventBrokerAPI eventBroker) { this.main = main; this.eventBroker = eventBroker; + INSTANCE = this; registerImpl(Stats.Bot.RUNTIME, runtime = createStat()); @@ -123,6 +128,14 @@ private void registerImpl(Key key, StatImpl stat) { statistics.put(StatKey.of(key), stat); } + public static Collection getStatKeys() { + return Collections.unmodifiableSet(getInstance().statistics.keySet()); + } + + public static StatsManager getInstance() { + return INSTANCE; + } + @Override public void setStatValue(Key key, double v) { if (key.namespace() == null) throw new UnsupportedOperationException(); From ac549002c0b9461768ec14f31cf148518ae6f968 Mon Sep 17 00:00:00 2001 From: dm94 Date: Mon, 20 May 2024 22:21:50 +0200 Subject: [PATCH 4/9] feat: Added a tokenizer for the stats condition --- .../config/actions/values/StatTypeValue.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index 95eae14e5..b176ab685 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -46,8 +46,17 @@ public class StatTypeValue implements Value, Parser { } } - private StatsAPI.Key getKeyFromString(String key) { - return StatsManager.getStatKeys().stream().filter(s -> s.name().equals(key)).findFirst().orElse(null); + private StatsAPI.Key getKeyFromString(String token) { + String[] tokenParts = token.split(":"); + String statKey = tokenParts[tokenParts.length - 1]; + String statCategory = tokenParts[tokenParts.length - 2]; + String statNamespace = tokenParts.length >= 3 ? tokenParts[0] : null; + + return StatsManager.getStatKeys().stream() + .filter(s -> statNamespace == null || s.namespace().equals(statNamespace)) + .filter(s -> s.category().equals(statCategory)) + .filter(s -> s.name().equals(statKey)) + .findFirst().orElse(null); } public enum StatData { @@ -77,6 +86,10 @@ public String toString() { return "stat-type(" + key.name().toLowerCase(Locale.ROOT) + "," + dataType + ")"; } + private String getKeyFormatted(Key key) { + return key.namespace() != null ? key.namespace() + ":" : "" + key.category() + ":" + key.name(); + } + @Override public String parse(String str) throws SyntaxException { String[] params = str.split(" *, *", 2); @@ -86,7 +99,7 @@ public String parse(String str) throws SyntaxException { if (key == null) { throw new SyntaxException("Unknown stat-type: '" + params[0] + "'", str, Values.getMeta(getClass()), - StatsManager.getStatKeys().stream().map(Key::name) + StatsManager.getStatKeys().stream().map(this::getKeyFormatted) .toArray(String[]::new)); } From db6ab878c6db5b528aa3352be2b72a5b9e49f1c1 Mon Sep 17 00:00:00 2001 From: dm94 Date: Mon, 20 May 2024 22:24:42 +0200 Subject: [PATCH 5/9] fix: Fixed the IDE format --- .../config/actions/values/StatTypeValue.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index b176ab685..fcf276c16 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -24,25 +24,18 @@ public class StatTypeValue implements Value, Parser { @Override public @Nullable Number get(Main main) { - if (stat == null && key != null) - stat = main.statsManager.getStat(key); + if (stat == null && key != null) stat = main.statsManager.getStat(key); if (dataType == null || key == null || stat == null) { return null; } switch (dataType) { - case INITIAL: - return stat.getInitial(); - case CURRENT: - return stat.getCurrent(); - case EARNED: - return stat.getEarned(); - case SPENT: - return stat.getSpent(); - case DIFFERENCE: - return stat.getEarned() - stat.getSpent(); - default: - throw new IllegalStateException("Undefined operation " + dataType); + case INITIAL: return stat.getInitial(); + case CURRENT: return stat.getCurrent(); + case EARNED: return stat.getEarned(); + case SPENT: return stat.getSpent(); + case DIFFERENCE: return stat.getEarned() - stat.getSpent(); + default: throw new IllegalStateException("Undefined operation " + dataType); } } From 650678fcf918f21858a37d2baf49a0bc9a8287d1 Mon Sep 17 00:00:00 2001 From: dm94 Date: Mon, 20 May 2024 22:39:39 +0200 Subject: [PATCH 6/9] fix: Fixed null exception --- .../config/actions/values/StatTypeValue.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index fcf276c16..d26b35e96 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -41,14 +41,15 @@ public class StatTypeValue implements Value, Parser { private StatsAPI.Key getKeyFromString(String token) { String[] tokenParts = token.split(":"); + + String statNamespace = tokenParts.length == 3 ? tokenParts[0] : null; + String statCategory = tokenParts.length >= 2 ? tokenParts[tokenParts.length - 2] : null; String statKey = tokenParts[tokenParts.length - 1]; - String statCategory = tokenParts[tokenParts.length - 2]; - String statNamespace = tokenParts.length >= 3 ? tokenParts[0] : null; return StatsManager.getStatKeys().stream() .filter(s -> statNamespace == null || s.namespace().equals(statNamespace)) - .filter(s -> s.category().equals(statCategory)) - .filter(s -> s.name().equals(statKey)) + .filter(s -> statCategory == null || s.category().equals(statCategory)) + .filter(s -> statKey == null || s.name().equals(statKey)) .findFirst().orElse(null); } @@ -79,8 +80,9 @@ public String toString() { return "stat-type(" + key.name().toLowerCase(Locale.ROOT) + "," + dataType + ")"; } - private String getKeyFormatted(Key key) { - return key.namespace() != null ? key.namespace() + ":" : "" + key.category() + ":" + key.name(); + private String getKeyFormatted(Key keyToFormat) { + return (keyToFormat.namespace() != null ? keyToFormat.namespace() + ":" : "") + keyToFormat.category() + ":" + + keyToFormat.name(); } @Override @@ -92,7 +94,8 @@ public String parse(String str) throws SyntaxException { if (key == null) { throw new SyntaxException("Unknown stat-type: '" + params[0] + "'", str, Values.getMeta(getClass()), - StatsManager.getStatKeys().stream().map(this::getKeyFormatted) + StatsManager.getStatKeys().stream() + .map(this::getKeyFormatted) .toArray(String[]::new)); } From cc4e9ffa6aee60d2c5088259ecc8db82b8de29f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= Date: Fri, 21 Jun 2024 00:29:21 +0200 Subject: [PATCH 7/9] Update src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java Co-authored-by: Pablo Herrera --- .../manolo8/darkbot/config/actions/values/StatTypeValue.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index d26b35e96..ce23e82e1 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -40,7 +40,7 @@ public class StatTypeValue implements Value, Parser { } private StatsAPI.Key getKeyFromString(String token) { - String[] tokenParts = token.split(":"); + String[] tokenParts = token.split(":", 3); String statNamespace = tokenParts.length == 3 ? tokenParts[0] : null; String statCategory = tokenParts.length >= 2 ? tokenParts[tokenParts.length - 2] : null; From 5b48f0e120827990b7cb1314b721c3326875a2e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= Date: Fri, 21 Jun 2024 00:29:33 +0200 Subject: [PATCH 8/9] Update src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java Co-authored-by: Pablo Herrera --- .../manolo8/darkbot/config/actions/values/StatTypeValue.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index ce23e82e1..246bca870 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -42,8 +42,8 @@ public class StatTypeValue implements Value, Parser { private StatsAPI.Key getKeyFromString(String token) { String[] tokenParts = token.split(":", 3); - String statNamespace = tokenParts.length == 3 ? tokenParts[0] : null; - String statCategory = tokenParts.length >= 2 ? tokenParts[tokenParts.length - 2] : null; + String statNamespace = tokenParts.length >= 2 ? tokenParts[0] : null; + String statCategory = tokenParts.length == 3 ? tokenParts[1] : null; String statKey = tokenParts[tokenParts.length - 1]; return StatsManager.getStatKeys().stream() From 0a19b7bbc86b8d199389388375a66f728ac97fe4 Mon Sep 17 00:00:00 2001 From: dm94 Date: Sun, 23 Jun 2024 10:23:49 +0200 Subject: [PATCH 9/9] fix: FIx for the comments --- .../config/actions/values/StatTypeValue.java | 18 +++++++++--------- .../darkbot/core/manager/StatsManager.java | 9 +-------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java index 246bca870..4f8ba3c08 100644 --- a/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java +++ b/src/main/java/com/github/manolo8/darkbot/config/actions/values/StatTypeValue.java @@ -40,16 +40,16 @@ public class StatTypeValue implements Value, Parser { } private StatsAPI.Key getKeyFromString(String token) { - String[] tokenParts = token.split(":", 3); + String[] tokenParts = token.split(":"); - String statNamespace = tokenParts.length >= 2 ? tokenParts[0] : null; - String statCategory = tokenParts.length == 3 ? tokenParts[1] : null; + String statNamespace = tokenParts.length == 3 ? tokenParts[0] : null; + String statCategory = tokenParts.length >= 2 ? tokenParts[tokenParts.length - 2] : null; String statKey = tokenParts[tokenParts.length - 1]; return StatsManager.getStatKeys().stream() - .filter(s -> statNamespace == null || s.namespace().equals(statNamespace)) - .filter(s -> statCategory == null || s.category().equals(statCategory)) - .filter(s -> statKey == null || s.name().equals(statKey)) + .filter(s -> statNamespace == null || s.namespace().equalsIgnoreCase(statNamespace)) + .filter(s -> statCategory == null || s.category().equalsIgnoreCase(statCategory)) + .filter(s -> s.name().equalsIgnoreCase(statKey)) .findFirst().orElse(null); } @@ -77,12 +77,12 @@ public static StatData of(String sd) { @Override public String toString() { - return "stat-type(" + key.name().toLowerCase(Locale.ROOT) + "," + dataType + ")"; + return "stat-type(" + getKeyFormatted(key) + "," + dataType + ")"; } private String getKeyFormatted(Key keyToFormat) { - return (keyToFormat.namespace() != null ? keyToFormat.namespace() + ":" : "") + keyToFormat.category() + ":" - + keyToFormat.name(); + return ((keyToFormat.namespace() != null ? keyToFormat.namespace() + ":" : "") + keyToFormat.category() + ":" + + keyToFormat.name()).toLowerCase(Locale.ROOT); } @Override diff --git a/src/main/java/com/github/manolo8/darkbot/core/manager/StatsManager.java b/src/main/java/com/github/manolo8/darkbot/core/manager/StatsManager.java index 03137980a..7e39daadb 100644 --- a/src/main/java/com/github/manolo8/darkbot/core/manager/StatsManager.java +++ b/src/main/java/com/github/manolo8/darkbot/core/manager/StatsManager.java @@ -33,8 +33,6 @@ public class StatsManager implements Manager, StatsAPI, NativeUpdatable { private final Main main; private final EventBrokerAPI eventBroker; - private static StatsManager INSTANCE; - private long address; public int userId; @@ -53,7 +51,6 @@ public class StatsManager implements Manager, StatsAPI, NativeUpdatable { public StatsManager(Main main, EventBrokerAPI eventBroker) { this.main = main; this.eventBroker = eventBroker; - INSTANCE = this; registerImpl(Stats.Bot.RUNTIME, runtime = createStat()); @@ -129,11 +126,7 @@ private void registerImpl(Key key, StatImpl stat) { } public static Collection getStatKeys() { - return Collections.unmodifiableSet(getInstance().statistics.keySet()); - } - - public static StatsManager getInstance() { - return INSTANCE; + return Collections.unmodifiableSet(Main.INSTANCE.statsManager.statistics.keySet()); } @Override