Skip to content

Commit 1fa80fc

Browse files
octylFractaldordsor21
authored andcommitted
Remove never-working caching and fix bugs in region code (#2716)
Fixes #2684
1 parent 9f9f98c commit 1fa80fc

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.sk89q.worldedit.world.chunk.Chunk;
3333
import com.sk89q.worldedit.world.storage.ChunkStore;
3434
import com.sk89q.worldedit.world.storage.MissingChunkException;
35+
import org.apache.logging.log4j.LogManager;
36+
import org.apache.logging.log4j.Logger;
3537
import org.enginehub.linbus.tree.LinCompoundTag;
3638
import org.enginehub.linbus.tree.LinDoubleTag;
3739
import org.enginehub.linbus.tree.LinFloatTag;
@@ -50,6 +52,8 @@
5052
*/
5153
public class SnapshotRestore {
5254

55+
private static final Logger LOGGER = LogManager.getLogger();
56+
5357
//FAWE start - Set instead of ArrayList
5458
private final Map<BlockVector2, Set<BlockVector3>> neededChunks = new LinkedHashMap<>();
5559
//FAWE end
@@ -223,6 +227,7 @@ public void restore() throws MaxChangedBlocksException {
223227
} catch (MissingChunkException me) {
224228
missingChunks.add(chunkPos);
225229
} catch (IOException | DataException me) {
230+
LOGGER.info(() -> "Failed to load chunk at " + chunkPos, me);
226231
errorChunks.add(chunkPos);
227232
lastErrorMessage = me.getMessage();
228233
}

worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/McRegionChunkStore.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@
2323
import com.sk89q.worldedit.math.BlockVector2;
2424
import com.sk89q.worldedit.world.DataException;
2525
import com.sk89q.worldedit.world.World;
26+
import org.enginehub.linbus.stream.LinBinaryIO;
27+
import org.enginehub.linbus.tree.LinCompoundTag;
28+
import org.enginehub.linbus.tree.LinRootEntry;
2629

2730
import javax.annotation.Nullable;
31+
import java.io.DataInputStream;
2832
import java.io.IOException;
2933
import java.io.InputStream;
3034

3135
public abstract class McRegionChunkStore extends ChunkStore {
3236

33-
protected String curFilename = null;
34-
protected McRegionReader cachedReader = null;
35-
3637
/**
3738
* Get the filename of a region file.
3839
*
@@ -51,22 +52,11 @@ protected McRegionReader getReader(BlockVector2 pos, String worldname, @Nullable
5152
IOException {
5253
//FAWE end
5354
String filename = getFilename(pos);
54-
if (curFilename != null) {
55-
if (curFilename.equals(filename)) {
56-
return cachedReader;
57-
} else {
58-
try {
59-
cachedReader.close();
60-
} catch (IOException ignored) {
61-
}
62-
}
63-
}
6455
//FAWE start - biome and entity restore
6556
InputStream stream = getInputStream(filename, worldname, folderOverride);
6657
//FAWE end
67-
cachedReader = new McRegionReader(stream);
6858
//curFilename = filename;
69-
return cachedReader;
59+
return new McRegionReader(stream);
7060
}
7161

7262
@Override
@@ -107,11 +97,4 @@ protected abstract InputStream getInputStream(String name, String worldName, @Nu
10797
IOException, DataException;
10898
//FAWE end
10999

110-
@Override
111-
public void close() throws IOException {
112-
if (cachedReader != null) {
113-
cachedReader.close();
114-
}
115-
}
116-
117100
}

worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/McRegionReader.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ counts. The chunk offset for a chunk (x, z) begins at byte 4*(x+z*32) in the
6060
import com.sk89q.worldedit.world.DataException;
6161

6262
import java.io.ByteArrayInputStream;
63+
import java.io.Closeable;
6364
import java.io.DataInputStream;
65+
import java.io.EOFException;
6466
import java.io.IOException;
6567
import java.io.InputStream;
6668
import java.util.zip.GZIPInputStream;
@@ -70,7 +72,7 @@ counts. The chunk offset for a chunk (x, z) begins at byte 4*(x+z*32) in the
7072
* Reader for a MCRegion file. This reader works on input streams, meaning
7173
* that it can be used to read files from non-file based sources.
7274
*/
73-
public class McRegionReader {
75+
public class McRegionReader implements Closeable {
7476

7577
protected static final int VERSION_GZIP = 1;
7678
protected static final int VERSION_DEFLATE = 2;
@@ -129,10 +131,10 @@ public synchronized InputStream getChunkInputStream(BlockVector2 position) throw
129131
throw new DataException("The chunk at " + position + " is not generated");
130132
}
131133

132-
int sectorNumber = offset >> 8;
134+
long sectorNumber = offset >> 8;
133135
int numSectors = offset & 0xFF;
134136

135-
stream.seek((long) sectorNumber * SECTOR_BYTES);
137+
stream.seek(sectorNumber * SECTOR_BYTES);
136138
int length = dataStream.readInt();
137139

138140
if (length > SECTOR_BYTES * numSectors) {
@@ -142,19 +144,17 @@ public synchronized InputStream getChunkInputStream(BlockVector2 position) throw
142144

143145
byte version = dataStream.readByte();
144146

147+
byte[] data = new byte[length - 1];
148+
try {
149+
dataStream.readFully(data);
150+
} catch (EOFException e) {
151+
throw new DataException("MCRegion file does not contain "
152+
+ x + "," + z + " in full");
153+
}
154+
145155
if (version == VERSION_GZIP) {
146-
byte[] data = new byte[length - 1];
147-
if (dataStream.read(data) < length - 1) {
148-
throw new DataException("MCRegion file does not contain "
149-
+ x + "," + z + " in full");
150-
}
151156
return new GZIPInputStream(new ByteArrayInputStream(data));
152157
} else if (version == VERSION_DEFLATE) {
153-
byte[] data = new byte[length - 1];
154-
if (dataStream.read(data) < length - 1) {
155-
throw new DataException("MCRegion file does not contain "
156-
+ x + "," + z + " in full");
157-
}
158158
return new InflaterInputStream(new ByteArrayInputStream(data));
159159
} else {
160160
throw new DataException("MCRegion chunk at "
@@ -187,6 +187,7 @@ public boolean hasChunk(int x, int z) {
187187
/**
188188
* Close the stream.
189189
*/
190+
@Override
190191
public void close() throws IOException {
191192
stream.close();
192193
}

0 commit comments

Comments
 (0)