|
1 | 1 | package de.rettichlp.common.manager; |
2 | 2 |
|
3 | | -import com.mojang.brigadier.Message; |
| 3 | +import de.rettichlp.common.listener.MessageListener; |
4 | 4 | import de.rettichlp.common.storage.schema.WantedEntry; |
5 | | -import lombok.NoArgsConstructor; |
6 | 5 | import net.minecraft.client.MinecraftClient; |
7 | 6 | import net.minecraft.client.network.ClientPlayNetworkHandler; |
8 | 7 |
|
|
15 | 14 | import static java.util.Optional.ofNullable; |
16 | 15 | import static java.util.regex.Pattern.compile; |
17 | 16 |
|
18 | | -@NoArgsConstructor |
19 | | -public class MessageManager { |
| 17 | +public class WantedManager implements MessageListener { |
20 | 18 |
|
21 | 19 | private final static Pattern WELCOME_BACK_PATTERN = compile("Willkommen zurück!"); |
22 | 20 | private final static Pattern ONLINE_WANTED_PLAYERS_HEADER_PATTERN = compile("Online Spieler mit WantedPunkten:"); |
23 | 21 | private final static Pattern ONLINE_WANTED_PLAYERS_ENTRY_PATTERN = compile("- (?<playerName>[A-Za-z0-9_]+) \\| (?<wantedPointAmount>\\d+) \\| (?<reason>.+)(?<afk> \\| AFK|)"); |
24 | 22 |
|
25 | | - private String rawMessage; |
26 | | - private ClientPlayNetworkHandler networkHandler; |
| 23 | + private final ClientPlayNetworkHandler networkHandler; |
| 24 | + |
27 | 25 | private long activeWantedCheck = 0; |
28 | 26 |
|
29 | | - public void process(Message message) { |
30 | | - this.rawMessage = message.getString(); |
| 27 | + public WantedManager() { |
31 | 28 | this.networkHandler = ofNullable(MinecraftClient.getInstance().player) |
32 | 29 | .map(clientPlayerEntity -> clientPlayerEntity.networkHandler) |
33 | 30 | .orElseThrow(); |
34 | | - |
35 | | - executeJoinCommands(); |
36 | | - readWantedPoints(); |
37 | 31 | } |
38 | 32 |
|
39 | | - private void executeJoinCommands() { |
40 | | - Matcher welcomeBackMatcher = WELCOME_BACK_PATTERN.matcher(this.rawMessage); |
| 33 | + @Override |
| 34 | + public void onMessage(String message) { |
| 35 | + Matcher welcomeBackMatcher = WELCOME_BACK_PATTERN.matcher(message); |
41 | 36 | if (welcomeBackMatcher.find()) { |
42 | | - |
43 | 37 | this.networkHandler.sendChatCommand("wanteds"); |
| 38 | + return; |
44 | 39 | } |
45 | | - } |
46 | 40 |
|
47 | | - public void readWantedPoints() { |
48 | | - Matcher onlineWantedPlayersHeaderMatcher = ONLINE_WANTED_PLAYERS_HEADER_PATTERN.matcher(this.rawMessage); |
| 41 | + Matcher onlineWantedPlayersHeaderMatcher = ONLINE_WANTED_PLAYERS_HEADER_PATTERN.matcher(message); |
49 | 42 | if (onlineWantedPlayersHeaderMatcher.find()) { |
50 | 43 | this.activeWantedCheck = currentTimeMillis(); |
51 | 44 | storage.resetWantedEntries(); |
52 | 45 | return; |
53 | 46 | } |
54 | 47 |
|
55 | | - Matcher onlineWantedPlayersEntryMatcher = ONLINE_WANTED_PLAYERS_ENTRY_PATTERN.matcher(this.rawMessage); |
| 48 | + Matcher onlineWantedPlayersEntryMatcher = ONLINE_WANTED_PLAYERS_ENTRY_PATTERN.matcher(message); |
56 | 49 | if (onlineWantedPlayersEntryMatcher.find() && (currentTimeMillis() - this.activeWantedCheck < 2000)) { |
57 | 50 | String playerName = onlineWantedPlayersEntryMatcher.group("playerName"); |
58 | 51 | int wantedPointAmount = parseInt(onlineWantedPlayersEntryMatcher.group("wantedPointAmount")); |
|
0 commit comments