summaryrefslogtreecommitdiffstats
path: root/src/net/minecraft/server/NetServerHandler.java
blob: 1168a6dfd01539e609523c97ab1acc603433d187 (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
package net.minecraft.server;


import java.io.PrintStream;
import java.util.*;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Player;
import org.bukkit.craftbukkit.CraftPlayer;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.event.Event.Type;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerMoveEvent;


public class NetServerHandler extends NetHandler
        implements ICommandListener {

    public static Logger a = Logger.getLogger("Minecraft");
    public NetworkManager b;
    public boolean c;
    private MinecraftServer d;
    private EntityPlayerMP e;
    private int f;
    private double g;
    private double h;
    private double i;
    private boolean j;
    private Map k;

    // CraftBukkit - next 2 lines
    private final CraftServer server;
    private final CraftPlayer player;

    public NetServerHandler(MinecraftServer minecraftserver, NetworkManager networkmanager, EntityPlayerMP entityplayermp) {
        c = false;
        f = 0;
        j = true;
        k = new HashMap();
        d = minecraftserver;
        b = networkmanager;
        networkmanager.a(this);
        e = entityplayermp;
        entityplayermp.a = this;

        // CraftBukkit - next 2 lines
        server = minecraftserver.server;
        player = new CraftPlayer(server, e);
    }

    // CraftBukkit start
    public CraftPlayer getPlayer() {
        return player;
    }
    // CraftBukkit end

    public void a() {
        b.a();
        if (f++ % 20 == 0) {
            b.a(new Packet0KeepAlive());
        }
    }

    public void a(String s) {
        b.a(new Packet255KickDisconnect(s));
        b.c();
        d.f.a(new Packet3Chat((new StringBuilder()).append("\247e").append(e.aw).append(" left the game.").toString()));
        d.f.c(e);
        c = true;
    }

    public void a(Packet10Flying packet10flying) {
        if (!j) {
            double d1 = packet10flying.b - h;

            if (packet10flying.a == g && d1 * d1 < 0.01D && packet10flying.c == i) {
                j = true;
            }
        }

        // CraftBukkit start
        Location from = new Location(player.getWorld(), g, h, i, e.v, e.w);
        Location to = player.getLocation();
        if (!from.equals(to)) {
            PlayerMoveEvent event = new PlayerMoveEvent(Type.PLAYER_MOVE, player, from, to);
            server.getPluginManager().callEvent(event);

            from = event.getFrom();
            to = event.isCancelled() ? from : event.getTo();

            e.p = to.getX();
            e.q = to.getY();
            e.r = to.getZ();
            e.v = to.getYaw();
            e.w = to.getPitch();
        }
        // CraftBukkit end

        if (j) {
            if (e.k != null) {
                float f1 = e.v;
                float f2 = e.w;

                e.k.A();
                double d3 = e.p;
                double d5 = e.q;
                double d7 = e.r;
                double d9 = 0.0D;
                double d10 = 0.0D;

                if (packet10flying.i) {
                    f1 = packet10flying.e;
                    f2 = packet10flying.f;
                }
                if (packet10flying.h && packet10flying.b == -999D && packet10flying.d == -999D) {
                    d9 = packet10flying.a;
                    d10 = packet10flying.c;
                }
                e.A = packet10flying.g;
                e.F();
                e.c(d9, 0.0D, d10);
                e.b(d3, d5, d7, f1, f2);
                e.s = d9;
                e.u = d10;
                if (e.k != null) {
                    d.e.b(e.k, true);
                }
                if (e.k != null) {
                    e.k.A();
                }
                d.f.b(e);
                g = e.p;
                h = e.q;
                i = e.r;
                d.e.f(e);
                return;
            }
            double d2 = e.q;

            g = e.p;
            h = e.q;
            i = e.r;
            double d4 = e.p;
            double d6 = e.q;
            double d8 = e.r;
            float f3 = e.v;
            float f4 = e.w;

            if (packet10flying.h && packet10flying.b == -999D && packet10flying.d == -999D) {
                packet10flying.h = false;
            }
            if (packet10flying.h) {
                d4 = packet10flying.a;
                d6 = packet10flying.b;
                d8 = packet10flying.c;
                double d11 = packet10flying.d - packet10flying.b;

                if (d11 > 1.6499999999999999D || d11 < 0.10000000000000001D) {
                    a("Illegal stance");
                    a.warning((new StringBuilder()).append(e.aw).append(" had an illegal stance: ").append(d11).toString());
                }
                e.ak = packet10flying.d;
            }
            if (packet10flying.i) {
                f3 = packet10flying.e;
                f4 = packet10flying.f;
            }
            e.F();
            e.R = 0.0F;
            e.b(g, h, i, f3, f4);
            double d12 = d4 - e.p;
            double d13 = d6 - e.q;
            double d14 = d8 - e.r;
            float f5 = 0.0625F;
            boolean flag = d.e.a(e, e.z.b().e(f5, f5, f5)).size() == 0;

            e.c(d12, d13, d14);
            d12 = d4 - e.p;
            d13 = d6 - e.q;
            if (d13 > -0.5D || d13 < 0.5D) {
                d13 = 0.0D;
            }
            d14 = d8 - e.r;
            double d15 = d12 * d12 + d13 * d13 + d14 * d14;
            boolean flag1 = false;

            if (d15 > 0.0625D) {
                flag1 = true;
                a.warning((new StringBuilder()).append(e.aw).append(" moved wrongly!").toString());
                System.out.println((new StringBuilder()).append("Got position ").append(d4).append(", ").append(d6).append(", ").append(d8).toString());
                System.out.println((new StringBuilder()).append("Expected ").append(e.p).append(", ").append(e.q).append(", ").append(e.r).toString());
            }
            e.b(d4, d6, d8, f3, f4);
            boolean flag2 = d.e.a(e, e.z.b().e(f5, f5, f5)).size() == 0;

            if (flag && (flag1 || !flag2)) {
                a(g, h, i, f3, f4);
                return;
            }
            e.A = packet10flying.g;
            d.f.b(e);
            e.b(e.q - d2, packet10flying.g);
        }
    }

    public void a(double d1, double d2, double d3, float f1,
            float f2) {

        // CraftBukkit start
        Location from = player.getLocation();
        Location to = new Location(player.getWorld(), d1, d2, d3, f1, f2);
        PlayerMoveEvent event = new PlayerMoveEvent(Type.PLAYER_TELEPORT, player, from, to);
        server.getPluginManager().callEvent(event);

        from = event.getFrom();
        to = event.isCancelled() ? from : event.getTo();

        d1 = to.getX();
        d2 = to.getY();
        d3 = to.getZ();
        f1 = to.getYaw();
        f2 = to.getPitch();
        // CraftBukkit end

        j = false;
        g = d1;
        h = d2;
        i = d3;
        e.b(d1, d2, d3, f1, f2);
        e.a.b(new Packet13PlayerLookMove(d1, d2 + 1.6200000047683716D, d2, d3, f1, f2, false));
    }

    public void a(Packet14BlockDig packet14blockdig) {
        if (packet14blockdig.e == 4) {
            e.L();
            return;
        }
        boolean flag = d.e.B = d.f.g(e.aw);
        boolean flag1 = false;

        if (packet14blockdig.e == 0) {
            flag1 = true;
        }
        if (packet14blockdig.e == 1) {
            flag1 = true;
        }
        int l = packet14blockdig.a;
        int i1 = packet14blockdig.b;
        int j1 = packet14blockdig.c;

        if (flag1) {
            double d1 = e.p - ((double) l + 0.5D);
            double d2 = e.q - ((double) i1 + 0.5D);
            double d3 = e.r - ((double) j1 + 0.5D);
            double d4 = d1 * d1 + d2 * d2 + d3 * d3;

            if (d4 > 36D) {
                return;
            }
            double d5 = e.q;

            e.q = e.ak;
            e.q = d5;
        }
        int k1 = packet14blockdig.d;
        int l1 = (int) MathHelper.e(l - d.e.m);
        int i2 = (int) MathHelper.e(j1 - d.e.o);

        if (l1 > i2) {
            i2 = l1;
        }
        if (packet14blockdig.e == 0) {
            if (i2 > 16 || flag) {
                e.c.a(l, i1, j1);
            }
        } else if (packet14blockdig.e == 2) {
            e.c.a();
        } else if (packet14blockdig.e == 1) {
            if (i2 > 16 || flag) {
                e.c.a(l, i1, j1, k1);
            }
        } else if (packet14blockdig.e == 3) {
            double d6 = e.p - ((double) l + 0.5D);
            double d7 = e.q - ((double) i1 + 0.5D);
            double d8 = e.r - ((double) j1 + 0.5D);
            double d9 = d6 * d6 + d7 * d7 + d8 * d8;

            if (d9 < 256D) {
                e.a.b(new Packet53BlockChange(l, i1, j1, d.e));
            }
        }
        d.e.B = false;
    }

    public void a(Packet15Place packet15place) {
        ItemStack itemstack = e.an.e();
        boolean flag = d.e.B = d.f.g(e.aw);

        if (packet15place.d == 255) {
            if (itemstack == null) {
                return;
            }
            e.c.a(e, d.e, itemstack);
        } else {
            int l = packet15place.a;
            int i1 = packet15place.b;
            int j1 = packet15place.c;
            int k1 = packet15place.d;
            int l1 = (int) MathHelper.e(l - d.e.m);
            int i2 = (int) MathHelper.e(j1 - d.e.o);

            if (l1 > i2) {
                i2 = l1;
            }
            if (i2 > 16 || flag) {
                e.c.a(e, d.e, itemstack, l, i1, j1, k1);
            }
            e.a.b(new Packet53BlockChange(l, i1, j1, d.e));
            if (k1 == 0) {
                i1--;
            }
            if (k1 == 1) {
                i1++;
            }
            if (k1 == 2) {
                j1--;
            }
            if (k1 == 3) {
                j1++;
            }
            if (k1 == 4) {
                l--;
            }
            if (k1 == 5) {
                l++;
            }
            e.a.b(new Packet53BlockChange(l, i1, j1, d.e));
        }
        if (itemstack != null && itemstack.a == 0) {
            e.an.a[e.an.c] = null;
        }
        e.am = true;
        e.an.a[e.an.c] = ItemStack.a(e.an.a[e.an.c]);
        Slot slot = e.ap.a(e.an, e.an.c);

        e.ap.a();
        e.am = false;
        if (!ItemStack.a(e.an.e(), packet15place.e)) {
            b(new Packet103(e.ap.f, slot.c, e.an.e()));
        }
        d.e.B = false;
    }

    public void a(String s, Object aobj[]) {
        a.info((new StringBuilder()).append(e.aw).append(" lost connection: ").append(s).toString());
        d.f.a(new Packet3Chat((new StringBuilder()).append("\247e").append(e.aw).append(" left the game.").toString()));
        d.f.c(e);
        c = true;
    }

    public void a(Packet packet) {
        a.warning((new StringBuilder()).append(getClass()).append(" wasn't prepared to deal with a ").append(packet.getClass()).toString());
        a("Protocol error, unexpected packet");
    }

    public void b(Packet packet) {
        b.a(packet);
    }

    public void a(Packet16BlockItemSwitch packet16blockitemswitch) {
        e.an.c = packet16blockitemswitch.a;
    }

    public void a(Packet3Chat packet3chat) {
        String s = packet3chat.a;

        if (s.length() > 100) {
            a("Chat message too long");
            return;
        }
        s = s.trim();
        for (int l = 0; l < s.length(); l++) {
            if (FontAllowedCharacters.a.indexOf(s.charAt(l)) < 0) {
                a("Illegal characters in chat");
                return;
            }
        }

        if (s.startsWith("/")) {
            c(s);
        } else {
            // CraftBukkit start
            PlayerChatEvent event = new PlayerChatEvent(Type.PLAYER_CHAT, player, s);
            server.getPluginManager().callEvent(event);
            s = (new StringBuilder()).append("<").append(event.getPlayer().getName()).append("> ").append(event.getMessage()).toString();
            // CraftBukkit stop
            
            a.info(s);
            d.f.a(new Packet3Chat(s));
        }
    }

    private void c(String s) {
        // CraftBukkit start
        PlayerChatEvent event = new PlayerChatEvent(Type.PLAYER_COMMAND, player, s);
        server.getPluginManager().callEvent(event);
        if (event.isCancelled()) return;
        s = event.getMessage();
        CraftPlayer player = (CraftPlayer)event.getPlayer();
        EntityPlayerMP e = player.getHandle();
        // CraftBukkit stop

        if (s.toLowerCase().startsWith("/me ")) {
            s = (new StringBuilder()).append("* ").append(e.aw).append(" ").append(s.substring(s.indexOf(" ")).trim()).toString();
            a.info(s);
            d.f.a(new Packet3Chat(s));
        } else if (s.toLowerCase().startsWith("/kill")) {
            e.a(null, 1000);
        } else if (s.toLowerCase().startsWith("/tell ")) {
            String as[] = s.split(" ");

            if (as.length >= 3) {
                s = s.substring(s.indexOf(" ")).trim();
                s = s.substring(s.indexOf(" ")).trim();
                s = (new StringBuilder()).append("\2477").append(e.aw).append(" whispers ").append(s).toString();
                a.info((new StringBuilder()).append(s).append(" to ").append(as[1]).toString());
                if (!d.f.a(as[1], new Packet3Chat(s))) {
                    b(new Packet3Chat("\247cThere's no player by that name online."));
                }
            }
        } else if (d.f.g(e.aw)) {
            String s1 = s.substring(1);

            a.info((new StringBuilder()).append(e.aw).append(" issued server command: ").append(s1).toString());
            d.a(s1, this);
        } else {
            String s2 = s.substring(1);

            a.info((new StringBuilder()).append(e.aw).append(" tried command: ").append(s2).toString());
        }
    }

    public void a(Packet18ArmAnimation packet18armanimation) {
        if (packet18armanimation.b == 1) {
            e.H();
        } else if (packet18armanimation.b == 104) {
            e.al = true;
        } else if (packet18armanimation.b == 105) {
            e.al = false;
        }
    }

    public void a(Packet255KickDisconnect packet255kickdisconnect) {
        b.a("disconnect.quitting", new Object[0]);
    }

    public int b() {
        return b.d();
    }

    public void b(String s) {
        b(((Packet) (new Packet3Chat((new StringBuilder()).append("\2477").append(s).toString()))));
    }

    public String c() {
        return e.aw;
    }

    public void a(Packet7 packet7) {
        Entity entity = d.e.a(packet7.b);

        if (entity != null && e.i(entity)) {
            if (packet7.c == 0) {
                e.g(entity);
            } else if (packet7.c == 1) {
                e.h(entity);
            }
        }
    }

    public void a(Packet9 packet9) {
        if (e.ba > 0) {
            return;
        } else {
            e = d.f.d(e);
            player.setHandle(e); // CraftBukkit
            return;
        }
    }

    public void a(Packet101 packet101) {
        e.K();
    }

    public void a(Packet102 packet102) {
        if (e.ap.f == packet102.a && e.ap.c(e)) {
            ItemStack itemstack = e.ap.a(packet102.b, packet102.c, e);

            if (ItemStack.a(packet102.e, itemstack)) {
                e.a.b(new Packet106(packet102.a, packet102.d, true));
                e.am = true;
                e.ap.a();
                e.J();
                e.am = false;
            } else {
                k.put(Integer.valueOf(e.ap.f), Short.valueOf(packet102.d));
                e.a.b(new Packet106(packet102.a, packet102.d, false));
                e.ap.a(e, false);
                ArrayList arraylist = new ArrayList();

                for (int l = 0; l < e.ap.e.size(); l++) {
                    arraylist.add(((Slot) e.ap.e.get(l)).c());
                }

                e.a(e.ap, arraylist);
            }
        }
    }

    public void a(Packet106 packet106) {
        Short short1 = (Short) k.get(Integer.valueOf(e.ap.f));

        if (short1 != null && packet106.b == short1.shortValue() && e.ap.f == packet106.a && !e.ap.c(e)) {
            e.ap.a(e, true);
        }
    }

    public void a(Packet130 packet130) {
        if (d.e.f(packet130.a, packet130.b, packet130.c)) {
            TileEntity tileentity = d.e.l(packet130.a, packet130.b, packet130.c);

            for (int l = 0; l < 4; l++) {
                boolean flag = true;

                if (packet130.d[l].length() > 15) {
                    flag = false;
                } else {
                    for (int k1 = 0; k1 < packet130.d[l].length(); k1++) {
                        if (FontAllowedCharacters.a.indexOf(packet130.d[l].charAt(k1)) < 0) {
                            flag = false;
                        }
                    }

                }
                if (!flag) {
                    packet130.d[l] = "!?";
                }
            }

            if (tileentity instanceof TileEntitySign) {
                int i1 = packet130.a;
                int j1 = packet130.b;
                int l1 = packet130.c;
                TileEntitySign tileentitysign = (TileEntitySign) tileentity;

                for (int i2 = 0; i2 < 4; i2++) {
                    tileentitysign.e[i2] = packet130.d[i2];
                }

                tileentitysign.d();
                d.e.g(i1, j1, l1);
            }
        }
    }

}