summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/PlayerInstance.java
blob: 1f43e4b6ba964c32cad805a8eff11fb5899e848e (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
package net.minecraft.server;

import java.util.ArrayList;
import java.util.List;

class PlayerInstance {

    private List b;
    private int chunkX;
    private int chunkZ;
    private ChunkCoordIntPair e;
    private short[] dirtyBlocks;
    private int dirtyCount;
    private int h;
    private int i;
    private int j;
    private int k;
    private int l;
    private int m;

    final PlayerManager playerManager;

    public PlayerInstance(PlayerManager playermanager, int i, int j) {
        this.playerManager = playermanager;
        this.b = new ArrayList();
        this.dirtyBlocks = new short[10];
        this.dirtyCount = 0;
        this.chunkX = i;
        this.chunkZ = j;
        this.e = new ChunkCoordIntPair(i, j);

        // CraftBukkit
        playermanager.world.chunkProviderServer.getChunkAt(i, j);
    }

    public void a(EntityPlayer entityplayer) {
        if (this.b.contains(entityplayer)) {
            throw new IllegalStateException("Failed to add player. " + entityplayer + " already is in chunk " + this.chunkX + ", " + this.chunkZ);
        } else {
            // CraftBukkit start
            if (entityplayer.g.add(this.e)) {
                entityplayer.netServerHandler.sendPacket(new Packet50PreChunk(this.e.x, this.e.z, true));
            }
            // CraftBukkit end

            this.b.add(entityplayer);
            entityplayer.f.add(this.e);
        }
    }

    public void b(EntityPlayer entityplayer) {
        if (!this.b.contains(entityplayer)) {
            // CraftBukkit - reduce console spam under certain conditions
            // (new IllegalStateException("Failed to remove player. " + entityplayer + " isn\'t in chunk " + this.chunkX + ", " + this.chunkZ)).printStackTrace();
        } else {
            this.b.remove(entityplayer);
            if (this.b.size() == 0) {
                long i = (long) this.chunkX + 2147483647L | (long) this.chunkZ + 2147483647L << 32;

                PlayerManager.b(this.playerManager).b(i);
                if (this.dirtyCount > 0) {
                    PlayerManager.c(this.playerManager).remove(this);
                }

                // CraftBukkit
                ((WorldServer) entityplayer.world).chunkProviderServer.queueUnload(this.chunkX, this.chunkZ);
            }

            entityplayer.f.remove(this.e);
            // CraftBukkit - contains -> remove
            if (entityplayer.g.remove(this.e)) {
                entityplayer.netServerHandler.sendPacket(new Packet50PreChunk(this.chunkX, this.chunkZ, false));
            }
        }
    }

    public void a(int i, int j, int k) {
        if (this.dirtyCount == 0) {
            PlayerManager.c(this.playerManager).add(this);
            this.h = this.i = i;
            this.j = this.k = j;
            this.l = this.m = k;
        }

        if (this.h > i) {
            this.h = i;
        }

        if (this.i < i) {
            this.i = i;
        }

        if (this.j > j) {
            this.j = j;
        }

        if (this.k < j) {
            this.k = j;
        }

        if (this.l > k) {
            this.l = k;
        }

        if (this.m < k) {
            this.m = k;
        }

        if (this.dirtyCount < 10) {
            short short1 = (short) (i << 12 | k << 8 | j);

            for (int l = 0; l < this.dirtyCount; ++l) {
                if (this.dirtyBlocks[l] == short1) {
                    return;
                }
            }

            this.dirtyBlocks[this.dirtyCount++] = short1;
        }
    }

    public void sendAll(Packet packet) {
        for (int i = 0; i < this.b.size(); ++i) {
            EntityPlayer entityplayer = (EntityPlayer) this.b.get(i);

            if (entityplayer.g.contains(this.e)) {
                entityplayer.netServerHandler.sendPacket(packet);
            }
        }
    }

    public void a() {
        if (this.dirtyCount != 0) {
            int i;
            int j;
            int k;

            if (this.dirtyCount == 1) {
                i = this.chunkX * 16 + this.h;
                j = this.j;
                k = this.chunkZ * 16 + this.l;

                // CraftBukkit start
                this.sendAll(new Packet53BlockChange(i, j, k, this.playerManager.world));
                if (Block.isTileEntity[this.playerManager.world.getTypeId(i, j, k)]) {
                    this.sendTileEntity(this.playerManager.world.getTileEntity(i, j, k));
                }
                // CraftBukkit end
            } else {
                int l;

                if (this.dirtyCount == 10) {
                    this.j = this.j / 2 * 2;
                    this.k = (this.k / 2 + 1) * 2;
                    i = this.h + this.chunkX * 16;
                    j = this.j;
                    k = this.l + this.chunkZ * 16;
                    l = this.i - this.h + 1;
                    int i1 = this.k - this.j + 2;
                    int j1 = this.m - this.l + 1;

                    // CraftBukkit start
                    this.sendAll(new Packet51MapChunk(i, j, k, l, i1, j1, this.playerManager.world));
                    List list = this.playerManager.world.getTileEntities(i, j, k, i + l, j + i1, k + j1);
                    // CraftBukkit end

                    for (int k1 = 0; k1 < list.size(); ++k1) {
                        this.sendTileEntity((TileEntity) list.get(k1));
                    }
                } else {
                    // CraftBukkit
                    this.sendAll(new Packet52MultiBlockChange(this.chunkX, this.chunkZ, this.dirtyBlocks, this.dirtyCount, this.playerManager.world));

                    for (i = 0; i < this.dirtyCount; ++i) {
                        // CraftBukkit start - Fixes TileEntity updates occurring upon a multi-block change; dirtyCount -> dirtyBlocks[i]
                        j = this.chunkX * 16 + (this.dirtyBlocks[i] >> 12 & 15);
                        k = this.dirtyBlocks[i] & 255;
                        l = this.chunkZ * 16 + (this.dirtyBlocks[i] >> 8 & 15);
                        // CraftBukkit end

                        // CraftBukkit start
                        if (Block.isTileEntity[this.playerManager.world.getTypeId(j, k, l)]) {
                            // System.out.println("Sending!"); // CraftBukkit
                            this.sendTileEntity(this.playerManager.world.getTileEntity(j, k, l));
                        }
                        // CraftBukkit end
                    }
                }
            }

            this.dirtyCount = 0;
        }
    }

    private void sendTileEntity(TileEntity tileentity) {
        if (tileentity != null) {
            Packet packet = tileentity.e();

            if (packet != null) {
                this.sendAll(packet);
            }
        }
    }
}