summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/NameReferencingFileConverter.java
blob: 45561e9bfdf23a48900b3c8cbe20cfaabad5b77f (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package net.minecraft.server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

import net.minecraft.util.com.google.common.base.Charsets;
import net.minecraft.util.com.google.common.collect.Iterators;
import net.minecraft.util.com.google.common.collect.Lists;
import net.minecraft.util.com.google.common.collect.Maps;
import net.minecraft.util.com.google.common.io.Files;
import net.minecraft.util.com.mojang.authlib.Agent;
import net.minecraft.util.com.mojang.authlib.GameProfile;
import net.minecraft.util.com.mojang.authlib.ProfileLookupCallback;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

// CraftBukkit - Imported for package private static methods

public class NameReferencingFileConverter {

    private static final Logger e = LogManager.getLogger();
    public static final File a = new File("banned-ips.txt");
    public static final File b = new File("banned-players.txt");
    public static final File c = new File("ops.txt");
    public static final File d = new File("white-list.txt");

    static List a(File file1, Map map) throws IOException { // CraftBukkit - Added throws
        List list = Files.readLines(file1, Charsets.UTF_8);
        Iterator iterator = list.iterator();

        while (iterator.hasNext()) {
            String s = (String) iterator.next();

            s = s.trim();
            if (!s.startsWith("#") && s.length() >= 1) {
                String[] astring = s.split("\\|");

                map.put(astring[0].toLowerCase(Locale.ROOT), astring);
            }
        }

        return list;
    }

    private static void a(MinecraftServer minecraftserver, Collection collection, ProfileLookupCallback profilelookupcallback) {
        String[] astring = (String[]) Iterators.toArray(Iterators.filter(collection.iterator(), new PredicateEmptyList()), String.class);

        if (minecraftserver.getOnlineMode()) {
            minecraftserver.getGameProfileRepository().findProfilesByNames(astring, Agent.MINECRAFT, profilelookupcallback);
        } else {
            String[] astring1 = astring;
            int i = astring.length;

            for (int j = 0; j < i; ++j) {
                String s = astring1[j];
                UUID uuid = EntityHuman.a(new GameProfile((UUID) null, s));
                GameProfile gameprofile = new GameProfile(uuid, s);

                profilelookupcallback.onProfileLookupSucceeded(gameprofile);
            }
        }
    }

    public static boolean a(MinecraftServer minecraftserver) {
        GameProfileBanList gameprofilebanlist = new GameProfileBanList(PlayerList.a);

        if (b.exists() && b.isFile()) {
            if (gameprofilebanlist.c().exists()) {
                /* CraftBukkit start - Exception is never thrown
                try {
                    gameprofilebanlist.load();
                } catch (FileNotFoundException filenotfoundexception) {
                    e.warn("Could not load existing file " + gameprofilebanlist.c().getName(), filenotfoundexception);
                }
                */
                gameprofilebanlist.load();
                // CraftBukkit end
            }

            try {
                HashMap hashmap = Maps.newHashMap();

                a(b, (Map) hashmap);
                GameProfileBanListEntryConverter gameprofilebanlistentryconverter = new GameProfileBanListEntryConverter(minecraftserver, hashmap, gameprofilebanlist);

                a(minecraftserver, hashmap.keySet(), gameprofilebanlistentryconverter);
                gameprofilebanlist.save();
                c(b);
                return true;
            } catch (IOException ioexception) {
                e.warn("Could not read old user banlist to convert it!", ioexception);
                return false;
            } catch (FileConversionException fileconversionexception) {
                e.error("Conversion failed, please try again later", fileconversionexception);
                return false;
            }
        } else {
            return true;
        }
    }

    public static boolean b(MinecraftServer minecraftserver) {
        IpBanList ipbanlist = new IpBanList(PlayerList.b);

        if (a.exists() && a.isFile()) {
            if (ipbanlist.c().exists()) {
                /* CraftBukkit start - Exception is never thrown
                try {
                    ipbanlist.load();
                } catch (FileNotFoundException filenotfoundexception) {
                    e.warn("Could not load existing file " + ipbanlist.c().getName(), filenotfoundexception);
                }
                */
                ipbanlist.load();
                // CraftBukkit end
            }

            try {
                HashMap hashmap = Maps.newHashMap();

                a(a, (Map) hashmap);
                Iterator iterator = hashmap.keySet().iterator();

                while (iterator.hasNext()) {
                    String s = (String) iterator.next();
                    String[] astring = (String[]) hashmap.get(s);
                    Date date = astring.length > 1 ? b(astring[1], (Date) null) : null;
                    String s1 = astring.length > 2 ? astring[2] : null;
                    Date date1 = astring.length > 3 ? b(astring[3], (Date) null) : null;
                    String s2 = astring.length > 4 ? astring[4] : null;

                    ipbanlist.add(new IpBanEntry(s, date, s1, date1, s2));
                }

                ipbanlist.save();
                c(a);
                return true;
            } catch (IOException ioexception) {
                e.warn("Could not parse old ip banlist to convert it!", ioexception);
                return false;
            }
        } else {
            return true;
        }
    }

    public static boolean c(MinecraftServer minecraftserver) {
        OpList oplist = new OpList(PlayerList.c);

        if (c.exists() && c.isFile()) {
            if (oplist.c().exists()) {
                /* CraftBukkit start - Exception is never thrown
                try {
                    oplist.load();
                } catch (FileNotFoundException filenotfoundexception) {
                    e.warn("Could not load existing file " + oplist.c().getName(), filenotfoundexception);
                }
                */
                oplist.load();
                // CraftBukkit end
            }

            try {
                List list = Files.readLines(c, Charsets.UTF_8);
                OpListProfileCallback oplistprofilecallback = new OpListProfileCallback(minecraftserver, oplist);

                a(minecraftserver, list, oplistprofilecallback);
                oplist.save();
                c(c);
                return true;
            } catch (IOException ioexception) {
                e.warn("Could not read old oplist to convert it!", ioexception);
                return false;
            } catch (FileConversionException fileconversionexception) {
                e.error("Conversion failed, please try again later", fileconversionexception);
                return false;
            }
        } else {
            return true;
        }
    }

    public static boolean d(MinecraftServer minecraftserver) {
        WhiteList whitelist = new WhiteList(PlayerList.d);

        if (d.exists() && d.isFile()) {
            if (whitelist.c().exists()) {
                /* CraftBukkit start - Exception is never thrown
                try {
                    whitelist.load();
                } catch (FileNotFoundException filenotfoundexception) {
                    e.warn("Could not load existing file " + whitelist.c().getName(), filenotfoundexception);
                }
                */
                whitelist.load();
                // CraftBukkit end
            }

            try {
                List list = Files.readLines(d, Charsets.UTF_8);
                WhiteListProfileCallback whitelistprofilecallback = new WhiteListProfileCallback(minecraftserver, whitelist);

                a(minecraftserver, list, whitelistprofilecallback);
                whitelist.save();
                c(d);
                return true;
            } catch (IOException ioexception) {
                e.warn("Could not read old whitelist to convert it!", ioexception);
                return false;
            } catch (FileConversionException fileconversionexception) {
                e.error("Conversion failed, please try again later", fileconversionexception);
                return false;
            }
        } else {
            return true;
        }
    }

    public static String a(String s) {
        if (!UtilColor.b(s) && s.length() <= 16) {
            MinecraftServer minecraftserver = MinecraftServer.getServer();
            GameProfile gameprofile = minecraftserver.getUserCache().a(s);

            if (gameprofile != null && gameprofile.getId() != null) {
                return gameprofile.getId().toString();
            } else if (!minecraftserver.N() && minecraftserver.getOnlineMode()) {
                ArrayList arraylist = Lists.newArrayList();
                GameProfileLookupCallback gameprofilelookupcallback = new GameProfileLookupCallback(minecraftserver, arraylist);

                a(minecraftserver, Lists.newArrayList(new String[] { s}), gameprofilelookupcallback);
                return arraylist.size() > 0 && ((GameProfile) arraylist.get(0)).getId() != null ? ((GameProfile) arraylist.get(0)).getId().toString() : "";
            } else {
                return EntityHuman.a(new GameProfile((UUID) null, s)).toString();
            }
        } else {
            return s;
        }
    }

    public static boolean a(DedicatedServer dedicatedserver, PropertyManager propertymanager) {
        File file1 = d(propertymanager);
        File file2 = new File(file1.getParentFile(), "playerdata");
        File file3 = new File(file1.getParentFile(), "unknownplayers");

        if (file1.exists() && file1.isDirectory()) {
            File[] afile = file1.listFiles();
            ArrayList arraylist = Lists.newArrayList();
            File[] afile1 = afile;
            int i = afile.length;

            for (int j = 0; j < i; ++j) {
                File file4 = afile1[j];
                String s = file4.getName();

                if (s.toLowerCase(Locale.ROOT).endsWith(".dat")) {
                    String s1 = s.substring(0, s.length() - ".dat".length());

                    if (s1.length() > 0) {
                        arraylist.add(s1);
                    }
                }
            }

            try {
                String[] astring = (String[]) arraylist.toArray(new String[arraylist.size()]);
                PlayerDatFileConverter playerdatfileconverter = new PlayerDatFileConverter(dedicatedserver, file2, file3, file1, astring);

                a(dedicatedserver, Lists.newArrayList(astring), playerdatfileconverter);
                return true;
            } catch (FileConversionException fileconversionexception) {
                e.error("Conversion failed, please try again later", fileconversionexception);
                return false;
            }
        } else {
            return true;
        }
    }

    private static void b(File file1) {
        if (file1.exists()) {
            if (!file1.isDirectory()) {
                throw new FileConversionException("Can\'t create directory " + file1.getName() + " in world save directory.", (PredicateEmptyList) null);
            }
        } else if (!file1.mkdirs()) {
            throw new FileConversionException("Can\'t create directory " + file1.getName() + " in world save directory.", (PredicateEmptyList) null);
        }
    }

    public static boolean a(PropertyManager propertymanager) {
        boolean flag = b(propertymanager);

        flag = flag && c(propertymanager);
        return flag;
    }

    private static boolean b(PropertyManager propertymanager) {
        boolean flag = false;

        if (b.exists() && b.isFile()) {
            flag = true;
        }

        boolean flag1 = false;

        if (a.exists() && a.isFile()) {
            flag1 = true;
        }

        boolean flag2 = false;

        if (c.exists() && c.isFile()) {
            flag2 = true;
        }

        boolean flag3 = false;

        if (d.exists() && d.isFile()) {
            flag3 = true;
        }

        if (!flag && !flag1 && !flag2 && !flag3) {
            return true;
        } else {
            e.warn("**** FAILED TO START THE SERVER AFTER ACCOUNT CONVERSION!");
            e.warn("** please remove the following files and restart the server:");
            if (flag) {
                e.warn("* " + b.getName());
            }

            if (flag1) {
                e.warn("* " + a.getName());
            }

            if (flag2) {
                e.warn("* " + c.getName());
            }

            if (flag3) {
                e.warn("* " + d.getName());
            }

            return false;
        }
    }

    private static boolean c(PropertyManager propertymanager) {
        File file1 = d(propertymanager);

        if (file1.exists() && file1.isDirectory()) {
            String[] astring = file1.list(new DatFilenameFilter());

            if (astring.length > 0) {
                e.warn("**** DETECTED OLD PLAYER FILES IN THE WORLD SAVE");
                e.warn("**** THIS USUALLY HAPPENS WHEN THE AUTOMATIC CONVERSION FAILED IN SOME WAY");
                e.warn("** please restart the server and if the problem persists, remove the directory \'{}\'", new Object[] { file1.getPath()});
                return false;
            }
        }

        return true;
    }

    private static File d(PropertyManager propertymanager) {
        String s = propertymanager.getString("level-name", "world");
        File file1 = new File(MinecraftServer.getServer().server.getWorldContainer(), s); // CraftBukkit - Respect container setting

        return new File(file1, "players");
    }

    private static void c(File file1) {
        File file2 = new File(file1.getName() + ".converted");

        file1.renameTo(file2);
    }

    private static Date b(String s, Date date) {
        Date date1;

        try {
            date1 = ExpirableListEntry.a.parse(s);
        } catch (ParseException parseexception) {
            date1 = date;
        }

        return date1;
    }

    static Logger a() {
        return e;
    }

    static Date a(String s, Date date) {
        return b(s, date);
    }

    static void a(File file1) {
        b(file1);
    }
}