Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package net.minecraft.server;
2
3import java.util.*;
4
5public class ChunkProviderLoadOrGenerate implements IChunkProvider {
6
7 private Set a = new HashSet();
8 private Chunk b;
9 private IChunkProvider c;
10 private IChunkLoader d;
11 private Map e = new HashMap();
12 private List f = new ArrayList();
13 private World g;
14
15 public ChunkProviderLoadOrGenerate(World world, IChunkLoader ichunkloader, IChunkProvider ichunkprovider) {
16 this.b = new EmptyChunk(world, new byte['\u8000'], 0, 0);
17 this.g = world;
18 this.d = ichunkloader;
19 this.c = ichunkprovider;
20 }
21
22 public boolean isChunkLoaded(int i, int j) {
23 return this.e.containsKey(Integer.valueOf(ChunkCoordIntPair.a(i, j)));
24 }
25
26 public Chunk getChunkAt(int i, int j) {
27 int k = ChunkCoordIntPair.a(i, j);
28
29 this.a.remove(Integer.valueOf(k));
30 Chunk chunk = (Chunk) this.e.get(Integer.valueOf(k));
31
32 if (chunk == null) {
33 chunk = this.d(i, j);
34 if (chunk == null) {
35 if (this.c == null) {
36 chunk = this.b;
37 } else {
38 chunk = this.c.getOrCreateChunk(i, j);
39 }
40 }
41
42 this.e.put(Integer.valueOf(k), chunk);
43 this.f.add(chunk);
44 if (chunk != null) {
45 chunk.loadNOP();
46 chunk.addEntities();
47 }
48
49 if (!chunk.done && this.isChunkLoaded(i + 1, j + 1) && this.isChunkLoaded(i, j + 1) && this.isChunkLoaded(i + 1, j)) {
50 this.getChunkAt(this, i, j);
51 }
52
53 if (this.isChunkLoaded(i - 1, j) && !this.getOrCreateChunk(i - 1, j).done && this.isChunkLoaded(i - 1, j + 1) && this.isChunkLoaded(i, j + 1) && this.isChunkLoaded(i - 1, j)) {
54 this.getChunkAt(this, i - 1, j);
55 }
56
57 if (this.isChunkLoaded(i, j - 1) && !this.getOrCreateChunk(i, j - 1).done && this.isChunkLoaded(i + 1, j - 1) && this.isChunkLoaded(i, j - 1) && this.isChunkLoaded(i + 1, j)) {
58 this.getChunkAt(this, i, j - 1);
59 }
60
61 if (this.isChunkLoaded(i - 1, j - 1) && !this.getOrCreateChunk(i - 1, j - 1).done && this.isChunkLoaded(i - 1, j - 1) && this.isChunkLoaded(i, j - 1) && this.isChunkLoaded(i - 1, j)) {
62 this.getChunkAt(this, i - 1, j - 1);
63 }
64 }
65
66 return chunk;
67 }
68
69 public Chunk getOrCreateChunk(int i, int j) {
70 Chunk chunk = (Chunk) this.e.get(Integer.valueOf(ChunkCoordIntPair.a(i, j)));
71
72 return chunk == null ? this.getChunkAt(i, j) : chunk;
73 }
74
75 private Chunk d(int i, int j) {
76 if (this.d == null) {
77 return null;
78 } else {
79 try {
80 Chunk chunk = this.d.a(this.g, i, j);
81
82 if (chunk != null) {
83 chunk.r = this.g.getTime();
84 }
85
86 return chunk;
87 } catch (Exception exception) {
88 exception.printStackTrace();
89 return null;
90 }
91 }
92 }
93
94 private void a(Chunk chunk) {
95 if (this.d != null) {
96 try {
97 this.d.b(this.g, chunk);
98 } catch (Exception exception) {
99 exception.printStackTrace();
100 }
101 }
102 }
103
104 private void b(Chunk chunk) {
105 if (this.d != null) {
106 try {
107 chunk.r = this.g.getTime();
108 this.d.a(this.g, chunk);
109 } catch (Exception ioexception) {
110 ioexception.printStackTrace();
111 }
112 }
113 }
114
115 public void getChunkAt(IChunkProvider ichunkprovider, int i, int j) {
116 Chunk chunk = this.getOrCreateChunk(i, j);
117
118 if (!chunk.done) {
119 chunk.done = true;
120 if (this.c != null) {
121 this.c.getChunkAt(ichunkprovider, i, j);
122 chunk.f();
123 }
124 }
125 }
126
127 public boolean saveChunks(boolean flag, IProgressUpdate iprogressupdate) {
128 int i = 0;
129
130 for (int j = 0; j < this.f.size(); ++j) {
131 Chunk chunk = (Chunk) this.f.get(j);
132
133 if (flag && !chunk.p) {
134 this.a(chunk);
135 }
136
137 if (chunk.a(flag)) {
138 this.b(chunk);
139 chunk.o = false;
140 ++i;
141 if (i == 24 && !flag) {
142 return false;
143 }
144 }
145 }
146
147 if (flag) {
148 if (this.d == null) {
149 return true;
150 }
151
152 this.d.b();
153 }
154
155 return true;
156 }
157
158 public boolean unloadChunks() {
159 for (int i = 0; i < 100; ++i) {
160 if (!this.a.isEmpty()) {
161 Integer integer = (Integer) this.a.iterator().next();
162 Chunk chunk = (Chunk) this.e.get(integer);
163
164 chunk.removeEntities();
165 this.b(chunk);
166 this.a(chunk);
167 this.a.remove(integer);
168 this.e.remove(integer);
169 this.f.remove(chunk);
170 }
171 }
172
173 if (this.d != null) {
174 this.d.a();
175 }
176
177 return this.c.unloadChunks();
178 }
179
180 public boolean canSave() {
181 return true;
182 }
183}