diff --git a/.gitignore b/.gitignore index b1a8308b6..be3b46bfa 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,6 @@ gradle-app.setting # When running the demo we generate the extensions folder /extensions/ + +# When compiling we get a docs folder +/docs \ No newline at end of file diff --git a/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java b/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java index 9b9eb8fb8..7b519b96e 100644 --- a/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java @@ -35,7 +35,7 @@ public class BlockEnumGenerator extends MinestomEnumGenerator { private final String targetVersion; private final File targetFolder; - private CodeBlock.Builder staticBlock = CodeBlock.builder(); + private final CodeBlock.Builder staticBlock = CodeBlock.builder(); public static void main(String[] args) throws IOException { @@ -161,8 +161,6 @@ public class BlockEnumGenerator extends MinestomEnumGenerator { } return blocks; - } catch (IOException e) { - throw e; } } @@ -178,8 +176,6 @@ public class BlockEnumGenerator extends MinestomEnumGenerator { try (BufferedReader bufferedReader = new BufferedReader(new FileReader(blockFile))) { PrismarineJSBlock[] blocks = gson.fromJson(bufferedReader, PrismarineJSBlock[].class); return Arrays.asList(blocks); - } catch (IOException e) { - throw e; } } diff --git a/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java b/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java index ab3900ece..4b4f44053 100644 --- a/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java @@ -85,8 +85,6 @@ public class ItemEnumGenerator extends MinestomEnumGenerator { items.add(item); } return items; - } catch (IOException e) { - throw e; } } diff --git a/src/lwjgl/java/net/minestom/server/map/PaletteGenerator.java b/src/lwjgl/java/net/minestom/server/map/PaletteGenerator.java index 9f6e2f413..cb3de987b 100644 --- a/src/lwjgl/java/net/minestom/server/map/PaletteGenerator.java +++ b/src/lwjgl/java/net/minestom/server/map/PaletteGenerator.java @@ -1,5 +1,8 @@ package net.minestom.server.map; +import it.unimi.dsi.fastutil.ints.Int2IntMap; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; + import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; @@ -10,7 +13,7 @@ import java.util.Map; public class PaletteGenerator { public static void main(String[] args) { - Map colors = new HashMap<>(); + Int2IntMap colors = new Int2IntOpenHashMap(); int highestIndex = 0; for(MapColors c : MapColors.values()) { if (c == MapColors.NONE) diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index 553c6095f..f366fb19c 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -217,7 +217,6 @@ public final class MinecraftServer { * @throws NullPointerException if {@code brandName} is null */ public static void setBrandName(@NotNull String brandName) { - Check.notNull(brandName, "The brand name cannot be null"); MinecraftServer.brandName = brandName; PacketUtils.sendGroupedPacket(connectionManager.getOnlinePlayers(), PluginMessagePacket.getBrandPacket()); @@ -275,7 +274,6 @@ public final class MinecraftServer { * @param difficulty the new server difficulty */ public static void setDifficulty(@NotNull Difficulty difficulty) { - Check.notNull(difficulty, "The server difficulty cannot be null."); MinecraftServer.difficulty = difficulty; // Send the packet to all online players @@ -478,9 +476,7 @@ public final class MinecraftServer { MinecraftServer.chunkViewDistance = chunkViewDistance; if (started) { - final Collection players = connectionManager.getOnlinePlayers(); - - players.forEach(player -> { + for (final Player player : connectionManager.getOnlinePlayers()) { final Chunk playerChunk = player.getChunk(); if (playerChunk != null) { @@ -490,7 +486,7 @@ public final class MinecraftServer { player.refreshVisibleChunks(playerChunk); } - }); + } } } @@ -514,12 +510,12 @@ public final class MinecraftServer { "The entity view distance must be between 0 and 32"); MinecraftServer.entityViewDistance = entityViewDistance; if (started) { - connectionManager.getOnlinePlayers().forEach(player -> { + for (final Player player : connectionManager.getOnlinePlayers()) { final Chunk playerChunk = player.getChunk(); if (playerChunk != null) { player.refreshVisibleEntities(playerChunk); } - }); + } } } diff --git a/src/main/java/net/minestom/server/UpdateManager.java b/src/main/java/net/minestom/server/UpdateManager.java index 6b546efca..ef22e6987 100644 --- a/src/main/java/net/minestom/server/UpdateManager.java +++ b/src/main/java/net/minestom/server/UpdateManager.java @@ -140,7 +140,6 @@ public final class UpdateManager { * @throws NullPointerException if threadProvider is null */ public synchronized void setThreadProvider(ThreadProvider threadProvider) { - Check.notNull(threadProvider, "The thread provider cannot be null"); this.threadProvider = threadProvider; } diff --git a/src/main/java/net/minestom/server/advancements/AdvancementTab.java b/src/main/java/net/minestom/server/advancements/AdvancementTab.java index 67515f0c6..5bc96795b 100644 --- a/src/main/java/net/minestom/server/advancements/AdvancementTab.java +++ b/src/main/java/net/minestom/server/advancements/AdvancementTab.java @@ -73,7 +73,6 @@ public class AdvancementTab implements Viewable { * @param parent the parent of this advancement, it cannot be null */ public void createAdvancement(@NotNull String identifier, @NotNull Advancement advancement, @NotNull Advancement parent) { - Check.argCondition(identifier == null, "the advancement identifier cannot be null"); Check.stateCondition(!advancementMap.containsKey(parent), "You tried to set a parent which doesn't exist or isn't registered"); cacheAdvancement(identifier, advancement, parent); diff --git a/src/main/java/net/minestom/server/chat/ColoredText.java b/src/main/java/net/minestom/server/chat/ColoredText.java index 394780a6e..dabdd1ea3 100644 --- a/src/main/java/net/minestom/server/chat/ColoredText.java +++ b/src/main/java/net/minestom/server/chat/ColoredText.java @@ -19,8 +19,11 @@ import java.util.regex.Pattern; */ public class ColoredText extends JsonMessage { - // the raw text - private String message; + /** + * The raw text StringBuilder + * Its a single StringBuilder instance for easier and faster concenation + */ + private final StringBuilder message = new StringBuilder(); /** * Creates a colored text. @@ -31,7 +34,7 @@ public class ColoredText extends JsonMessage { * @see #of(String) to create a colored text */ private ColoredText(@NotNull String message) { - this.message = message; + this.message.append(message); refreshUpdate(); } @@ -79,7 +82,7 @@ public class ColoredText extends JsonMessage { */ @NotNull public ColoredText append(@NotNull ChatColor color, @NotNull String message) { - this.message += color + message; + this.message.append(color).append(message); refreshUpdate(); return this; } @@ -116,7 +119,7 @@ public class ColoredText extends JsonMessage { */ @NotNull public String getMessage() { - return message; + return message.toString(); } /** @@ -160,7 +163,7 @@ public class ColoredText extends JsonMessage { protected List getComponents() { final List objects = new ArrayList<>(); // No message, return empty list - if (message.isEmpty()) + if (getMessage().isEmpty()) return objects; boolean inFormat = false; diff --git a/src/main/java/net/minestom/server/chat/RichMessage.java b/src/main/java/net/minestom/server/chat/RichMessage.java index b260208a1..97a3e4d0a 100644 --- a/src/main/java/net/minestom/server/chat/RichMessage.java +++ b/src/main/java/net/minestom/server/chat/RichMessage.java @@ -37,7 +37,6 @@ public class RichMessage extends JsonMessage { * @return the created rich message object */ public static RichMessage of(@NotNull ColoredText coloredText) { - Check.notNull(coloredText, "ColoredText cannot be null"); RichMessage richMessage = new RichMessage(); appendText(richMessage, coloredText); @@ -58,7 +57,6 @@ public class RichMessage extends JsonMessage { * @return the rich message */ public RichMessage append(@NotNull ColoredText coloredText) { - Check.notNull(coloredText, "ColoredText cannot be null"); appendText(this, coloredText); return this; diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index 4fe28836c..e42bbd0d9 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -185,8 +185,6 @@ public final class CommandManager { * @return true if the command hadn't been cancelled and has been successful */ public boolean execute(@NotNull CommandSender sender, @NotNull String command) { - Check.notNull(sender, "Source cannot be null"); - Check.notNull(command, "Command string cannot be null"); // Command event if (sender instanceof Player) { diff --git a/src/main/java/net/minestom/server/command/CommandSender.java b/src/main/java/net/minestom/server/command/CommandSender.java index d2cfea6db..a5f928af2 100644 --- a/src/main/java/net/minestom/server/command/CommandSender.java +++ b/src/main/java/net/minestom/server/command/CommandSender.java @@ -39,7 +39,7 @@ public interface CommandSender extends PermissionHandler { * */ default void sendMessage(@NotNull JsonMessage text) { if (this instanceof Player) { - ((Player) this).sendMessage((JsonMessage) text); + this.sendMessage(text); } else { sendMessage(text.getRawMessage()); } 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 d027b2d4e..90fdaa85e 100644 --- a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java +++ b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java @@ -1,5 +1,6 @@ package net.minestom.server.command.builder; +import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.condition.CommandCondition; @@ -58,6 +59,7 @@ public class CommandDispatcher { * @param commandString the command (containing the command name and the args if any) * @return the result of the parsing, null if the command doesn't exist */ + @Nullable public CommandResult parse(@NotNull String commandString) { commandString = commandString.trim(); @@ -131,7 +133,7 @@ public class CommandDispatcher { // Contains all the syntaxes that are not fully correct, used to later, retrieve the "most correct syntax" // Number of correct argument - The data about the failing argument - TreeMap syntaxesSuggestions = new TreeMap<>(Collections.reverseOrder()); + Int2ObjectRBTreeMap syntaxesSuggestions = new Int2ObjectRBTreeMap<>(Collections.reverseOrder()); for (CommandSyntax syntax : syntaxes) { final Argument[] arguments = syntax.getArguments(); @@ -257,7 +259,7 @@ public class CommandDispatcher { { // Get closest valid syntax if (!syntaxesSuggestions.isEmpty()) { - final int max = syntaxesSuggestions.firstKey(); // number of correct arguments in the most correct syntax + final int max = syntaxesSuggestions.firstIntKey(); // number of correct arguments in the most correct syntax final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max); final CommandSyntax syntax = suggestionHolder.syntax; final ArgumentSyntaxException argumentSyntaxException = suggestionHolder.argumentSyntaxException; diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 910ebb26c..0f817882c 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -242,7 +242,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @throws IllegalStateException if you try to teleport an entity before settings its instance */ public void teleport(@NotNull Position position, @Nullable long[] chunks, @Nullable Runnable callback) { - Check.notNull(position, "Teleport position cannot be null"); Check.stateCondition(instance == null, "You need to use Entity#setInstance before teleporting an entity!"); final ChunkCallback endCallback = (chunk) -> { @@ -329,7 +328,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P @Override public boolean addViewer(@NotNull Player player) { - Check.notNull(player, "Viewer cannot be null"); boolean result = this.viewers.add(player); if (!result) return false; @@ -339,7 +337,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P @Override public boolean removeViewer(@NotNull Player player) { - Check.notNull(player, "Viewer cannot be null"); if (!viewers.remove(player)) return false; @@ -757,7 +754,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager} */ public void setInstance(@NotNull Instance instance) { - Check.notNull(instance, "instance cannot be null!"); Check.stateCondition(!instance.isRegistered(), "Instances need to be registered, please use InstanceManager#registerInstance or InstanceManager#registerSharedInstance"); @@ -866,7 +862,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @return the distance between this and {@code entity} */ public float getDistance(@NotNull Entity entity) { - Check.notNull(entity, "Entity cannot be null"); return getPosition().getDistance(entity.getPosition()); } @@ -888,7 +883,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @throws IllegalStateException if {@link #getInstance()} returns null */ public void addPassenger(@NotNull Entity entity) { - Check.notNull(entity, "Passenger cannot be null"); Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance"); if (entity.getVehicle() != null) { @@ -909,7 +903,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P * @throws IllegalStateException if {@link #getInstance()} returns null */ public void removePassenger(@NotNull Entity entity) { - Check.notNull(entity, "Passenger cannot be null"); Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance"); if (!passengers.remove(entity)) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 8ace0671d..82d8e4bf6 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -272,7 +272,6 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { * @return true if damage has been applied, false if it didn't */ public boolean damage(@NotNull DamageType type, float value) { - Check.notNull(type, "The damage type cannot be null!"); if (isDead()) return false; if (isInvulnerable() || isImmune(type)) { diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 4925ced5c..ba2ce704e 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -536,7 +536,7 @@ public class Player extends LivingEntity implements CommandSender { // #buildDeathScreenText can return null, check here if (deathText != null) { - CombatEventPacket deathPacket = CombatEventPacket.death(this, Optional.empty(), deathText); + CombatEventPacket deathPacket = CombatEventPacket.death(this, null, deathText); playerConnection.sendPacket(deathPacket); } @@ -670,7 +670,6 @@ public class Player extends LivingEntity implements CommandSender { * @param spawnPosition the new position of the player */ public void setInstance(@NotNull Instance instance, @NotNull Position spawnPosition) { - Check.notNull(instance, "instance cannot be null!"); Check.argCondition(this.instance == instance, "Instance should be different than the current one"); // true if the chunks need to be sent to the client, can be false if the instances share the same chunks (eg SharedInstance) @@ -1364,7 +1363,6 @@ public class Player extends LivingEntity implements CommandSender { * @param resourcePack the resource pack */ public void setResourcePack(@NotNull ResourcePack resourcePack) { - Check.notNull(resourcePack, "The resource pack cannot be null"); final String url = resourcePack.getUrl(); final String hash = resourcePack.getHash(); @@ -1737,7 +1735,6 @@ public class Player extends LivingEntity implements CommandSender { * @param gameMode the new player GameMode */ public void setGameMode(@NotNull GameMode gameMode) { - Check.notNull(gameMode, "GameMode cannot be null"); this.gameMode = gameMode; // Condition to prevent sending the packets before spawning the player @@ -1766,7 +1763,6 @@ public class Player extends LivingEntity implements CommandSender { * @param dimensionType the new player dimension */ protected void sendDimension(@NotNull DimensionType dimensionType) { - Check.notNull(dimensionType, "Dimension cannot be null!"); Check.argCondition(dimensionType.equals(getDimensionType()), "The dimension needs to be different than the current one!"); this.dimensionType = dimensionType; @@ -1900,7 +1896,6 @@ public class Player extends LivingEntity implements CommandSender { * @return true if the inventory has been opened/sent to the player, false otherwise (cancelled by event) */ public boolean openInventory(@NotNull Inventory inventory) { - Check.notNull(inventory, "Inventory cannot be null, use Player#closeInventory() to close current"); InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(inventory, this); diff --git a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java index 9104590dc..aea57a783 100644 --- a/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java +++ b/src/main/java/net/minestom/server/entity/ai/goal/EatBlockGoal.java @@ -7,7 +7,6 @@ import net.minestom.server.instance.Instance; import net.minestom.server.utils.BlockPosition; import org.jetbrains.annotations.NotNull; -import java.util.Map; import java.util.Random; public class EatBlockGoal extends GoalSelector { diff --git a/src/main/java/net/minestom/server/entity/pathfinding/NavigableEntity.java b/src/main/java/net/minestom/server/entity/pathfinding/NavigableEntity.java index 5e0b21145..0b8f62d16 100644 --- a/src/main/java/net/minestom/server/entity/pathfinding/NavigableEntity.java +++ b/src/main/java/net/minestom/server/entity/pathfinding/NavigableEntity.java @@ -30,7 +30,6 @@ public interface NavigableEntity { * @param speed define how far the entity will move */ default void moveTowards(@NotNull Position direction, float speed) { - Check.notNull(direction, "The direction cannot be null"); final Position position = getNavigableEntity().getPosition(); 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 eaec31f87..73a583ff4 100644 --- a/src/main/java/net/minestom/server/event/handler/EventHandler.java +++ b/src/main/java/net/minestom/server/event/handler/EventHandler.java @@ -37,8 +37,6 @@ public interface EventHandler { * @return true if the callback collection changed as a result of the call */ default boolean addEventCallback(@NotNull Class eventClass, @NotNull EventCallback eventCallback) { - Check.notNull(eventClass, "Event class cannot be null"); - Check.notNull(eventCallback, "Event callback cannot be null"); Collection callbacks = getEventCallbacks(eventClass); return callbacks.add(eventCallback); } @@ -52,8 +50,6 @@ public interface EventHandler { * @return true if the callback was removed as a result of this call */ default boolean removeEventCallback(@NotNull Class eventClass, @NotNull EventCallback eventCallback) { - Check.notNull(eventClass, "Event class cannot be null"); - Check.notNull(eventCallback, "Event callback cannot be null"); Collection callbacks = getEventCallbacks(eventClass); return callbacks.remove(eventCallback); } @@ -67,7 +63,6 @@ public interface EventHandler { */ @NotNull default Collection getEventCallbacks(@NotNull Class eventClass) { - Check.notNull(eventClass, "Event class cannot be null"); return getEventCallbacksMap().computeIfAbsent(eventClass, clazz -> new CopyOnWriteArraySet<>()); } diff --git a/src/main/java/net/minestom/server/extras/selfmodification/MinestomExtensionClassLoader.java b/src/main/java/net/minestom/server/extras/selfmodification/MinestomExtensionClassLoader.java index 7df07b288..6afbee103 100644 --- a/src/main/java/net/minestom/server/extras/selfmodification/MinestomExtensionClassLoader.java +++ b/src/main/java/net/minestom/server/extras/selfmodification/MinestomExtensionClassLoader.java @@ -58,8 +58,7 @@ public class MinestomExtensionClassLoader extends HierarchyClassLoader { } catch (ClassNotFoundException e) { for(MinestomExtensionClassLoader child : children) { try { - Class loaded = child.loadClassAsChild(name, resolve); - return loaded; + return child.loadClassAsChild(name, resolve); } catch (ClassNotFoundException e1) { // move on to next child } diff --git a/src/main/java/net/minestom/server/instance/BlockModifier.java b/src/main/java/net/minestom/server/instance/BlockModifier.java index 3cfb2b852..c5a7fdfe2 100644 --- a/src/main/java/net/minestom/server/instance/BlockModifier.java +++ b/src/main/java/net/minestom/server/instance/BlockModifier.java @@ -71,13 +71,10 @@ public interface BlockModifier { } default void setBlock(@NotNull BlockPosition blockPosition, @NotNull Block block) { - Check.notNull(blockPosition, "The block position cannot be null"); - Check.notNull(block, "The block cannot be null"); setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block); } default void setBlockStateId(@NotNull BlockPosition blockPosition, short blockStateId) { - Check.notNull(blockPosition, "The block position cannot be null"); setBlockStateId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockStateId); } @@ -97,7 +94,6 @@ public interface BlockModifier { } default void setCustomBlock(@NotNull BlockPosition blockPosition, @NotNull String customBlockId) { - Check.notNull(blockPosition, "The block position cannot be null"); setCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), customBlockId); } @@ -106,7 +102,6 @@ public interface BlockModifier { } default void setSeparateBlocks(@NotNull BlockPosition blockPosition, short blockStateId, short customBlockId) { - Check.notNull(blockPosition, "The block position cannot be null"); setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockStateId, customBlockId, null); } diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 736091c18..d3e678844 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -1,5 +1,6 @@ package net.minestom.server.instance; +import it.unimi.dsi.fastutil.ints.IntSet; import net.minestom.server.MinecraftServer; import net.minestom.server.Viewable; import net.minestom.server.data.Data; diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 652f79d9f..f1ca1d782 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -508,7 +508,6 @@ public class InstanceContainer extends Instance { @Override public ChunkBatch createChunkBatch(@NotNull Chunk chunk) { - Check.notNull(chunk, "The chunk of a ChunkBatch cannot be null"); return new ChunkBatch(this, chunk, false); } @@ -587,7 +586,6 @@ public class InstanceContainer extends Instance { * @throws NullPointerException if {@code chunkSupplier} is null */ public void setChunkSupplier(@NotNull ChunkSupplier chunkSupplier) { - Check.notNull(chunkSupplier, "The chunk supplier cannot be null!"); this.chunkSupplier = chunkSupplier; } 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 5eeb7d930..949029f78 100644 --- a/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java +++ b/src/main/java/net/minestom/server/inventory/click/InventoryClickProcessor.java @@ -19,7 +19,6 @@ import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.function.BiConsumer; public class InventoryClickProcessor { @@ -161,12 +160,12 @@ public class InventoryClickProcessor { if (cursor.isAir()) { // if held item [key] is air then set clicked to held resultClicked = ItemStack.getAirItem(); - resultHeld = clicked; } else { // Otherwise replace held item and held resultClicked = cursor; - resultHeld = clicked; } + + resultHeld = clicked; } clickResult.setClicked(resultClicked); @@ -278,7 +277,7 @@ public class InventoryClickProcessor { // End left if (!leftDraggingMap.containsKey(player)) return null; - final Set slots = leftDraggingMap.get(player); + final IntSet slots = leftDraggingMap.get(player); final int slotCount = slots.size(); final int cursorAmount = stackingRule.getAmount(cursor); if (slotCount > cursorAmount) @@ -287,7 +286,7 @@ public class InventoryClickProcessor { final int slotSize = (int) ((float) cursorAmount / (float) slotCount); int finalCursorAmount = cursorAmount; - for (Integer s : slots) { + for (int s : slots) { final ItemStack draggedItem = cursor.clone(); ItemStack slotItem = itemGetter.apply(s); @@ -322,12 +321,12 @@ public class InventoryClickProcessor { // End right if (!rightDraggingMap.containsKey(player)) return null; - final Set slots = rightDraggingMap.get(player); + final IntSet slots = rightDraggingMap.get(player); final int size = slots.size(); int cursorAmount = stackingRule.getAmount(cursor); if (size > cursorAmount) return null; - for (Integer s : slots) { + for (int s : slots) { ItemStack draggedItem = cursor.clone(); ItemStack slotItem = itemGetter.apply(s); diff --git a/src/main/java/net/minestom/server/item/ItemDisplay.java b/src/main/java/net/minestom/server/item/ItemDisplay.java index eb71fb57f..74344bb75 100644 --- a/src/main/java/net/minestom/server/item/ItemDisplay.java +++ b/src/main/java/net/minestom/server/item/ItemDisplay.java @@ -1,6 +1,5 @@ package net.minestom.server.item; -import net.minestom.server.chat.ColoredText; import net.minestom.server.chat.JsonMessage; public class ItemDisplay { diff --git a/src/main/java/net/minestom/server/item/ItemStack.java b/src/main/java/net/minestom/server/item/ItemStack.java index 400339743..b11e94e4e 100644 --- a/src/main/java/net/minestom/server/item/ItemStack.java +++ b/src/main/java/net/minestom/server/item/ItemStack.java @@ -1,5 +1,7 @@ package net.minestom.server.item; +import it.unimi.dsi.fastutil.objects.Object2ShortMap; +import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap; import net.minestom.server.chat.JsonMessage; import net.minestom.server.data.Data; import net.minestom.server.data.DataContainer; @@ -18,7 +20,6 @@ import net.minestom.server.utils.Direction; import net.minestom.server.utils.NBTUtils; import net.minestom.server.utils.clone.PublicCloneable; import net.minestom.server.utils.ownership.OwnershipHandler; -import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; @@ -38,11 +39,11 @@ import java.util.*; */ public class ItemStack implements DataContainer, PublicCloneable { - public static final OwnershipHandler DATA_OWNERSHIP = new OwnershipHandler(); + public static final OwnershipHandler DATA_OWNERSHIP = new OwnershipHandler<>(); public static final String OWNERSHIP_DATA_KEY = "ownership_identifier"; private static final StackingRule VANILLA_STACKING_RULE = new VanillaStackingRule(64); - private UUID identifier; + private final UUID identifier; private Material material; @@ -56,7 +57,7 @@ public class ItemStack implements DataContainer, PublicCloneable { private boolean unbreakable; private List lore; - private Map enchantmentMap; + private Object2ShortMap enchantmentMap; private List attributes; private int hideFlag; @@ -78,7 +79,7 @@ public class ItemStack implements DataContainer, PublicCloneable { this.damage = damage; this.lore = new ArrayList<>(); - this.enchantmentMap = new HashMap<>(); + this.enchantmentMap = new Object2ShortOpenHashMap<>(); this.attributes = new ArrayList<>(); this.itemMeta = findMeta(); @@ -117,7 +118,6 @@ public class ItemStack implements DataContainer, PublicCloneable { * @throws NullPointerException if {@code defaultStackingRule} is null */ public static void setDefaultStackingRule(@NotNull StackingRule defaultStackingRule) { - Check.notNull(defaultStackingRule, "StackingRule cannot be null!"); ItemStack.defaultStackingRule = defaultStackingRule; } @@ -341,7 +341,7 @@ public class ItemStack implements DataContainer, PublicCloneable { * @param enchantment the enchantment type */ public void removeEnchantment(@NotNull Enchantment enchantment) { - this.enchantmentMap.remove(enchantment); + this.enchantmentMap.removeShort(enchantment); } /** @@ -581,7 +581,7 @@ public class ItemStack implements DataContainer, PublicCloneable { itemStack.setStackingRule(stackingRule); } - itemStack.enchantmentMap = new HashMap<>(enchantmentMap); + itemStack.enchantmentMap = new Object2ShortOpenHashMap<>(enchantmentMap); itemStack.attributes = new ArrayList<>(attributes); itemStack.hideFlag = hideFlag; @@ -635,7 +635,6 @@ public class ItemStack implements DataContainer, PublicCloneable { * @throws NullPointerException if {@code stackingRule} is null */ public void setStackingRule(@NotNull StackingRule stackingRule) { - Check.notNull(stackingRule, "The stacking rule cannot be null!"); this.stackingRule = stackingRule; } diff --git a/src/main/java/net/minestom/server/item/firework/FireworkEffectType.java b/src/main/java/net/minestom/server/item/firework/FireworkEffectType.java index 1762934ad..598764b7b 100644 --- a/src/main/java/net/minestom/server/item/firework/FireworkEffectType.java +++ b/src/main/java/net/minestom/server/item/firework/FireworkEffectType.java @@ -1,7 +1,7 @@ package net.minestom.server.item.firework; -import java.util.HashMap; -import java.util.Map; +import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap; +import it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap; /** * An enumeration that representing all available firework types. @@ -15,7 +15,7 @@ public enum FireworkEffectType { BURST((byte) 4), ; - private static final Map BY_ID = new HashMap<>(); + private static final Byte2ObjectMap BY_ID = new Byte2ObjectOpenHashMap<>(); static { for (FireworkEffectType value : values()) { diff --git a/src/main/java/net/minestom/server/item/metadata/EnchantedBookMeta.java b/src/main/java/net/minestom/server/item/metadata/EnchantedBookMeta.java index 69c60258e..7f280c8b2 100644 --- a/src/main/java/net/minestom/server/item/metadata/EnchantedBookMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/EnchantedBookMeta.java @@ -1,17 +1,18 @@ package net.minestom.server.item.metadata; +import it.unimi.dsi.fastutil.objects.Object2ShortMap; +import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap; import net.minestom.server.item.Enchantment; import net.minestom.server.utils.NBTUtils; import org.jetbrains.annotations.NotNull; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.Collections; -import java.util.HashMap; import java.util.Map; public class EnchantedBookMeta extends ItemMeta { - private final Map storedEnchantmentMap = new HashMap<>(); + private final Object2ShortMap storedEnchantmentMap = new Object2ShortOpenHashMap<>(); /** * Gets the stored enchantment map. @@ -45,7 +46,7 @@ public class EnchantedBookMeta extends ItemMeta { * @param enchantment the enchantment type */ public void removeStoredEnchantment(@NotNull Enchantment enchantment) { - this.storedEnchantmentMap.remove(enchantment); + this.storedEnchantmentMap.removeShort(enchantment); } /** diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java index 685a2ad1c..86ddb4df5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java @@ -31,19 +31,19 @@ public class CombatEventPacket implements ServerPacket { return packet; } - public static CombatEventPacket end(int durationInTicks, Optional opponent) { + public static CombatEventPacket end(int durationInTicks, Entity opponent) { CombatEventPacket packet = new CombatEventPacket(); packet.type = EventType.END_COMBAT; packet.duration = durationInTicks; - packet.opponent = opponent.map(Entity::getEntityId).orElse(-1); + packet.opponent = opponent != null ? opponent.getEntityId() : -1; return packet; } - public static CombatEventPacket death(Player player, Optional killer, JsonMessage message) { + public static CombatEventPacket death(Player player, Entity killer, JsonMessage message) { CombatEventPacket packet = new CombatEventPacket(); packet.type = EventType.DEATH; packet.playerID = player.getEntityId(); - packet.opponent = killer.map(Entity::getEntityId).orElse(-1); + packet.opponent = killer != null ? killer.getEntityId() : -1; packet.deathMessage = message; return packet; } diff --git a/src/main/java/net/minestom/server/registry/ResourceGatherer.java b/src/main/java/net/minestom/server/registry/ResourceGatherer.java index c93165c0e..7af935d5b 100644 --- a/src/main/java/net/minestom/server/registry/ResourceGatherer.java +++ b/src/main/java/net/minestom/server/registry/ResourceGatherer.java @@ -4,6 +4,7 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import it.unimi.dsi.fastutil.io.FastBufferedInputStream; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; @@ -169,7 +170,7 @@ public class ResourceGatherer { private static File download(@NotNull String version, @NotNull String url, @NotNull String sha1Source) throws IOException { File target = new File(TMP_FOLDER, "server_" + version + ".jar"); // Download - try (BufferedInputStream in = new BufferedInputStream(new URL(url).openStream())) { + try (FastBufferedInputStream in = new FastBufferedInputStream(new URL(url).openStream())) { Files.copy(in, target.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw new IOException("Failed to download Minecraft server jar.", e); diff --git a/src/main/java/net/minestom/server/resourcepack/ResourcePack.java b/src/main/java/net/minestom/server/resourcepack/ResourcePack.java index 18c2f3faf..f0837c540 100644 --- a/src/main/java/net/minestom/server/resourcepack/ResourcePack.java +++ b/src/main/java/net/minestom/server/resourcepack/ResourcePack.java @@ -14,7 +14,6 @@ public class ResourcePack { private final String hash; public ResourcePack(@NotNull String url, @Nullable String hash) { - Check.notNull(url, "The resource pack url cannot be null"); this.url = url; // Optional, set to empty if null this.hash = hash == null ? "" : hash; diff --git a/src/main/java/net/minestom/server/storage/StorageManager.java b/src/main/java/net/minestom/server/storage/StorageManager.java index f5888a580..4e3bd718a 100644 --- a/src/main/java/net/minestom/server/storage/StorageManager.java +++ b/src/main/java/net/minestom/server/storage/StorageManager.java @@ -36,7 +36,6 @@ public final class StorageManager { * @return the specified {@link StorageLocation} */ public StorageLocation getLocation(@NotNull String location, @NotNull StorageOptions storageOptions, @NotNull StorageSystem storageSystem) { - Check.notNull(storageOptions, "The storage option cannot be null"); return locationMap.computeIfAbsent(location, s -> new StorageLocation(storageSystem, location, storageOptions)); } diff --git a/src/main/java/net/minestom/server/utils/entity/EntityFinder.java b/src/main/java/net/minestom/server/utils/entity/EntityFinder.java index e048ad979..2d8e34b44 100644 --- a/src/main/java/net/minestom/server/utils/entity/EntityFinder.java +++ b/src/main/java/net/minestom/server/utils/entity/EntityFinder.java @@ -281,20 +281,20 @@ public class EntityFinder { closestDistance = distance; } } - return Arrays.asList(entity); + return Collections.singletonList(entity); } else if (targetSelector == TargetSelector.RANDOM_PLAYER) { Collection instancePlayers = instance != null ? instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers(); final int index = ThreadLocalRandom.current().nextInt(instancePlayers.size()); final Player player = instancePlayers.stream().skip(index).findFirst().orElseThrow(); - return Arrays.asList(player); + return Collections.singletonList(player); } else if (targetSelector == TargetSelector.ALL_PLAYERS) { return new ArrayList<>(instance != null ? instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers()); } else if (targetSelector == TargetSelector.ALL_ENTITIES) { return new ArrayList<>(instance.getEntities()); } else if (targetSelector == TargetSelector.SELF) { - return Arrays.asList(self); + return Collections.singletonList(self); } throw new IllegalStateException("Weird thing happened"); } diff --git a/src/main/java/net/minestom/server/world/DimensionType.java b/src/main/java/net/minestom/server/world/DimensionType.java index 3906e4346..41293a67f 100644 --- a/src/main/java/net/minestom/server/world/DimensionType.java +++ b/src/main/java/net/minestom/server/world/DimensionType.java @@ -2,10 +2,10 @@ package net.minestom.server.world; import net.minestom.server.utils.NamespaceID; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.Objects; -import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; /** @@ -24,7 +24,7 @@ public class DimensionType { .raidCapable(true) .skylightEnabled(true) .ceilingEnabled(false) - .fixedTime(Optional.empty()) + .fixedTime(null) .ambientLight(0.0f) .logicalHeight(256) .infiniburn(NamespaceID.from("minecraft:infiniburn_overworld")) @@ -39,7 +39,10 @@ public class DimensionType { private final float ambientLight; private final boolean ceilingEnabled; private final boolean skylightEnabled; - private final Optional fixedTime; + + @Nullable + private final Long fixedTime; + private final boolean raidCapable; private final boolean respawnAnchorSafe; private final boolean ultrawarm; @@ -51,7 +54,7 @@ public class DimensionType { private final NamespaceID infiniburn; DimensionType(NamespaceID name, boolean natural, float ambientLight, boolean ceilingEnabled, - boolean skylightEnabled, Optional fixedTime, boolean raidCapable, + boolean skylightEnabled, @Nullable Long fixedTime, boolean raidCapable, boolean respawnAnchorSafe, boolean ultrawarm, boolean bedSafe, String effects, boolean piglinSafe, int logicalHeight, int coordinateScale, NamespaceID infiniburn) { this.name = name; @@ -106,7 +109,7 @@ public class DimensionType { .setInt("logical_height", logicalHeight) .setInt("coordinate_scale", coordinateScale) .setString("name", name.toString()); - fixedTime.ifPresent(time -> nbt.setLong("fixed_time", time)); + if (fixedTime != null) nbt.setLong("fixed_time", fixedTime); return nbt; } @@ -143,7 +146,8 @@ public class DimensionType { return this.skylightEnabled; } - public Optional getFixedTime() { + @Nullable + public Long getFixedTime() { return this.fixedTime; } @@ -203,7 +207,9 @@ public class DimensionType { private float ambientLight; private boolean ceilingEnabled; private boolean skylightEnabled; - private Optional fixedTime = Optional.empty(); + + @Nullable + private Long fixedTime = null; private boolean raidCapable; private boolean respawnAnchorSafe; private boolean ultrawarm; @@ -242,7 +248,7 @@ public class DimensionType { return this; } - public DimensionType.DimensionTypeBuilder fixedTime(Optional fixedTime) { + public DimensionType.DimensionTypeBuilder fixedTime(Long fixedTime) { this.fixedTime = fixedTime; return this; } diff --git a/src/test/java/demo/commands/DimensionCommand.java b/src/test/java/demo/commands/DimensionCommand.java index bfbf0667b..285fba90f 100644 --- a/src/test/java/demo/commands/DimensionCommand.java +++ b/src/test/java/demo/commands/DimensionCommand.java @@ -36,8 +36,7 @@ public class DimensionCommand implements CommandProcessor { // targetDimensionType = DimensionType.OVERWORLD; //} - DimensionType finalTargetDimensionType = targetDimensionType; - Optional targetInstance = MinecraftServer.getInstanceManager().getInstances().stream().filter(in -> in.getDimensionType() == finalTargetDimensionType).findFirst(); + Optional targetInstance = MinecraftServer.getInstanceManager().getInstances().stream().filter(in -> in.getDimensionType() == targetDimensionType).findFirst(); if (targetInstance.isPresent()) { player.sendMessage("You were in " + instance.getDimensionType()); player.setInstance(targetInstance.get()); diff --git a/src/test/java/demo/commands/ReloadExtensionCommand.java b/src/test/java/demo/commands/ReloadExtensionCommand.java index a7f21874b..305cba1bd 100644 --- a/src/test/java/demo/commands/ReloadExtensionCommand.java +++ b/src/test/java/demo/commands/ReloadExtensionCommand.java @@ -25,11 +25,10 @@ public class ReloadExtensionCommand extends Command { private static String[] extensionsName; static { - List extensionsName = MinecraftServer.getExtensionManager().getExtensions() + ReloadExtensionCommand.extensionsName = MinecraftServer.getExtensionManager().getExtensions() .stream() .map(extension -> extension.getDescription().getName()) - .collect(Collectors.toList()); - ReloadExtensionCommand.extensionsName = extensionsName.toArray(new String[0]); + .toArray(String[]::new); } public ReloadExtensionCommand() {