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


import java.io.IOException;
import java.util.*;
import org.bukkit.craftbukkit.CraftChunk;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.event.Event.Type;
import org.bukkit.event.world.ChunkLoadedEvent;


public class ChunkProviderServer
        implements IChunkProvider {

    private Set a;
    private Chunk b;
    private IChunkProvider c;
    private IChunkLoader d;
    private Map e;
    private List f;
    private WorldServer g;

    public ChunkProviderServer(WorldServer worldserver, IChunkLoader ichunkloader, IChunkProvider ichunkprovider) {
        a = new HashSet();
        e = new HashMap();
        f = new ArrayList();
        b = new Chunk(worldserver, new byte[32768], 0, 0);
        b.q = true;
        b.p = true;
        g = worldserver;
        d = ichunkloader;
        c = ichunkprovider;
    }

    public boolean a(int i, int j) {
        ChunkCoordinates chunkcoordinates = new ChunkCoordinates(i, j);

        return e.containsKey(chunkcoordinates);
    }

    public void c(int i, int j) {
        int k = (i * 16 + 8) - g.m;
        int l = (j * 16 + 8) - g.o;
        char c1 = '\200';

        if (k < -c1 || k > c1 || l < -c1 || l > c1) {
            a.add(new ChunkCoordinates(i, j));
        }
    }

    public Chunk d(int i, int j) {
        ChunkCoordinates chunkcoordinates = new ChunkCoordinates(i, j);

        a.remove(new ChunkCoordinates(i, j));
        Chunk chunk = (Chunk) e.get(chunkcoordinates);

        if (chunk == null) {
            chunk = e(i, j);
            if (chunk == null) {
                if (c == null) {
                    chunk = b;
                } else {
                    chunk = c.b(i, j);
                }
            }
            e.put(chunkcoordinates, chunk);
            f.add(chunk);
            chunk.c();
            if (chunk != null) {
                chunk.d();
            }

            // Craftbukkit start
            CraftServer server = g.getServer();
            if (server != null) {
                /*
                 * If it's a new world, the first few chunks are generated inside
                 * the World constructor. We can't reliably alter that, so we have
                 * no way of creating a CraftWorld/CraftServer at that point.
                 */
                CraftWorld world = g.getWorld();
                CraftChunk cchunk = world.updateChunk(i, j);
                server.getPluginManager().callEvent(new ChunkLoadedEvent(Type.CHUNK_LOADED, cchunk));
            }
            // Craftbukkit end

            if (!chunk.n && a(i + 1, j + 1) && a(i, j + 1) && a(i + 1, j)) {
                a(this, i, j);
            }
            if (a(i - 1, j) && !b(i - 1, j).n && a(i - 1, j + 1) && a(i, j + 1) && a(i - 1, j)) {
                a(this, i - 1, j);
            }
            if (a(i, j - 1) && !b(i, j - 1).n && a(i + 1, j - 1) && a(i, j - 1) && a(i + 1, j)) {
                a(this, i, j - 1);
            }
            if (a(i - 1, j - 1) && !b(i - 1, j - 1).n && a(i - 1, j - 1) && a(i, j - 1) && a(i - 1, j)) {
                a(this, i - 1, j - 1);
            }
        }
        return chunk;
    }

    public Chunk b(int i, int j) {
        ChunkCoordinates chunkcoordinates = new ChunkCoordinates(i, j);
        Chunk chunk = (Chunk) e.get(chunkcoordinates);

        if (chunk == null) {
            if (g.x) {
                return d(i, j);
            } else {
                return b;
            }
        } else {
            return chunk;
        }
    }

    private Chunk e(int i, int j) {
        if (d == null) {
            return null;
        }
        try {
            Chunk chunk = d.a(g, i, j);

            if (chunk != null) {
                chunk.s = g.e;
            }
            return chunk;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }

    private void a(Chunk chunk) {
        if (d == null) {
            return;
        }
        try {
            d.b(g, chunk);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void b(Chunk chunk) {
        if (d == null) {
            return;
        }
        try {
            chunk.s = g.e;
            d.a(g, chunk);
        } catch (Throwable ioexception) { // Craftbukkit: Cast down to compile
            ioexception.printStackTrace();
        }
    }

    public void a(IChunkProvider ichunkprovider, int i, int j) {
        Chunk chunk = b(i, j);

        if (!chunk.n) {
            chunk.n = true;
            if (c != null) {
                c.a(ichunkprovider, i, j);
                chunk.f();
            }
        }
    }

    public boolean a(boolean flag, IProgressUpdate iprogressupdate) {
        int i = 0;

        for (int j = 0; j < f.size(); j++) {
            Chunk chunk = (Chunk) f.get(j);

            if (flag && !chunk.p) {
                a(chunk);
            }
            if (!chunk.a(flag)) {
                continue;
            }
            b(chunk);
            chunk.o = false;
            if (++i == 24 && !flag) {
                return false;
            }
        }

        if (flag) {
            if (d == null) {
                return true;
            }
            d.b();
        }
        return true;
    }

    public boolean a() {
        if (!g.C) {
            for (int i = 0; i < 100; i++) {
                if (!a.isEmpty()) {
                    ChunkCoordinates chunkcoordinates = (ChunkCoordinates) a.iterator().next();
                    Chunk chunk = b(chunkcoordinates.a, chunkcoordinates.b);

                    chunk.e();
                    b(chunk);
                    a(chunk);
                    a.remove(chunkcoordinates);
                    e.remove(chunkcoordinates);
                    f.remove(chunk);
                }
            }

            if (d != null) {
                d.a();
            }
        }
        return c.a();
    }

    public boolean b() {
        return !g.C;
    }
}