aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kr/syeyoung/dungeonsguide/commands/CommandReparty.java
blob: 26f8cfd02c56e5ab11388a817611a12a2907d5dd (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *     Dungeons Guide - The most intelligent Hypixel Skyblock Dungeons Mod
 *     Copyright (C) 2021  cyoung06
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package kr.syeyoung.dungeonsguide.commands;

import kr.syeyoung.dungeonsguide.features.FeatureRegistry;
import kr.syeyoung.dungeonsguide.utils.TextUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

import java.util.ArrayList;
import java.util.List;

public class CommandReparty extends CommandBase {
    private String command;
    public CommandReparty() {
        command = FeatureRegistry.ETC_REPARTY.<String>getParameter("command").getValue();
        command = command.replace(" ", "");
    }

    @Override
    public String getCommandName() {
        return command;
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return command;
    }

    @Override
    public void processCommand(ICommandSender sender, String[] args) {
        requestReparty();
    }

    public enum Phase {
        NOT,
        REQUESTED,
        RECEIVE_PARTYMEMBERS,
        DISBAND,
        REPARTY
    }

    private final List<String> players = new ArrayList<String>();
    private long nextTrigger = Long.MAX_VALUE;
    private Phase phase = Phase.NOT;
    @SubscribeEvent
    public void onChat(ClientChatReceivedEvent e) {
        if (e.type == 2) return;
        if (e.message.getFormattedText().startsWith("§6Party Members ") && phase == Phase.REQUESTED) {
            players.clear();
            phase = Phase.RECEIVE_PARTYMEMBERS;
        }
        if (e.message.getFormattedText().startsWith("§cYou are not currently in a party.§r") && phase == Phase.REQUESTED) {
            phase = Phase.NOT;
        }
        String txt = e.message.getFormattedText();
        if (txt.startsWith("§eParty ") && txt.contains(":")) {
            String playerNames = TextUtils.stripColor(txt.split(":")[1]);
            String myname = Minecraft.getMinecraft().getSession().getUsername();
            for (String s : playerNames.split(" ")) {
                if (s.isEmpty()) continue;
                if (s.equals("●")) continue;
                if (s.startsWith("[")) continue;
                if (s.equalsIgnoreCase(myname)) continue;
                players.add(s);
            }
        }
        if (e.message.getFormattedText().equals("§9§m-----------------------------§r") && phase == Phase.RECEIVE_PARTYMEMBERS) {
            phase = Phase.DISBAND;
            nextTrigger = System.currentTimeMillis() + 500;
        }
    }

    @SubscribeEvent
    public void onTick(TickEvent.ClientTickEvent e) {
        if (nextTrigger < System.currentTimeMillis() && (phase == Phase.DISBAND || phase == Phase.REPARTY)) {
            if (phase == Phase.DISBAND) {
                nextTrigger = System.currentTimeMillis() + 1000;
                phase = Phase.REPARTY;
                Minecraft.getMinecraft().thePlayer.sendChatMessage("/p disband");
            } else {
                phase = Phase.NOT;
                nextTrigger = Long.MAX_VALUE;
                StringBuilder sb = new StringBuilder();
                sb.append("/p invite");
                for (String player : players) {
                    sb.append(" ").append(player);
                }
                Minecraft.getMinecraft().thePlayer.sendChatMessage(sb.toString());
            }
        }
    }

    public void requestReparty() {
        if (phase == Phase.NOT) {
            phase = Phase.REQUESTED;
            Minecraft.getMinecraft().thePlayer.sendChatMessage("/pl");
        }
    }

    @Override
    public int getRequiredPermissionLevel() {
        return 0;
    }
}