From b5ae6e65a138b34f102a39acb8212e8682e3dc9f Mon Sep 17 00:00:00 2001 From: Craftinators Date: Fri, 24 Feb 2023 17:46:39 -0500 Subject: [PATCH] clean: annotation & variable modifier changes --- .../hmccosmetics/util/InventoryUtils.java | 10 +++- .../hmccosmetics/util/MessagesUtil.java | 12 +++-- .../hmccosmetics/util/PlayerUtils.java | 9 +++- .../util/builder/ColorBuilder.java | 3 +- .../util/builder/ItemBuilder.java | 9 +++- .../hmccosmetics/util/misc/Keys.java | 11 ++-- .../hmccosmetics/util/misc/StringUtils.java | 5 +- .../hmccosmetics/util/misc/Utils.java | 2 +- .../util/packets/PacketManager.java | 52 ++++++++++--------- .../WrapperPlayServerNamedEntitySpawn.java | 5 +- .../WrapperPlayServerRelEntityMove.java | 3 +- 11 files changed, 77 insertions(+), 44 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/InventoryUtils.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/InventoryUtils.java index 7d8cb00e..c4ce0717 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/InventoryUtils.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/InventoryUtils.java @@ -3,6 +3,8 @@ package com.hibiscusmc.hmccosmetics.util; import com.comphenix.protocol.wrappers.EnumWrappers; import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot; import org.bukkit.inventory.EquipmentSlot; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public class InventoryUtils { @@ -58,6 +60,8 @@ public class InventoryUtils { }; } + @Contract(pure = true) + @Nullable public static CosmeticSlot BukkitCosmeticSlot(int slot) { switch (slot) { case 36 -> { @@ -81,6 +85,8 @@ public class InventoryUtils { } } + @Contract(pure = true) + @Nullable public static CosmeticSlot NMSCosmeticSlot(int slot) { switch (slot) { case 5 -> { @@ -104,7 +110,9 @@ public class InventoryUtils { } } - public static EquipmentSlot getEquipmentSlot(CosmeticSlot slot) { + @Contract(pure = true) + @Nullable + public static EquipmentSlot getEquipmentSlot(@NotNull CosmeticSlot slot) { switch (slot) { case HELMET -> { return EquipmentSlot.HEAD; diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/MessagesUtil.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/MessagesUtil.java index b6413093..8d295195 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/MessagesUtil.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/MessagesUtil.java @@ -15,6 +15,8 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.title.Title; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.spongepowered.configurate.ConfigurationNode; import java.time.Duration; @@ -24,9 +26,9 @@ import java.util.logging.Level; public class MessagesUtil { private static String prefix; - private static HashMap messages = new HashMap<>(); + private static final HashMap messages = new HashMap<>(); - public static void setup(ConfigurationNode config) { + public static void setup(@NotNull ConfigurationNode config) { prefix = config.node("prefix").getString(""); for (ConfigurationNode node : config.childrenMap().values()) { if (node.virtual()) continue; @@ -35,7 +37,7 @@ public class MessagesUtil { } } - public static void sendMessage(CosmeticUser user, String key) { + public static void sendMessage(@NotNull CosmeticUser user, String key) { sendMessage(user.getPlayer(), key); } @@ -92,6 +94,7 @@ public class MessagesUtil { return processString(player, key, null); } + @Nullable public static Component processString(Player player, String key, TagResolver placeholders) { if (!messages.containsKey(key)) return null; if (messages.get(key) == null) return null; @@ -104,14 +107,17 @@ public class MessagesUtil { return Adventure.MINI_MESSAGE.deserialize(message); } + @NotNull public static Component processStringNoKey(String message) { return processStringNoKey(null, message, null); } + @NotNull public static Component processStringNoKey(Player player, String message) { return processStringNoKey(player, message, null); } + @NotNull public static Component processStringNoKey(Player player, String message, TagResolver placeholders) { message = message.replaceAll("%prefix%", prefix); if (Hooks.isActiveHook("PlaceholderAPI") && player != null) message = PlaceholderAPI.setPlaceholders(player, message); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/PlayerUtils.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/PlayerUtils.java index d3b7e791..739a0a66 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/PlayerUtils.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/PlayerUtils.java @@ -6,12 +6,15 @@ import com.hibiscusmc.hmccosmetics.config.Settings; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; public class PlayerUtils { + @Nullable public static WrappedSignedProperty getSkin(Player player) { WrappedSignedProperty skinData = WrappedGameProfile.fromPlayer(player).getProperties() .get("textures").stream().findAny().orElse(null); @@ -22,11 +25,13 @@ public class PlayerUtils { return new WrappedSignedProperty("textures", skinData.getValue(), skinData.getSignature()); } - public static List getNearbyPlayers(Player player) { + @NotNull + public static List getNearbyPlayers(@NotNull Player player) { return getNearbyPlayers(player.getLocation()); } - public static List getNearbyPlayers(Location location) { + @NotNull + public static List getNearbyPlayers(@NotNull Location location) { List players = new ArrayList<>(); int viewDistance = Settings.getViewDistance(); for (Entity entity : location.getWorld().getNearbyEntities(location, viewDistance, viewDistance, viewDistance)) { diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ColorBuilder.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ColorBuilder.java index 623a17a9..61dff661 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ColorBuilder.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ColorBuilder.java @@ -6,6 +6,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.inventory.meta.PotionMeta; +import org.jetbrains.annotations.NotNull; public class ColorBuilder { @@ -13,7 +14,7 @@ public class ColorBuilder { return canBeColored(new ItemStack(material)); } - public static boolean canBeColored(final ItemStack itemStack) { + public static boolean canBeColored(final @NotNull ItemStack itemStack) { final ItemMeta itemMeta = itemStack.getItemMeta(); return (itemMeta instanceof LeatherArmorMeta || diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ItemBuilder.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ItemBuilder.java index 20bcb307..11d59364 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ItemBuilder.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/builder/ItemBuilder.java @@ -8,6 +8,8 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; @@ -33,7 +35,7 @@ public class ItemBuilder { * @param itemStack builder ItemStack */ - ItemBuilder(final ItemStack itemStack) { + ItemBuilder(final @NotNull ItemStack itemStack) { this.material = itemStack.getType(); this.itemMeta = itemStack.hasItemMeta() ? itemStack.getItemMeta() : Bukkit.getItemFactory().getItemMeta(this.material); @@ -44,6 +46,8 @@ public class ItemBuilder { * @return */ + @Contract("_ -> new") + @NotNull public static ItemBuilder from(final Material material) { return new ItemBuilder(material); } @@ -52,7 +56,8 @@ public class ItemBuilder { * @param itemStack builder ItemStack * @return */ - + @Contract("_ -> new") + @NotNull public static ItemBuilder from(final ItemStack itemStack) { return new ItemBuilder(itemStack); } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Keys.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Keys.java index 0539c0ab..d42b8653 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Keys.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Keys.java @@ -6,6 +6,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataType; import org.checkerframework.checker.nullness.qual.Nullable; +import org.jetbrains.annotations.NotNull; public class Keys { @@ -13,7 +14,7 @@ public class Keys { public static final NamespacedKey ITEM_KEY = new NamespacedKey(plugin, "cosmetic"); public static final NamespacedKey TOKEN_KEY = new NamespacedKey(plugin, "token-key"); - public static void setKey(final ItemStack itemStack) { + public static void setKey(final @NotNull ItemStack itemStack) { final ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta == null) { @@ -26,7 +27,7 @@ public class Keys { } public static void setKey( - final ItemStack itemStack, + final @NotNull ItemStack itemStack, final NamespacedKey key, final PersistentDataType type, final Z value) { @@ -41,7 +42,7 @@ public class Keys { itemStack.setItemMeta(itemMeta); } - public static boolean hasKey(final ItemStack itemStack) { + public static boolean hasKey(final @NotNull ItemStack itemStack) { final ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta == null) { @@ -51,7 +52,7 @@ public class Keys { return itemMeta.getPersistentDataContainer().has(ITEM_KEY, PersistentDataType.BYTE); } - public static boolean hasKey(final ItemStack itemStack, final NamespacedKey key, final PersistentDataType type) { + public static boolean hasKey(final @NotNull ItemStack itemStack, final NamespacedKey key, final PersistentDataType type) { final ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta == null) { @@ -62,7 +63,7 @@ public class Keys { } @Nullable - public static Z getValue(final ItemStack itemStack, final NamespacedKey key, final PersistentDataType type) { + public static Z getValue(final @NotNull ItemStack itemStack, final NamespacedKey key, final PersistentDataType type) { final ItemMeta itemMeta = itemStack.getItemMeta(); if (itemMeta == null) { diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/StringUtils.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/StringUtils.java index 9531f734..090186fe 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/StringUtils.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/StringUtils.java @@ -1,6 +1,7 @@ package com.hibiscusmc.hmccosmetics.util.misc; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.NotNull; public class StringUtils { @@ -8,15 +9,17 @@ public class StringUtils { * @param parsed message to be parsed * @return MiniMessage parsed string */ - + @NotNull public static Component parse(final String parsed) { return Adventure.MINI_MESSAGE.deserialize(parsed); } + @NotNull public static String parseStringToString(final String parsed) { return Adventure.SERIALIZER.serialize(Adventure.MINI_MESSAGE.deserialize(parsed)); } + @NotNull public static String formatArmorItemType(String type) { type = type.toLowerCase(); final String[] parts = type.split(" "); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Utils.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Utils.java index aff64bb1..38309bcc 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Utils.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/misc/Utils.java @@ -29,7 +29,7 @@ public class Utils { return original; } - public static T replaceIf(final @Nullable T original, final T replacement, final Predicate predicate) { + public static T replaceIf(final @Nullable T original, final T replacement, final @NotNull Predicate predicate) { if (predicate.test(original)) return replacement; return original; } diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/PacketManager.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/PacketManager.java index 64148865..dbac7bfa 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/PacketManager.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/PacketManager.java @@ -22,6 +22,7 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Collections; @@ -31,11 +32,11 @@ import java.util.UUID; public class PacketManager extends BasePacket { public static void sendEntitySpawnPacket( - final Location location, + final @NotNull Location location, final int entityId, final EntityType entityType, final UUID uuid, - final List sendTo + final @NotNull List sendTo ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY); packet.getModifier().writeDefaults(); @@ -64,7 +65,7 @@ public class PacketManager extends BasePacket { public static void ridingMountPacket( int mountId, int passengerId, - List sendTo + @NotNull List sendTo ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.MOUNT); packet.getIntegers().write(0, mountId); @@ -84,7 +85,7 @@ public class PacketManager extends BasePacket { } } public static void equipmentSlotUpdate( - Player player, + @NotNull Player player, CosmeticSlot cosmetic, List sendTo ) { @@ -111,7 +112,7 @@ public class PacketManager extends BasePacket { } public static void armorStandMetaPacket( - Entity entity, + @NotNull Entity entity, List sendTo ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_METADATA); @@ -147,9 +148,9 @@ public class PacketManager extends BasePacket { } public static void sendLookPacket( - int entityId, - Location location, - List sendTo + int entityId, + @NotNull Location location, + @NotNull List sendTo ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_HEAD_ROTATION); packet.getIntegers().write(0, entityId); @@ -166,10 +167,10 @@ public class PacketManager extends BasePacket { } public static void sendRotationPacket( - int entityId, - Location location, - boolean onGround, - List sendTo + int entityId, + @NotNull Location location, + boolean onGround, + @NotNull List sendTo ) { float ROTATION_FACTOR = 256.0F / 360.0F; float yaw = location.getYaw() * ROTATION_FACTOR; @@ -188,7 +189,7 @@ public class PacketManager extends BasePacket { int entityId, int yaw, boolean onGround, - List sendTo + @NotNull List sendTo ) { float ROTATION_FACTOR = 256.0F / 360.0F; float yaw2 = yaw * ROTATION_FACTOR; @@ -225,7 +226,7 @@ public class PacketManager extends BasePacket { public static void sendRidingPacket( final int mountId, final int passengerId, - final List sendTo + final @NotNull List sendTo ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.MOUNT); packet.getIntegers().write(0, mountId); @@ -240,7 +241,7 @@ public class PacketManager extends BasePacket { * @param entityId The entity to delete for a player * @param sendTo The players the packet should be sent to */ - public static void sendEntityDestroyPacket(final int entityId, List sendTo) { + public static void sendEntityDestroyPacket(final int entityId, @NotNull List sendTo) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_DESTROY); packet.getModifier().write(0, new IntArrayList(new int[]{entityId})); for (final Player p : sendTo) sendPacket(p, packet); @@ -251,7 +252,7 @@ public class PacketManager extends BasePacket { * @param entityId The Entity ID that camera will go towards * @param sendTo The players that will be sent this packet */ - public static void sendCameraPacket(final int entityId, List sendTo) { + public static void sendCameraPacket(final int entityId, @NotNull List sendTo) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.CAMERA); packet.getIntegers().write(0, entityId); for (final Player p : sendTo) sendPacket(p, packet); @@ -266,10 +267,10 @@ public class PacketManager extends BasePacket { * @param sendTo Who should it send the packet to? */ public static void sendFakePlayerSpawnPacket( - final Location location, + final @NotNull Location location, final UUID uuid, final int entityId, - final List sendTo + final @NotNull List sendTo ) { WrapperPlayServerNamedEntitySpawn wrapper = new WrapperPlayServerNamedEntitySpawn(); wrapper.setEntityID(entityId); @@ -326,7 +327,7 @@ public class PacketManager extends BasePacket { */ public static void sendPlayerOverlayPacket( final int playerId, - final List sendTo + final @NotNull List sendTo ) { /* 0x01 = Is on fire @@ -398,7 +399,7 @@ public class PacketManager extends BasePacket { public static void sendLeashPacket( final int leashedEntity, final int entityId, - final List sendTo + final @NotNull List sendTo ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.ATTACH_ENTITY); packet.getIntegers().write(0, leashedEntity); @@ -417,9 +418,9 @@ public class PacketManager extends BasePacket { */ public static void sendTeleportPacket( final int entityId, - final Location location, + final @NotNull Location location, boolean onGround, - final List sendTo + final @NotNull List sendTo ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.ENTITY_TELEPORT); packet.getIntegers().write(0, entityId); @@ -444,10 +445,10 @@ public class PacketManager extends BasePacket { */ public static void sendMovePacket( final int entityId, - final Location from, - final Location to, + final @NotNull Location from, + final @NotNull Location to, final boolean onGround, - List sendTo + @NotNull List sendTo ) { PacketContainer packet = new PacketContainer(PacketType.Play.Server.REL_ENTITY_MOVE); WrapperPlayServerRelEntityMove wrapper = new WrapperPlayServerRelEntityMove(packet); @@ -470,6 +471,7 @@ public class PacketManager extends BasePacket { sendMovePacket(entityId, from, to, onGround, getViewers(to)); } + @NotNull public static List getViewers(Location location) { ArrayList viewers = new ArrayList(); if (Settings.getViewDistance() <= 0) { diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/wrappers/WrapperPlayServerNamedEntitySpawn.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/wrappers/WrapperPlayServerNamedEntitySpawn.java index fc58d000..8c76c463 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/wrappers/WrapperPlayServerNamedEntitySpawn.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/wrappers/WrapperPlayServerNamedEntitySpawn.java @@ -6,6 +6,7 @@ import com.comphenix.protocol.events.PacketEvent; import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; import java.util.UUID; @@ -58,7 +59,7 @@ public class WrapperPlayServerNamedEntitySpawn extends AbstractPacket { * @param event - the packet event. * @return The spawned entity. */ - public Entity getEntity(PacketEvent event) { + public Entity getEntity(@NotNull PacketEvent event) { return getEntity(event.getPlayer().getWorld()); } @@ -96,7 +97,7 @@ public class WrapperPlayServerNamedEntitySpawn extends AbstractPacket { * * @param position - the new position. */ - public void setPosition(Vector position) { + public void setPosition(@NotNull Vector position) { setX(position.getX()); setY(position.getY()); setZ(position.getZ()); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/wrappers/WrapperPlayServerRelEntityMove.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/wrappers/WrapperPlayServerRelEntityMove.java index a778e03f..9e5e4166 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/wrappers/WrapperPlayServerRelEntityMove.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/wrappers/WrapperPlayServerRelEntityMove.java @@ -5,6 +5,7 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import org.bukkit.World; import org.bukkit.entity.Entity; +import org.jetbrains.annotations.NotNull; public class WrapperPlayServerRelEntityMove extends AbstractPacket { public static final PacketType TYPE = @@ -55,7 +56,7 @@ public class WrapperPlayServerRelEntityMove extends AbstractPacket { * @param event - the packet event. * @return The spawned entity. */ - public Entity getEntity(PacketEvent event) { + public Entity getEntity(@NotNull PacketEvent event) { return getEntity(event.getPlayer().getWorld()); }