From af1f976e531c58ae01f858fba09dc9995dfddba2 Mon Sep 17 00:00:00 2001 From: themode Date: Sun, 11 Oct 2020 18:57:24 +0200 Subject: [PATCH] Comments --- .../java/net/minestom/server/Viewable.java | 2 +- .../notifications/Notification.java | 2 +- .../notifications/NotificationCenter.java | 6 ++- .../java/net/minestom/server/data/Data.java | 2 +- .../net/minestom/server/data/DataManager.java | 2 +- .../net/minestom/server/data/DataType.java | 8 ++++ .../server/data/SerializableDataImpl.java | 4 +- .../net/minestom/server/entity/Player.java | 5 +-- .../server/inventory/EquipmentHandler.java | 2 +- .../minestom/server/inventory/Inventory.java | 29 +++++------- .../inventory/InventoryClickHandler.java | 12 ++--- .../server/inventory/InventoryType.java | 3 ++ .../server/inventory/PlayerInventory.java | 16 +++---- .../server/permission/BasicPermission.java | 2 +- .../minestom/server/scoreboard/Sidebar.java | 45 +++++++++++-------- .../server/storage/StorageLocation.java | 10 ++--- .../server/storage/StorageManager.java | 5 ++- 17 files changed, 85 insertions(+), 70 deletions(-) diff --git a/src/main/java/net/minestom/server/Viewable.java b/src/main/java/net/minestom/server/Viewable.java index 7fe2c0fe8..5acb8a477 100644 --- a/src/main/java/net/minestom/server/Viewable.java +++ b/src/main/java/net/minestom/server/Viewable.java @@ -8,7 +8,7 @@ import java.util.HashSet; import java.util.Set; /** - * Represent something in the instance which can be showed or hidden to players + * Represent something which can be displayed or hidden to players */ public interface Viewable { diff --git a/src/main/java/net/minestom/server/advancements/notifications/Notification.java b/src/main/java/net/minestom/server/advancements/notifications/Notification.java index c5204617d..8a5c664f1 100644 --- a/src/main/java/net/minestom/server/advancements/notifications/Notification.java +++ b/src/main/java/net/minestom/server/advancements/notifications/Notification.java @@ -38,7 +38,7 @@ public class Notification { } /** - * Get the frame type of the notification + * Get the {@link FrameType} of the notification * * @return the notification frame type */ diff --git a/src/main/java/net/minestom/server/advancements/notifications/NotificationCenter.java b/src/main/java/net/minestom/server/advancements/notifications/NotificationCenter.java index 2bfa47355..03558cd82 100644 --- a/src/main/java/net/minestom/server/advancements/notifications/NotificationCenter.java +++ b/src/main/java/net/minestom/server/advancements/notifications/NotificationCenter.java @@ -10,7 +10,11 @@ import java.sql.Date; import java.util.Collection; /** - * Used to send one or multiples {@link Notification} + * Used to send one or multiples {@link Notification}. + *

+ * Works by sending a completed advancement and remove it immediately. + *

+ * You can simply create a {@link Notification} object and call {@link #send(Notification, Player)}. */ public class NotificationCenter { diff --git a/src/main/java/net/minestom/server/data/Data.java b/src/main/java/net/minestom/server/data/Data.java index 53a7cca2c..0fb085b8b 100644 --- a/src/main/java/net/minestom/server/data/Data.java +++ b/src/main/java/net/minestom/server/data/Data.java @@ -25,7 +25,7 @@ public interface Data { @Override public Set getKeys() { - return Collections.EMPTY_SET; + return Collections.emptySet(); } @Override diff --git a/src/main/java/net/minestom/server/data/DataManager.java b/src/main/java/net/minestom/server/data/DataManager.java index fd8b3d8ec..69ab111c4 100644 --- a/src/main/java/net/minestom/server/data/DataManager.java +++ b/src/main/java/net/minestom/server/data/DataManager.java @@ -14,7 +14,7 @@ import java.util.UUID; public final class DataManager { - private Map dataTypeMap = new HashMap<>(); + private final Map dataTypeMap = new HashMap<>(); { registerType(Byte.class, new ByteData()); diff --git a/src/main/java/net/minestom/server/data/DataType.java b/src/main/java/net/minestom/server/data/DataType.java index b4c1897ef..6b15f52ec 100644 --- a/src/main/java/net/minestom/server/data/DataType.java +++ b/src/main/java/net/minestom/server/data/DataType.java @@ -3,6 +3,14 @@ package net.minestom.server.data; import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryWriter; +/** + * Represents an object which can be encoded and decoded back. + *

+ * Used by {@link DataManager} for {@link SerializableDataImpl} + * and by the storage API in {@link net.minestom.server.storage.StorageLocation}. + * + * @param the type of the object + */ public abstract class DataType { /** diff --git a/src/main/java/net/minestom/server/data/SerializableDataImpl.java b/src/main/java/net/minestom/server/data/SerializableDataImpl.java index 61e1a18b3..8c7213012 100644 --- a/src/main/java/net/minestom/server/data/SerializableDataImpl.java +++ b/src/main/java/net/minestom/server/data/SerializableDataImpl.java @@ -19,13 +19,13 @@ public class SerializableDataImpl extends DataImpl implements SerializableData { * Class name -> Class * Used to cache class instances so we don't load them by name every time */ - private static ConcurrentHashMap nameToClassMap = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap nameToClassMap = new ConcurrentHashMap<>(); /** * Data key -> Class * Used to know the type of an element of this data object (for serialization purpose) */ - private ConcurrentHashMap dataType = new ConcurrentHashMap<>(); + private final ConcurrentHashMap dataType = new ConcurrentHashMap<>(); /** * Set a value to a specific key diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 449c1bbfb..70acd58dc 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -469,9 +469,8 @@ public class Player extends LivingEntity implements CommandSender { ColoredText.of(getUsername() + " was killed by poor programming."); chatMessage = RichMessage.of(coloredChatMessage); } - MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> { - player.sendMessage(chatMessage); - }); + MinecraftServer.getConnectionManager().getOnlinePlayers() + .forEach(player -> player.sendMessage(chatMessage)); } super.kill(); } diff --git a/src/main/java/net/minestom/server/inventory/EquipmentHandler.java b/src/main/java/net/minestom/server/inventory/EquipmentHandler.java index 6d1108516..9f9b0434b 100644 --- a/src/main/java/net/minestom/server/inventory/EquipmentHandler.java +++ b/src/main/java/net/minestom/server/inventory/EquipmentHandler.java @@ -46,7 +46,7 @@ public interface EquipmentHandler { /** * Get the {@link ItemStack} in the specific hand * - * @param hand the hand to get the {@link ItemStack} from + * @param hand the Hand to get the {@link ItemStack} from * @return the {@link ItemStack} in {@code hand} */ default ItemStack getItemInHand(Player.Hand hand) { diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index 08aaffce9..254073f34 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -32,7 +32,7 @@ import java.util.concurrent.atomic.AtomicInteger; public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable { // incremented each time an inventory is created (used in the window packets) - private static AtomicInteger lastInventoryId = new AtomicInteger(); + private static final AtomicInteger LAST_INVENTORY_ID = new AtomicInteger(); // the id of this inventory private final byte id; @@ -73,7 +73,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View } private static byte generateId() { - byte newInventoryId = (byte) lastInventoryId.incrementAndGet(); + byte newInventoryId = (byte) LAST_INVENTORY_ID.incrementAndGet(); if (newInventoryId == Byte.MAX_VALUE) newInventoryId = 1; return newInventoryId; @@ -366,11 +366,10 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View if (isInWindow) { setItemStack(slot, clickResult.getClicked()); - setCursorPlayerItem(player, clickResult.getCursor()); } else { playerInventory.setItemStack(slot, offset, clickResult.getClicked()); - setCursorPlayerItem(player, clickResult.getCursor()); } + setCursorPlayerItem(player, clickResult.getCursor()); if (!clickResult.isCancel()) callClickEvent(player, this, slot, ClickType.LEFT_CLICK, clicked, cursor); @@ -393,11 +392,10 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View if (isInWindow) { setItemStack(slot, clickResult.getClicked()); - setCursorPlayerItem(player, clickResult.getCursor()); } else { playerInventory.setItemStack(slot, offset, clickResult.getClicked()); - setCursorPlayerItem(player, clickResult.getCursor()); } + setCursorPlayerItem(player, clickResult.getCursor()); if (!clickResult.isCancel()) callClickEvent(player, this, slot, ClickType.RIGHT_CLICK, clicked, cursor); @@ -419,7 +417,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View clickResult = clickProcessor.shiftClick(this, player, slot, clicked, cursor, // Player inventory loop new InventoryClickLoopHandler(0, PlayerInventory.INVENTORY_SIZE, 1, - i -> PlayerInventoryUtils.convertToPacketSlot(i), + PlayerInventoryUtils::convertToPacketSlot, index -> isClickInWindow(index) ? getItemStack(index) : playerInventory.getItemStack(index, offset), (index, itemStack) -> { if (isClickInWindow(index)) { @@ -511,12 +509,11 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View if (isInWindow) { if (resultClicked != null) setItemStack(slot, resultClicked); - setCursorPlayerItem(player, clickResult.getCursor()); } else { if (resultClicked != null) playerInventory.setItemStack(slot, offset, resultClicked); - setCursorPlayerItem(player, clickResult.getCursor()); } + setCursorPlayerItem(player, clickResult.getCursor()); return !clickResult.isCancel(); } @@ -567,20 +564,18 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View // Start by looping through the opened inventory new InventoryClickLoopHandler(0, getSize(), 1, i -> i, - index -> getItemStack(index), - (index, itemStack) -> setItemStack(index, itemStack)), + this::getItemStack, + this::setItemStack), // Looping through player inventory new InventoryClickLoopHandler(0, PlayerInventory.INVENTORY_SIZE - 9, 1, - i -> PlayerInventoryUtils.convertToPacketSlot(i), + PlayerInventoryUtils::convertToPacketSlot, index -> playerInventory.getItemStack(index, offset), (index, itemStack) -> playerInventory.setItemStack(index, offset, itemStack)), - // Player hotbar + // Player hot bar new InventoryClickLoopHandler(0, 9, 1, - i -> PlayerInventoryUtils.convertToPacketSlot(i), + PlayerInventoryUtils::convertToPacketSlot, index -> playerInventory.getItemStack(index, offset), - (index, itemStack) -> { - playerInventory.setItemStack(index, offset, itemStack); - })); + (index, itemStack) -> playerInventory.setItemStack(index, offset, itemStack))); if (clickResult == null) return false; diff --git a/src/main/java/net/minestom/server/inventory/InventoryClickHandler.java b/src/main/java/net/minestom/server/inventory/InventoryClickHandler.java index 06a50b3f4..83830db5a 100644 --- a/src/main/java/net/minestom/server/inventory/InventoryClickHandler.java +++ b/src/main/java/net/minestom/server/inventory/InventoryClickHandler.java @@ -14,7 +14,7 @@ import net.minestom.server.item.ItemStack; public interface InventoryClickHandler { /** - * Called when a player left click in the inventory. Can also be to drop the cursor item + * Called when a {@link Player} left click in the inventory. Can also be to drop the cursor item * * @param player the player who clicked * @param slot the slot number @@ -23,7 +23,7 @@ public interface InventoryClickHandler { boolean leftClick(Player player, int slot); /** - * Called when a player right click in the inventory. Can also be to drop the cursor item + * Called when a {@link Player} right click in the inventory. Can also be to drop the cursor item * * @param player the player who clicked * @param slot the slot number @@ -32,7 +32,7 @@ public interface InventoryClickHandler { boolean rightClick(Player player, int slot); /** - * Called when a player shift click in the inventory + * Called when a {@link Player} shift click in the inventory * * @param player the player who clicked * @param slot the slot number @@ -41,7 +41,7 @@ public interface InventoryClickHandler { boolean shiftClick(Player player, int slot); // shift + left/right click have the same behavior /** - * Called when a player held click in the inventory + * Called when a {@link Player} held click in the inventory * * @param player the player who clicked * @param slot the slot number @@ -53,7 +53,7 @@ public interface InventoryClickHandler { boolean middleClick(Player player, int slot); /** - * Called when a player press the drop button + * Called when a {@link Player} press the drop button * * @param player the player who clicked * @param mode @@ -66,7 +66,7 @@ public interface InventoryClickHandler { boolean dragging(Player player, int slot, int button); /** - * Called when a player double click in the inventory + * Called when a {@link Player} double click in the inventory * * @param player the player who clicked * @param slot the slot number diff --git a/src/main/java/net/minestom/server/inventory/InventoryType.java b/src/main/java/net/minestom/server/inventory/InventoryType.java index fe4c4125d..c407a7547 100644 --- a/src/main/java/net/minestom/server/inventory/InventoryType.java +++ b/src/main/java/net/minestom/server/inventory/InventoryType.java @@ -1,5 +1,8 @@ package net.minestom.server.inventory; +/** + * Represents a type of {@link Inventory} + */ public enum InventoryType { CHEST_1_ROW(0, 9), diff --git a/src/main/java/net/minestom/server/inventory/PlayerInventory.java b/src/main/java/net/minestom/server/inventory/PlayerInventory.java index 04376a617..28b216e43 100644 --- a/src/main/java/net/minestom/server/inventory/PlayerInventory.java +++ b/src/main/java/net/minestom/server/inventory/PlayerInventory.java @@ -27,18 +27,18 @@ import java.util.concurrent.CopyOnWriteArrayList; import static net.minestom.server.utils.inventory.PlayerInventoryUtils.*; /** - * Represents the inventory of a {@link Player} + * Represents the inventory of a {@link Player}. */ public class PlayerInventory implements InventoryModifier, InventoryClickHandler, EquipmentHandler { public static final int INVENTORY_SIZE = 46; - private Player player; - private ItemStack[] items = new ItemStack[INVENTORY_SIZE]; + private final Player player; + private final ItemStack[] items = new ItemStack[INVENTORY_SIZE]; private ItemStack cursorItem = ItemStack.getAirItem(); - private List inventoryConditions = new CopyOnWriteArrayList<>(); - private InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); + private final List inventoryConditions = new CopyOnWriteArrayList<>(); + private final InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); public PlayerInventory(Player player) { this.player = player; @@ -425,11 +425,11 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler final ItemStack cursor = getCursorItem(); final ItemStack clicked = getItemStack(slot, OFFSET); - final boolean hotbarClick = convertToPacketSlot(slot) < 9; + final boolean hotBarClick = convertToPacketSlot(slot) < 9; final InventoryClickResult clickResult = clickProcessor.shiftClick(null, player, slot, clicked, cursor, new InventoryClickLoopHandler(0, items.length, 1, i -> { - if (hotbarClick) { + if (hotBarClick) { return i < 9 ? i + 9 : i - 9; } else { return convertSlot(i, OFFSET); @@ -507,7 +507,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler new InventoryClickLoopHandler(0, items.length, 1, i -> i < 9 ? i + 9 : i - 9, index -> items[index], - (index, itemStack) -> setItemStack(index, itemStack))); + this::setItemStack)); if (clickResult == null) return false; diff --git a/src/main/java/net/minestom/server/permission/BasicPermission.java b/src/main/java/net/minestom/server/permission/BasicPermission.java index c79919723..168f74fa4 100644 --- a/src/main/java/net/minestom/server/permission/BasicPermission.java +++ b/src/main/java/net/minestom/server/permission/BasicPermission.java @@ -3,7 +3,7 @@ package net.minestom.server.permission; import net.minestom.server.command.CommandSender; /** - * Basic Permission implementation that only requires the permission to be given to a player to be considered applied + * Basic {@link Permission} implementation that only requires the permission to be given to the {@link CommandSender} to be considered applied * (eg. no arguments) */ public class BasicPermission implements Permission { diff --git a/src/main/java/net/minestom/server/scoreboard/Sidebar.java b/src/main/java/net/minestom/server/scoreboard/Sidebar.java index 27fdc53f6..8d9189f8a 100644 --- a/src/main/java/net/minestom/server/scoreboard/Sidebar.java +++ b/src/main/java/net/minestom/server/scoreboard/Sidebar.java @@ -18,7 +18,14 @@ import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.atomic.AtomicInteger; /** - * Represents a sidebar which can contain up to 16 {@link ScoreboardLine}'s + * Represents a sidebar which can contain up to 16 {@link ScoreboardLine}. + *

+ * In order to use it you need to create a new instance using {@link #Sidebar(String)} and create new lines + * with {@link #createLine(ScoreboardLine)}. You can then add a {@link Player} to the viewing list using {@link #addViewer(Player)} + * and remove him later with {@link #removeViewer(Player)}. + *

+ * Lines can be modified using their respective identifier using + * {@link #updateLineContent(String, ColoredText)} and {@link #updateLineScore(String, int)}. */ public class Sidebar implements Scoreboard { @@ -106,10 +113,10 @@ public class Sidebar implements Scoreboard { } /** - * Updates a line content through the given identifier + * Updates a {@link ScoreboardLine} content through the given identifier * - * @param id The identifier of the line - * @param content The new content for the line + * @param id The identifier of the {@link ScoreboardLine} + * @param content The new content for the {@link ScoreboardLine} */ public void updateLineContent(String id, ColoredText content) { final ScoreboardLine scoreboardLine = getLine(id); @@ -120,10 +127,10 @@ public class Sidebar implements Scoreboard { } /** - * Updates the score of a line through the given identifier + * Updates the score of a {@link ScoreboardLine} through the given identifier * * @param id The identifier of the team - * @param score The new score for the line + * @param score The new score for the {@link ScoreboardLine} */ public void updateLineScore(String id, int score) { final ScoreboardLine scoreboardLine = getLine(id); @@ -150,7 +157,7 @@ public class Sidebar implements Scoreboard { /** * Removes a {@link ScoreboardLine} through the given identifier * - * @param id The identifier of the line + * @param id the identifier of the {@link ScoreboardLine} */ public void removeLine(String id) { synchronized (lines) { @@ -189,7 +196,7 @@ public class Sidebar implements Scoreboard { @Override public boolean removeViewer(Player player) { - boolean result = this.viewers.remove(player); + final boolean result = this.viewers.remove(player); PlayerConnection playerConnection = player.getPlayerConnection(); ScoreboardObjectivePacket scoreboardObjectivePacket = this.getDestructionObjectivePacket(); playerConnection.sendPacket(scoreboardObjectivePacket); @@ -219,17 +226,17 @@ public class Sidebar implements Scoreboard { /** * The identifier is used to modify the line later */ - private String id; + private final String id; /** * The content for the line */ - private ColoredText content; + private final ColoredText content; /** * The score of the line */ private int line; - private String teamName; + private final String teamName; /** * The name of the score ({@code entityName}) which is essentially an identifier */ @@ -350,19 +357,19 @@ public class Sidebar implements Scoreboard { } /** - * This class is used to create a team for the sidebar + * This class is used to create a team for the {@link Sidebar} */ private static class SidebarTeam { - private String teamName; + private final String teamName; private ColoredText prefix, suffix; - private String entityName; + private final String entityName; - private ColoredText teamDisplayName = ColoredText.of("displaynametest"); - private byte friendlyFlags = 0x00; - private TeamsPacket.NameTagVisibility nameTagVisibility = TeamsPacket.NameTagVisibility.NEVER; - private TeamsPacket.CollisionRule collisionRule = TeamsPacket.CollisionRule.NEVER; - private int teamColor = 2; + private final ColoredText teamDisplayName = ColoredText.of("displaynametest"); + private final byte friendlyFlags = 0x00; + private final TeamsPacket.NameTagVisibility nameTagVisibility = TeamsPacket.NameTagVisibility.NEVER; + private final TeamsPacket.CollisionRule collisionRule = TeamsPacket.CollisionRule.NEVER; + private final int teamColor = 2; /** diff --git a/src/main/java/net/minestom/server/storage/StorageLocation.java b/src/main/java/net/minestom/server/storage/StorageLocation.java index 9ce25f6fb..52eb9c72b 100644 --- a/src/main/java/net/minestom/server/storage/StorageLocation.java +++ b/src/main/java/net/minestom/server/storage/StorageLocation.java @@ -178,7 +178,7 @@ public class StorageLocation { } /** - * Save a specified cached data and remove it from memory + * Save a specified cached {@link SerializableData} and remove it from memory * * @param key the specified cached data key */ @@ -197,18 +197,16 @@ public class StorageLocation { } /** - * Save the whole cached data + * Save the all the cached {@link SerializableData} */ public void saveCachedData() { synchronized (cachedData) { - cachedData.forEach((key, data) -> { - set(key, data.getIndexedSerializedData()); - }); + cachedData.forEach((key, data) -> set(key, data.getIndexedSerializedData())); } } /** - * Save an unique cached data + * Save an unique cached {@link SerializableData} * * @param key the data key */ diff --git a/src/main/java/net/minestom/server/storage/StorageManager.java b/src/main/java/net/minestom/server/storage/StorageManager.java index 3abaf9442..9b0b448c0 100644 --- a/src/main/java/net/minestom/server/storage/StorageManager.java +++ b/src/main/java/net/minestom/server/storage/StorageManager.java @@ -10,13 +10,14 @@ import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; -public class StorageManager { +public final class StorageManager { + private static final Logger LOGGER = LoggerFactory.getLogger(StorageManager.class); private Supplier defaultStorageSystemSupplier = null; // Location -> storage location object - private Map locationMap = new HashMap<>(); + private final Map locationMap = new HashMap<>(); /** * Used to get an access to the specified location