aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/gamemodes/SBGamemodes.java
blob: 183241545bada66d68137c315f810882f687c141 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package io.github.moulberry.notenoughupdates.gamemodes;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.util.SBInfo;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;
import java.util.HashMap;

public class SBGamemodes {

    private static final Gson gson = new Gson();

    public static final int MODIFIER_DEVILISH = 0b1;
    public static final int MODIFIER_NOBANK = 0b10;
    public static final int MODIFIER_SMALLISLAND = 0b100;

    public static final String MODIFIER_DEVILISH_DISPLAY = EnumChatFormatting.DARK_PURPLE+"Devilish";
    public static final String MODIFIER_NOBANK_DISPLAY = EnumChatFormatting.RED+"No"+EnumChatFormatting.GOLD+"Bank";
    public static final String MODIFIER_SMALLISLAND_DISPLAY = EnumChatFormatting.GREEN+"SmallIsland";

    public static final String MODIFIER_DEVILISH_DESC = EnumChatFormatting.DARK_PURPLE+"Devilish\n" +
            "You are NOT allowed to use fairy souls.";
    public static final String MODIFIER_NOBANK_DESC = EnumChatFormatting.RED+"No"+EnumChatFormatting.GOLD+"Bank\n" +
            "You are NOT allowed to use the bank.";
    public static final String MODIFIER_SMALLISLAND_DESC = EnumChatFormatting.GREEN+"SmallIsland\n" +
            "Your private island is 1/4 the normal size.";

    private static HashMap<String, Gamemode> currentGamemode = new HashMap<>();
    private static long lastDeathExemption = 0;

    public static class Gamemode {
        public HardcoreMode hardcoreMode = HardcoreMode.NORMAL;
        public IronmanMode ironmanMode = IronmanMode.NORMAL;
        public int gamemodeModifiers = 0;

        public boolean locked = true;
    }

    public enum HardcoreMode {
        NORMAL("Normal", "Normal"),
        SOFTCORE(EnumChatFormatting.RED+"Soft"+EnumChatFormatting.DARK_RED+"core\n" +
                "You only have 1 life.\nDying will remove your hardcore status.\nDeaths to the void or \'unknown\' are exempted.",
                "You died.", "You fell into the void"),
        HARDCORE(EnumChatFormatting.DARK_RED+"Hardcore\n" +
                "You only have 1 life.\nDying will remove your hardcore status.");

        public final String display;
        public final String desc;
        private String[] exemptions;

        HardcoreMode(String display, String... exemptions) {
            this.display = display.split("\n")[0];
            this.desc = display;
            this.exemptions = exemptions;
        }

        public boolean isExemption(String line) {
            for(String exemption : exemptions) {
                if(line.contains(exemption)) return true;
            }
            return false;
        }
    }

    public enum IronmanMode {
        NORMAL("Normal"),
        IRONMAN(EnumChatFormatting.WHITE+"Ironman\n" +
                "You are NOT allowed to trade or use the auction house.",
                "You   ", "Auction House", "Auctions Browser", "Auction View"),
        IRONMANPLUS(EnumChatFormatting.WHITE+"Ironman"+EnumChatFormatting.GOLD+"+\n" +
                "You are NOT allowed to trade, use the auction house or bazaar.",
                "You   ", "Auction House", "Auctions Browser", "Auction View", "Bazaar"),
        ULTIMATE_IRONMAN(EnumChatFormatting.DARK_AQUA+"Ultimate "+EnumChatFormatting.WHITE+"Ironman\n" +
                 "You are NOT allowed to trade or use the auction house.\n" +
                      "You are restricted to 1 inventory. (No containers, no echest, no wardrobe).",
                "You   ", "Auction House", "Auctions Browser", "Auction View", "Chest",
                "Wardrobe", "Weapon Rack", "Shelves"),
        ULTIMATE_IRONMANPLUS(EnumChatFormatting.DARK_AQUA+"Ultimate "+EnumChatFormatting.WHITE+"Ironman"+EnumChatFormatting.GOLD+"+\n" +
                "You are NOT allowed to trade, use the auction house or bazaar.\n" +
                     "You are restricted to 1 inventory. (No containers, no echest, no wardrobe).",
                "You   ", "Auction House", "Auctions Browser", "Auction View", "Bazaar",
                "Chest", "Wardrobe", "Weapon Rack", "Shelves");

        public final String display;
        public final String desc;
        private final String[] bannedInventories;

        IronmanMode(String display, String... bannedInventories) {
            this.display = display.split("\n")[0];
            this.desc = display;
            this.bannedInventories = bannedInventories;
        }

        public boolean isBanned(String inventoryName) {
            for(String banned : bannedInventories) {
                if(inventoryName.contains(banned + " ") || inventoryName.endsWith(banned)) return true;
            }
            return false;
        }
    }

    public static Gamemode getGamemode() {
        String currentProfile = SBInfo.getInstance().currentProfile;

        if(currentProfile == null || currentProfile.isEmpty()) return null;

        return currentGamemode.computeIfAbsent(currentProfile, k -> new Gamemode());
    }

    public static void loadFromFile() {
        File configDir = NotEnoughUpdates.INSTANCE.manager.configLocation;
        File gamemodeFile = new File(configDir,
                "gamemodes/gamemodes-"+Minecraft.getMinecraft().thePlayer.getUniqueID().toString()+".json");
        gamemodeFile.getParentFile().mkdirs();

        if(!gamemodeFile.exists()) {
            return;
        }

        try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(gamemodeFile), StandardCharsets.UTF_8))) {
            String line = reader.readLine();
            String decoded = decrypt(line);
            currentGamemode = gson.fromJson(decoded, GamemodeWrapper.class).currentGamemode;
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static class GamemodeWrapper {
        private HashMap<String, Gamemode> currentGamemode;

        public GamemodeWrapper(HashMap<String, Gamemode> currentGamemode) {
            this.currentGamemode = currentGamemode;
        }
    }

    public static void saveToFile() {
        File configDir = NotEnoughUpdates.INSTANCE.manager.configLocation;
        File gamemodeFile = new File(configDir,
                "gamemodes/gamemodes-"+Minecraft.getMinecraft().thePlayer.getUniqueID().toString()+".json");
        gamemodeFile.getParentFile().mkdirs();

        try {
            gamemodeFile.createNewFile();

            try(BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(gamemodeFile), StandardCharsets.UTF_8))) {
                JsonObject obj = new JsonObject();
                writer.write(encrypt(gson.toJson(new GamemodeWrapper(currentGamemode), GamemodeWrapper.class)));
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static Key getKeyFromPlayerUUID() {
        byte[] bytes = ByteBuffer.allocate(2 * Long.SIZE / Byte.SIZE)
                .putLong(Minecraft.getMinecraft().thePlayer.getUniqueID().getLeastSignificantBits())
                .putLong(Minecraft.getMinecraft().thePlayer.getUniqueID().getMostSignificantBits())
                .array();
        SecretKeySpec key = new SecretKeySpec(bytes, "AES");

        return key;
    }


    public static String encrypt(String value) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, getKeyFromPlayerUUID());
            String encrypt = Base64.getEncoder().encodeToString(cipher.doFinal(value.getBytes()));
            return encrypt;
        } catch (Exception e) {
            e