From e0d85f842b1b96b2b1c055e66d65b65d2f14c306 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Sat, 2 Jan 2021 11:18:32 -0500 Subject: [PATCH 1/5] Fix exception thrown when plants fake grow at max world height. --- .../com/gmail/nossr50/listeners/BlockListener.java | 12 +++++++++--- .../nossr50/util/blockmeta/BitSetChunkStore.java | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index a2a4c2a76..a6d8e7900 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -253,12 +253,18 @@ public class BlockListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockGrow(BlockGrowEvent event) { + Block block = event.getBlock(); + World world = block.getWorld(); + /* WORLD BLACKLIST CHECK */ - if(WorldBlacklist.isWorldBlacklisted(event.getBlock().getWorld())) + if(WorldBlacklist.isWorldBlacklisted(world)) return; - BlockState blockState = event.getBlock().getState(); - mcMMO.getPlaceStore().setFalse(blockState); + // Minecraft is dumb, the events still throw when a plant "grows" higher than the max block height. Even though no new block is created + if (block.getY() >= world.getMaxHeight()) + return; + + mcMMO.getPlaceStore().setFalse(block); } /** diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java b/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java index dce7ab174..a1fe92e4b 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java @@ -82,7 +82,7 @@ public class BitSetChunkStore implements ChunkStore, Serializable { private int coordToIndex(int x, int y, int z) { if (x < 0 || x >= 16 || y < 0 || y >= worldHeight || z < 0 || z >= 16) - throw new IndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(String.format("x: %d y: %d z: %d World Height: %d", x, y, z, worldHeight)); return (z * 16 + x) + (256 * y); } From a39c7420b94d9c5a97f50a19025c5754ef819343 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Sat, 2 Jan 2021 13:32:30 -0800 Subject: [PATCH 2/5] Add relocation for Kyori's examination library --- pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pom.xml b/pom.xml index 49d76da30..a0e35a0be 100755 --- a/pom.xml +++ b/pom.xml @@ -121,6 +121,10 @@ + + net.kyori.examination + com.gmail.nossr50.kyori.examination + net.kyori.adventure com.gmail.nossr50.mcmmo.kyori.adventure From d08c9391b045b786700a38d6a0385ec95621e85b Mon Sep 17 00:00:00 2001 From: nossr50 Date: Sat, 2 Jan 2021 13:36:59 -0800 Subject: [PATCH 3/5] Add nullability annotations to blockmeta --- .../util/blockmeta/BitSetChunkStore.java | 10 ++-- .../nossr50/util/blockmeta/ChunkManager.java | 23 ++++---- .../util/blockmeta/ChunkManagerFactory.java | 3 +- .../nossr50/util/blockmeta/ChunkStore.java | 3 +- .../util/blockmeta/HashChunkManager.java | 56 ++++++++++--------- .../util/blockmeta/McMMOSimpleRegionFile.java | 11 ++-- src/test/java/ChunkStoreTest.java | 3 +- 7 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java b/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java index a1fe92e4b..2f4ffe9ce 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java @@ -2,6 +2,8 @@ package com.gmail.nossr50.util.blockmeta; import org.bukkit.Bukkit; import org.bukkit.World; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.*; import java.util.BitSet; @@ -19,7 +21,7 @@ public class BitSetChunkStore implements ChunkStore, Serializable { private int worldHeight; private UUID worldUid; - public BitSetChunkStore(World world, int cx, int cz) { + public BitSetChunkStore(@NotNull World world, int cx, int cz) { this.cx = cx; this.cz = cz; this.worldUid = world.getUID(); @@ -50,7 +52,7 @@ public class BitSetChunkStore implements ChunkStore, Serializable { } @Override - public UUID getWorldId() { + public @NotNull UUID getWorldId() { return worldUid; } @@ -153,7 +155,7 @@ public class BitSetChunkStore implements ChunkStore, Serializable { dirty = false; } - private static BitSetChunkStore deserialize(DataInputStream in) throws IOException { + private static BitSetChunkStore deserialize(@NotNull DataInputStream in) throws IOException { int magic = in.readInt(); // Can be used to determine the format of the file int fileVersionNumber = in.readInt(); @@ -182,7 +184,7 @@ public class BitSetChunkStore implements ChunkStore, Serializable { public static final short STREAM_MAGIC = (short)0xACDC; - public static ChunkStore readChunkStore(DataInputStream inputStream) throws IOException { + public static @NotNull ChunkStore readChunkStore(DataInputStream inputStream) throws IOException { if (inputStream.markSupported()) inputStream.mark(2); short magicNumber = inputStream.readShort(); diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManager.java index 5cb50ac7f..57c66a899 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManager.java @@ -3,6 +3,7 @@ package com.gmail.nossr50.util.blockmeta; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockState; +import org.jetbrains.annotations.Nullable; public interface ChunkManager { void closeAll(); @@ -14,7 +15,7 @@ public interface ChunkManager { * @param cz Chunk Z coordinate that is to be saved * @param world World that the Chunk is in */ - void saveChunk(int cx, int cz, World world); + void saveChunk(int cx, int cz, @Nullable World world); /** * Informs the ChunkletManager a chunk is unloaded @@ -23,7 +24,7 @@ public interface ChunkManager { * @param cz Chunk Z coordinate that is unloaded * @param world World that the chunk was unloaded in */ - void chunkUnloaded(int cx, int cz, World world); + void chunkUnloaded(int cx, int cz, @Nullable World world); /** * Save all ChunkletStores related to the given world @@ -53,7 +54,7 @@ public interface ChunkManager { * @param world World to check in * @return true if the given location is set to true, false if otherwise */ - boolean isTrue(int x, int y, int z, World world); + boolean isTrue(int x, int y, int z, @Nullable World world); /** * Check to see if a given block location is set to true @@ -61,7 +62,7 @@ public interface ChunkManager { * @param block Block location to check * @return true if the given block location is set to true, false if otherwise */ - boolean isTrue(Block block); + boolean isTrue(@Nullable Block block); /** * Check to see if a given BlockState location is set to true @@ -69,7 +70,7 @@ public interface ChunkManager { * @param blockState BlockState to check * @return true if the given BlockState location is set to true, false if otherwise */ - boolean isTrue(BlockState blockState); + boolean isTrue(@Nullable BlockState blockState); /** * Set a given location to true, should create stores as necessary if the location does not exist @@ -79,21 +80,21 @@ public interface ChunkManager { * @param z Z coordinate to set * @param world World to set in */ - void setTrue(int x, int y, int z, World world); + void setTrue(int x, int y, int z, @Nullable World world); /** * Set a given block location to true, should create stores as necessary if the location does not exist * * @param block Block location to set */ - void setTrue(Block block); + void setTrue(@Nullable Block block); /** * Set a given BlockState location to true, should create stores as necessary if the location does not exist * * @param blockState BlockState location to set */ - void setTrue(BlockState blockState); + void setTrue(@Nullable BlockState blockState); /** * Set a given location to false, should not create stores if one does not exist for the given location @@ -103,21 +104,21 @@ public interface ChunkManager { * @param z Z coordinate to set * @param world World to set in */ - void setFalse(int x, int y, int z, World world); + void setFalse(int x, int y, int z, @Nullable World world); /** * Set a given block location to false, should not create stores if one does not exist for the given location * * @param block Block location to set */ - void setFalse(Block block); + void setFalse(@Nullable Block block); /** * Set a given BlockState location to false, should not create stores if one does not exist for the given location * * @param blockState BlockState location to set */ - void setFalse(BlockState blockState); + void setFalse(@Nullable BlockState blockState); /** * Delete any ChunkletStores that are empty diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManagerFactory.java b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManagerFactory.java index a290c5e2a..e2c47662e 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManagerFactory.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManagerFactory.java @@ -1,9 +1,10 @@ package com.gmail.nossr50.util.blockmeta; import com.gmail.nossr50.config.HiddenConfig; +import org.jetbrains.annotations.NotNull; public class ChunkManagerFactory { - public static ChunkManager getChunkManager() { + public static @NotNull ChunkManager getChunkManager() { HiddenConfig hConfig = HiddenConfig.getInstance(); if (hConfig.getChunkletsEnabled()) { diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkStore.java b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkStore.java index eca783ccd..df01b93ee 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkStore.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkStore.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.util.blockmeta; import org.bukkit.World; +import org.jetbrains.annotations.NotNull; import java.util.UUID; @@ -36,7 +37,7 @@ public interface ChunkStore { */ int getChunkZ(); - UUID getWorldId(); + @NotNull UUID getWorldId(); /** * Checks the value at the given coordinates diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkManager.java index 888937872..f3023e0e3 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkManager.java @@ -5,14 +5,16 @@ import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockState; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.*; import java.util.*; public class HashChunkManager implements ChunkManager { - private final HashMap regionMap = new HashMap<>(); // Tracks active regions - private final HashMap> chunkUsageMap = new HashMap<>(); // Tracks active chunks by region - private final HashMap chunkMap = new HashMap<>(); // Tracks active chunks + private final @NotNull HashMap regionMap = new HashMap<>(); // Tracks active regions + private final @NotNull HashMap> chunkUsageMap = new HashMap<>(); // Tracks active chunks by region + private final @NotNull HashMap chunkMap = new HashMap<>(); // Tracks active chunks @Override public synchronized void closeAll() { @@ -32,7 +34,7 @@ public class HashChunkManager implements ChunkManager { regionMap.clear(); } - private synchronized ChunkStore readChunkStore(World world, int cx, int cz) throws IOException { + private synchronized @Nullable ChunkStore readChunkStore(@NotNull World world, int cx, int cz) throws IOException { McMMOSimpleRegionFile rf = getSimpleRegionFile(world, cx, cz, false); if (rf == null) return null; // If there is no region file, there can't be a chunk @@ -43,7 +45,7 @@ public class HashChunkManager implements ChunkManager { } } - private synchronized void writeChunkStore(World world, ChunkStore data) { + private synchronized void writeChunkStore(@NotNull World world, @NotNull ChunkStore data) { if (!data.isDirty()) return; // Don't save unchanged data try { @@ -58,7 +60,7 @@ public class HashChunkManager implements ChunkManager { } } - private synchronized McMMOSimpleRegionFile getSimpleRegionFile(World world, int cx, int cz, boolean createIfAbsent) { + private synchronized @Nullable McMMOSimpleRegionFile getSimpleRegionFile(World world, int cx, int cz, boolean createIfAbsent) { CoordinateKey regionKey = toRegionKey(world.getUID(), cx, cz); return regionMap.computeIfAbsent(regionKey, k -> { @@ -73,7 +75,7 @@ public class HashChunkManager implements ChunkManager { }); } - private ChunkStore loadChunk(int cx, int cz, World world) { + private @Nullable ChunkStore loadChunk(int cx, int cz, World world) { try { return readChunkStore(world, cx, cz); } @@ -82,7 +84,7 @@ public class HashChunkManager implements ChunkManager { return null; } - private void unloadChunk(int cx, int cz, World world) { + private void unloadChunk(int cx, int cz, @NotNull World world) { CoordinateKey chunkKey = toChunkKey(world.getUID(), cx, cz); ChunkStore chunkStore = chunkMap.remove(chunkKey); // Remove from chunk map if (chunkStore == null) @@ -102,7 +104,7 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void saveChunk(int cx, int cz, World world) { + public synchronized void saveChunk(int cx, int cz, @Nullable World world) { if (world == null) return; @@ -120,7 +122,7 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void chunkUnloaded(int cx, int cz, World world) { + public synchronized void chunkUnloaded(int cx, int cz, @Nullable World world) { if (world == null) return; @@ -128,7 +130,7 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void saveWorld(World world) { + public synchronized void saveWorld(@Nullable World world) { if (world == null) return; @@ -148,7 +150,7 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void unloadWorld(World world) { + public synchronized void unloadWorld(@Nullable World world) { if (world == null) return; @@ -185,7 +187,7 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized boolean isTrue(int x, int y, int z, World world) { + public synchronized boolean isTrue(int x, int y, int z, @Nullable World world) { if (world == null) return false; @@ -214,7 +216,7 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized boolean isTrue(Block block) { + public synchronized boolean isTrue(@Nullable Block block) { if (block == null) return false; @@ -222,7 +224,7 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized boolean isTrue(BlockState blockState) { + public synchronized boolean isTrue(@Nullable BlockState blockState) { if (blockState == null) return false; @@ -230,12 +232,12 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void setTrue(int x, int y, int z, World world) { + public synchronized void setTrue(int x, int y, int z, @Nullable World world) { set(x, y, z, world, true); } @Override - public synchronized void setTrue(Block block) { + public synchronized void setTrue(@Nullable Block block) { if (block == null) return; @@ -243,7 +245,7 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void setTrue(BlockState blockState) { + public synchronized void setTrue(@Nullable BlockState blockState) { if (blockState == null) return; @@ -251,12 +253,12 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void setFalse(int x, int y, int z, World world) { + public synchronized void setFalse(int x, int y, int z, @Nullable World world) { set(x, y, z, world, false); } @Override - public synchronized void setFalse(Block block) { + public synchronized void setFalse(@Nullable Block block) { if (block == null) return; @@ -264,14 +266,14 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void setFalse(BlockState blockState) { + public synchronized void setFalse(@Nullable BlockState blockState) { if (blockState == null) return; setFalse(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld()); } - public synchronized void set(int x, int y, int z, World world, boolean value){ + public synchronized void set(int x, int y, int z, @Nullable World world, boolean value){ if (world == null) return; @@ -307,15 +309,15 @@ public class HashChunkManager implements ChunkManager { cStore.set(ix, y, iz, value); } - private CoordinateKey blockCoordinateToChunkKey(UUID worldUid, int x, int y, int z) { + private CoordinateKey blockCoordinateToChunkKey(@NotNull UUID worldUid, int x, int y, int z) { return toChunkKey(worldUid, x >> 4, z >> 4); } - private CoordinateKey toChunkKey(UUID worldUid, int cx, int cz){ + private CoordinateKey toChunkKey(@NotNull UUID worldUid, int cx, int cz){ return new CoordinateKey(worldUid, cx, cz); } - private CoordinateKey toRegionKey(UUID worldUid, int cx, int cz) { + private CoordinateKey toRegionKey(@NotNull UUID worldUid, int cx, int cz) { // Compute region index (32x32 chunk regions) int rx = cx >> 5; int rz = cz >> 5; @@ -323,11 +325,11 @@ public class HashChunkManager implements ChunkManager { } private static final class CoordinateKey { - public final UUID worldID; + public final @NotNull UUID worldID; public final int x; public final int z; - private CoordinateKey(UUID worldID, int x, int z) { + private CoordinateKey(@NotNull UUID worldID, int x, int z) { this.worldID = worldID; this.x = x; this.z = z; diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/McMMOSimpleRegionFile.java b/src/main/java/com/gmail/nossr50/util/blockmeta/McMMOSimpleRegionFile.java index bef730ff4..3f61c9bef 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/McMMOSimpleRegionFile.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/McMMOSimpleRegionFile.java @@ -19,6 +19,9 @@ */ package com.gmail.nossr50.util.blockmeta; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.io.*; import java.util.BitSet; import java.util.zip.DeflaterOutputStream; @@ -54,7 +57,7 @@ public class McMMOSimpleRegionFile { private final int segmentMask; // File location - private final File parent; + private final @NotNull File parent; // File access private final RandomAccessFile file; @@ -62,7 +65,7 @@ public class McMMOSimpleRegionFile { private final int rx; private final int rz; - public McMMOSimpleRegionFile(File f, int rx, int rz) { + public McMMOSimpleRegionFile(@NotNull File f, int rx, int rz) { this.rx = rx; this.rz = rz; this.parent = f; @@ -104,7 +107,7 @@ public class McMMOSimpleRegionFile { } } - public synchronized DataOutputStream getOutputStream(int x, int z) { + public synchronized @NotNull DataOutputStream getOutputStream(int x, int z) { int index = getChunkIndex(x, z); // Get chunk index return new DataOutputStream(new DeflaterOutputStream(new McMMOSimpleChunkBuffer(this, index))); } @@ -144,7 +147,7 @@ public class McMMOSimpleRegionFile { file.writeInt(chunkNumBytes[index]); } - public synchronized DataInputStream getInputStream(int x, int z) throws IOException { + public synchronized @Nullable DataInputStream getInputStream(int x, int z) throws IOException { int index = getChunkIndex(x, z); // Get chunk index int byteLength = chunkNumBytes[index]; // Get byte length of data diff --git a/src/test/java/ChunkStoreTest.java b/src/test/java/ChunkStoreTest.java index f45b0e0c9..43f105bad 100644 --- a/src/test/java/ChunkStoreTest.java +++ b/src/test/java/ChunkStoreTest.java @@ -2,6 +2,7 @@ import com.gmail.nossr50.util.blockmeta.*; import com.google.common.io.Files; import org.bukkit.Bukkit; import org.bukkit.World; +import org.jetbrains.annotations.NotNull; import org.junit.*; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -227,7 +228,7 @@ public class ChunkStoreTest { } @Override - public UUID getWorldId() { + public @NotNull UUID getWorldId() { return worldUid; } From 10694042e9d6ebe8bff60f9571af978bbdcb8b8e Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Sat, 2 Jan 2021 16:46:35 -0500 Subject: [PATCH 4/5] Move legacy Serializable usage into a subclass. --- .../util/blockmeta/BitSetChunkStore.java | 172 ++++++++++-------- 1 file changed, 101 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java b/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java index 2f4ffe9ce..a04ed501b 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java @@ -3,33 +3,35 @@ package com.gmail.nossr50.util.blockmeta; import org.bukkit.Bukkit; import org.bukkit.World; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.*; import java.util.BitSet; import java.util.UUID; -public class BitSetChunkStore implements ChunkStore, Serializable { - private static final long serialVersionUID = -1L; - transient private boolean dirty = false; - // Bitset store conforms to a "bottom-up" bit ordering consisting of a stack of {worldHeight} Y planes, each Y plane consists of 16 Z rows of 16 X bits. - private BitSet store; +public class BitSetChunkStore implements ChunkStore { private static final int CURRENT_VERSION = 8; private static final int MAGIC_NUMBER = 0xEA5EDEBB; - private int cx; - private int cz; - private int worldHeight; - private UUID worldUid; + + private final int cx; + private final int cz; + private final int worldHeight; + private final UUID worldUid; + // Bitset store conforms to a "bottom-up" bit ordering consisting of a stack of {worldHeight} Y planes, each Y plane consists of 16 Z rows of 16 X bits. + private final BitSet store; + + private transient boolean dirty = false; public BitSetChunkStore(@NotNull World world, int cx, int cz) { - this.cx = cx; - this.cz = cz; - this.worldUid = world.getUID(); - this.worldHeight = world.getMaxHeight(); - this.store = new BitSet(16 * 16 * worldHeight); + this(world.getUID(), world.getMaxHeight(), cx, cz); } - private BitSetChunkStore() {} + private BitSetChunkStore(@NotNull UUID worldUid, int worldHeight, int cx, int cz) { + this.cx = cx; + this.cz = cz; + this.worldUid = worldUid; + this.worldHeight = worldHeight; + this.store = new BitSet(16 * 16 * worldHeight); + } @Override public boolean isDirty() { @@ -83,58 +85,24 @@ public class BitSetChunkStore implements ChunkStore, Serializable { } private int coordToIndex(int x, int y, int z) { + return coordToIndex(x, y, z, worldHeight); + } + + private static int coordToIndex(int x, int y, int z, int worldHeight) { if (x < 0 || x >= 16 || y < 0 || y >= worldHeight || z < 0 || z >= 16) throw new IndexOutOfBoundsException(String.format("x: %d y: %d z: %d World Height: %d", x, y, z, worldHeight)); return (z * 16 + x) + (256 * y); } - private void fixWorldHeight() { + private static int getWorldHeight(UUID worldUid, int storedWorldHeight) + { World world = Bukkit.getWorld(worldUid); // Not sure how this case could come up, but might as well handle it gracefully. Loading a chunkstore for an unloaded world? if (world == null) - return; + return storedWorldHeight; - // Lop off any extra data if the world height has shrunk - int currentWorldHeight = world.getMaxHeight(); - if (currentWorldHeight < worldHeight) - { - store.clear(coordToIndex(16, currentWorldHeight, 16), store.length()); - worldHeight = currentWorldHeight; - dirty = true; - } - // If the world height has grown, update the worldHeight variable, but don't bother marking it dirty as unless something else changes we don't need to force a file write; - else if (currentWorldHeight > worldHeight) - worldHeight = currentWorldHeight; - } - - @Deprecated - private void writeObject(ObjectOutputStream out) throws IOException { - throw new UnsupportedOperationException("Serializable support should only be used for legacy deserialization"); - } - - @Deprecated - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.readInt(); // Magic number - in.readInt(); // Format version - long lsb = in.readLong(); - long msb = in.readLong(); - worldUid = new UUID(msb, lsb); - cx = in.readInt(); - cz = in.readInt(); - - boolean[][][] oldStore = (boolean[][][]) in.readObject(); - worldHeight = oldStore[0][0].length; - store = new BitSet(16 * 16 * worldHeight / 8); - for (int x = 0; x < 16; x++) { - for (int z = 0; z < 16; z++) { - for (int y = 0; y < worldHeight; y++) { - store.set(coordToIndex(x, y, z), oldStore[x][z][y]); - } - } - } - dirty = true; - fixWorldHeight(); + return world.getMaxHeight(); } private void serialize(DataOutputStream out) throws IOException { @@ -163,26 +131,34 @@ public class BitSetChunkStore implements ChunkStore, Serializable { if (magic != MAGIC_NUMBER || fileVersionNumber != CURRENT_VERSION) throw new IOException(); - BitSetChunkStore chunkStore = new BitSetChunkStore(); - long lsb = in.readLong(); long msb = in.readLong(); - chunkStore.worldUid = new UUID(msb, lsb); - chunkStore.cx = in.readInt(); - chunkStore.cz = in.readInt(); + UUID worldUid = new UUID(msb, lsb); + int cx = in.readInt(); + int cz = in.readInt(); - chunkStore.worldHeight = in.readInt(); + int worldHeight = in.readInt(); byte[] temp = new byte[in.readInt()]; in.readFully(temp); - chunkStore.store = BitSet.valueOf(temp); + BitSet stored = BitSet.valueOf(temp); + + int currentWorldHeight = getWorldHeight(worldUid, worldHeight); + + boolean worldHeightShrunk = currentWorldHeight < worldHeight; + // Lop off extra data if world height has shrunk + if (worldHeightShrunk) + stored.clear(coordToIndex(16, currentWorldHeight, 16, worldHeight), stored.length()); + + BitSetChunkStore chunkStore = new BitSetChunkStore(worldUid, currentWorldHeight, cx, cz); + chunkStore.store.or(stored); + chunkStore.dirty = worldHeightShrunk; // In the expanded case there is no reason to re-write it unless the data changes - chunkStore.fixWorldHeight(); return chunkStore; } public static class Serialization { - public static final short STREAM_MAGIC = (short)0xACDC; + public static final short STREAM_MAGIC = (short)0xACDC; // Rock on public static @NotNull ChunkStore readChunkStore(DataInputStream inputStream) throws IOException { if (inputStream.markSupported()) @@ -198,7 +174,7 @@ public class BitSetChunkStore implements ChunkStore, Serializable { { // Creates a new stream with the two magic number bytes and then the rest of the original stream... Java is so dumb. I just wanted to look at two bytes. PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream, 2); - pushbackInputStream.unread((magicNumber >>> 0) & 0xFF); + pushbackInputStream.unread((magicNumber) & 0xFF); pushbackInputStream.unread((magicNumber >>> 8) & 0xFF); inputStream = new DataInputStream(pushbackInputStream); } @@ -218,8 +194,61 @@ public class BitSetChunkStore implements ChunkStore, Serializable { ((BitSetChunkStore)chunkStore).serialize(outputStream); } - // Handles loading the old serialized classes even though we have changed name/package + // Handles loading the old serialized class private static class LegacyDeserializationInputStream extends ObjectInputStream { + private static class LegacyChunkStoreDeserializer implements Serializable + { + private static final long serialVersionUID = -1L; + + private int cx; + private int cz; + private int worldHeight; + private UUID worldUid; + private boolean[][][] store; + + private LegacyChunkStoreDeserializer() {} + + @Deprecated + private void writeObject(ObjectOutputStream out) throws IOException { + throw new UnsupportedOperationException("You goofed."); + } + + @Deprecated + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { + in.readInt(); // Magic number + in.readInt(); // Format version + long lsb = in.readLong(); + long msb = in.readLong(); + + worldUid = new UUID(msb, lsb); + cx = in.readInt(); + cz = in.readInt(); + + store = (boolean[][][]) in.readObject(); + worldHeight = store[0][0].length; + } + + public BitSetChunkStore convert() + { + int currentWorldHeight = getWorldHeight(worldUid, worldHeight); + + BitSetChunkStore converted = new BitSetChunkStore(worldUid, currentWorldHeight, cx, cz); + + // Read old data into new chunkstore + for (int x = 0; x < 16; x++) { + for (int z = 0; z < 16; z++) { + for (int y = 0; y < worldHeight && y < currentWorldHeight; y++) { + converted.store.set(converted.coordToIndex(x, y, z), store[x][z][y]); + } + } + } + // Mark dirty so it will be re-written in new format on close + converted.dirty = true; + return converted; + } + } + + public LegacyDeserializationInputStream(InputStream in) throws IOException { super(in); enableResolveObject(true); @@ -229,13 +258,14 @@ public class BitSetChunkStore implements ChunkStore, Serializable { protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { ObjectStreamClass read = super.readClassDescriptor(); if (read.getName().contentEquals("com.gmail.nossr50.util.blockmeta.chunkmeta.PrimitiveChunkStore")) - return ObjectStreamClass.lookup(BitSetChunkStore.class); + return ObjectStreamClass.lookup(LegacyChunkStoreDeserializer.class); return read; } public ChunkStore readLegacyChunkStore(){ try { - return (ChunkStore) readObject(); + LegacyChunkStoreDeserializer deserializer = (LegacyChunkStoreDeserializer)readObject(); + return deserializer.convert(); } catch (IOException | ClassNotFoundException e) { return null; } From 5b41b04777c9bdd5a67bb5f643a4fae1c7ff15d3 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Sat, 2 Jan 2021 17:29:15 -0500 Subject: [PATCH 5/5] Use @NotNull in methods rather than @Nullable Separate safe external methods from internal only methods Remove unnecessary methods --- src/main/java/com/gmail/nossr50/mcMMO.java | 1 - .../nossr50/util/blockmeta/ChunkManager.java | 125 +--------------- .../nossr50/util/blockmeta/ChunkStore.java | 1 - .../util/blockmeta/HashChunkManager.java | 134 ++++-------------- .../util/blockmeta/NullChunkManager.java | 40 ++---- .../util/blockmeta/UserBlockTracker.java | 56 ++++++++ src/test/java/ChunkStoreTest.java | 18 ++- 7 files changed, 109 insertions(+), 266 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/util/blockmeta/UserBlockTracker.java diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index f3f6bb9ac..c956b65f4 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -345,7 +345,6 @@ public class mcMMO extends JavaPlugin { formulaManager.saveFormula(); holidayManager.saveAnniversaryFiles(); - placeStore.cleanUp(); // Cleanup empty metadata stores placeStore.closeAll(); } diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManager.java index 57c66a899..a0b61ba6f 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkManager.java @@ -1,127 +1,10 @@ package com.gmail.nossr50.util.blockmeta; import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.BlockState; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; -public interface ChunkManager { +public interface ChunkManager extends UserBlockTracker { void closeAll(); - - /** - * Saves a given Chunk's Chunklet data - * - * @param cx Chunk X coordinate that is to be saved - * @param cz Chunk Z coordinate that is to be saved - * @param world World that the Chunk is in - */ - void saveChunk(int cx, int cz, @Nullable World world); - - /** - * Informs the ChunkletManager a chunk is unloaded - * - * @param cx Chunk X coordinate that is unloaded - * @param cz Chunk Z coordinate that is unloaded - * @param world World that the chunk was unloaded in - */ - void chunkUnloaded(int cx, int cz, @Nullable World world); - - /** - * Save all ChunkletStores related to the given world - * - * @param world World to save - */ - void saveWorld(World world); - - /** - * Unload all ChunkletStores from memory related to the given world after saving them - * - * @param world World to unload - */ - void unloadWorld(World world); - - /** - * Save all ChunkletStores - */ - void saveAll(); - - /** - * Check to see if a given location is set to true - * - * @param x X coordinate to check - * @param y Y coordinate to check - * @param z Z coordinate to check - * @param world World to check in - * @return true if the given location is set to true, false if otherwise - */ - boolean isTrue(int x, int y, int z, @Nullable World world); - - /** - * Check to see if a given block location is set to true - * - * @param block Block location to check - * @return true if the given block location is set to true, false if otherwise - */ - boolean isTrue(@Nullable Block block); - - /** - * Check to see if a given BlockState location is set to true - * - * @param blockState BlockState to check - * @return true if the given BlockState location is set to true, false if otherwise - */ - boolean isTrue(@Nullable BlockState blockState); - - /** - * Set a given location to true, should create stores as necessary if the location does not exist - * - * @param x X coordinate to set - * @param y Y coordinate to set - * @param z Z coordinate to set - * @param world World to set in - */ - void setTrue(int x, int y, int z, @Nullable World world); - - /** - * Set a given block location to true, should create stores as necessary if the location does not exist - * - * @param block Block location to set - */ - void setTrue(@Nullable Block block); - - /** - * Set a given BlockState location to true, should create stores as necessary if the location does not exist - * - * @param blockState BlockState location to set - */ - void setTrue(@Nullable BlockState blockState); - - /** - * Set a given location to false, should not create stores if one does not exist for the given location - * - * @param x X coordinate to set - * @param y Y coordinate to set - * @param z Z coordinate to set - * @param world World to set in - */ - void setFalse(int x, int y, int z, @Nullable World world); - - /** - * Set a given block location to false, should not create stores if one does not exist for the given location - * - * @param block Block location to set - */ - void setFalse(@Nullable Block block); - - /** - * Set a given BlockState location to false, should not create stores if one does not exist for the given location - * - * @param blockState BlockState location to set - */ - void setFalse(@Nullable BlockState blockState); - - /** - * Delete any ChunkletStores that are empty - */ - void cleanUp(); + void chunkUnloaded(int cx, int cz, @NotNull World world); + void unloadWorld(@NotNull World world); } diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkStore.java b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkStore.java index df01b93ee..e21006628 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkStore.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/ChunkStore.java @@ -1,6 +1,5 @@ package com.gmail.nossr50.util.blockmeta; -import org.bukkit.World; import org.jetbrains.annotations.NotNull; import java.util.UUID; diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkManager.java index f3023e0e3..f6d88665f 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/HashChunkManager.java @@ -1,6 +1,5 @@ package com.gmail.nossr50.util.blockmeta; -import com.gmail.nossr50.mcMMO; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.block.Block; @@ -8,13 +7,16 @@ import org.bukkit.block.BlockState; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.*; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; import java.util.*; public class HashChunkManager implements ChunkManager { - private final @NotNull HashMap regionMap = new HashMap<>(); // Tracks active regions - private final @NotNull HashMap> chunkUsageMap = new HashMap<>(); // Tracks active chunks by region - private final @NotNull HashMap chunkMap = new HashMap<>(); // Tracks active chunks + private final HashMap regionMap = new HashMap<>(); // Tracks active regions + private final HashMap> chunkUsageMap = new HashMap<>(); // Tracks active chunks by region + private final HashMap chunkMap = new HashMap<>(); // Tracks active chunks @Override public synchronized void closeAll() { @@ -23,7 +25,10 @@ public class HashChunkManager implements ChunkManager { { if (!chunkStore.isDirty()) continue; - writeChunkStore(Bukkit.getWorld(chunkStore.getWorldId()), chunkStore); + World world = Bukkit.getWorld(chunkStore.getWorldId()); + if (world == null) + continue; // Oh well + writeChunkStore(world, chunkStore); } // Clear in memory chunks chunkMap.clear(); @@ -104,56 +109,12 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void saveChunk(int cx, int cz, @Nullable World world) { - if (world == null) - return; - - CoordinateKey chunkKey = toChunkKey(world.getUID(), cx, cz); - - ChunkStore out = chunkMap.get(chunkKey); - - if (out == null) - return; - - if (!out.isDirty()) - return; - - writeChunkStore(world, out); - } - - @Override - public synchronized void chunkUnloaded(int cx, int cz, @Nullable World world) { - if (world == null) - return; - + public synchronized void chunkUnloaded(int cx, int cz, @NotNull World world) { unloadChunk(cx, cz, world); } @Override - public synchronized void saveWorld(@Nullable World world) { - if (world == null) - return; - - UUID wID = world.getUID(); - - // Save all teh chunks - for (ChunkStore chunkStore : chunkMap.values()) { - if (!chunkStore.isDirty()) - continue; - if (!wID.equals(chunkStore.getWorldId())) - continue; - try { - writeChunkStore(world, chunkStore); - } - catch (Exception ignore) { } - } - } - - @Override - public synchronized void unloadWorld(@Nullable World world) { - if (world == null) - return; - + public synchronized void unloadWorld(@NotNull World world) { UUID wID = world.getUID(); // Save and remove all the chunks @@ -179,18 +140,7 @@ public class HashChunkManager implements ChunkManager { } } - @Override - public synchronized void saveAll() { - for (World world : mcMMO.p.getServer().getWorlds()) { - saveWorld(world); - } - } - - @Override - public synchronized boolean isTrue(int x, int y, int z, @Nullable World world) { - if (world == null) - return false; - + private synchronized boolean isTrue(int x, int y, int z, @NotNull World world) { CoordinateKey chunkKey = blockCoordinateToChunkKey(world.getUID(), x, y, z); // Get chunk, load from file if necessary @@ -216,67 +166,36 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized boolean isTrue(@Nullable Block block) { - if (block == null) - return false; - + public synchronized boolean isTrue(@NotNull Block block) { return isTrue(block.getX(), block.getY(), block.getZ(), block.getWorld()); } @Override - public synchronized boolean isTrue(@Nullable BlockState blockState) { - if (blockState == null) - return false; - + public synchronized boolean isTrue(@NotNull BlockState blockState) { return isTrue(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld()); } @Override - public synchronized void setTrue(int x, int y, int z, @Nullable World world) { - set(x, y, z, world, true); + public synchronized void setTrue(@NotNull Block block) { + set(block.getX(), block.getY(), block.getZ(), block.getWorld(), true); } @Override - public synchronized void setTrue(@Nullable Block block) { - if (block == null) - return; - - setTrue(block.getX(), block.getY(), block.getZ(), block.getWorld()); + public synchronized void setTrue(@NotNull BlockState blockState) { + set(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld(), true); } @Override - public synchronized void setTrue(@Nullable BlockState blockState) { - if (blockState == null) - return; - - setTrue(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld()); + public synchronized void setFalse(@NotNull Block block) { + set(block.getX(), block.getY(), block.getZ(), block.getWorld(), false); } @Override - public synchronized void setFalse(int x, int y, int z, @Nullable World world) { - set(x, y, z, world, false); + public synchronized void setFalse(@NotNull BlockState blockState) { + set(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld(), false); } - @Override - public synchronized void setFalse(@Nullable Block block) { - if (block == null) - return; - - setFalse(block.getX(), block.getY(), block.getZ(), block.getWorld()); - } - - @Override - public synchronized void setFalse(@Nullable BlockState blockState) { - if (blockState == null) - return; - - setFalse(blockState.getX(), blockState.getY(), blockState.getZ(), blockState.getWorld()); - } - - public synchronized void set(int x, int y, int z, @Nullable World world, boolean value){ - if (world == null) - return; - + private synchronized void set(int x, int y, int z, @NotNull World world, boolean value){ CoordinateKey chunkKey = blockCoordinateToChunkKey(world.getUID(), x, y, z); // Get/Load/Create chunkstore @@ -350,7 +269,4 @@ public class HashChunkManager implements ChunkManager { return Objects.hash(worldID, x, z); } } - - @Override - public synchronized void cleanUp() {} } diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/NullChunkManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/NullChunkManager.java index b777fa349..203376780 100644 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/NullChunkManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/NullChunkManager.java @@ -3,6 +3,7 @@ package com.gmail.nossr50.util.blockmeta; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockState; +import org.jetbrains.annotations.NotNull; public class NullChunkManager implements ChunkManager { @@ -10,53 +11,30 @@ public class NullChunkManager implements ChunkManager { public void closeAll() {} @Override - public void saveChunk(int cx, int cz, World world) {} + public void chunkUnloaded(int cx, int cz, @NotNull World world) {} @Override - public void chunkUnloaded(int cx, int cz, World world) {} + public void unloadWorld(@NotNull World world) {} @Override - public void saveWorld(World world) {} - - @Override - public void unloadWorld(World world) {} - - @Override - public void saveAll() {} - - @Override - public boolean isTrue(int x, int y, int z, World world) { + public boolean isTrue(@NotNull Block block) { return false; } @Override - public boolean isTrue(Block block) { + public boolean isTrue(@NotNull BlockState blockState) { return false; } @Override - public boolean isTrue(BlockState blockState) { - return false; - } + public void setTrue(@NotNull Block block) {} @Override - public void setTrue(int x, int y, int z, World world) {} + public void setTrue(@NotNull BlockState blockState) {} @Override - public void setTrue(Block block) {} + public void setFalse(@NotNull Block block) {} @Override - public void setTrue(BlockState blockState) {} - - @Override - public void setFalse(int x, int y, int z, World world) {} - - @Override - public void setFalse(Block block) {} - - @Override - public void setFalse(BlockState blockState) {} - - @Override - public void cleanUp() {} + public void setFalse(@NotNull BlockState blockState) {} } diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/UserBlockTracker.java b/src/main/java/com/gmail/nossr50/util/blockmeta/UserBlockTracker.java new file mode 100644 index 000000000..e5428b0c1 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/UserBlockTracker.java @@ -0,0 +1,56 @@ +package com.gmail.nossr50.util.blockmeta; + +import com.gmail.nossr50.mcMMO; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.jetbrains.annotations.NotNull; + +/** + * Contains blockstore methods that are safe for external plugins to access. + * An instance can be retrieved via {@link mcMMO#getPlaceStore() mcMMO.getPlaceStore()} + */ +public interface UserBlockTracker { + /** + * Check to see if a given block location is set to true + * + * @param block Block location to check + * @return true if the given block location is set to true, false if otherwise + */ + boolean isTrue(@NotNull Block block); + + /** + * Check to see if a given BlockState location is set to true + * + * @param blockState BlockState to check + * @return true if the given BlockState location is set to true, false if otherwise + */ + boolean isTrue(@NotNull BlockState blockState); + + /** + * Set a given block location to true + * + * @param block Block location to set + */ + void setTrue(@NotNull Block block); + + /** + * Set a given BlockState location to true + * + * @param blockState BlockState location to set + */ + void setTrue(@NotNull BlockState blockState); + + /** + * Set a given block location to false + * + * @param block Block location to set + */ + void setFalse(@NotNull Block block); + + /** + * Set a given BlockState location to false + * + * @param blockState BlockState location to set + */ + void setFalse(@NotNull BlockState blockState); +} diff --git a/src/test/java/ChunkStoreTest.java b/src/test/java/ChunkStoreTest.java index 43f105bad..8a090dfaf 100644 --- a/src/test/java/ChunkStoreTest.java +++ b/src/test/java/ChunkStoreTest.java @@ -2,6 +2,7 @@ import com.gmail.nossr50.util.blockmeta.*; import com.google.common.io.Files; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.block.Block; import org.jetbrains.annotations.NotNull; import org.junit.*; import org.junit.runner.RunWith; @@ -141,9 +142,20 @@ public class ChunkStoreTest { @Test public void testRegressionChunkMirrorBug() { ChunkManager chunkManager = new HashChunkManager(); - chunkManager.setTrue(15,0,15, mockWorld); - chunkManager.setFalse(-15, 0, -15, mockWorld); - Assert.assertTrue(chunkManager.isTrue(15, 0, 15, mockWorld)); + Block mockBlockA = mock(Block.class); + Mockito.when(mockBlockA.getX()).thenReturn(15); + Mockito.when(mockBlockA.getZ()).thenReturn(15); + Mockito.when(mockBlockA.getY()).thenReturn(0); + Mockito.when(mockBlockA.getWorld()).thenReturn(mockWorld); + Block mockBlockB = mock(Block.class); + Mockito.when(mockBlockB.getX()).thenReturn(-15); + Mockito.when(mockBlockB.getZ()).thenReturn(-15); + Mockito.when(mockBlockB.getY()).thenReturn(0); + Mockito.when(mockBlockB.getWorld()).thenReturn(mockWorld); + + chunkManager.setTrue(mockBlockA); + chunkManager.setFalse(mockBlockB); + Assert.assertTrue(chunkManager.isTrue(mockBlockA)); } private interface Delegate {