diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index eed4447e6..035c423b2 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -102,8 +102,6 @@ public class MinecraftServer { //Rate Limiting private static int rateLimit = 0; - // Networking - private static PacketProcessor packetProcessor; private static PacketListenerManager packetListenerManager; private static NettyServer nettyServer; @@ -161,7 +159,8 @@ public class MinecraftServer { Fluid.values(); connectionManager = new ConnectionManager(); - packetProcessor = new PacketProcessor(); + // Networking + final PacketProcessor packetProcessor = new PacketProcessor(); packetListenerManager = new PacketListenerManager(); instanceManager = new InstanceManager(); diff --git a/src/main/java/net/minestom/server/UpdateManager.java b/src/main/java/net/minestom/server/UpdateManager.java index 728ad460e..1af893cce 100644 --- a/src/main/java/net/minestom/server/UpdateManager.java +++ b/src/main/java/net/minestom/server/UpdateManager.java @@ -13,6 +13,7 @@ import net.minestom.server.utils.thread.MinestomThread; import net.minestom.server.utils.validate.Check; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.function.Consumer; @@ -61,7 +62,7 @@ public final class UpdateManager { //Tick Callbacks tickStartCallbacks.forEach(Runnable::run); - ArrayList> futures; + List> futures; // Server tick (instance/chunk/entity) // Synchronize with the update manager instance, like the signal for chunk load/unload diff --git a/src/main/java/net/minestom/server/advancements/AdvancementManager.java b/src/main/java/net/minestom/server/advancements/AdvancementManager.java index 70bc1dea4..2aae5b1ca 100644 --- a/src/main/java/net/minestom/server/advancements/AdvancementManager.java +++ b/src/main/java/net/minestom/server/advancements/AdvancementManager.java @@ -11,7 +11,7 @@ import java.util.Map; */ public class AdvancementManager { - private Map advancementTabMap = new HashMap<>(); + private final Map advancementTabMap = new HashMap<>(); /** * Create a new tab with a single advancement diff --git a/src/main/java/net/minestom/server/advancements/AdvancementTab.java b/src/main/java/net/minestom/server/advancements/AdvancementTab.java index e822585f1..48120492d 100644 --- a/src/main/java/net/minestom/server/advancements/AdvancementTab.java +++ b/src/main/java/net/minestom/server/advancements/AdvancementTab.java @@ -16,14 +16,14 @@ import java.util.*; */ public class AdvancementTab implements Viewable { - private static Map> playerTabMap = new HashMap<>(); + private static final Map> PLAYER_TAB_MAP = new HashMap<>(); - private Set viewers = new HashSet<>(); + private final Set viewers = new HashSet<>(); - private AdvancementRoot root; + private final AdvancementRoot root; // Advancement -> its parent - private Map advancementMap = new HashMap<>(); + private final Map advancementMap = new HashMap<>(); // Packet cache, updated every time the tab changes protected ByteBuf createBuffer; @@ -47,7 +47,7 @@ public class AdvancementTab implements Viewable { * @return all the advancement tabs that the player sees */ public static Set getTabs(Player player) { - return playerTabMap.getOrDefault(player, null); + return PLAYER_TAB_MAP.getOrDefault(player, null); } /** @@ -171,7 +171,7 @@ public class AdvancementTab implements Viewable { * @param player the player */ private void addPlayer(Player player) { - Set tabs = playerTabMap.computeIfAbsent(player, p -> new HashSet<>()); + Set tabs = PLAYER_TAB_MAP.computeIfAbsent(player, p -> new HashSet<>()); tabs.add(this); } @@ -181,13 +181,13 @@ public class AdvancementTab implements Viewable { * @param player the player */ private void removePlayer(Player player) { - if (!playerTabMap.containsKey(player)) { + if (!PLAYER_TAB_MAP.containsKey(player)) { return; } - Set tabs = playerTabMap.get(player); + Set tabs = PLAYER_TAB_MAP.get(player); tabs.remove(this); if (tabs.isEmpty()) { - playerTabMap.remove(player); + PLAYER_TAB_MAP.remove(player); } } diff --git a/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java b/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java index a032ed959..e606bc7ca 100644 --- a/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java +++ b/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java @@ -42,7 +42,6 @@ public class BenchmarkManager { private boolean enabled = false; private volatile boolean stop = false; - private Thread thread; private long time; @@ -51,7 +50,7 @@ public class BenchmarkManager { time = updateOption.getTimeUnit().toMilliseconds(updateOption.getValue()); - this.thread = new Thread(null, () -> { + final Thread thread = new Thread(null, () -> { while (!stop) { refreshData(); @@ -67,7 +66,7 @@ public class BenchmarkManager { }, MinecraftServer.THREAD_NAME_BENCHMARK, 0L); - this.thread.start(); + thread.start(); this.enabled = true; } diff --git a/src/main/java/net/minestom/server/chat/ColoredText.java b/src/main/java/net/minestom/server/chat/ColoredText.java index 3010ceadd..891cb09b0 100644 --- a/src/main/java/net/minestom/server/chat/ColoredText.java +++ b/src/main/java/net/minestom/server/chat/ColoredText.java @@ -61,7 +61,7 @@ public class ColoredText { } private static String toLegacy(String message, char colorChar) { - String result = ""; + StringBuilder result = new StringBuilder(); for (int i = 0; i < message.length(); i++) { final char c = message.charAt(i); @@ -71,19 +71,19 @@ public class ColoredText { final char nextChar = message.charAt(i + 1); final ChatColor color = ChatColor.fromLegacyColorCodes(nextChar); if (color != ChatColor.NO_COLOR) { - String replacement = color.toString(); - result += replacement; + final String replacement = color.toString(); + result.append(replacement); i++; // Increment to ignore the color code } else { - result += c; + result.append(c); } } } else { - result += c; + result.append(c); } } - return result; + return result.toString(); } public ColoredText appendLegacy(String message, char colorChar) { diff --git a/src/main/java/net/minestom/server/chat/TranslatableText.java b/src/main/java/net/minestom/server/chat/TranslatableText.java index 916ea49de..b93918ce9 100644 --- a/src/main/java/net/minestom/server/chat/TranslatableText.java +++ b/src/main/java/net/minestom/server/chat/TranslatableText.java @@ -39,11 +39,11 @@ public class TranslatableText { final String prefix = "{@"; final String suffix = "}"; - String content = code; + StringBuilder content = new StringBuilder(code); if (arguments != null && arguments.length > 0) { for (String arg : arguments) { - content += "," + arg; + content.append("," + arg); } } diff --git a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java index 47d6a2f2f..2177a14c0 100644 --- a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java +++ b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java @@ -104,37 +104,41 @@ public class CommandDispatcher { // true if the arg is valid, false otherwise boolean correct = false; // the raw string representing the correct argument syntax - String argValue = ""; + StringBuilder argValue = new StringBuilder(); if (useRemaining) { for (int i = argIndex; i < args.length; i++) { final String arg = args[i]; if (argValue.length() > 0) - argValue += " "; - argValue += arg; + argValue.append(" "); + argValue.append(arg); } - correctionResult = argument.getCorrectionResult(argValue); + final String argValueString = argValue.toString(); + + correctionResult = argument.getCorrectionResult(argValueString); if (correctionResult == Argument.SUCCESS) { correct = true; - argsValues[argIndex] = argValue; + argsValues[argIndex] = argValueString; } } else { for (int i = argIndex; i < args.length; i++) { final String arg = args[i]; - argValue += arg; + argValue.append(arg); - correctionResult = argument.getCorrectionResult(argValue); + final String argValueString = argValue.toString(); + + correctionResult = argument.getCorrectionResult(argValueString); if (correctionResult == Argument.SUCCESS) { correct = true; - argsValues[argIndex] = argValue; + argsValues[argIndex] = argValueString; argIndex = i + 1; break; } else { if (!argument.allowSpace()) break; - argValue += " "; + argValue.append(" "); } } } @@ -145,7 +149,7 @@ public class CommandDispatcher { syntaxCorrect = false; CommandSuggestionHolder suggestionHolder = new CommandSuggestionHolder(); suggestionHolder.syntax = syntax; - suggestionHolder.argValue = argValue; + suggestionHolder.argValue = argValue.toString(); suggestionHolder.correctionResult = correctionResult; suggestionHolder.argIndex = argCount; syntaxesSuggestions.put(argCount, suggestionHolder); @@ -222,7 +226,7 @@ public class CommandDispatcher { return result; } - private class CommandSuggestionHolder { + private static class CommandSuggestionHolder { private CommandSyntax syntax; private String argValue; private int correctionResult; @@ -230,7 +234,7 @@ public class CommandDispatcher { } - private class CommandResult { + private static class CommandResult { // Command private Command command; diff --git a/src/main/java/net/minestom/server/data/Data.java b/src/main/java/net/minestom/server/data/Data.java index 47c293302..86b15a406 100644 --- a/src/main/java/net/minestom/server/data/Data.java +++ b/src/main/java/net/minestom/server/data/Data.java @@ -27,7 +27,7 @@ public class Data { } }; - protected ConcurrentHashMap data = new ConcurrentHashMap(); + protected final ConcurrentHashMap data = new ConcurrentHashMap<>(); /** * Set a value to a specific key diff --git a/src/main/java/net/minestom/server/entity/EntityManager.java b/src/main/java/net/minestom/server/entity/EntityManager.java index 42f7b64dd..5e9e22b7a 100644 --- a/src/main/java/net/minestom/server/entity/EntityManager.java +++ b/src/main/java/net/minestom/server/entity/EntityManager.java @@ -28,16 +28,15 @@ public final class EntityManager { private void waitingPlayersTick() { Player waitingPlayer; while ((waitingPlayer = waitingPlayers.poll()) != null) { - final Player playerCache = waitingPlayer; - playerCache.init(); + waitingPlayer.init(); - PlayerLoginEvent loginEvent = new PlayerLoginEvent(playerCache); - playerCache.callEvent(PlayerLoginEvent.class, loginEvent); + PlayerLoginEvent loginEvent = new PlayerLoginEvent(waitingPlayer); + waitingPlayer.callEvent(PlayerLoginEvent.class, loginEvent); final Instance spawningInstance = loginEvent.getSpawningInstance(); Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent"); - playerCache.setInstance(spawningInstance); + waitingPlayer.setInstance(spawningInstance); } } diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 0014b7ddb..aa62b1608 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -35,7 +35,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { // Bounding box used for items' pickup (see LivingEntity#setBoundingBox) protected BoundingBox expandedBoundingBox; - private float[] attributeValues = new float[Attribute.values().length]; + private final float[] attributeValues = new float[Attribute.values().length]; private boolean isHandActive; private boolean offHand; diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 19e5f780a..ec16baeb9 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1858,8 +1858,8 @@ public class Player extends LivingEntity implements CommandSender { if (isFood && !allowFood) return null; - ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(updatedItem, - isOffhand ? Hand.OFF : Hand.MAIN); + final Hand hand = isOffhand ? Hand.OFF : Hand.MAIN; + ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(this, hand, updatedItem); callEvent(ItemUpdateStateEvent.class, itemUpdateStateEvent); return itemUpdateStateEvent; diff --git a/src/main/java/net/minestom/server/entity/hologram/Hologram.java b/src/main/java/net/minestom/server/entity/hologram/Hologram.java index 14da946b1..9c2da8faa 100644 --- a/src/main/java/net/minestom/server/entity/hologram/Hologram.java +++ b/src/main/java/net/minestom/server/entity/hologram/Hologram.java @@ -17,7 +17,7 @@ public class Hologram implements Viewable { private static final float OFFSET_Y = -0.9875f; - private HologramEntity entity; + private final HologramEntity entity; private Position position; private ColoredText text; @@ -124,7 +124,7 @@ public class Hologram implements Viewable { } - private class HologramEntity extends EntityArmorStand { + private static class HologramEntity extends EntityArmorStand { public HologramEntity(Position spawnPosition) { super(spawnPosition); diff --git a/src/main/java/net/minestom/server/entity/pathfinding/PFBlockDescription.java b/src/main/java/net/minestom/server/entity/pathfinding/PFBlockDescription.java index d0d08b5da..b2dc2518f 100644 --- a/src/main/java/net/minestom/server/entity/pathfinding/PFBlockDescription.java +++ b/src/main/java/net/minestom/server/entity/pathfinding/PFBlockDescription.java @@ -7,7 +7,7 @@ import net.minestom.server.instance.block.Block; public class PFBlockDescription implements IBlockDescription { - private static Short2ObjectMap blockDescriptionMap = new Short2ObjectOpenHashMap<>(); + private static final Short2ObjectMap BLOCK_DESCRITION_MAP = new Short2ObjectOpenHashMap<>(); /** * Get the {@link PFBlockDescription} linked to the block state id @@ -18,19 +18,19 @@ public class PFBlockDescription implements IBlockDescription { * @return the {@link PFBlockDescription} linked to {@code blockStateId} */ public static PFBlockDescription getBlockDescription(short blockStateId) { - if (!blockDescriptionMap.containsKey(blockStateId)) { - synchronized (blockDescriptionMap) { + if (!BLOCK_DESCRITION_MAP.containsKey(blockStateId)) { + synchronized (BLOCK_DESCRITION_MAP) { final Block block = Block.fromStateId(blockStateId); final PFBlockDescription blockDescription = new PFBlockDescription(block); - blockDescriptionMap.put(blockStateId, blockDescription); + BLOCK_DESCRITION_MAP.put(blockStateId, blockDescription); return blockDescription; } } - return blockDescriptionMap.get(blockStateId); + return BLOCK_DESCRITION_MAP.get(blockStateId); } - private Block block; + private final Block block; public PFBlockDescription(Block block) { this.block = block; diff --git a/src/main/java/net/minestom/server/entity/pathfinding/PFBlockObject.java b/src/main/java/net/minestom/server/entity/pathfinding/PFBlockObject.java index d2bdd1fb0..c8309c426 100644 --- a/src/main/java/net/minestom/server/entity/pathfinding/PFBlockObject.java +++ b/src/main/java/net/minestom/server/entity/pathfinding/PFBlockObject.java @@ -8,7 +8,7 @@ import net.minestom.server.instance.block.Block; public class PFBlockObject implements IBlockObject { - private static Short2ObjectMap blockObjectsMap = new Short2ObjectOpenHashMap<>(); + private static final Short2ObjectMap BLOCK_OBJECT_MAP = new Short2ObjectOpenHashMap<>(); /** * Get the {@link PFBlockObject} linked to the block state id @@ -19,19 +19,19 @@ public class PFBlockObject implements IBlockObject { * @return the {@link PFBlockObject} linked to {@code blockStateId} */ public static PFBlockObject getBlockObject(short blockStateId) { - if (!blockObjectsMap.containsKey(blockStateId)) { - synchronized (blockObjectsMap) { + if (!BLOCK_OBJECT_MAP.containsKey(blockStateId)) { + synchronized (BLOCK_OBJECT_MAP) { final Block block = Block.fromStateId(blockStateId); final PFBlockObject blockObject = new PFBlockObject(block); - blockObjectsMap.put(blockStateId, blockObject); + BLOCK_OBJECT_MAP.put(blockStateId, blockObject); return blockObject; } } - return blockObjectsMap.get(blockStateId); + return BLOCK_OBJECT_MAP.get(blockStateId); } - private Block block; + private final Block block; public PFBlockObject(Block block) { this.block = block; diff --git a/src/main/java/net/minestom/server/entity/pathfinding/PFInstanceSpace.java b/src/main/java/net/minestom/server/entity/pathfinding/PFInstanceSpace.java index ea655ca7d..70869ca20 100644 --- a/src/main/java/net/minestom/server/entity/pathfinding/PFInstanceSpace.java +++ b/src/main/java/net/minestom/server/entity/pathfinding/PFInstanceSpace.java @@ -31,14 +31,11 @@ public class PFInstanceSpace implements IInstanceSpace { return null; } - final PFColumnarSpace columnarSpace = - chunkSpaceMap.computeIfAbsent(chunk, c -> { - final PFColumnarSpace cs = new PFColumnarSpace(this, c); - c.setColumnarSpace(cs); - return cs; - }); - - return columnarSpace; + return chunkSpaceMap.computeIfAbsent(chunk, c -> { + final PFColumnarSpace cs = new PFColumnarSpace(this, c); + c.setColumnarSpace(cs); + return cs; + }); } public Instance getInstance() { diff --git a/src/main/java/net/minestom/server/event/handler/EventHandler.java b/src/main/java/net/minestom/server/event/handler/EventHandler.java index 260683d3c..a75833f89 100644 --- a/src/main/java/net/minestom/server/event/handler/EventHandler.java +++ b/src/main/java/net/minestom/server/event/handler/EventHandler.java @@ -43,7 +43,7 @@ public interface EventHandler { * @param the event type */ default void callEvent(Class eventClass, E event) { - List eventCallbacks = getEventCallbacks(eventClass); + final List eventCallbacks = getEventCallbacks(eventClass); for (EventCallback eventCallback : eventCallbacks) { eventCallback.run(event); } diff --git a/src/main/java/net/minestom/server/event/instance/InstanceChunkLoadEvent.java b/src/main/java/net/minestom/server/event/instance/InstanceChunkLoadEvent.java index 8ec4bf37a..ee821d891 100644 --- a/src/main/java/net/minestom/server/event/instance/InstanceChunkLoadEvent.java +++ b/src/main/java/net/minestom/server/event/instance/InstanceChunkLoadEvent.java @@ -8,8 +8,8 @@ import net.minestom.server.instance.Instance; */ public class InstanceChunkLoadEvent extends Event { - private Instance instance; - private int chunkX, chunkZ; + private final Instance instance; + private final int chunkX, chunkZ; public InstanceChunkLoadEvent(Instance instance, int chunkX, int chunkZ) { this.instance = instance; diff --git a/src/main/java/net/minestom/server/event/instance/InstanceChunkUnloadEvent.java b/src/main/java/net/minestom/server/event/instance/InstanceChunkUnloadEvent.java index 12fdb1f9f..3bfc0475a 100644 --- a/src/main/java/net/minestom/server/event/instance/InstanceChunkUnloadEvent.java +++ b/src/main/java/net/minestom/server/event/instance/InstanceChunkUnloadEvent.java @@ -8,8 +8,8 @@ import net.minestom.server.instance.Instance; */ public class InstanceChunkUnloadEvent extends Event { - private Instance instance; - private int chunkX, chunkZ; + private final Instance instance; + private final int chunkX, chunkZ; public InstanceChunkUnloadEvent(Instance instance, int chunkX, int chunkZ) { this.instance = instance; diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java index b44043f13..f27813187 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java @@ -8,14 +8,15 @@ import net.minestom.server.item.ItemStack; public class InventoryClickEvent extends Event { - private Player player; - private Inventory inventory; - private int slot; - private ClickType clickType; - private ItemStack clickedItem; - private ItemStack cursorItem; + private final Player player; + private final Inventory inventory; + private final int slot; + private final ClickType clickType; + private final ItemStack clickedItem; + private final ItemStack cursorItem; public InventoryClickEvent(Player player, Inventory inventory, int slot, ClickType clickType, ItemStack clicked, ItemStack cursor) { + this.player = player; this.inventory = inventory; this.slot = slot; this.clickType = clickType; diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryCloseEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryCloseEvent.java index 088f022fa..a6601bd2b 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryCloseEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryCloseEvent.java @@ -6,8 +6,8 @@ import net.minestom.server.inventory.Inventory; public class InventoryCloseEvent extends Event { - private Player player; - private Inventory inventory; + private final Player player; + private final Inventory inventory; private Inventory newInventory; public InventoryCloseEvent(Player player, Inventory inventory) { diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java index dd7cb5839..b8c338039 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java @@ -6,7 +6,7 @@ import net.minestom.server.inventory.Inventory; public class InventoryOpenEvent extends CancellableEvent { - private Player player; + private final Player player; private Inventory inventory; public InventoryOpenEvent(Player player, Inventory inventory) { diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java index 0f0f804c9..1a848aa60 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java @@ -9,10 +9,10 @@ import net.minestom.server.utils.item.ItemStackUtils; public class InventoryPreClickEvent extends CancellableEvent { - private Player player; - private Inventory inventory; - private int slot; - private ClickType clickType; + private final Player player; + private final Inventory inventory; + private final int slot; + private final ClickType clickType; private ItemStack clickedItem; private ItemStack cursorItem; diff --git a/src/main/java/net/minestom/server/event/item/ItemUpdateStateEvent.java b/src/main/java/net/minestom/server/event/item/ItemUpdateStateEvent.java index bcfa450f7..8b0f1a364 100644 --- a/src/main/java/net/minestom/server/event/item/ItemUpdateStateEvent.java +++ b/src/main/java/net/minestom/server/event/item/ItemUpdateStateEvent.java @@ -6,23 +6,29 @@ import net.minestom.server.item.ItemStack; public class ItemUpdateStateEvent extends Event { - private ItemStack itemStack; - private Player.Hand hand; + private Player player; + private final Player.Hand hand; + private final ItemStack itemStack; private boolean handAnimation; - public ItemUpdateStateEvent(ItemStack itemStack, Player.Hand hand) { - this.itemStack = itemStack; + public ItemUpdateStateEvent(Player player, Player.Hand hand, ItemStack itemStack) { + this.player = player; this.hand = hand; + this.itemStack = itemStack; } - public ItemStack getItemStack() { - return itemStack; + public Player getPlayer() { + return player; } public Player.Hand getHand() { return hand; } + public ItemStack getItemStack() { + return itemStack; + } + public void setHandAnimation(boolean handAnimation) { this.handAnimation = handAnimation; } diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index f6ad1968b..6b58f4434 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -28,7 +28,6 @@ import net.minestom.server.world.biomes.Biome; import java.io.IOException; import java.util.*; import java.util.concurrent.CopyOnWriteArraySet; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; // TODO light data & API @@ -46,10 +45,6 @@ public abstract class Chunk implements Viewable { protected Biome[] biomes; protected int chunkX, chunkZ; - // blocks id based on coord, see Chunk#getBlockIndex - //public short[] blocksStateId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; - //protected short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; - // Used to get all blocks with data (no null) // Key is still chunk coord protected Int2ObjectMap blocksData = new Int2ObjectOpenHashMap<>(16 * 16); // Start with the size of a single row @@ -59,7 +54,7 @@ public abstract class Chunk implements Viewable { // (block index)/(last update in ms) protected Int2LongMap updatableBlocksLastUpdate = new Int2LongOpenHashMap(); - protected AtomicBoolean packetUpdated = new AtomicBoolean(false); + protected volatile boolean packetUpdated; // Block entities protected Set blockEntities = new CopyOnWriteArraySet<>(); @@ -114,8 +109,10 @@ public abstract class Chunk implements Viewable { public abstract CustomBlock getCustomBlock(int x, int y, int z); protected CustomBlock getCustomBlock(int index) { - final int[] pos = ChunkUtils.indexToChunkPosition(index); - return getCustomBlock(pos[0], pos[1], pos[2]); + final int x = ChunkUtils.blockIndexToChunkPositionX(index); + final int y = ChunkUtils.blockIndexToChunkPositionY(index); + final int z = ChunkUtils.blockIndexToChunkPositionZ(index); + return getCustomBlock(x, y, z); } protected abstract void refreshBlockValue(int x, int y, int z, short blockStateId, short customId); @@ -142,7 +139,7 @@ public abstract class Chunk implements Viewable { return; // Block all chunk operation during the update - IntIterator iterator = new IntOpenHashSet(updatableBlocks).iterator(); + final IntIterator iterator = new IntOpenHashSet(updatableBlocks).iterator(); while (iterator.hasNext()) { final int index = iterator.nextInt(); final CustomBlock customBlock = getCustomBlock(index); @@ -156,12 +153,7 @@ public abstract class Chunk implements Viewable { this.updatableBlocksLastUpdate.put(index, time); // Refresh last update time - final int[] blockPos = ChunkUtils.indexToPosition(index, chunkX, chunkZ); - final int x = blockPos[0]; - final int y = blockPos[1]; - final int z = blockPos[2]; - - final BlockPosition blockPosition = new BlockPosition(x, y, z); + final BlockPosition blockPosition = ChunkUtils.getBlockPosition(index, chunkX, chunkZ); final Data data = getData(index); customBlock.update(instance, blockPosition, data); } @@ -192,7 +184,7 @@ public abstract class Chunk implements Viewable { public void setFullDataPacket(ByteBuf fullDataPacket) { this.fullDataPacket = fullDataPacket; - this.packetUpdated.set(true); + this.packetUpdated = true; } protected boolean isBlockEntity(short blockStateId) { @@ -214,13 +206,13 @@ public abstract class Chunk implements Viewable { } /** - * Retrieve the updated data packet + * Retrieve (and save if needed) the updated data packet * * @param consumer the consumer called once the packet is sure to be up-to-date */ public void retrieveDataBuffer(Consumer consumer) { final ByteBuf data = getFullDataPacket(); - if (data == null || !packetUpdated.get()) { + if (data == null || !packetUpdated) { PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), packet -> { setFullDataPacket(packet); consumer.accept(packet); @@ -325,9 +317,7 @@ public abstract class Chunk implements Viewable { final PlayerConnection playerConnection = player.getPlayerConnection(); - retrieveDataBuffer(buf -> { - playerConnection.sendPacket(buf, true); - }); + retrieveDataBuffer(buf -> playerConnection.sendPacket(buf, true)); // TODO do not hardcode if (MinecraftServer.isFixLighting()) { @@ -372,15 +362,13 @@ public abstract class Chunk implements Viewable { public void sendChunkUpdate() { final Set chunkViewers = getViewers(); if (!chunkViewers.isEmpty()) { - retrieveDataBuffer(buf -> { - chunkViewers.forEach(player -> { - final PlayerConnection playerConnection = player.getPlayerConnection(); - if (!PlayerUtils.isNettyClient(playerConnection)) - return; + retrieveDataBuffer(buf -> chunkViewers.forEach(player -> { + final PlayerConnection playerConnection = player.getPlayerConnection(); + if (!PlayerUtils.isNettyClient(playerConnection)) + return; - playerConnection.sendPacket(buf, true); - }); - }); + playerConnection.sendPacket(buf, true); + })); } } diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 6db3aede8..d2054d73b 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -18,8 +18,9 @@ import java.util.concurrent.CopyOnWriteArraySet; public class DynamicChunk extends Chunk { - public short[] blocksStateId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; - private short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; + // blocks id based on coordinate, see Chunk#getBlockIndex + private final short[] blocksStateId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; + private final short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; public DynamicChunk(Biome[] biomes, int chunkX, int chunkZ) { super(biomes, chunkX, chunkZ); @@ -50,8 +51,7 @@ public class DynamicChunk extends Chunk { } final int index = getBlockIndex(x, y, z); - if (blockStateId != 0 - || (blockStateId == 0 && customId != 0 && updateConsumer != null)) { // Allow custom air block for update purpose, refused if no update consumer has been found + if (blockStateId != 0 || customId != 0 && updateConsumer != null) { // Allow custom air block for update purpose, refused if no update consumer has been found this.blocksStateId[index] = blockStateId; this.customBlocksId[index] = customId; } else { @@ -66,7 +66,7 @@ public class DynamicChunk extends Chunk { this.blockEntities.remove(index); - this.packetUpdated.set(false); + this.packetUpdated = false; return; } @@ -93,7 +93,7 @@ public class DynamicChunk extends Chunk { this.blockEntities.remove(index); } - this.packetUpdated.set(false); + this.packetUpdated = false; } @Override @@ -102,8 +102,7 @@ public class DynamicChunk extends Chunk { if (!MathUtils.isBetween(index, 0, blocksStateId.length)) { return 0; // TODO: custom invalid block } - final short id = blocksStateId[index]; - return id; + return blocksStateId[index]; } @Override @@ -112,8 +111,7 @@ public class DynamicChunk extends Chunk { if (!MathUtils.isBetween(index, 0, blocksStateId.length)) { return 0; // TODO: custom invalid block } - final short id = customBlocksId[index]; - return id; + return customBlocksId[index]; } @Override @@ -179,7 +177,7 @@ public class DynamicChunk extends Chunk { dos.writeShort(customBlockId); // Data - final boolean hasData = (data != null && (data instanceof SerializableData)); + final boolean hasData = data instanceof SerializableData; dos.writeBoolean(hasData); if (hasData) { final byte[] d = ((SerializableData) data).getSerializedData(); @@ -190,8 +188,7 @@ public class DynamicChunk extends Chunk { } } - final byte[] result = output.toByteArray(); - return result; + return output.toByteArray(); } @Override diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 6f015b24a..2f5f5a3c5 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -48,9 +48,9 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta private boolean registered; - private DimensionType dimensionType; + private final DimensionType dimensionType; - private WorldBorder worldBorder; + private final WorldBorder worldBorder; // Tick since the creation of the instance private long worldAge; @@ -61,24 +61,24 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta private UpdateOption timeUpdate = new UpdateOption(1, TimeUnit.TICK); private long lastTimeUpdate; - private Map, List> eventCallbacks = new ConcurrentHashMap<>(); + private final Map, List> eventCallbacks = new ConcurrentHashMap<>(); // Entities present in this instance - protected Set players = new CopyOnWriteArraySet<>(); - protected Set creatures = new CopyOnWriteArraySet<>(); - protected Set objectEntities = new CopyOnWriteArraySet<>(); - protected Set experienceOrbs = new CopyOnWriteArraySet<>(); + protected final Set players = new CopyOnWriteArraySet<>(); + protected final Set creatures = new CopyOnWriteArraySet<>(); + protected final Set objectEntities = new CopyOnWriteArraySet<>(); + protected final Set experienceOrbs = new CopyOnWriteArraySet<>(); // Entities per chunk - protected Map> chunkEntities = new ConcurrentHashMap<>(); + protected final Map> chunkEntities = new ConcurrentHashMap<>(); protected UUID uniqueId; - protected List> nextTick = Collections.synchronizedList(new ArrayList<>()); + protected final List> nextTick = Collections.synchronizedList(new ArrayList<>()); private Data data; private ExplosionSupplier explosionSupplier; // Pathfinder - private PFInstanceSpace instanceSpace = new PFInstanceSpace(this); + private final PFInstanceSpace instanceSpace = new PFInstanceSpace(this); public Instance(UUID uniqueId, DimensionType dimensionType) { this.uniqueId = uniqueId; @@ -754,13 +754,13 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta */ public void removeEntity(Entity entity) { final Instance entityInstance = entity.getInstance(); - if (entityInstance == null || entityInstance != this) + if (entityInstance != this) return; RemoveEntityFromInstanceEvent event = new RemoveEntityFromInstanceEvent(this, entity); callCancellableEvent(RemoveEntityFromInstanceEvent.class, event, () -> { // Remove this entity from players viewable list and send delete entities packet - entity.getViewers().forEach(p -> entity.removeViewer(p)); + entity.getViewers().forEach(entity::removeViewer); // Remove the entity from cache final Chunk chunk = getChunkAt(entity.getPosition()); @@ -832,8 +832,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta } private Set getEntitiesInChunk(long index) { - final Set entities = chunkEntities.computeIfAbsent(index, i -> new CopyOnWriteArraySet<>()); - return entities; + return chunkEntities.computeIfAbsent(index, i -> new CopyOnWriteArraySet<>()); } /** @@ -864,7 +863,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta // time this.worldAge++; - this.time += 1 * timeRate; + this.time += timeRate; if (timeUpdate != null && !CooldownUtils.hasCooldown(time, lastTimeUpdate, timeUpdate)) { PacketWriterUtils.writeAndSend(getPlayers(), getTimePacket()); diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index e346f2952..2da77fda8 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -54,8 +54,8 @@ public class InstanceContainer extends Instance { private ChunkGenerator chunkGenerator; // WARNING: need to be synchronized properly - private Long2ObjectMap chunks = new Long2ObjectOpenHashMap(); - private Set scheduledChunksToRemove = new HashSet<>(); + private final Long2ObjectMap chunks = new Long2ObjectOpenHashMap<>(); + private final Set scheduledChunksToRemove = new HashSet<>(); private ReadWriteLock changingBlockLock = new ReentrantReadWriteLock(); private Map currentlyChangingBlocks = new HashMap<>(); @@ -351,8 +351,9 @@ public class InstanceContainer extends Instance { Check.notNull(getStorageFolder(), "You cannot save the instance if no StorageFolder has been defined"); this.storageFolder.set(UUID_KEY, getUniqueId(), UUID.class); - Data data = getData(); + final Data data = getData(); if (data != null) { + // Save the instance data Check.stateCondition(!(data instanceof SerializableData), "Instance#getData needs to be a SerializableData in order to be saved"); this.storageFolder.set(DATA_KEY, (SerializableData) getData(), SerializableData.class); diff --git a/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java b/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java index 0f5842285..cb64c6df9 100644 --- a/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java +++ b/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java @@ -11,7 +11,7 @@ import java.util.function.Consumer; public class MinestomBasicChunkLoader implements IChunkLoader { private final static Logger LOGGER = LoggerFactory.getLogger(MinestomBasicChunkLoader.class); - private StorageFolder storageFolder; + private final StorageFolder storageFolder; public MinestomBasicChunkLoader(StorageFolder storageFolder) { this.storageFolder = storageFolder; diff --git a/src/main/java/net/minestom/server/instance/SharedInstance.java b/src/main/java/net/minestom/server/instance/SharedInstance.java index da6490685..01e940e8a 100644 --- a/src/main/java/net/minestom/server/instance/SharedInstance.java +++ b/src/main/java/net/minestom/server/instance/SharedInstance.java @@ -19,7 +19,7 @@ import java.util.function.Consumer; */ public class SharedInstance extends Instance { - private InstanceContainer instanceContainer; + private final InstanceContainer instanceContainer; public SharedInstance(UUID uniqueId, InstanceContainer instanceContainer) { super(uniqueId, instanceContainer.getDimensionType()); diff --git a/src/main/java/net/minestom/server/instance/StaticChunk.java b/src/main/java/net/minestom/server/instance/StaticChunk.java index b61908ff2..090efd6c6 100644 --- a/src/main/java/net/minestom/server/instance/StaticChunk.java +++ b/src/main/java/net/minestom/server/instance/StaticChunk.java @@ -70,8 +70,10 @@ public class StaticChunk extends Chunk { fullDataPacket.chunkZ = chunkZ; short[] blocksStateId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]; for (int i = 0; i < blocksStateId.length; i++) { - final int[] pos = ChunkUtils.indexToPosition(i, 0, 0); - blocksStateId[i] = blockProvider.getBlockStateId(pos[0], pos[1], pos[2]); + final int x = ChunkUtils.blockIndexToChunkPositionX(i); + final int y = ChunkUtils.blockIndexToChunkPositionY(i); + final int z = ChunkUtils.blockIndexToChunkPositionZ(i); + blocksStateId[i] = blockProvider.getBlockStateId(x, y, z); } fullDataPacket.blocksStateId = blocksStateId; fullDataPacket.customBlocksId = new short[0]; diff --git a/src/main/java/net/minestom/server/instance/WorldBorder.java b/src/main/java/net/minestom/server/instance/WorldBorder.java index a1bc0a505..8d3c54d05 100644 --- a/src/main/java/net/minestom/server/instance/WorldBorder.java +++ b/src/main/java/net/minestom/server/instance/WorldBorder.java @@ -11,7 +11,7 @@ import net.minestom.server.utils.Position; */ public class WorldBorder { - private Instance instance; + private final Instance instance; private float centerX, centerZ; diff --git a/src/main/java/net/minestom/server/instance/batch/BlockBatch.java b/src/main/java/net/minestom/server/instance/batch/BlockBatch.java index 9ba095d92..9aee89b42 100644 --- a/src/main/java/net/minestom/server/instance/batch/BlockBatch.java +++ b/src/main/java/net/minestom/server/instance/batch/BlockBatch.java @@ -87,7 +87,7 @@ public class BlockBatch implements InstanceBatch { } } - private class BlockData { + private static class BlockData { private int x, y, z; private boolean hasCustomBlock; diff --git a/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java b/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java index 52fa398de..bd1e09954 100644 --- a/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java +++ b/src/main/java/net/minestom/server/instance/batch/ChunkBatch.java @@ -18,8 +18,8 @@ import java.util.function.Consumer; */ public class ChunkBatch implements InstanceBatch { - private InstanceContainer instance; - private Chunk chunk; + private final InstanceContainer instance; + private final Chunk chunk; // Give it the max capacity by default (avoid resizing) private List dataList = @@ -110,7 +110,7 @@ public class ChunkBatch implements InstanceBatch { } } - private class BlockData { + private static class BlockData { private int x, y, z; private boolean hasCustomBlock; diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index 41a401df1..695f05489 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -27,25 +27,26 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.atomic.AtomicInteger; public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable { - private static volatile byte lastInventoryId; + private static AtomicInteger lastInventoryId = new AtomicInteger(); - private byte id; + private final byte id; private final InventoryType inventoryType; private String title; - private int size; + private final int size; - private int offset; + private final int offset; - private ItemStack[] itemStacks; - private Set viewers = new CopyOnWriteArraySet<>(); - private ConcurrentHashMap cursorPlayersItem = new ConcurrentHashMap<>(); + private final ItemStack[] itemStacks; + private final Set viewers = new CopyOnWriteArraySet<>(); + private final ConcurrentHashMap cursorPlayersItem = new ConcurrentHashMap<>(); - private List inventoryConditions = new CopyOnWriteArrayList<>(); - private InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); + private final List inventoryConditions = new CopyOnWriteArrayList<>(); + private final InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); // Cached windows packet @@ -64,8 +65,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View } private static byte generateId() { - byte newInventoryId = ++lastInventoryId; - if (newInventoryId < 0) + byte newInventoryId = (byte) lastInventoryId.incrementAndGet(); + if (newInventoryId == Byte.MAX_VALUE) newInventoryId = 1; return newInventoryId; } diff --git a/src/main/java/net/minestom/server/inventory/InventoryProperty.java b/src/main/java/net/minestom/server/inventory/InventoryProperty.java index dbbb2d722..9e1ef9639 100644 --- a/src/main/java/net/minestom/server/inventory/InventoryProperty.java +++ b/src/main/java/net/minestom/server/inventory/InventoryProperty.java @@ -33,7 +33,7 @@ public enum InventoryProperty { BREWING_STAND_FUEL_TIME((short) 1); - private short property; + private final short property; InventoryProperty(short property) { this.property = property; diff --git a/src/main/java/net/minestom/server/inventory/InventoryType.java b/src/main/java/net/minestom/server/inventory/InventoryType.java index 0e30a7e86..fe4c4125d 100644 --- a/src/main/java/net/minestom/server/inventory/InventoryType.java +++ b/src/main/java/net/minestom/server/inventory/InventoryType.java @@ -26,8 +26,8 @@ public enum InventoryType { CARTOGRAPHY(21, 3), STONE_CUTTER(22, 2); - private int windowType; - private int slot; + private final int windowType; + private final int slot; InventoryType(int windowType, int slot) { this.windowType = windowType; diff --git a/src/main/java/net/minestom/server/inventory/click/InventoryClickLoopHandler.java b/src/main/java/net/minestom/server/inventory/click/InventoryClickLoopHandler.java index a2e380501..54e183581 100644 --- a/src/main/java/net/minestom/server/inventory/click/InventoryClickLoopHandler.java +++ b/src/main/java/net/minestom/server/inventory/click/InventoryClickLoopHandler.java @@ -8,12 +8,12 @@ import java.util.function.BiConsumer; public class InventoryClickLoopHandler { - private int start; - private int end; - private int step; - private Int2IntFunction indexModifier; - private Int2ObjectFunction itemGetter; - private BiConsumer itemSetter; + private final int start; + private final int end; + private final int step; + private final Int2IntFunction indexModifier; + private final Int2ObjectFunction itemGetter; + private final BiConsumer itemSetter; public InventoryClickLoopHandler(int start, int end, int step, Int2IntFunction indexModifier, diff --git a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java index fc8d1dec4..9fe9ef6cb 100644 --- a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java +++ b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java @@ -23,8 +23,8 @@ import java.util.function.BiConsumer; public class InventoryClickProcessor { // Dragging maps - private Map leftDraggingMap = new HashMap<>(); - private Map rightDraggingMap = new HashMap<>(); + private final Map leftDraggingMap = new HashMap<>(); + private final Map rightDraggingMap = new HashMap<>(); public InventoryClickResult leftClick(Inventory inventory, Player player, int slot, ItemStack clicked, ItemStack cursor) { final InventoryClickResult clickResult = startCondition(inventory, player, slot, ClickType.LEFT_CLICK, clicked, cursor); diff --git a/src/main/java/net/minestom/server/item/attribute/ItemAttribute.java b/src/main/java/net/minestom/server/item/attribute/ItemAttribute.java index 9d625b267..baee41cd4 100644 --- a/src/main/java/net/minestom/server/item/attribute/ItemAttribute.java +++ b/src/main/java/net/minestom/server/item/attribute/ItemAttribute.java @@ -7,12 +7,12 @@ import java.util.UUID; public class ItemAttribute { - private UUID uuid; - private String internalName; - private Attribute attribute; - private AttributeOperation operation; - private double value; - private AttributeSlot slot; + private final UUID uuid; + private final String internalName; + private final Attribute attribute; + private final AttributeOperation operation; + private final double value; + private final AttributeSlot slot; public ItemAttribute(UUID uuid, String internalName, Attribute attribute, AttributeOperation operation, double value, AttributeSlot slot) { this.uuid = uuid; diff --git a/src/main/java/net/minestom/server/listener/PlayerVehicleListener.java b/src/main/java/net/minestom/server/listener/PlayerVehicleListener.java index 7498f80aa..087131e0a 100644 --- a/src/main/java/net/minestom/server/listener/PlayerVehicleListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerVehicleListener.java @@ -42,7 +42,7 @@ public class PlayerVehicleListener { public static void boatSteerListener(ClientSteerBoatPacket packet, Player player) { final Entity vehicle = player.getVehicle(); - if (vehicle == null || !(vehicle instanceof EntityBoat)) + if (!(vehicle instanceof EntityBoat)) return; EntityBoat boat = (EntityBoat) vehicle; diff --git a/src/main/java/net/minestom/server/network/packet/PacketReader.java b/src/main/java/net/minestom/server/network/packet/PacketReader.java index ff90eedd7..cae009a4d 100644 --- a/src/main/java/net/minestom/server/network/packet/PacketReader.java +++ b/src/main/java/net/minestom/server/network/packet/PacketReader.java @@ -17,8 +17,8 @@ import java.util.UUID; public class PacketReader extends InputStream { - private ByteBuf buffer; - private NBTReader nbtReader = new NBTReader(this, false); + private final ByteBuf buffer; + private final NBTReader nbtReader = new NBTReader(this, false); public PacketReader(ByteBuf buffer) { this.buffer = buffer; diff --git a/src/main/java/net/minestom/server/network/packet/PacketWriter.java b/src/main/java/net/minestom/server/network/packet/PacketWriter.java index 61aee8c0e..33e62c7e1 100644 --- a/src/main/java/net/minestom/server/network/packet/PacketWriter.java +++ b/src/main/java/net/minestom/server/network/packet/PacketWriter.java @@ -20,8 +20,8 @@ import java.util.function.Consumer; public class PacketWriter extends OutputStream { - private ByteBuf buffer = Unpooled.buffer(); - private NBTWriter nbtWriter = new NBTWriter(this, false); + private final ByteBuf buffer = Unpooled.buffer(); + private final NBTWriter nbtWriter = new NBTWriter(this, false); public void writeBoolean(boolean b) { buffer.writeBoolean(b); diff --git a/src/main/java/net/minestom/server/storage/StorageFolder.java b/src/main/java/net/minestom/server/storage/StorageFolder.java index d770fea2a..874aa0393 100644 --- a/src/main/java/net/minestom/server/storage/StorageFolder.java +++ b/src/main/java/net/minestom/server/storage/StorageFolder.java @@ -19,10 +19,10 @@ public class StorageFolder { private static final DataManager DATA_MANAGER = MinecraftServer.getDataManager(); - private StorageSystem storageSystem; - private String folderPath; + private final StorageSystem storageSystem; + private final String folderPath; - private Map cachedData; + private final Map cachedData; protected StorageFolder(StorageSystem storageSystem, String folderPath, StorageOptions storageOptions) { this.storageSystem = storageSystem; @@ -50,27 +50,26 @@ public class StorageFolder { } public void set(String key, T object, Class type) { - DataType dataType = DATA_MANAGER.getDataType(type); + final DataType dataType = DATA_MANAGER.getDataType(type); Check.notNull(dataType, "You can only save registered DataType type!"); PacketWriter packetWriter = new PacketWriter(); dataType.encode(packetWriter, object); // Encode - byte[] encodedValue = packetWriter.toByteArray(); // Retrieve bytes + final byte[] encodedValue = packetWriter.toByteArray(); // Retrieve bytes set(key, encodedValue); } public T get(String key, Class type) { - DataType dataType = DATA_MANAGER.getDataType(type); + final DataType dataType = DATA_MANAGER.getDataType(type); Check.notNull(dataType, "You can only save registered DataType type!"); - byte[] data = get(key); + final byte[] data = get(key); if (data == null) return null; PacketReader packetReader = new PacketReader(data); - T value = dataType.decode(packetReader); - return value; + return dataType.decode(packetReader); } public T getOrDefault(String key, Class type, T defaultValue) { @@ -95,7 +94,7 @@ public class StorageFolder { } // Load it from the storage system - byte[] bytes = get(key); + final byte[] bytes = get(key); SerializableData data; if (bytes != null) { @@ -128,7 +127,7 @@ public class StorageFolder { } // Load it from the storage system and cache it - byte[] bytes = get(key); + final byte[] bytes = get(key); SerializableData data; if (bytes != null) { @@ -151,7 +150,7 @@ public class StorageFolder { */ public void saveAndRemoveCachedData(String key) { synchronized (cachedData) { - SerializableData serializableData = cachedData.get(key); + final SerializableData serializableData = cachedData.get(key); if (serializableData == null) return; @@ -173,8 +172,8 @@ public class StorageFolder { try { synchronized (cachedData) { for (Map.Entry entry : cachedData.entrySet()) { - String key = entry.getKey(); - SerializableData data = entry.getValue(); + final String key = entry.getKey(); + final SerializableData data = entry.getValue(); set(key, data.getSerializedData()); } @@ -192,7 +191,7 @@ public class StorageFolder { public void saveCachedData(String key) { try { synchronized (cachedData) { - SerializableData data = cachedData.get(key); + final SerializableData data = cachedData.get(key); set(key, data.getSerializedData()); } } catch (IOException e) { diff --git a/src/main/java/net/minestom/server/storage/StorageManager.java b/src/main/java/net/minestom/server/storage/StorageManager.java index 83d04ddf0..75a90e7ab 100644 --- a/src/main/java/net/minestom/server/storage/StorageManager.java +++ b/src/main/java/net/minestom/server/storage/StorageManager.java @@ -30,9 +30,8 @@ public class StorageManager { */ public StorageFolder getFolder(String folderPath, StorageOptions storageOptions, StorageSystem storageSystem) { Check.notNull(storageOptions, "The storage option cannot be null"); - StorageFolder storageFolder = - folderMap.computeIfAbsent(folderPath, s -> new StorageFolder(storageSystem, folderPath, storageOptions)); - return storageFolder; + return folderMap.computeIfAbsent(folderPath, + s -> new StorageFolder(storageSystem, folderPath, storageOptions)); } /** @@ -47,7 +46,7 @@ public class StorageManager { public StorageFolder getFolder(String folderPath, StorageOptions storageOptions) { Check.notNull(defaultStorageSystemSupplier, "You need to either define a default storage system or specify your storage system for this specific folder"); - StorageSystem storageSystem = defaultStorageSystemSupplier.get(); + final StorageSystem storageSystem = defaultStorageSystemSupplier.get(); return getFolder(folderPath, storageOptions, storageSystem); } diff --git a/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java b/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java index 0bd3a6074..5c24a99b4 100644 --- a/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java +++ b/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java @@ -42,8 +42,7 @@ public class FileStorageSystem implements StorageSystem { @Override public byte[] get(String key) { try { - byte[] result = this.rocksDB.get(getKey(key)); - return result; + return rocksDB.get(getKey(key)); } catch (RocksDBException e) { e.printStackTrace(); return null; diff --git a/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java b/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java index bb0a61173..1ca260217 100644 --- a/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java +++ b/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java @@ -9,8 +9,8 @@ import net.minestom.server.utils.chunk.ChunkUtils; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicBoolean; /** * Separate chunks into group of linked chunks @@ -105,16 +105,16 @@ public class PerGroupChunkProvider extends ThreadProvider { } @Override - public ArrayList> update(long time) { + public List> update(long time) { // Set of already-updated instances this tick final Set updatedInstance = new HashSet<>(); - ArrayList> futures = new ArrayList<>(); + List> futures = new ArrayList<>(); instanceInstanceMap.forEach((instance, instanceMap) -> { - // True if the instance ended its tick call - AtomicBoolean instanceUpdated = new AtomicBoolean(false); + // True if the instance ended its tick callĀ¬ + final CountDownLatch countDownLatch = new CountDownLatch(1); // Update all the chunks + instances instanceMap.keySet().forEach(chunksIndexes -> { @@ -124,12 +124,15 @@ public class PerGroupChunkProvider extends ThreadProvider { // Used to check if the instance has already been updated this tick if (shouldUpdateInstance) { updateInstance(instance, time); - instanceUpdated.set(true); + countDownLatch.countDown(); } // Wait for the instance to be updated // Needed because the instance tick is used to unload waiting chunks - while (!instanceUpdated.get()) { + try { + countDownLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); } // Tick all this chunk group diff --git a/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java b/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java index 82a78a27c..1b2ff3717 100644 --- a/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/PerInstanceThreadProvider.java @@ -7,6 +7,7 @@ import net.minestom.server.utils.chunk.ChunkUtils; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.Future; @@ -15,43 +16,43 @@ import java.util.concurrent.Future; */ public class PerInstanceThreadProvider extends ThreadProvider { - private Map instanceChunkMap = new HashMap<>(); + private Map instanceChunkMap = new HashMap<>(); - @Override - public void onChunkLoad(Instance instance, int chunkX, int chunkZ) { - // Add the loaded chunk to the instance chunks list - LongSet chunkCoordinates = getChunkCoordinates(instance); - final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ); - chunkCoordinates.add(index); - } + @Override + public void onChunkLoad(Instance instance, int chunkX, int chunkZ) { + // Add the loaded chunk to the instance chunks list + LongSet chunkCoordinates = getChunkCoordinates(instance); + final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ); + chunkCoordinates.add(index); + } - @Override - public void onChunkUnload(Instance instance, int chunkX, int chunkZ) { - LongSet chunkCoordinates = getChunkCoordinates(instance); - final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ); - // Remove the unloaded chunk from the instance list - chunkCoordinates.remove(index); + @Override + public void onChunkUnload(Instance instance, int chunkX, int chunkZ) { + LongSet chunkCoordinates = getChunkCoordinates(instance); + final long index = ChunkUtils.getChunkIndex(chunkX, chunkZ); + // Remove the unloaded chunk from the instance list + chunkCoordinates.remove(index); - } + } - @Override - public ArrayList> update(long time) { - ArrayList> futures = new ArrayList<>(); + @Override + public List> update(long time) { + List> futures = new ArrayList<>(); - instanceChunkMap.forEach((instance, chunkIndexes) -> { + instanceChunkMap.forEach((instance, chunkIndexes) -> { - futures.add(pool.submit(() -> { - // Tick instance - updateInstance(instance, time); - // Tick chunks - chunkIndexes.forEach((long chunkIndex) -> processChunkTick(instance, chunkIndex, time)); - })); - }); - return futures; - } + futures.add(pool.submit(() -> { + // Tick instance + updateInstance(instance, time); + // Tick chunks + chunkIndexes.forEach((long chunkIndex) -> processChunkTick(instance, chunkIndex, time)); + })); + }); + return futures; + } - private LongSet getChunkCoordinates(Instance instance) { - return instanceChunkMap.computeIfAbsent(instance, inst -> new LongArraySet()); - } + private LongSet getChunkCoordinates(Instance instance) { + return instanceChunkMap.computeIfAbsent(instance, inst -> new LongArraySet()); + } } diff --git a/src/main/java/net/minestom/server/thread/ThreadProvider.java b/src/main/java/net/minestom/server/thread/ThreadProvider.java index 657773e8e..19611ca95 100644 --- a/src/main/java/net/minestom/server/thread/ThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/ThreadProvider.java @@ -7,7 +7,7 @@ import net.minestom.server.instance.Instance; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.thread.MinestomThread; -import java.util.ArrayList; +import java.util.List; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; @@ -55,9 +55,9 @@ public abstract class ThreadProvider { * Perform a server tick for all chunks based on their linked thread * * @param time the update time in milliseconds - * @return + * @return the futures to execute to complete the tick */ - public abstract ArrayList> update(long time); + public abstract List> update(long time); /** * Get the current size of the thread pool diff --git a/src/main/java/net/minestom/server/utils/ArrayUtils.java b/src/main/java/net/minestom/server/utils/ArrayUtils.java index d9fd9c642..9c88b2d9e 100644 --- a/src/main/java/net/minestom/server/utils/ArrayUtils.java +++ b/src/main/java/net/minestom/server/utils/ArrayUtils.java @@ -64,7 +64,7 @@ public final class ArrayUtils { public static int[] toArray(IntList list) { int[] array = new int[list.size()]; for (int i = 0; i < array.length; i++) { - array[i] = list.get(i); + array[i] = list.getInt(i); } return array; } diff --git a/src/main/java/net/minestom/server/utils/MathUtils.java b/src/main/java/net/minestom/server/utils/MathUtils.java index 6d83fb761..dfb1e7ce2 100644 --- a/src/main/java/net/minestom/server/utils/MathUtils.java +++ b/src/main/java/net/minestom/server/utils/MathUtils.java @@ -64,11 +64,11 @@ public final class MathUtils { } public static int setBetween(int number, int min, int max) { - return number > max ? max : number < min ? min : number; + return number > max ? max : Math.max(number, min); } public static float setBetween(float number, float min, float max) { - return number > max ? max : number < min ? min : number; + return number > max ? max : Math.max(number, min); } public static int clamp(int value, int min, int max) { diff --git a/src/main/java/net/minestom/server/utils/PrimitiveConversion.java b/src/main/java/net/minestom/server/utils/PrimitiveConversion.java index 3d4a8b2ad..5eebc1414 100644 --- a/src/main/java/net/minestom/server/utils/PrimitiveConversion.java +++ b/src/main/java/net/minestom/server/utils/PrimitiveConversion.java @@ -23,21 +23,21 @@ public class PrimitiveConversion { } public static String getObjectClassString(String clazz) { - if (clazz == "boolean") + if (clazz.equals("boolean")) return "java.lang.Boolean"; - if (clazz == "byte") + if (clazz.equals("byte")) return "java.lang.Byte"; - if (clazz == "char") + if (clazz.equals("char")) return "java.lang.Character"; - if (clazz == "short") + if (clazz.equals("short")) return "java.lang.Short"; - if (clazz == "int") + if (clazz.equals("int")) return "java.lang.Integer"; - if (clazz == "long") + if (clazz.equals("long")) return "java.lang.Long"; - if (clazz == "float") + if (clazz.equals("float")) return "java.lang.Float"; - if (clazz == "double") + if (clazz.equals("double")) return "java.lang.Double"; return clazz; } diff --git a/src/main/java/net/minestom/server/utils/WeightedRandom.java b/src/main/java/net/minestom/server/utils/WeightedRandom.java index 9ca16031f..ba674fe1d 100644 --- a/src/main/java/net/minestom/server/utils/WeightedRandom.java +++ b/src/main/java/net/minestom/server/utils/WeightedRandom.java @@ -41,7 +41,7 @@ public class WeightedRandom { public E get(Random rng) { final double p = rng.nextDouble() * totalWeight; for (int i = 0; i < entries.size(); i++) { - final double weightSum = weightSums.get(i); + final double weightSum = weightSums.getDouble(i); if (weightSum >= p) { return entries.get(i); } diff --git a/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java b/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java index a66e49e84..c23ed5507 100644 --- a/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java +++ b/src/main/java/net/minestom/server/utils/chunk/ChunkUtils.java @@ -85,8 +85,6 @@ public final class ChunkUtils { * @return the chunk Z based on the index */ public static int getChunkCoordZ(long index) { - final int chunkX = (int) (index >> 32); - final int chunkZ = (int) index; return (int) index; } @@ -117,8 +115,7 @@ public final class ChunkUtils { for (int z = startLoop; z < endLoop; z++) { final int chunkX = getChunkCoordinate((int) (position.getX() + Chunk.CHUNK_SIZE_X * x)); final int chunkZ = getChunkCoordinate((int) (position.getZ() + Chunk.CHUNK_SIZE_Z * z)); - visibleChunks[counter] = getChunkIndex(chunkX, chunkZ); - counter++; + visibleChunks[counter++] = getChunkIndex(chunkX, chunkZ); } } return visibleChunks; @@ -130,8 +127,7 @@ public final class ChunkUtils { * @param instance the instance of the chunks * @param chunkX the chunk X * @param chunkZ the chunk Z - * @return an array containing all the loaded neighbours - * can be deserialized using {@link #indexToChunkPosition(int)} + * @return an array containing all the loaded neighbours chunk index */ public static long[] getNeighbours(Instance instance, int chunkX, int chunkZ) { LongList chunks = new LongArrayList(); @@ -185,36 +181,76 @@ public final class ChunkUtils { * @return the instance position of the block located in {@code index} */ public static BlockPosition getBlockPosition(int index, int chunkX, int chunkZ) { - final int[] pos = indexToPosition(index, chunkX, chunkZ); - return new BlockPosition(pos[0], pos[1], pos[2]); + final int x = blockIndexToPositionX(index, chunkX); + final int y = blockIndexToPositionY(index); + final int z = blockIndexToPositionZ(index, chunkZ); + return new BlockPosition(x, y, z); } /** - * @param index an index computed from {@link #getBlockIndex(int, int, int)} + * Convert a block chunk index to its instance position X + * + * @param index the block chunk index from {@link #getBlockIndex(int, int, int)} * @param chunkX the chunk X - * @param chunkZ the chunk Z - * @return the world position of the specified index with its chunks being {@code chunkX} and {@code chunk Z} - * positions in the array are in the order X/Y/Z + * @return the X coordinate of the block index */ - public static int[] indexToPosition(int index, int chunkX, int chunkZ) { - int z = (byte) (index >> 12 & 0xF); - final int y = (index >>> 4 & 0xFF); - // index >> 0 = index + public static int blockIndexToPositionX(int index, int chunkX) { int x = (byte) (index & 0xF); - x += 16 * chunkX; - z += 16 * chunkZ; - - return new int[]{x, y, z}; + return x; } /** - * @param index an index computed from {@link #getBlockIndex(int, int, int)} - * @return the chunk position (O-15) of the specified index, - * positions in the array are in the order X/Y/Z + * Convert a block chunk index to its instance position Y + * + * @param index the block chunk index from {@link #getBlockIndex(int, int, int)} + * @return the Y coordinate of the block index */ - public static int[] indexToChunkPosition(int index) { - return indexToPosition(index, 0, 0); + public static int blockIndexToPositionY(int index) { + return (index >>> 4 & 0xFF); + } + + /** + * Convert a block chunk index to its instance position Z + * + * @param index the block chunk index from {@link #getBlockIndex(int, int, int)} + * @param chunkZ the chunk Z + * @return the Z coordinate of the block index + */ + public static int blockIndexToPositionZ(int index, int chunkZ) { + int z = (byte) (index >> 12 & 0xF); + z += 16 * chunkZ; + return z; + } + + /** + * Convert a block index to a chunk position X + * + * @param index an index computed from {@link #getBlockIndex(int, int, int)} + * @return the chunk position X (O-15) of the specified index + */ + public static int blockIndexToChunkPositionX(int index) { + return blockIndexToPositionX(index, 0); + } + + /** + * Convert a block index to a chunk position Y + * + * @param index an index computed from {@link #getBlockIndex(int, int, int)} + * @return the chunk position Y (O-255) of the specified index + */ + public static int blockIndexToChunkPositionY(int index) { + return blockIndexToPositionY(index); + } + + /** + * Convert a block index to a chunk position Z + * + * @param index an index computed from {@link #getBlockIndex(int, int, int)} + * @return the chunk position Z (O-15) of the specified index + */ + public static int blockIndexToChunkPositionZ(int index) { + return blockIndexToPositionZ(index, 0); } }