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