aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/misc/GT_Command.java
blob: a91a696d37d02ef993ef2f7f77c6da8848627521 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package gregtech.common.misc;

import com.gtnewhorizon.structurelib.StructureLib;
import gregtech.GT_Mod;
import gregtech.api.enums.GT_Values;
import gregtech.api.interfaces.IGlobalWirelessEnergy;
import gregtech.api.objects.GT_ChunkManager;
import gregtech.common.GT_Pollution;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.EnumChatFormatting;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Stream;

public final class GT_Command extends CommandBase implements IGlobalWirelessEnergy {

    @Override
    public String getCommandName() {
        return "gt";
    }

    @Override
    public String getCommandUsage(ICommandSender sender) {
        return "Usage: gt <subcommand>. Valid subcommands are: toggle, chunks, pollution.";
    }

    private void printHelp(ICommandSender sender) {
        sender.addChatMessage(new ChatComponentText("Usage: gt <toggle|chunks|pollution|global_energy_add|global_energy_set|global_energy_join>"));
        sender.addChatMessage(new ChatComponentText("\"toggle D1\" - toggles general.Debug (D1)"));
        sender.addChatMessage(new ChatComponentText("\"toggle D2\" - toggles general.Debug2 (D2)"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugCleanroom\" - toggles cleanroom debug log"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugDriller\" - toggles oil drill debug log"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugBlockPump\" - Possible issues with pumps"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugBlockMiner\" - Possible issues with miners"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugEntityCramming\" - How long it takes and how many entities it finds"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugWorldGen\" - toggles generic worldgen debug"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugOrevein\" - toggles worldgen ore vein debug"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugSmallOres\" - toggles worldgen small vein debug"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugStones\" - toggles worldgen stones debug"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugChunkloaders\" - toggles chunkloaders debug"));
        sender.addChatMessage(new ChatComponentText("\"toggle debugMulti\" - toggles structurelib debug"));
        sender.addChatMessage(new ChatComponentText("\"chunks\" - print a list of the force loaded chunks"));
        sender.addChatMessage(new ChatComponentText(
                "\"pollution <amount>\" - adds the <amount> of the pollution to the current chunk, " +
                        "\n if <amount> isnt specified, will add" + GT_Mod.gregtechproxy.mPollutionSmogLimit + "gibbl."
        ));
        sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + " --- Global wireless EU controls ---"));
        sender.addChatMessage(new ChatComponentText("Allows you to set the amount of EU in a users wireless network."));
        sender.addChatMessage(new ChatComponentText("Usage: global_energy_set [Name] [EU]"));
        sender.addChatMessage(new ChatComponentText("Allows you to add EU to a users wireless network. Also accepts negative numbers."));
        sender.addChatMessage(new ChatComponentText("Usage: global_energy_add [Name] [EU]"));
        sender.addChatMessage(new ChatComponentText("Allows you to join two users together into one network. Can be undone by writing the users name twice."));
        sender.addChatMessage(new ChatComponentText("Usage: global_energy_join [User joining] [User to join]"));
    }

    @Override
    public List addTabCompletionOptions(ICommandSender sender, String[] ss) {
        List<String> l = new ArrayList<>();
        String test = ss.length == 0 ? "" : ss[0].trim();
        if (ss.length == 0 || ss.length == 1 && (test.isEmpty() || Stream.of("toggle", "chunks", "pollution", "global_energy_add", "global_energy_set", "global_energy_join").anyMatch(s -> s.startsWith(test)))) {
            Stream.of("toggle", "chunks", "pollution", "global_energy_add", "global_energy_set", "global_energy_join")
                    .filter(s -> test.isEmpty() || s.startsWith(test))
                    .forEach(l::add);
        } else if (test.equals("toggle")) {
            String test1 = ss[1].trim();
            Stream.of("D1", "D2", "debugCleanroom", "debugDriller", "debugBlockPump", "debugBlockMiner", "debugWorldGen", "debugEntityCramming",
                    "debugOrevein", "debugSmallOres", "debugStones", "debugChunkloaders", "debugMulti", "debugWorldData")
                    .filter(s -> test1.isEmpty() || s.startsWith(test1))
                    .forEach(l::add);
        }
        return l;
    }

    @Override
    public void processCommand(ICommandSender sender, String[] strings) {
        if (strings.length < 1) {
            printHelp(sender);
            return;
        }

        String username;
        String uuid;

        switch (strings[0]) {
            case "toggle":
                if (strings.length < 2) {
                    printHelp(sender);
                    return;
                }
                if ("debugMulti".equals(strings[1])) {
                    StructureLib.DEBUG_MODE = !StructureLib.DEBUG_MODE;
                    sender.addChatMessage(new ChatComponentText(strings[1] + " = " + (StructureLib.DEBUG_MODE ? "true" : "false")));
                    return;
                }
                try {
                    Field field = GT_Values.class.getDeclaredField(strings[1]);
                    if (field.getType() != boolean.class) {
                        sender.addChatMessage(new ChatComponentText("Wrong variable: " + strings[1]));
                        return;
                    }
                    boolean b = !field.getBoolean(null);
                    field.setBoolean(null, b);
                    sender.addChatMessage(new ChatComponentText(strings[1] + " = " + (b ? "true" : "false")));
                } catch (Exception e) {
                    sender.addChatMessage(new ChatComponentText("No such variable: " + strings[0]));
                }
                break;
            case "chunks":
                GT_ChunkManager.printTickets();
                sender.addChatMessage(new ChatComponentText("Forced chunks logged to GregTech.log"));
                break;
            case "pollution":
                ChunkCoordinates coordinates = sender.getPlayerCoordinates();
                int amount = (strings.length < 2) ? GT_Mod.gregtechproxy.mPollutionSmogLimit : Integer.parseInt(strings[1]);
                GT_Pollution.addPollution(sender
                        .getEntityWorld()
                        .getChunkFromBlockCoords(
                            coordinates.posX,
                            coordinates.posZ
                        ),
                    amount
                );
                break;
            case "global_energy_add":

                username = strings[1];
                uuid = IGlobalWirelessEnergy.super.GetUUIDFromUsername(username);

                String EU_String = strings[2];

                // Usage is /gt global_energy_add username EU

                if (uuid.equals("")) {
                    sender.addChatMessage(new ChatComponentText("User " + username + " has no global energy network."));
                    break;
                }

                if (addEUToGlobalEnergyMap(uuid, new BigInteger(EU_String)))
                    sender.addChatMessage(new ChatComponentText("Successfully added " + EU_String + "EU to the global energy network of " + username + "."));
                else
                    sender.addChatMessage(new ChatComponentText("Failed to add " + EU_String + "EU to the global energy map of " + username +
                        ". Insufficient energy in network. " + username + "currently has " + IGlobalWirelessEnergy.super.GetUserEU(uuid).toString() + " in their network."));

                break;
            case "global_energy_set":

                // Usage is /gt global_energy_set username EU

                username = strings[1];
                uuid = IGlobalWirelessEnergy.super.GetUUIDFromUsername(username);

                String EU_String_0 = strings[2];

                IGlobalWirelessEnergy.super.SetUserEU(uuid, new BigInteger(EU_String_0));
                break;

            case "global_energy_join":

                // Usage is /gt global_energy_join username_of_you username_to_join

                String username_0 = strings[1];
                String username_1 = strings[2];

                String uuid_0 = IGlobalWirelessEnergy.super.GetUUIDFromUsername(username_0);
                String uuid_1 = IGlobalWirelessEnergy.super.GetUUIDFromUsername(username_1);

                if (uuid_0.equals("")) {
                    sender.addChatMessage(new ChatComponentText("User " + username_0 + " has no global energy network."));
                    break;
                }

                if (uuid_0.equals("")) {
                    sender.addChatMessage(new ChatComponentText("User " + username_1 + " has no global energy network."));
                    break;
                }

                IGlobalWirelessEnergy.super.JoinUserNetwork(uuid_0, uuid_1);

                sender.addChatMessage(new ChatComponentText("Success! " + username_0 + " has joined " + username_1 + "."));
                sender.addChatMessage(new ChatComponentText("To undo this simply join your own network again with /gt global_energy_join " + username_0 + " " + username_0 + "."));

                break;
            default:
                sender.addChatMessage(new ChatComponentText( EnumChatFormatting.RED + "Invalid command/syntax detected."));
                printHelp(sender);
        }
    }
}