aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/Reparty.java
blob: d6e799aba3b30e09afc56061415d1819ca3c6e33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package me.xmrvizzy.skyblocker.skyblock.dungeon;

import me.xmrvizzy.skyblocker.SkyblockerMod;
import me.xmrvizzy.skyblocker.chat.ChatFilterResult;
import me.xmrvizzy.skyblocker.chat.ChatPatternListener;
import me.xmrvizzy.skyblocker.utils.Utils;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.text.Text;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Reparty extends ChatPatternListener {
    private static final MinecraftClient client = MinecraftClient.getInstance();
    private static final SkyblockerMod skyblocker = SkyblockerMod.getInstance();
    public static final Pattern PLAYER = Pattern.compile(" ([a-zA-Z0-9_]{2,16}) ●");
    private static final int BASE_DELAY = 10;

    private String[] players;
    private int playersSoFar;
    private boolean repartying;

    public Reparty() {
        super("^(?:You are not currently in a party\\.|Party (?:Membe|Moderato)rs(?: \\(([0-9]+)\\)|:( .*)))$");
        this.repartying = false;
        ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(ClientCommandManager.literal("rp").executes(context -> {
            if (!Utils.isOnSkyblock() || this.repartying || client.player == null) return 0;
            this.repartying = true;
            SkyblockerMod.getInstance().messageScheduler.sendMessageAfterCooldown("/p list");
            return 0;
        })));
    }

    @Override
    public ChatFilterResult state() {
        return this.repartying ? ChatFilterResult.FILTER : ChatFilterResult.PASS;
    }

    @Override
    public boolean onMatch(Text message, Matcher matcher) {
        if (matcher.group(1) != null) {
            this.playersSoFar = 0;
            this.players = new String[Integer.parseInt(matcher.group(1)) - 1];
        } else if (matcher.group(2) != null) {
            Matcher m = PLAYER.matcher(matcher.group(2));
            while (m.find()) {
                this.players[playersSoFar++] = m.group(1);
            }
        } else {
            this.repartying = false;
            return false;
        }
        if (this.playersSoFar == this.players.length) reparty();
        return false;
    }

    private void reparty() {
        ClientPlayerEntity playerEntity = client.player;
        if (playerEntity == null) {
            this.repartying = false;
            return;
        }
        sendCommand("p disband", 1);
        for (int i = 0; i < this.players.length; ++i) {
            String command = "p invite " + this.players[i];
            sendCommand(command, i + 2);
        }
        skyblocker.scheduler.schedule(() -> this.repartying = false, this.players.length + 2);
    }

    private void sendCommand(String command, int delay) {
        skyblocker.messageScheduler.queueMessage(command, delay * BASE_DELAY);
    }
}