summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/MinecraftServer.java
blob: ae4d9c66d1208fb2442af0822491cfeacb0a38b6 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package net.minecraft.server;

import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

// CraftBukkit start
import java.net.UnknownHostException;
import joptsimple.OptionSet;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.scheduler.CraftScheduler;
import org.bukkit.event.Event;
import org.bukkit.event.world.WorldEvent;
//CraftBukkit end

public class MinecraftServer implements ICommandListener, Runnable {

    public static Logger a = Logger.getLogger("Minecraft");
    public static HashMap b = new HashMap();
    public NetworkListenThread c;
    public PropertyManager d;
    //public WorldServer e;
    public ServerConfigurationManager f;
    private boolean o = true;
    public boolean g = false;
    int h = 0;
    public String i;
    public int j;
    private List p = new ArrayList();
    private List q = Collections.synchronizedList(new ArrayList());
    public EntityTracker k;
    public boolean l;
    public boolean m;
    public boolean n;
	public int spawnProtection;
    public List<WorldServer> worlds = new ArrayList<WorldServer>();

    // Craftbukkit start - adds argument OptionSet
    public MinecraftServer(OptionSet options) {
        new ThreadSleepForever(this);
        // CraftBukkit start
        this.options = options;
    }
    public CraftServer server;
    public OptionSet options;
    public ConsoleCommandSender console = new ConsoleCommandSender();

    private boolean d() throws UnknownHostException {
        // CraftBukkit end -- added throws UnknownHostException
        ThreadCommandReader threadcommandreader = new ThreadCommandReader(this);

        threadcommandreader.setDaemon(true);
        threadcommandreader.start();
        ConsoleLogManager.a();
        a.info("Starting minecraft server version Beta 1.2_01");
        if (Runtime.getRuntime().maxMemory() / 1024L / 1024L < 512L) {
            a.warning("**** NOT ENOUGH RAM!");
            a.warning("To start the server with more ram, launch it as \"java -Xmx1024M -Xms1024M -jar minecraft_server.jar\"");
        }

        a.info("Loading properties");
        this.d = new PropertyManager(new File("server.properties"));
        String s = this.d.a("server-ip", "");

        this.l = this.d.a("online-mode", true);
        this.m = this.d.a("spawn-animals", true);
        this.n = this.d.a("pvp", true);
		this.spawnProtection = this.d.a("spawn-protection", 16);
        InetAddress inetaddress = null;

        if (s.length() > 0) {
            inetaddress = InetAddress.getByName(s);
        }

        int i = this.d.a("server-port", 25565);

        a.info("Starting Minecraft server on " + (s.length() == 0 ? "*" : s) + ":" + i);

        try {
            this.c = new NetworkListenThread(this, inetaddress, i);
            // CraftBukkit: Be more generic; IOException -> Throwable
        } catch (Throwable ioexception) {
            a.warning("**** FAILED TO BIND TO PORT!");
            a.log(Level.WARNING, "The exception was: " + ioexception.toString());
            a.warning("Perhaps a server is already running on that port?");
            return false;
        }

        if (!this.l) {
            a.warning("**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!");
            a.warning("The server will make no attempt to authenticate usernames. Beware.");
            a.warning("While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.");
            a.warning("To change this, set \"online-mode\" to \"true\" in the server.settings file.");
        }

        this.f = new ServerConfigurationManager(this);
        this.k = new EntityTracker(this);
        String s1 = this.d.a("level-name", "world");

        a.info("Preparing level \"" + s1 + "\"");
        this.c(s1);
        a.info("Done! For help, type \"help\" or \"?\"");
        return true;
    }

    private void c(String s) {
        a.info("Preparing start region");

        // Craftbukkit start
        WorldServer world = new WorldServer(this, new File("."), s, this.d.a("hellworld", false) ? -1 : 0);
        world.a(new WorldManager(this, world));
        world.k = this.d.a("spawn-monsters", true) ? 1 : 0;
        world.a(this.d.a("spawn-monsters", true), this.m);
        this.f.a(world);
        worlds.add(world);
        // Craftbukkit end

        short short1 = 196;
        long i = System.currentTimeMillis();

        for (int j = -short1; j <= short1 && this.o; j += 16) {
            for (int k = -short1; k <= short1 && this.o; k += 16) {
                long l = System.currentTimeMillis();

                if (l < i) {
                    i = l;
                }

                if (l > i + 1000L) {
                    int i1 = (short1 * 2 + 1) * (short1 * 2 + 1);
                    int j1 = (j + short1) * (short1 * 2 + 1) + k + 1;

                    this.a("Preparing spawn area", j1 * 100 / i1);
                    i = l;
                }

                // Craftbukkit start
                for (WorldServer worldserver : worlds) {
                    world.A.d(world.spawnX + j >> 4, world.spawnZ + k >> 4);

                    while (world.d() && this.o) {
                        ;
                    }
                }
                // Craftbukkit end
            }
        }

        this.e();
    }

    private void a(String s, int i) {
        this.i = s;
        this.j = i;
        a.info(s + ": " + i + "%");
    }

    private void e() {
        this.i = null;
        this.j = 0;

        server.loadPlugins(); // CraftBukkit
    }

    private void f() {
        a.info("Saving chunks");

        // Craftbukkit start
        for (WorldServer world : worlds) {
            world.a(true, (IProgressUpdate) null);
            
            Event worldSaved = new WorldEvent( Event.Type.WORLD_SAVED, world.getWorld() );
            server.getPluginManager().callEvent( worldSaved );
        }
        // Craftbukkit end
    }

    private void g() {
        a.info("Stopping server");
        // CraftBukkit
        if(server != null) {
            server.disablePlugins();
        }

        if (this.f != null) {
            this.f.d();
        }

        if (this.worlds.size() > 0) { // Craftbukkit
            this.f();
        }
    }

    public void a() {
        this.o = false;
    }

    public void run() {
        try {
            if (this.d()) {
                long i = System.currentTimeMillis();
                long j = 0L;

                while (this.o) {
                    long k = System.currentTimeMillis();
                    long l = k - i;

                    if (l > 2000L) {
                        a.warning("Can\'t keep up! Did the system time change, or is the server overloaded?");
                        l = 2000L;
                    }

                    if (l < 0L) {
                        a.warning("Time ran backwards! Did the system time change?");
                        l = 0L;
                    }

                    j += l;
                    i = k;

                    while (j > 50L) {
                        j -= 50L;
                        this.h();
                    }

                    Thread.sleep(1L);
                }
            } else {
                while (this.o) {
                    this.b();

                    try {
                        Thread.sleep(10L);
                    } catch (InterruptedException interruptedexception) {
                        interruptedexception.printStackTrace();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            a.log(Level.SEVERE, "Unexpected exception", exception);

            while (this.o) {
                this.b();

                try {
                    Thread.sleep(10L);
                } catch (InterruptedException interruptedexception1) {
                    interruptedexception1.printStackTrace();
                }
            }
        } finally {
            try {
                this.g();
                this.g = true;
            } finally {
                System.exit(0);
            }
        }
    }

    private void h() {
        ArrayList arraylist = new ArrayList();
        Iterator iterator = b.keySet().iterator();

        while (iterator.hasNext()) {
            String s = (String) iterator.next();
            int i = ((Integer) b.get(s)).intValue();

            if (i > 0) {
                b.put(s, Integer.valueOf(i - 1));
            } else {
                arraylist.add(s);
            }
        }

        int j;

        for (j = 0; j < arraylist.size(); ++j) {
            b.remove(arraylist.get(j));
        }

        AxisAlignedBB.a();
        Vec3D.a();
        ++this.h;

        // Craftbukkit start
        if (this.h % 20 == 0) {
            for (int i = 0; i < this.f.b.size(); ++i) {
                EntityPlayer entityplayer = (EntityPlayer) this.f.b.get(i);
                entityplayer.a.b((Packet) (new Packet4UpdateTime(entityplayer.world.e)));
            }
        }

        ((CraftScheduler) server.getScheduler()).mainThreadHeartbeat(this.h);
        
        for (WorldServer world : worlds) {
            world.f();

            while (world.d()) {
                ;
            }
            
            world.c();
        }
        // Craftbukkit end

        this.c.a();
        this.f.b();
        this.k.a();

        for (j = 0; j < this.p.size(); ++j) {
            ((IUpdatePlayerListBox) this.p.get(j)).a();
        }

        try {
            this.b();
        } catch (Exception exception) {
            a.log(Level.WARNING, "Unexpected exception while parsing console command", exception);
        }
    }

    public void a(String s, ICommandListener icommandlistener) {
        this.q.add(new ServerCommand(s, icommandlistener));
    }

    public void b() {
        while (this.q.size() > 0) {
            ServerCommand servercommand = (ServerCommand) this.q.remove(0);
            String s = servercommand.a;
            ICommandListener icommandlistener = servercommand.b;
            String s1 = icommandlistener.c();

            // Craftbukkit start
            if (server.dispatchCommand(console, s)) {
                continue;
            }
            // Craftbukkit end

            if (!s.toLowerCase().startsWith("help") && !s.toLowerCase().startsWith("?")) {
                if (s.toLowerCase().startsWith("list")) {
                    icommandlistener.b("Connected players: " + this.f.c());
                } else if (s.toLowerCase().startsWith("stop")) {
                    this.a(s1, "Stopping the server..");
                    this.o = false;
                } else if (s.toLowerCase().startsWith("save-all")) {
                    this.a(s1, "Forcing save..");

                    // Craftbukkit start
                    for (WorldServer world : worlds) {
                        world.a(true, (IProgressUpdate) null);
                        
                        Event worldSaved = new WorldEvent( Event.Type.WORLD_SAVED, world.getWorld() );
                        server.getPluginManager().callEvent( worldSaved );
                    }
                    
                    this.f.d();
                    // Craftbukkit end

                    this.a(s1, "Save complete.");
                } else if (s.toLowerCase().startsWith("save-off")) {
                    this.a(s1, "Disabling level saving..");

                    // Craftbukkit start
                    for (WorldServer world : worlds) {
                        world.C = true;
                    }
                    // Craftbukkit end
                } else if (s.toLowerCase().startsWith("save-on")) {
                    this.a(s1, "Enabling level saving..");

                    // Craftbukkit start
                    for (WorldServer world : worlds) {
                        world.C = false;
                    }
                    // Craftbukkit end
                } else {
                    String s2;

                    if (s.toLowerCase().startsWith("op ")) {
                        s2 = s.substring(s.indexOf(" ")).trim();
                        this.f.e(s2);
                        this.a(s1, "Opping " + s2);
                        this.f.a(s2, "§eYou are now op!");
                    } else if (s.toLowerCase().startsWith("deop ")) {
                        s2 = s.substring(s.indexOf(" ")).trim();
                        this.f.f(s2);
                        this.f.a(s2, "§eYou are no longer op!");
                        this.a(s1, "De-opping " + s2);
                    } else if (s.toLowerCase().startsWith("ban-ip ")) {
                        s2 = s.substring(s.indexOf(" ")).trim();
                        this.f.c(s2);
                        this.a(s1, "Banning ip " + s2);
                    } else if (s.toLowerCase().startsWith("pardon-ip ")) {
                        s2 = s.substring(s.indexOf(" ")).trim();
                        this.f.d(s2);
                        this.a(s1, "Pardoning ip " + s2);
                    } else {
                        EntityPlayer entityplayer;

                        if (s.toLowerCase().startsWith("ban ")) {
                            s2 = s.substring(s.indexOf(" ")).trim();
                            this.f.a(s2);
                            this.a(s1, "Banning " + s2);
                            entityplayer = this.f.h(s2);
                            if (entityplayer != null) {
                                entityplayer.a.a("Banned by admin");
                            }
                        } else if (s.toLowerCase().startsWith("pardon ")) {
                            s2 = s.substring(s.indexOf(" ")).trim();
                            this.f.b(s2);
                            this.a(s1, "Pardoning " + s2);
                        } else if (s.toLowerCase().startsWith("kick ")) {
                            s2 = s.substring(s.indexOf(" ")).trim();
                            entityplayer = null;

                            for (int i = 0; i < this.f.b.size(); ++i) {
                                EntityPlayer entityplayer1 = (EntityPlayer) this.f.b.get(i);

                                if (entityplayer1.name.equalsIgnoreCase(s2)) {
                                    entityplayer = entityplayer1;
                                }
                            }

                            if (entityplayer != null) {
                                entityplayer.a.a("Kicked by admin");
                                this.a(s1, "Kicking " + entityplayer.name);
                            } else {
                                icommandlistener.b("Can\'t find user " + s2 + ". No kick.");
                            }
                        } else {
                            String[] astring;
                            EntityPlayer entityplayer2;

                            if (s.toLowerCase().startsWith("tp ")) {
                                astring = s.split(" ");
                                if (astring.length == 3) {
                                    entityplayer = this.f.h(astring[1]);
                                    entityplayer2 = this.f.h(astring[2]);
                                    if (entityplayer == null) {
                                        icommandlistener.b("Can\'t find user " + astring[1] + ". No tp.");
                                    } else if (entityplayer2 == null) {
                                        icommandlistener.b("Can\'t find user " + astring[2] + ". No tp.");
                                    } else {
                                        entityplayer.a.a(entityplayer2.locX, entityplayer2.locY, entityplayer2.locZ, entityplayer2.yaw, entityplayer2.pitch);
                                        this.a(s1, "Teleporting " + astring[1] + " to " + astring[2] + ".");
                                    }
                                } else {
                                    icommandlistener.b("Syntax error, please provice a source and a target.");
                                }
                            } else if (s.toLowerCase().startsWith("give ")) {
                                astring = s.split(" ");
                                if (astring.length != 3 && astring.length != 4) {
                                    return;
                                }

                                String s3 = astring[1];

                                entityplayer2 = this.f.h(s3);
                                if (entityplayer2 != null) {
                                    try {
                                        int j = Integer.parseInt(astring[2]);

                                        if (Item.byId[j] != null) {
                                            this.a(s1, "Giving " + entityplayer2.name + " some " + j);
                                            int k = 1;

                                            if (astring.length > 3) {
                                                k = this.b(astring[3], 1);
                                            }

                                            if (k < 1) {
                                                k = 1;
                                            }

                                            if (k > 64) {
                                                k = 64;
                                            }

                                            entityplayer2.b(new ItemStack(j, k, 0));
                                        } else {
                                            icommandlistener.b("There\'s no item with id " + j);
                                        }
                                    } catch (NumberFormatException numberformatexception) {
                                        icommandlistener.b("There\'s no item with id " + astring[2]);
                                    }
                                } else {
                                    icommandlistener.b("Can\'t find user " + s3);
                                }
                            } else if (s.toLowerCase().startsWith("say ")) {
                                s = s.substring(s.indexOf(" ")).trim();
                                a.info("[" + s1 + "] " + s);
                                this.f.a((Packet) (new Packet3Chat("§d[Server] " + s)));
                            } else if (s.toLowerCase().startsWith("tell ")) {
                                astring = s.split(" ");
                                if (astring.length >= 3) {
                                    s = s.substring(s.indexOf(" ")).trim();
                                    s = s.substring(s.indexOf(" ")).trim();
                                    a.info("[" + s1 + "->" + astring[1] + "] " + s);
                                    s = "§7" + s1 + " whispers " + s;
                                    a.info(s);
                                    if (!this.f.a(astring[1], (Packet) (new Packet3Chat(s)))) {
                                        icommandlistener.b("There\'s no player by that name online.");
                                    }
                                }
                            } else {
                                a.info("Unknown console command. Type \"help\" for help.");
                            }
                        }
                    }
                }
            } else {
                icommandlistener.b("To run the server without a gui, start it like this:");
                icommandlistener.b("   java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui");
                icommandlistener.b("Console commands:");
                icommandlistener.b("   help  or  ?               shows this message");
                icommandlistener.b("   kick <player>             removes a player from the server");
                icommandlistener.b("   ban <player>              bans a player from the server");
                icommandlistener.b("   pardon <player>           pardons a banned player so that they can connect again");
                icommandlistener.b("   ban-ip <ip>               bans an IP address from the server");
                icommandlistener.b("   pardon-ip <ip>            pardons a banned IP address so that they can connect again");
                icommandlistener.b("   op <player>               turns a player into an op");
                icommandlistener.b("   deop <player>             removes op status from a player");
                icommandlistener.b("   tp <player1> <player2>    moves one player to the same location as another player");
                icommandlistener.b("   give <player> <id> [num]  gives a player a resource");
                icommandlistener.b("   tell <player> <message>   sends a private message to a player");
                icommandlistener.b("   stop                      gracefully stops the server");
                icommandlistener.b("   save-all                  forces a server-wide level save");
                icommandlistener.b("   save-off                  disables terrain saving (useful for backup scripts)");
                icommandlistener.b("   save-on                   re-enables terrain saving");
                icommandlistener.b("   list                      lists all currently connected players");
                icommandlistener.b("   say <message>             broadcasts a message to all players");
            }
        }
    }

    private void a(String s, String s1) {
        String s2 = s + ": " + s1;

        this.f.i("§7(" + s2 + ")");
        a.info(s2);
    }

    private int b(String s, int i) {
        try {
            return Integer.parseInt(s);
        } catch (NumberFormatException numberformatexception) {
            return i;
        }
    }

    public void a(IUpdatePlayerListBox iupdateplayerlistbox) {
        this.p.add(iupdateplayerlistbox);
    }

    // Craftbukkit start - replaces main(String args[])
    public static void main(final OptionSet options) {
        try {
            MinecraftServer minecraftserver = new MinecraftServer(options);

            // CraftBukkit - remove gui

            (new ThreadServerApplication("Server thread", minecraftserver)).start();
        } catch (Exception exception) {
            a.log(Level.SEVERE, "Failed to start the minecraft server", exception);
        }
    }

    public File a(String s) {
        return new File(s);
    }

    public void b(String s) {
        a.info(s);
    }

    public String c() {
        return "CONSOLE";
    }

    public static boolean a(MinecraftServer minecraftserver) {
        return minecraftserver.o;
    }
}