From d08c9391b045b786700a38d6a0385ec95621e85b Mon Sep 17 00:00:00 2001 From: nossr50 Date: Sat, 2 Jan 2021 13:36:59 -0800 Subject: [PATCH] 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; }