Cleanup and catch some NPE

This commit is contained in:
themode 2020-10-31 04:38:53 +01:00
parent 00ce2be258
commit 2e4a2f57cf
4 changed files with 34 additions and 22 deletions

View File

@ -676,6 +676,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
blockActionPacket.blockId = block.getBlockId();
final Chunk chunk = getChunkAt(blockPosition);
Check.notNull(chunk, "The chunk at " + blockPosition + " is not loaded!");
chunk.sendPacketToViewers(blockActionPacket);
}

View File

@ -107,8 +107,9 @@ public class InstanceContainer extends Instance {
}
@Override
public void setCustomBlock(int x, int y, int z, short customBlockId, Data data) {
public void setCustomBlock(int x, int y, int z, short customBlockId, @Nullable Data data) {
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
Check.notNull(customBlock, "The custom block with the id " + customBlockId + " does not exist.");
setBlock(x, y, z, customBlock.getDefaultBlockStateId(), customBlock, data);
}
@ -136,13 +137,11 @@ public class InstanceContainer extends Instance {
if (ChunkUtils.isLoaded(chunk)) {
UNSAFE_setBlock(chunk, x, y, z, blockStateId, customBlock, data);
} else {
if (hasEnabledAutoChunkLoad()) {
final int chunkX = ChunkUtils.getChunkCoordinate(x);
final int chunkZ = ChunkUtils.getChunkCoordinate(z);
loadChunk(chunkX, chunkZ, c -> UNSAFE_setBlock(c, x, y, z, blockStateId, customBlock, data));
} else {
throw new IllegalStateException("Tried to set a block to an unloaded chunk with auto chunk load disabled");
}
Check.stateCondition(!hasEnabledAutoChunkLoad(),
"Tried to set a block to an unloaded chunk with auto chunk load disabled");
final int chunkX = ChunkUtils.getChunkCoordinate(x);
final int chunkZ = ChunkUtils.getChunkCoordinate(z);
loadChunk(chunkX, chunkZ, c -> UNSAFE_setBlock(c, x, y, z, blockStateId, customBlock, data));
}
}
@ -159,7 +158,8 @@ public class InstanceContainer extends Instance {
* @param customBlock the {@link CustomBlock}, null if none
* @param data the {@link Data}, null if none
*/
private void UNSAFE_setBlock(Chunk chunk, int x, int y, int z, short blockStateId, CustomBlock customBlock, Data data) {
private void UNSAFE_setBlock(@NotNull Chunk chunk, int x, int y, int z, short blockStateId,
@Nullable CustomBlock customBlock, @Nullable Data data) {
if (chunk.isReadOnly()) {
return;
}
@ -216,7 +216,7 @@ public class InstanceContainer extends Instance {
}
}
private void setAlreadyChanged(BlockPosition blockPosition, short blockStateId) {
private void setAlreadyChanged(@NotNull BlockPosition blockPosition, short blockStateId) {
currentlyChangingBlocks.put(blockPosition, Block.fromStateId(blockStateId));
}
@ -228,7 +228,7 @@ public class InstanceContainer extends Instance {
* @param blockStateId the block state id
* @return true if the block changed since the last update
*/
private boolean isAlreadyChanged(BlockPosition blockPosition, short blockStateId) {
private boolean isAlreadyChanged(@NotNull BlockPosition blockPosition, short blockStateId) {
final Block changedBlock = currentlyChangingBlocks.get(blockPosition);
if (changedBlock == null)
return false;
@ -257,7 +257,7 @@ public class InstanceContainer extends Instance {
* @param previousBlock the block which has been destroyed
* @param blockPosition the block position
*/
private void callBlockDestroy(Chunk chunk, int index, CustomBlock previousBlock, BlockPosition blockPosition) {
private void callBlockDestroy(@NotNull Chunk chunk, int index, @NotNull CustomBlock previousBlock, @NotNull BlockPosition blockPosition) {
final Data previousData = chunk.getBlockData(index);
previousBlock.onDestroy(this, blockPosition, previousData);
}
@ -271,7 +271,7 @@ public class InstanceContainer extends Instance {
* @param index the block index
* @param blockPosition the block position
*/
private void callBlockPlace(Chunk chunk, int index, BlockPosition blockPosition) {
private void callBlockPlace(@NotNull Chunk chunk, int index, @NotNull BlockPosition blockPosition) {
final CustomBlock actualBlock = chunk.getCustomBlock(index);
if (actualBlock == null)
return;
@ -286,7 +286,7 @@ public class InstanceContainer extends Instance {
* @param blockPosition the block position
* @return the modified block state id
*/
private short executeBlockPlacementRule(short blockStateId, BlockPosition blockPosition) {
private short executeBlockPlacementRule(short blockStateId, @NotNull BlockPosition blockPosition) {
final BlockPlacementRule blockPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(blockStateId);
if (blockPlacementRule != null) {
return blockPlacementRule.blockRefresh(this, blockPosition, blockStateId);

View File

@ -2,6 +2,8 @@ package net.minestom.server.instance.block;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
@ -23,7 +25,7 @@ public class BlockManager {
* @throws IllegalArgumentException if <code>customBlock</code> block id is not greater than 0
* @throws IllegalStateException if the id of <code>customBlock</code> is already registered
*/
public void registerCustomBlock(CustomBlock customBlock) {
public void registerCustomBlock(@NotNull CustomBlock customBlock) {
final short id = customBlock.getCustomBlockId();
Check.argCondition(id <= 0, "Custom block ID must be greater than 0, got: " + id);
Check.stateCondition(customBlocksInternalId[id] != null, "a CustomBlock with the id " + id + " already exists");
@ -39,7 +41,7 @@ public class BlockManager {
* @param blockPlacementRule the block placement rule to register
* @throws IllegalArgumentException if <code>blockPlacementRule</code> block id is negative
*/
public void registerBlockPlacementRule(BlockPlacementRule blockPlacementRule) {
public void registerBlockPlacementRule(@NotNull BlockPlacementRule blockPlacementRule) {
final short id = blockPlacementRule.getBlockId();
Check.argCondition(id < 0, "Block ID must be >= 0, got: " + id);
@ -52,7 +54,8 @@ public class BlockManager {
* @param block the block to check
* @return the block placement rule associated with the block, null if not any
*/
public BlockPlacementRule getBlockPlacementRule(Block block) {
@Nullable
public BlockPlacementRule getBlockPlacementRule(@NotNull Block block) {
return this.placementRules[block.getBlockId()];
}
@ -62,6 +65,7 @@ public class BlockManager {
* @param blockStateId the block id to check
* @return the block placement rule associated with the id, null if not any
*/
@Nullable
public BlockPlacementRule getBlockPlacementRule(short blockStateId) {
final Block block = Block.fromStateId(blockStateId); // Convert block alternative
return getBlockPlacementRule(block);
@ -73,7 +77,8 @@ public class BlockManager {
* @param identifier the custom block identifier
* @return the {@link CustomBlock} associated with the identifier, null if not any
*/
public CustomBlock getCustomBlock(String identifier) {
@Nullable
public CustomBlock getCustomBlock(@NotNull String identifier) {
return customBlocksId.get(identifier);
}
@ -83,6 +88,7 @@ public class BlockManager {
* @param id the custom block id
* @return the {@link CustomBlock} associated with the id, null if not any
*/
@Nullable
public CustomBlock getCustomBlock(short id) {
return customBlocksInternalId[id];
}

View File

@ -7,6 +7,8 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.Position;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class ChunkUtils {
@ -20,7 +22,7 @@ public final class ChunkUtils {
* @param chunk the chunk to check
* @return true if the chunk is loaded, false otherwise
*/
public static boolean isLoaded(Chunk chunk) {
public static boolean isLoaded(@Nullable Chunk chunk) {
return chunk != null && chunk.isLoaded();
}
@ -32,7 +34,7 @@ public final class ChunkUtils {
* @param z instance Z coordinate
* @return true if the chunk is loaded, false otherwise
*/
public static boolean isLoaded(Instance instance, float x, float z) {
public static boolean isLoaded(@NotNull Instance instance, float x, float z) {
final int chunkX = getChunkCoordinate((int) x);
final int chunkZ = getChunkCoordinate((int) z);
@ -99,7 +101,8 @@ public final class ChunkUtils {
* @param range how far should it retrieves chunk
* @return an array containing chunks index
*/
public static long[] getChunksInRange(final Position position, int range) {
@NotNull
public static long[] getChunksInRange(@NotNull Position position, int range) {
range = range * 2;
long[] visibleChunks = new long[MathUtils.square(range + 1)];
final int startLoop = -(range / 2);
@ -123,7 +126,8 @@ public final class ChunkUtils {
* @param chunkZ the chunk Z
* @return an array containing all the loaded neighbours chunk index
*/
public static long[] getNeighbours(Instance instance, int chunkX, int chunkZ) {
@NotNull
public static long[] getNeighbours(@NotNull Instance instance, int chunkX, int chunkZ) {
LongList chunks = new LongArrayList();
// Constants used to loop through the neighbors
final int[] posX = {1, 0, -1};
@ -176,6 +180,7 @@ public final class ChunkUtils {
* @param chunkZ the chunk Z
* @return the instance position of the block located in {@code index}
*/
@NotNull
public static BlockPosition getBlockPosition(int index, int chunkX, int chunkZ) {
final int x = blockIndexToPositionX(index, chunkX);
final int y = blockIndexToPositionY(index);