Skip to content
This repository was archived by the owner on Jul 20, 2025. It is now read-only.

Commit 755b204

Browse files
authored
Merge pull request #38 from Lezurex/alert-rules
Custom alert rules
2 parents 7176059 + 6ee03c8 commit 755b204

File tree

15 files changed

+689
-41
lines changed

15 files changed

+689
-41
lines changed

LabyMod/addons-1.8/config/StatsAddon.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@
44
"reloadStatsKey": 34,
55
"versionCheckerEnabled": false,
66
"cooldown": 750,
7-
"rankWarnLevel": 100,
8-
"winrateWarnLevel": 40,
7+
"rankWarnLevel": 10,
8+
"winrateWarnLevel": 90,
99
"showStatsMessages": true,
10-
"cookiesPerGameWarnLevel": 1200,
11-
"webserverEnabled": true
10+
"cookiesPerGameWarnLevel": 6000,
11+
"webserverEnabled": true,
12+
"alertRules": [
13+
{
14+
"params": [
15+
{
16+
"value": 80.0,
17+
"paramType": "WINRATE",
18+
"compareType": "GREATER_THAN"
19+
},
20+
{
21+
"value": 10.0,
22+
"paramType": "GAMES",
23+
"compareType": "GREATER_THAN"
24+
}
25+
]
26+
}
27+
]
1228
}
1329
}

libs/lm_api_mc1.8.9.jar

602 KB
Binary file not shown.

src/main/java/com/voxcrafterlp/statsaddon/StatsAddon.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import com.voxcrafterlp.statsaddon.events.TickListener;
77
import com.voxcrafterlp.statsaddon.objects.PlayerStats;
88
import com.voxcrafterlp.statsaddon.objects.StatsType;
9+
import com.voxcrafterlp.statsaddon.objects.alertRules.AlertRuleManager;
910
import com.voxcrafterlp.statsaddon.utils.KeyPressUtil;
1011
import com.voxcrafterlp.statsaddon.utils.StatsChecker;
1112
import com.voxcrafterlp.statsaddon.utils.VersionChecker;
1213
import com.voxcrafterlp.statsaddon.webserver.Webserver;
14+
import javafx.scene.control.Alert;
1315
import lombok.Getter;
1416
import lombok.Setter;
1517
import net.labymod.api.LabyModAPI;
@@ -21,9 +23,6 @@
2123
import net.labymod.utils.Material;
2224
import net.labymod.utils.ModColor;
2325
import net.labymod.utils.ServerData;
24-
import net.minecraft.client.Minecraft;
25-
import net.minecraft.client.entity.EntityPlayerSP;
26-
import net.minecraft.client.network.NetHandlerPlayClient;
2726

2827
import java.util.HashMap;
2928
import java.util.List;
@@ -42,12 +41,13 @@ public class StatsAddon extends LabyModAddon {
4241
/**
4342
* String in default semantic versioning syntax: vX.Y.Z
4443
*/
45-
private final String currentVersion = "v2.0.2";
46-
private final boolean isPreRelease = false;
44+
private final String currentVersion = "v2.1.0";
45+
private final boolean isPreRelease = true;
4746

4847
private final String[] allowedPreReleaseUUIDs = new String[] {
4948
"4f08412d-5e85-46ec-89fd-028c1ed073a3",
5049
"20c018b7-970b-4eac-bbeb-713e72503f05",
50+
"96389139-c792-429e-b2c7-16532b38ed13"
5151
};
5252

5353
@Getter
@@ -57,6 +57,7 @@ public class StatsAddon extends LabyModAddon {
5757
private StatsChecker statsChecker;
5858
private Webserver webserver;
5959
private KeyPressUtil keyPressUtil;
60+
private AlertRuleManager alertRuleManager;
6061
@Setter
6162
private boolean lmcDoubled, online, websiteMessageShown;
6263

@@ -118,6 +119,7 @@ public void accept(final ServerData serverData) {
118119
});
119120

120121
this.statsChecker = new StatsChecker();
122+
this.alertRuleManager = new AlertRuleManager();
121123

122124
new Thread(() -> {
123125
System.out.println("Starting webserver..");
@@ -187,6 +189,8 @@ public void loadConfig() {
187189
else
188190
enabledGamemods.put(string, (!this.getConfig().has(string) || this.getConfig().get(string).getAsBoolean()));
189191
});
192+
193+
this.alertRuleManager.loadConfig();
190194
}
191195

192196
@Override

src/main/java/com/voxcrafterlp/statsaddon/objects/PlayerStats.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,36 +98,42 @@ public void performStatsAnalysis() {
9898
e.printStackTrace();
9999
}
100100

101-
if (this.wins != -1) {
102-
boolean firstMessageSent = false;
101+
if (StatsAddon.getInstance().getAlertRuleManager().getAlertRules().size() == 0) {
102+
if (this.wins != -1) {
103+
boolean firstMessageSent = false;
103104

104-
if (this.rank < StatsAddon.getInstance().getRankWarnLevel() && this.rank > 0) {
105-
LabyMod.getInstance().displayMessageInChat(StatsAddon.getInstance().getGamemodePrefix() + "\u00A74Achtung! \u00A77Potentiell gef\u00E4hrlicher Gegner\u00A77!");
106-
firstMessageSent = true;
107-
this.sendAlert(AlertType.RANK);
108-
}
109-
110-
if (this.winRate > (double) StatsAddon.getInstance().getWinrateWarnLevel()) {
111-
if (!firstMessageSent) {
112-
LabyMod.getInstance().displayMessageInChat(StatsAddon.getInstance().getGamemodePrefix() + "\u00A74Achtung! \u00A77Potentiell gef\u00E4hrlicher Gegner\u00A77!");
105+
if (this.rank < StatsAddon.getInstance().getRankWarnLevel() && this.rank > 0) {
106+
LabyMod.getInstance().displayMessageInChat(StatsAddon.getInstance()
107+
.getGamemodePrefix() + "\u00A74Achtung! \u00A77Potentiell gef\u00E4hrlicher Gegner\u00A77!");
113108
firstMessageSent = true;
109+
this.sendAlert(AlertType.RANK);
114110
}
115-
this.sendAlert(AlertType.WINRATE);
116-
}
117111

118-
final double cookiesPerGame = this.getCookiesPerGame();
119-
if (cookiesPerGame > 0) {
120-
if (cookiesPerGame > (double) StatsAddon.getInstance().getCookiesPerGameWarnLevel()) {
112+
if (this.winRate > (double) StatsAddon.getInstance().getWinrateWarnLevel() && this.playedGames > 10) {
121113
if (!firstMessageSent) {
122-
LabyMod.getInstance().displayMessageInChat(StatsAddon.getInstance().getGamemodePrefix() + "\u00A74Achtung! \u00A77Potentiell gef\u00E4hrlicher Gegner\u00A77!");
114+
LabyMod.getInstance().displayMessageInChat(StatsAddon.getInstance()
115+
.getGamemodePrefix() + "\u00A74Achtung! \u00A77Potentiell gef\u00E4hrlicher Gegner\u00A77!");
123116
firstMessageSent = true;
124117
}
125-
this.sendAlert(AlertType.COOKIESPERGAME);
118+
this.sendAlert(AlertType.WINRATE);
126119
}
120+
121+
final double cookiesPerGame = this.getCookiesPerGame();
122+
if (cookiesPerGame > 0) {
123+
if (cookiesPerGame > (double) StatsAddon.getInstance().getCookiesPerGameWarnLevel()) {
124+
if (!firstMessageSent) {
125+
LabyMod.getInstance().displayMessageInChat(StatsAddon.getInstance()
126+
.getGamemodePrefix() + "\u00A74Achtung! \u00A77Potentiell gef\u00E4hrlicher Gegner\u00A77!");
127+
firstMessageSent = true;
128+
}
129+
this.sendAlert(AlertType.COOKIESPERGAME);
130+
}
131+
}
132+
// If a warn message has been sent, player has been warned at least once.
133+
this.warned = firstMessageSent;
127134
}
128-
// If a warn message has been sent, player has been warned at least once.
129-
this.warned = firstMessageSent;
130135
}
136+
StatsAddon.getInstance().getAlertRuleManager().checkAll(this);
131137
}
132138

133139
/**
@@ -176,6 +182,9 @@ public void sendAlert(AlertType type) {
176182
case COOKIESPERGAME:
177183
LabyMod.getInstance().notifyMessageProfile(this.playerInfo.getGameProfile(), (int) this.getCookiesPerGame() + " Cookies/Spiel");
178184
break;
185+
case RULE:
186+
LabyMod.getInstance().notifyMessageProfile(this.playerInfo.getGameProfile(), "Regel ausgelöst!");
187+
break;
179188
}
180189

181190

@@ -259,6 +268,7 @@ public enum AlertType {
259268
RANK,
260269
WINRATE,
261270
COOKIESPERGAME,
271+
RULE
262272

263273
}
264274

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.voxcrafterlp.statsaddon.objects.alertRules;
2+
3+
import com.google.common.collect.Lists;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonObject;
6+
import com.voxcrafterlp.statsaddon.StatsAddon;
7+
import com.voxcrafterlp.statsaddon.objects.PlayerStats;
8+
import lombok.Getter;
9+
import net.labymod.main.LabyMod;
10+
11+
import java.util.List;
12+
13+
public class AlertRule {
14+
15+
@Getter
16+
private final List<StatsParam> statsParams;
17+
18+
public AlertRule(List<StatsParam> statsParams) {
19+
this.statsParams = statsParams;
20+
}
21+
22+
public boolean check(PlayerStats playerStats) {
23+
if (statsParams.size() == 0) return false;
24+
boolean failed = false;
25+
for (StatsParam statsParam : statsParams) {
26+
if (!statsParam.check(playerStats))
27+
failed = true;
28+
if (failed) break;
29+
}
30+
if (!failed) {
31+
LabyMod.getInstance().displayMessageInChat(StatsAddon.getInstance()
32+
.getGamemodePrefix() + "\u00A77Regel für Spieler \u00A7c" + playerStats
33+
.getPlayerName() + "\u00A77 ausgelöst\u00A78! (\u00A77" + getHumanReadable() + "\u00A78)");
34+
return true;
35+
}
36+
return false;
37+
}
38+
39+
public void addParam(StatsParam statsParam) {
40+
statsParams.add(statsParam);
41+
}
42+
43+
public String getHumanReadable() {
44+
StringBuilder stringBuilder = new StringBuilder();
45+
for (int i = 0; i < statsParams.size(); i++) {
46+
StatsParam statsParam = statsParams.get(i);
47+
stringBuilder.append(statsParam.genHumanReadable());
48+
if (i < statsParams.size() - 1)
49+
stringBuilder.append(", ");
50+
}
51+
return stringBuilder.toString();
52+
}
53+
54+
public JsonObject serialize() {
55+
JsonObject jsonObject = new JsonObject();
56+
JsonArray jsonArray = new JsonArray();
57+
for (StatsParam statsParam : statsParams) {
58+
jsonArray.add(statsParam.serialize());
59+
}
60+
jsonObject.add("params", jsonArray);
61+
return jsonObject;
62+
}
63+
64+
public static AlertRule deserialize(JsonObject jsonObject) {
65+
JsonArray jsonArray = jsonObject.getAsJsonArray("params");
66+
List<StatsParam> statsParams = Lists.newCopyOnWriteArrayList();
67+
jsonArray.forEach(param -> {
68+
try {
69+
statsParams.add(StatsParam.deserialize(param.getAsJsonObject()));
70+
} catch (IllegalArgumentException e) {
71+
e.printStackTrace();
72+
}
73+
});
74+
return new AlertRule(statsParams);
75+
}
76+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.voxcrafterlp.statsaddon.objects.alertRules;
2+
3+
import com.google.common.collect.Lists;
4+
import com.google.gson.JsonArray;
5+
import com.voxcrafterlp.statsaddon.StatsAddon;
6+
import com.voxcrafterlp.statsaddon.objects.PlayerStats;
7+
import lombok.Getter;
8+
import net.labymod.main.LabyMod;
9+
10+
import java.util.List;
11+
12+
public class AlertRuleManager {
13+
14+
@Getter
15+
private final List<AlertRule> alertRules = Lists.newCopyOnWriteArrayList();
16+
17+
public void loadConfig() {
18+
JsonArray jsonArray = StatsAddon.getInstance().getConfig().getAsJsonArray("alertRules");
19+
if (jsonArray != null) {
20+
jsonArray.forEach(jsonElement -> {
21+
alertRules.add(AlertRule.deserialize(jsonElement.getAsJsonObject()));
22+
});
23+
}
24+
}
25+
26+
public void checkAll(PlayerStats playerStats) {
27+
System.out.println(alertRules.toString());
28+
for (AlertRule alertRule : alertRules) {
29+
// Only the first positive alarm has to be sent
30+
if (alertRule.check(playerStats)) {
31+
playerStats.sendAlert(PlayerStats.AlertType.RULE);
32+
playerStats.setWarned(true);
33+
break;
34+
}
35+
}
36+
}
37+
38+
public void save() {
39+
if (StatsAddon.getInstance().getConfig().has("alertRules"))
40+
StatsAddon.getInstance().getConfig().remove("alertRules");
41+
StatsAddon.getInstance().getConfig().add("alertRules", new JsonArray());
42+
JsonArray jsonArray = StatsAddon.getInstance().getConfig().getAsJsonArray("alertRules");
43+
alertRules.forEach(alertRule -> {
44+
jsonArray.add(alertRule.serialize());
45+
});
46+
StatsAddon.getInstance().saveConfig();
47+
}
48+
}

0 commit comments

Comments
 (0)