diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketChat.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketChat.java index bb5e5909..e35bff30 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketChat.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketChat.java @@ -8,6 +8,7 @@ import com.willfp.eco.core.proxy.proxies.ChatComponentProxy; import com.willfp.eco.util.ProxyUtils; import com.willfp.eco.util.packets.AbstractPacketAdapter; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import org.jetbrains.annotations.NotNull; public class PacketChat extends AbstractPacketAdapter { public PacketChat(AbstractEcoPlugin plugin) { @@ -15,7 +16,7 @@ public class PacketChat extends AbstractPacketAdapter { } @Override - public void onSend(PacketContainer packet) { + public void onSend(@NotNull PacketContainer packet) { for (int i = 0; i < packet.getChatComponents().size(); i++) { WrappedChatComponent component = packet.getChatComponents().read(i); if (component == null) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java index 2ff2704a..7b836f43 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketOpenWindowMerchant.java @@ -9,6 +9,7 @@ import com.willfp.ecoenchants.display.EnchantDisplay; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.MerchantRecipe; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -22,7 +23,7 @@ public class PacketOpenWindowMerchant extends AbstractPacketAdapter { } @Override - public void onSend(PacketContainer packet) { + public void onSend(@NotNull PacketContainer packet) { List recipes = packet.getMerchantRecipeLists().readSafely(0); recipes = recipes.stream().peek(merchantRecipe -> { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetCreativeSlot.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetCreativeSlot.java index 00fbdaff..a5ed19fa 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetCreativeSlot.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetCreativeSlot.java @@ -5,6 +5,7 @@ import com.comphenix.protocol.events.PacketContainer; import com.willfp.eco.util.packets.AbstractPacketAdapter; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import com.willfp.ecoenchants.display.EnchantDisplay; +import org.jetbrains.annotations.NotNull; public class PacketSetCreativeSlot extends AbstractPacketAdapter { public PacketSetCreativeSlot(AbstractEcoPlugin plugin) { @@ -12,7 +13,7 @@ public class PacketSetCreativeSlot extends AbstractPacketAdapter { } @Override - public void onReceive(PacketContainer packet) { + public void onReceive(@NotNull PacketContainer packet) { packet.getItemModifier().modify(0, (item) -> { item = EnchantDisplay.revertDisplay(item); return item; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetSlot.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetSlot.java index d0264bfb..adca834c 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetSlot.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketSetSlot.java @@ -6,6 +6,7 @@ import com.willfp.eco.util.packets.AbstractPacketAdapter; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import com.willfp.ecoenchants.display.EnchantDisplay; import org.bukkit.inventory.ItemFlag; +import org.jetbrains.annotations.NotNull; public class PacketSetSlot extends AbstractPacketAdapter { public PacketSetSlot(AbstractEcoPlugin plugin) { @@ -13,7 +14,7 @@ public class PacketSetSlot extends AbstractPacketAdapter { } @Override - public void onSend(PacketContainer packet) { + public void onSend(@NotNull PacketContainer packet) { packet.getItemModifier().modify(0, (item) -> { boolean hideEnchants = false; diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java index 093f77a7..07dde2b8 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/display/packets/PacketWindowItems.java @@ -6,6 +6,7 @@ import com.willfp.eco.util.packets.AbstractPacketAdapter; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import com.willfp.ecoenchants.display.EnchantDisplay; import org.bukkit.inventory.ItemFlag; +import org.jetbrains.annotations.NotNull; public class PacketWindowItems extends AbstractPacketAdapter { public PacketWindowItems(AbstractEcoPlugin plugin) { @@ -13,7 +14,7 @@ public class PacketWindowItems extends AbstractPacketAdapter { } @Override - public void onSend(PacketContainer packet) { + public void onSend(@NotNull PacketContainer packet) { packet.getItemListModifier().modify(0, (itemStacks) -> { if (itemStacks == null) return null; itemStacks.forEach(item -> { diff --git a/eco-util/src/main/java/com/willfp/eco/util/BlockUtils.java b/eco-util/src/main/java/com/willfp/eco/util/BlockUtils.java index 0d1c042f..b2b9433f 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/BlockUtils.java +++ b/eco-util/src/main/java/com/willfp/eco/util/BlockUtils.java @@ -1,21 +1,28 @@ package com.willfp.eco.util; +import lombok.experimental.UtilityClass; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.List; import java.util.Set; +@UtilityClass public class BlockUtils { - private static Set getNearbyBlocks(Block start, List allowedMaterials, HashSet blocks, int limit) { + private Set getNearbyBlocks(@NotNull final Block start, + @NotNull final List allowedMaterials, + @NotNull final HashSet blocks, + final int limit) { for (BlockFace face : BlockFace.values()) { Block block = start.getRelative(face); if (!blocks.contains(block) && allowedMaterials.contains(block.getType())) { blocks.add(block); - if (blocks.size() > limit) return blocks; - if (blocks.size() > 2500) return blocks; // anti stack overflow + if (blocks.size() > limit || blocks.size() > 2500) { + return blocks; + } blocks.addAll(getNearbyBlocks(block, allowedMaterials, blocks, limit)); } } @@ -24,15 +31,17 @@ public class BlockUtils { /** - * Get a set of all blocks in contact with each other of a specific type + * Get a set of all blocks in contact with each other of a specific type. * - * @param start The initial block - * @param allowedMaterials A list of all valid {@link Material}s - * @param limit The maximum size of vein to return + * @param start The initial block. + * @param allowedMaterials A list of all valid {@link Material}s. + * @param limit The maximum size of vein to return. * - * @return A set of all {@link Block}s + * @return A set of all {@link Block}s. */ - public static Set getVein(Block start, List allowedMaterials, int limit) { + public Set getVein(@NotNull final Block start, + @NotNull final List allowedMaterials, + final int limit) { return getNearbyBlocks(start, allowedMaterials, new HashSet<>(), limit); } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/ClassUtils.java b/eco-util/src/main/java/com/willfp/eco/util/ClassUtils.java index 0aa9094e..81b2c813 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/ClassUtils.java +++ b/eco-util/src/main/java/com/willfp/eco/util/ClassUtils.java @@ -1,16 +1,18 @@ package com.willfp.eco.util; +import lombok.experimental.UtilityClass; +import org.jetbrains.annotations.NotNull; + +@UtilityClass public class ClassUtils { /** - * Get if a class exists - * - * @param className The class to check - * - * @return If the class exists + * Get if a class exists. * + * @param className The class to check. + * @return If the class exists. * @see Class#forName(String) */ - public static boolean exists(String className) { + public boolean exists(@NotNull final String className) { try { Class.forName(className); return true; diff --git a/eco-util/src/main/java/com/willfp/eco/util/DurabilityUtils.java b/eco-util/src/main/java/com/willfp/eco/util/DurabilityUtils.java index 7ffe483b..a250787f 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/DurabilityUtils.java +++ b/eco-util/src/main/java/com/willfp/eco/util/DurabilityUtils.java @@ -1,5 +1,6 @@ package com.willfp.eco.util; +import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import org.bukkit.Sound; import org.bukkit.SoundCategory; @@ -10,26 +11,31 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.Damageable; import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; -/** - * Contains methods for damaging/repairing items - */ +@UtilityClass public class DurabilityUtils { /** - * Damage an item in a player's inventory - * The slot of a held item can be obtained with {@link PlayerInventory#getHeldItemSlot()} - * Armor slots are 39 (helmet), 38 (chestplate), 37 (leggings), 36 (boots) + * Damage an item in a player's inventory. + * The slot of a held item can be obtained with {@link PlayerInventory#getHeldItemSlot()}. + * Armor slots are 39 (helmet), 38 (chestplate), 37 (leggings), 36 (boots). * - * @param player The player - * @param item The item to damage - * @param damage The amount of damage to deal - * @param slot The slot in the inventory of the item + * @param player The player. + * @param item The item to damage. + * @param damage The amount of damage to deal. + * @param slot The slot in the inventory of the item. */ - public static void damageItem(Player player, ItemStack item, int damage, int slot) { - if (item == null) return; - if (item.getItemMeta() == null) return; + public void damageItem(@NotNull final Player player, + @NotNull final ItemStack item, + final int damage, + final int slot) { + if (item.getItemMeta() == null) { + return; + } - if (item.getItemMeta().isUnbreakable()) return; + if (item.getItemMeta().isUnbreakable()) { + return; + } PlayerItemDamageEvent event3 = new PlayerItemDamageEvent(player, item, damage); Bukkit.getPluginManager().callEvent(event3); @@ -55,19 +61,22 @@ public class DurabilityUtils { } /** - * Damage an item in a player's inventory without breaking it - * The slot of a held item can be obtained with {@link PlayerInventory#getHeldItemSlot()} - * Armor slots are 39 (helmet), 38 (chestplate), 37 (leggings), 36 (boots) + * Damage an item in a player's inventory without breaking it. * - * @param item The item to damage - * @param damage The amount of damage to deal - * @param player The player + * @param item The item to damage. + * @param damage The amount of damage to deal. + * @param player The player. */ - public static void damageItemNoBreak(ItemStack item, int damage, Player player) { - if (item == null) return; - if (item.getItemMeta() == null) return; + public void damageItemNoBreak(@NotNull final ItemStack item, + final int damage, + @NotNull final Player player) { + if (item.getItemMeta() == null) { + return; + } - if (item.getItemMeta().isUnbreakable()) return; + if (item.getItemMeta().isUnbreakable()) { + return; + } PlayerItemDamageEvent event3 = new PlayerItemDamageEvent(player, item, damage); Bukkit.getPluginManager().callEvent(event3); @@ -87,18 +96,21 @@ public class DurabilityUtils { } /** - * Repair an item in a player's inventory - * The slot of a held item can be obtained with {@link PlayerInventory#getHeldItemSlot()} - * Armor slots are 39 (helmet), 38 (chestplate), 37 (leggings), 36 (boots) + * Repair an item in a player's inventory. * - * @param item The item to damage - * @param repair The amount of damage to heal + * @param item The item to damage. + * @param repair The amount of damage to heal. */ - public static void repairItem(ItemStack item, int repair) { - if (item == null) return; - if (item.getItemMeta() == null) return; + public void repairItem(@NotNull final ItemStack item, + final int repair) { + if (item.getItemMeta() == null) { + return; + } + + if (item.getItemMeta().isUnbreakable()) { + return; + } - if (item.getItemMeta().isUnbreakable()) return; if (item.getItemMeta() instanceof Damageable) { Damageable meta = (Damageable) item.getItemMeta(); meta.setDamage(meta.getDamage() - repair); diff --git a/eco-util/src/main/java/com/willfp/eco/util/LightningUtils.java b/eco-util/src/main/java/com/willfp/eco/util/LightningUtils.java index b75df107..b10fbe90 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/LightningUtils.java +++ b/eco-util/src/main/java/com/willfp/eco/util/LightningUtils.java @@ -1,22 +1,20 @@ package com.willfp.eco.util; +import lombok.experimental.UtilityClass; import org.bukkit.Location; import org.bukkit.entity.LivingEntity; +import org.jetbrains.annotations.NotNull; -/** - * Class containing methods for striking lightning - */ +@UtilityClass public class LightningUtils { - /** - * Strike lightning on player without fire + * Strike lightning on player without fire. * - * @param victim The entity to smite - * @param damage The damage to deal + * @param victim The entity to smite. + * @param damage The damage to deal. */ - public static void strike(LivingEntity victim, double damage) { - if (victim == null) return; - + public void strike(@NotNull final LivingEntity victim, + final double damage) { Location loc = victim.getLocation(); victim.getWorld().strikeLightningEffect(loc); diff --git a/eco-util/src/main/java/com/willfp/eco/util/NumberUtils.java b/eco-util/src/main/java/com/willfp/eco/util/NumberUtils.java index 353fdb42..24d117ac 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/NumberUtils.java +++ b/eco-util/src/main/java/com/willfp/eco/util/NumberUtils.java @@ -1,15 +1,24 @@ package com.willfp.eco.util; +import lombok.experimental.UtilityClass; + import java.text.DecimalFormat; import java.util.Random; import java.util.TreeMap; +@UtilityClass public class NumberUtils { + /** + * The RNG to use. + */ private static final Random RANDOM = new Random(); + + /** + * Set of roman numerals to look up. + */ private static final TreeMap NUMERALS = new TreeMap<>(); static { - NUMERALS.put(1000, "M"); NUMERALS.put(900, "CM"); NUMERALS.put(500, "D"); @@ -23,107 +32,102 @@ public class NumberUtils { NUMERALS.put(5, "V"); NUMERALS.put(4, "IV"); NUMERALS.put(1, "I"); - } /** - * Bias the input value according to a curve + * Bias the input value according to a curve. * - * @param input The input value - * @param bias The bias between -1 and 1, where higher values bias input values to lower output values - * - * @return The biased output + * @param input The input value. + * @param bias The bias between -1 and 1, where higher values bias input values to lower output values. + * @return The biased output. */ - public static double bias(double input, double bias) { + public double bias(final double input, + final double bias) { double k = Math.pow(1 - bias, 3); return (input * k) / (input * k - input + 1); } /** - * If value is above maximum, set it to maximum + * If value is above maximum, set it to maximum. * - * @param toChange The value to test - * @param limit The maximum - * - * @return The new value + * @param toChange The value to test. + * @param limit The maximum. + * @return The new value. */ - public static int equalIfOver(int toChange, int limit) { - if (toChange > limit) { - toChange = limit; - } - return toChange; + public int equalIfOver(final int toChange, + final int limit) { + return Math.min(toChange, limit); } /** - * If value is above maximum, set it to maximum + * If value is above maximum, set it to maximum. * - * @param toChange The value to test - * @param limit The maximum - * - * @return The new value + * @param toChange The value to test. + * @param limit The maximum. + * @return The new value. */ - public static double equalIfOver(double toChange, double limit) { - if (toChange > limit) { - toChange = limit; - } - return toChange; + public double equalIfOver(final double toChange, + final double limit) { + return Math.min(toChange, limit); } /** - * Get Roman Numeral from number + * Get Roman Numeral from number. * - * @param number The number to convert - * - * @return The number, converted to a roman numeral + * @param number The number to convert. + * @return The number, converted to a roman numeral. */ - public static String toNumeral(int number) { + public String toNumeral(final int number) { if (number >= 1 && number <= 4096) { int l = NUMERALS.floorKey(number); if (number == l) { return NUMERALS.get(number); } return NUMERALS.get(l) + toNumeral(number - l); - } else return String.valueOf(number); + } else { + return String.valueOf(number); + } } /** - * Generate random integer in range + * Generate random integer in range. * - * @param min Minimum - * @param max Maximum - * - * @return Random integer + * @param min Minimum. + * @param max Maximum. + * @return Random integer. */ - public static int randInt(int min, int max) { + public int randInt(final int min, + final int max) { return (int) ((long) min + Math.random() * ((long) max - min + 1)); } /** - * Generate random double in range + * Generate random double in range. * - * @param min Minimum - * @param max Maximum - * - * @return Random double + * @param min Minimum. + * @param max Maximum. + * @return Random double. */ - public static double randFloat(double min, double max) { + public double randFloat(final double min, + final double max) { return RANDOM.nextFloat() * (max - min) + min; } /** - * Generate random double with a triangular distribution + * Generate random double with a triangular distribution. * - * @param minimum Minimum - * @param maximum Maximum - * @param peak Peak - * - * @return Random double + * @param minimum Minimum. + * @param maximum Maximum. + * @param peak Peak. + * @return Random double. */ - public static double triangularDistribution(double minimum, double maximum, double peak) { - double F = (peak - minimum) / (maximum - minimum); + public double triangularDistribution(final double minimum, + final double maximum, + final double peak) { + double f = (peak - minimum) / (maximum - minimum); double rand = Math.random(); - if (rand < F) { + if (rand < f) { return minimum + Math.sqrt(rand * (maximum - minimum) * (peak - minimum)); } else { return maximum - Math.sqrt((1 - rand) * (maximum - minimum) * (maximum - peak)); @@ -131,24 +135,22 @@ public class NumberUtils { } /** - * Get Log base 2 of a number + * Get Log base 2 of a number. * - * @param N The number - * - * @return The result + * @param N The number. + * @return The result. */ - public static int log2(int N) { + public int log2(final int N) { return (int) (Math.log(N) / Math.log(2)); } /** - * Format double to string + * Format double to string. * - * @param toFormat The number to format - * - * @return Formatted + * @param toFormat The number to format. + * @return Formatted. */ - public static String format(double toFormat) { + public String format(final double toFormat) { DecimalFormat df = new DecimalFormat("0.00"); String formatted = df.format(toFormat); diff --git a/eco-util/src/main/java/com/willfp/eco/util/StringUtils.java b/eco-util/src/main/java/com/willfp/eco/util/StringUtils.java index c6031b31..304606b5 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/StringUtils.java +++ b/eco-util/src/main/java/com/willfp/eco/util/StringUtils.java @@ -1,8 +1,10 @@ package com.willfp.eco.util; import com.willfp.eco.util.integrations.placeholder.PlaceholderManager; +import lombok.experimental.UtilityClass; import net.md_5.bungee.api.ChatColor; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collection; @@ -12,16 +14,17 @@ import java.util.stream.Collectors; import static net.md_5.bungee.api.ChatColor.COLOR_CHAR; +@UtilityClass public class StringUtils { /** - * Translate a string - converts Placeholders and Color codes + * Translate a string - converts Placeholders and Color codes. * - * @param message The message to translate - * @param player The player to translate placeholders with respect to - * - * @return The message, translated + * @param message The message to translate. + * @param player The player to translate placeholders with respect to. + * @return The message, translated. */ - public static String translate(String message, @Nullable Player player) { + public String translate(@NotNull String message, + @Nullable final Player player) { message = PlaceholderManager.translatePlaceholders(message, player); message = translateHexColorCodes(message); message = ChatColor.translateAlternateColorCodes('&', message); @@ -29,15 +32,13 @@ public class StringUtils { } /** - * Translate a string without respect to a player - * - * @param message The message to translate - * - * @return The message, translated + * Translate a string without respect to a player. * + * @param message The message to translate. + * @return The message, translated. * @see StringUtils#translate(String, Player) */ - public static String translate(String message) { + public String translate(@NotNull String message) { message = PlaceholderManager.translatePlaceholders(message, null); message = translateHexColorCodes(message); message = ChatColor.translateAlternateColorCodes('&', message); @@ -60,15 +61,16 @@ public class StringUtils { } /** - * Internal implementation of {@link String#valueOf} - * Formats collections and doubles better + * Internal implementation of {@link String#valueOf}. + * Formats collections and doubles better. * - * @param object The object to convert to string - * - * @return The object stringified + * @param object The object to convert to string. + * @return The object stringified. */ - public static String internalToString(@Nullable Object object) { - if (object == null) return "null"; + public String internalToString(@Nullable final Object object) { + if (object == null) { + return "null"; + } if (object instanceof Integer) { return ((Integer) object).toString(); @@ -79,19 +81,21 @@ public class StringUtils { } else if (object instanceof Collection) { Collection c = (Collection) object; return c.stream().map(StringUtils::internalToString).collect(Collectors.joining(", ")); - } else return String.valueOf(object); + } else { + return String.valueOf(object); + } } /** - * Remove a string of characters from the start of a string + * Remove a string of characters from the start of a string. * - * @param s The string to remove the prefix from - * @param prefix The substring to remove - * - * @return The string with the prefix removed + * @param s The string to remove the prefix from. + * @param prefix The substring to remove. + * @return The string with the prefix removed. */ - public static String removePrefix(String s, String prefix) { - if (s != null && prefix != null && s.startsWith(prefix)) { + public String removePrefix(@NotNull final String s, + @NotNull final String prefix) { + if (s.startsWith(prefix)) { return s.substring(prefix.length()); } return s; diff --git a/eco-util/src/main/java/com/willfp/eco/util/VectorUtils.java b/eco-util/src/main/java/com/willfp/eco/util/VectorUtils.java index 7fc27ff1..3e67d810 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/VectorUtils.java +++ b/eco-util/src/main/java/com/willfp/eco/util/VectorUtils.java @@ -1,19 +1,21 @@ package com.willfp.eco.util; +import lombok.experimental.UtilityClass; import org.bukkit.util.NumberConversions; import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +@UtilityClass public class VectorUtils { /** - * If vector has all components as finite + * If vector has all components as finite. * - * @param vector The vector to check - * - * @return If the vector is finite + * @param vector The vector to check. + * @return If the vector is finite. */ - public static boolean isFinite(Vector vector) { + public boolean isFinite(@NotNull final Vector vector) { try { NumberConversions.checkFinite(vector.getX(), "x not finite"); NumberConversions.checkFinite(vector.getY(), "y not finite"); @@ -26,13 +28,12 @@ public class VectorUtils { /** * Only keep largest part of normalised vector. - * For example: -0.8, 0.01, -0.2 would become -1, 0, 0 + * For example: -0.8, 0.01, -0.2 would become -1, 0, 0. * - * @param vec The vector to simplify - * - * @return The vector, simplified + * @param vec The vector to simplify. + * @return The vector, simplified. */ - public static Vector simplifyVector(Vector vec) { + public Vector simplifyVector(@NotNull final Vector vec) { double x = Math.abs(vec.getX()); double y = Math.abs(vec.getY()); double z = Math.abs(vec.getZ()); @@ -59,13 +60,12 @@ public class VectorUtils { } /** - * Get circle as relative vectors + * Get circle as relative vectors. * - * @param radius The radius - * - * @return An array of {@link Vector}s + * @param radius The radius. + * @return An array of {@link Vector}s. */ - public static Vector[] getCircle(int radius) { + public Vector[] getCircle(final int radius) { ArrayList circleVecs = new ArrayList<>(); int xoffset = -radius; @@ -89,13 +89,12 @@ public class VectorUtils { } /** - * Get square as relative vectors + * Get square as relative vectors. * - * @param radius The radius of the square - * - * @return An array of {@link Vector}s + * @param radius The radius of the square. + * @return An array of {@link Vector}s. */ - public static Vector[] getSquare(int radius) { + public Vector[] getSquare(final int radius) { ArrayList circleVecs = new ArrayList<>(); int xoffset = -radius; @@ -114,30 +113,20 @@ public class VectorUtils { } /** - * Get cube as relative vectors + * Get cube as relative vectors. * - * @param radius The radius of the cube - * - * @return An array of {@link Vector}s + * @param radius The radius of the cube. + * @return An array of {@link Vector}s. */ - public static Vector[] getCube(int radius) { + public Vector[] getCube(final int radius) { ArrayList cubeVecs = new ArrayList<>(); - int xoffset = -radius; - int zoffset = -radius; - int yoffset = -radius; - - while (yoffset <= radius) { - while (zoffset <= radius) { - while (xoffset <= radius) { - cubeVecs.add(new Vector(xoffset, yoffset, zoffset)); - xoffset++; + for (int y = -radius; y <= radius; y++) { + for (int z = -radius; z <= radius; z++) { + for (int x = -radius; x <= radius; x++) { + cubeVecs.add(new Vector(x, y, z)); } - xoffset = -radius; - zoffset++; } - zoffset = -radius; - yoffset++; } return cubeVecs.toArray(new Vector[0]); diff --git a/eco-util/src/main/java/com/willfp/eco/util/drops/telekinesis/EcoTelekinesisTests.java b/eco-util/src/main/java/com/willfp/eco/util/drops/telekinesis/EcoTelekinesisTests.java index f8899d2b..ac283cbf 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/drops/telekinesis/EcoTelekinesisTests.java +++ b/eco-util/src/main/java/com/willfp/eco/util/drops/telekinesis/EcoTelekinesisTests.java @@ -1,6 +1,6 @@ package com.willfp.eco.util.drops.telekinesis; -import com.willfp.eco.util.lambda.ObjectBiCallable; +import com.willfp.eco.util.lambda.ObjectInputCallable; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -11,7 +11,7 @@ public class EcoTelekinesisTests implements TelekinesisTests { /** * Set of tests that return if the player is telekinetic. */ - private final Set> tests = new HashSet<>(); + private final Set> tests = new HashSet<>(); /** * Register a new test to check against. @@ -19,7 +19,7 @@ public class EcoTelekinesisTests implements TelekinesisTests { * @param test The test to register, where the boolean output is if the player is telekinetic. */ @Override - public void registerTest(@NotNull final ObjectBiCallable test) { + public void registerTest(@NotNull final ObjectInputCallable test) { tests.add(test); } @@ -33,7 +33,7 @@ public class EcoTelekinesisTests implements TelekinesisTests { */ @Override public boolean testPlayer(@NotNull final Player player) { - for (ObjectBiCallable test : tests) { + for (ObjectInputCallable test : tests) { if (test.call(player)) { return true; } diff --git a/eco-util/src/main/java/com/willfp/eco/util/drops/telekinesis/TelekinesisTests.java b/eco-util/src/main/java/com/willfp/eco/util/drops/telekinesis/TelekinesisTests.java index dca7219d..ad5ab5d0 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/drops/telekinesis/TelekinesisTests.java +++ b/eco-util/src/main/java/com/willfp/eco/util/drops/telekinesis/TelekinesisTests.java @@ -1,6 +1,6 @@ package com.willfp.eco.util.drops.telekinesis; -import com.willfp.eco.util.lambda.ObjectBiCallable; +import com.willfp.eco.util.lambda.ObjectInputCallable; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -10,7 +10,7 @@ public interface TelekinesisTests { * * @param test The test to register, where the boolean output is if the player is telekinetic. */ - void registerTest(@NotNull ObjectBiCallable test); + void registerTest(@NotNull ObjectInputCallable test); /** * Test the player for telekinesis. diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/AnticheatManager.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/AnticheatManager.java index a189b609..a2997450 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/AnticheatManager.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/AnticheatManager.java @@ -1,25 +1,32 @@ package com.willfp.eco.util.integrations.anticheat; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import lombok.experimental.UtilityClass; import org.bukkit.entity.Player; import org.bukkit.event.Listener; +import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.Set; -/** - * Utility class for Anticheat Integrations - */ +@UtilityClass public class AnticheatManager { - private static final AbstractEcoPlugin plugin = AbstractEcoPlugin.getInstance(); - private static final Set anticheats = new HashSet<>(); + /** + * The linked {@link AbstractEcoPlugin} to register anticheat listeners to. + */ + private final AbstractEcoPlugin plugin = AbstractEcoPlugin.getInstance(); /** - * Register a new anticheat - * - * @param anticheat The anticheat to register + * A set of all registered anticheats. */ - public static void register(AnticheatWrapper anticheat) { + private final Set anticheats = new HashSet<>(); + + /** + * Register a new anticheat. + * + * @param anticheat The anticheat to register. + */ + public void register(@NotNull final AnticheatWrapper anticheat) { if (anticheat instanceof Listener) { plugin.getEventManager().registerListener((Listener) anticheat); } @@ -27,21 +34,21 @@ public class AnticheatManager { } /** - * Exempt a player from triggering anticheats + * Exempt a player from triggering anticheats. * - * @param player The player to exempt + * @param player The player to exempt. */ - public static void exemptPlayer(Player player) { + public void exemptPlayer(@NotNull final Player player) { anticheats.forEach(anticheat -> anticheat.exempt(player)); } /** - * Unexempt a player from triggering anticheats - * This is ran a tick after it is called to ensure that there are no event timing conflicts + * Unexempt a player from triggering anticheats. + * This is ran a tick after it is called to ensure that there are no event timing conflicts. * - * @param player The player to remove the exemption + * @param player The player to remove the exemption. */ - public static void unexemptPlayer(Player player) { + public void unexemptPlayer(@NotNull final Player player) { plugin.getScheduler().runLater(() -> { anticheats.forEach(anticheat -> anticheat.unexempt(player)); }, 1); diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/AnticheatWrapper.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/AnticheatWrapper.java index 7a1ec8bb..60e5837b 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/AnticheatWrapper.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/AnticheatWrapper.java @@ -2,22 +2,20 @@ package com.willfp.eco.util.integrations.anticheat; import com.willfp.eco.util.integrations.Integration; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; -/** - * Interface for anticheat integrations - */ public interface AnticheatWrapper extends Integration { /** - * Exempt a player from checks + * Exempt a player from checks. * - * @param player The player to exempt + * @param player The player to exempt. */ - void exempt(Player player); + void exempt(@NotNull Player player); /** - * Unexempt a player from checks + * Unexempt a player from checks. * - * @param player The player to unexempt + * @param player The player to unexempt. */ - void unexempt(Player player); + void unexempt(@NotNull Player player); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatAAC.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatAAC.java index dea11d8d..56631f57 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatAAC.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatAAC.java @@ -6,6 +6,7 @@ import me.konsolas.aac.api.AACExemption; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.Listener; +import org.jetbrains.annotations.NotNull; public class AnticheatAAC implements AnticheatWrapper, Listener { private final AACExemption ecoEnchantsExemption = new AACExemption("EcoEnchants"); @@ -17,12 +18,12 @@ public class AnticheatAAC implements AnticheatWrapper, Listener { } @Override - public void exempt(Player player) { + public void exempt(@NotNull Player player) { api.addExemption(player, ecoEnchantsExemption); } @Override - public void unexempt(Player player) { + public void unexempt(@NotNull Player player) { api.removeExemption(player, ecoEnchantsExemption); } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatMatrix.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatMatrix.java index 954158fe..e62859cf 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatMatrix.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatMatrix.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.Set; @@ -20,12 +21,12 @@ public class AnticheatMatrix implements AnticheatWrapper, Listener { } @Override - public void exempt(Player player) { + public void exempt(@NotNull Player player) { this.exempt.add(player.getUniqueId()); } @Override - public void unexempt(Player player) { + public void unexempt(@NotNull Player player) { this.exempt.remove(player.getUniqueId()); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatNCP.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatNCP.java index cf1a44ec..13816c9d 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatNCP.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatNCP.java @@ -4,6 +4,7 @@ import com.willfp.eco.util.integrations.anticheat.AnticheatWrapper; import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.Set; @@ -18,7 +19,7 @@ public class AnticheatNCP implements AnticheatWrapper { } @Override - public void exempt(Player player) { + public void exempt(@NotNull Player player) { if (!NCPExemptionManager.isExempted(player, CheckType.ALL)) { return; } @@ -29,7 +30,7 @@ public class AnticheatNCP implements AnticheatWrapper { } @Override - public void unexempt(Player player) { + public void unexempt(@NotNull Player player) { if (exempt.remove(player.getUniqueId())) { NCPExemptionManager.unexempt(player, CheckType.ALL); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatSpartan.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatSpartan.java index b0e70653..73ababab 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatSpartan.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/anticheat/plugins/AnticheatSpartan.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.Set; @@ -20,12 +21,12 @@ public class AnticheatSpartan implements AnticheatWrapper, Listener { } @Override - public void exempt(Player player) { + public void exempt(@NotNull Player player) { this.exempt.add(player.getUniqueId()); } @Override - public void unexempt(Player player) { + public void unexempt(@NotNull Player player) { this.exempt.remove(player.getUniqueId()); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/PlaceholderEntry.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/PlaceholderEntry.java index 2a3543c1..305d2b89 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/PlaceholderEntry.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/PlaceholderEntry.java @@ -1,63 +1,72 @@ package com.willfp.eco.util.integrations.placeholder; -import com.willfp.eco.util.lambda.ObjectBiCallable; +import com.willfp.eco.util.lambda.ObjectInputCallable; +import lombok.Getter; +import org.apache.commons.lang.Validate; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -/** - * A placeholder entry consists of an identifier and an {@link ObjectBiCallable} to fetch the result - */ public class PlaceholderEntry { + /** + * The name of the placeholder, used in lookups. + */ + @Getter private final String identifier; - private final ObjectBiCallable function; + + /** + * The lambda to retrieve the output of the placeholder given a player. + */ + private final ObjectInputCallable function; + + /** + * If the placeholder requires a player to lookup. + */ private final boolean requiresPlayer; /** - * Create a placeholder entry that doesn't require a player + * Create a placeholder entry that doesn't require a player. * - * @param identifier The identifier of the placeholder - * @param function A lambda to get the result of the placeholder + * @param identifier The identifier of the placeholder. + * @param function A lambda to get the result of the placeholder given a player. */ - public PlaceholderEntry(String identifier, ObjectBiCallable function) { + public PlaceholderEntry(@NotNull final String identifier, + @NotNull final ObjectInputCallable function) { this(identifier, function, false); } /** - * Create a placeholder entry that may require a player + * Create a placeholder entry that may require a player. * - * @param identifier The identifier of the placeholder - * @param function A lambda to get the result of the placeholder - * @param requiresPlayer If the placeholder requires a player + * @param identifier The identifier of the placeholder. + * @param function A lambda to get the result of the placeholder. + * @param requiresPlayer If the placeholder requires a player. */ - public PlaceholderEntry(String identifier, ObjectBiCallable function, boolean requiresPlayer) { + public PlaceholderEntry(@NotNull final String identifier, + @NotNull final ObjectInputCallable function, + final boolean requiresPlayer) { this.identifier = identifier; this.function = function; this.requiresPlayer = requiresPlayer; } /** - * Get the identifier of the placeholder + * Get the result of the placeholder with respect to a player. * - * @return The identifier + * @param player The player to translate with respect to. + * @return The result of the placeholder. */ - public String getIdentifier() { - return this.identifier; - } - - /** - * Get the result of the placeholder with respect to a player - * - * @param player The player to translate with respect to - * @return The result of the placeholder - */ - public String getResult(@Nullable Player player) { + public String getResult(@Nullable final Player player) { + if (player == null) { + Validate.isTrue(!requiresPlayer, "null player passed to requiresPlayer placeholder."); + } return this.function.call(player); } /** - * Get if the placeholder requires a player to get a result + * Get if the placeholder requires a player to get a result. * - * @return If the placeholder requires a player + * @return If the placeholder requires a player. */ public boolean requiresPlayer() { return requiresPlayer; diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/PlaceholderIntegration.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/PlaceholderIntegration.java index d69cae9b..38f42382 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/PlaceholderIntegration.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/PlaceholderIntegration.java @@ -2,23 +2,23 @@ package com.willfp.eco.util.integrations.placeholder; import com.willfp.eco.util.integrations.Integration; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -/** - * Interface for Placeholder integrations - */ public interface PlaceholderIntegration extends Integration { /** - * Register the integration with the specified plugin - * Not to be confused with internal registration in {@link PlaceholderManager#addIntegration(PlaceholderIntegration)} + * Register the integration with the specified plugin. + * Not to be confused with internal registration in {@link PlaceholderManager#addIntegration(PlaceholderIntegration)}. */ void registerIntegration(); /** - * Translate all the placeholders in a string with respect to a player + * Translate all the placeholders in a string with respect to a player. * - * @param text The text to translate - * @param player The player to translate with respect to - * @return The string, translated + * @param text The text to translate. + * @param player The player to translate with respect to. + * @return The string, translated. */ - String translate(String text, Player player); + String translate(@NotNull String text, + @Nullable Player player); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/plugins/PlaceholderIntegrationPAPI.java b/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/plugins/PlaceholderIntegrationPAPI.java index 09406337..4cf98a82 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/plugins/PlaceholderIntegrationPAPI.java +++ b/eco-util/src/main/java/com/willfp/eco/util/integrations/placeholder/plugins/PlaceholderIntegrationPAPI.java @@ -59,7 +59,7 @@ public class PlaceholderIntegrationPAPI extends PlaceholderExpansion implements } @Override - public String translate(String text, Player player) { + public String translate(@NotNull String text, Player player) { return PlaceholderAPI.setPlaceholders(player, text); } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/interfaces/EcoRunnable.java b/eco-util/src/main/java/com/willfp/eco/util/interfaces/EcoRunnable.java index 579d2801..7a494763 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/interfaces/EcoRunnable.java +++ b/eco-util/src/main/java/com/willfp/eco/util/interfaces/EcoRunnable.java @@ -1,5 +1,11 @@ package com.willfp.eco.util.interfaces; public interface EcoRunnable extends Runnable { + /** + * The EcoRunnable interface is generally used for repeating tasks. + * This method is to retrieve the ticks between repetitions. + * + * @return The ticks between repetitions. + */ long getTime(); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/interfaces/Registerable.java b/eco-util/src/main/java/com/willfp/eco/util/interfaces/Registerable.java index a1b81658..67e62db0 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/interfaces/Registerable.java +++ b/eco-util/src/main/java/com/willfp/eco/util/interfaces/Registerable.java @@ -1,8 +1,8 @@ package com.willfp.eco.util.interfaces; -/** - * Interface for objects that can be internally registered - */ public interface Registerable { + /** + * Register an object with its respective registry. + */ void register(); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/lambda/Callable.java b/eco-util/src/main/java/com/willfp/eco/util/lambda/Callable.java index 1efc0862..e7fbdec3 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/lambda/Callable.java +++ b/eco-util/src/main/java/com/willfp/eco/util/lambda/Callable.java @@ -1,9 +1,12 @@ package com.willfp.eco.util.lambda; /** - * Simple functional interface to run some code on demand + * Represents code that can be later executed. */ @FunctionalInterface public interface Callable { + /** + * Execute the lambda. + */ void call(); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/lambda/InputCallable.java b/eco-util/src/main/java/com/willfp/eco/util/lambda/InputCallable.java index 5f87f58f..3e55bbf6 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/lambda/InputCallable.java +++ b/eco-util/src/main/java/com/willfp/eco/util/lambda/InputCallable.java @@ -1,11 +1,16 @@ package com.willfp.eco.util.lambda; /** - * Functional Interface that requires an object + * Functional Interface that requires an object as a parameter. * - * @param The type of the object to require + * @param The type of the object to require. */ @FunctionalInterface public interface InputCallable { + /** + * Execute the lambda. + * + * @param object The lambda parameter. + */ void call(A object); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectCallable.java b/eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectCallable.java index 7abdf9fe..72863098 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectCallable.java +++ b/eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectCallable.java @@ -7,5 +7,10 @@ package com.willfp.eco.util.lambda; */ @FunctionalInterface public interface ObjectCallable { + /** + * Execute the lambda. + * + * @return The return value of the specified type. + */ A call(); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectBiCallable.java b/eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectInputCallable.java similarity index 55% rename from eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectBiCallable.java rename to eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectInputCallable.java index b26f346c..674c981e 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectBiCallable.java +++ b/eco-util/src/main/java/com/willfp/eco/util/lambda/ObjectInputCallable.java @@ -1,12 +1,18 @@ package com.willfp.eco.util.lambda; /** - * Functional Interface to return a value of a specified type given a certain parameter + * Functional Interface to return a value of a specified type given a certain parameter. * * @param The type of object to return * @param The type of object for the parameter */ @FunctionalInterface -public interface ObjectBiCallable { +public interface ObjectInputCallable { + /** + * Execute the lambda. + * + * @param object The lambda parameter. + * @return The required return type. + */ A call(B object); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/optional/Prerequisite.java b/eco-util/src/main/java/com/willfp/eco/util/optional/Prerequisite.java index 775fd0a6..4e92e8ff 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/optional/Prerequisite.java +++ b/eco-util/src/main/java/com/willfp/eco/util/optional/Prerequisite.java @@ -2,17 +2,22 @@ package com.willfp.eco.util.optional; import com.willfp.eco.util.ClassUtils; import com.willfp.eco.util.lambda.ObjectCallable; +import lombok.Getter; import org.bukkit.Bukkit; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Prerequisite { + /** + * All existing prerequisites are registered on creation. + */ private static final List values = new ArrayList<>(); /** - * Requires the server to be running minecraft version 1.16 or higher + * Requires the server to be running minecraft version 1.16 or higher. */ public static final Prerequisite MinVer1_16 = new Prerequisite( () -> !Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3].contains("15"), @@ -20,24 +25,38 @@ public class Prerequisite { ); /** - * Requires the server to be running an implementation of paper + * Requires the server to be running an implementation of paper. */ public static final Prerequisite HasPaper = new Prerequisite( () -> ClassUtils.exists("com.destroystokyo.paper.event.player.PlayerElytraBoostEvent"), "Requires server to be running paper (or a fork)" ); + /** + * If the necessary prerequisite condition has been met. + */ + @Getter private boolean isMet; + + /** + * Retrieve if the necessary prerequisite condition is met. + */ private final ObjectCallable isMetCallable; + + /** + * The description of the requirements of the prerequisite. + */ + @Getter private final String description; /** - * Create a prerequisite + * Create a prerequisite. * * @param isMetCallable An {@link ObjectCallable} that returns if the prerequisite is met * @param description The description of the prerequisite, shown to the user if it isn't */ - public Prerequisite(ObjectCallable isMetCallable, String description) { + public Prerequisite(@NotNull final ObjectCallable isMetCallable, + @NotNull final String description) { this.isMetCallable = isMetCallable; this.isMet = isMetCallable.call(); this.description = description; @@ -45,42 +64,26 @@ public class Prerequisite { } /** - * Get the description of the prerequisite - * - * @return The description + * Refresh the condition set in the callable, updates {@link this#isMet}. */ - public String getDescription() { - return description; - } - - /** - * Get if the prerequisite has been met - * - * @return If the prerequisite is met - */ - public boolean isMet() { - return isMet; - } - private void refresh() { this.isMet = this.isMetCallable.call(); } /** - * Update all prerequisites' {@link Prerequisite#isMet} + * Update all prerequisites' {@link Prerequisite#isMet}. */ public static void update() { values.forEach(Prerequisite::refresh); } /** - * Check if all prerequisites in array are met + * Check if all prerequisites in array are met. * - * @param prerequisites A primitive array of prerequisites to check - * - * @return If all the prerequisites are met + * @param prerequisites A primitive array of prerequisites to check. + * @return If all the prerequisites are met. */ - public static boolean areMet(Prerequisite[] prerequisites) { + public static boolean areMet(@NotNull final Prerequisite[] prerequisites) { update(); return Arrays.stream(prerequisites).allMatch(Prerequisite::isMet); } diff --git a/eco-util/src/main/java/com/willfp/eco/util/packets/AbstractPacketAdapter.java b/eco-util/src/main/java/com/willfp/eco/util/packets/AbstractPacketAdapter.java index 47a56aad..d4d91a6b 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/packets/AbstractPacketAdapter.java +++ b/eco-util/src/main/java/com/willfp/eco/util/packets/AbstractPacketAdapter.java @@ -7,62 +7,115 @@ import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import lombok.Getter; +import org.jetbrains.annotations.NotNull; import java.util.Collections; public abstract class AbstractPacketAdapter extends PacketAdapter { + /** + * The packet type to listen for. + */ private final PacketType type; + + /** + * Whether the packet adapter should be registered after the server has loaded. + *

+ * Useful for monitor priority adapters that must be ran last. + */ + @Getter private final boolean postLoad; - protected AbstractPacketAdapter(AbstractEcoPlugin plugin, PacketType type, ListenerPriority priority, boolean postLoad) { + /** + * Create a new packet adapter for a specified plugin and type. + * + * @param plugin The plugin that ProtocolLib should mark as the owner. + * @param type The {@link PacketType} to listen for. + * @param priority The priority at which the adapter should be ran on packet send/receive. + * @param postLoad If the packet adapter should be registered after the server has loaded. + */ + protected AbstractPacketAdapter(@NotNull final AbstractEcoPlugin plugin, + @NotNull final PacketType type, + @NotNull final ListenerPriority priority, + final boolean postLoad) { super(plugin, priority, Collections.singletonList(type)); this.type = type; this.postLoad = postLoad; } - protected AbstractPacketAdapter(AbstractEcoPlugin plugin, PacketType type, boolean postLoad) { - super(plugin, Collections.singletonList(type)); - this.type = type; - this.postLoad = postLoad; + /** + * Create a new packet adapter for a specified plugin and type. + * + * @param plugin The plugin that ProtocolLib should mark as the owner. + * @param type The {@link PacketType} to listen for. + * @param postLoad If the packet adapter should be registered after the server has loaded. + */ + protected AbstractPacketAdapter(@NotNull final AbstractEcoPlugin plugin, + @NotNull final PacketType type, + final boolean postLoad) { + this(plugin, type, ListenerPriority.NORMAL, postLoad); } - public void onReceive(PacketContainer packet) { + /** + * The code that should be executed once the packet has been received. + * + * @param packet The packet. + */ + public void onReceive(@NotNull final PacketContainer packet) { // Empty rather than abstract as implementations don't need both } - public void onSend(PacketContainer packet) { + /** + * THe code that should be executed once the packet has been sent. + * + * @param packet The packet. + */ + public void onSend(@NotNull final PacketContainer packet) { // Empty rather than abstract as implementations don't need both } + /** + * Boilerplate to assert that the packet is of the specified type. + * + * @param event The ProtocolLib event. + */ @Override - public final void onPacketReceiving(PacketEvent event) { - if (event.getPacket() == null) + public final void onPacketReceiving(final PacketEvent event) { + if (event.getPacket() == null) { return; + } - if (!event.getPacket().getType().equals(type)) + if (!event.getPacket().getType().equals(type)) { return; + } onReceive(event.getPacket()); } + /** + * Boilerplate to assert that the packet is of the specified type. + * + * @param event The ProtocolLib event. + */ @Override - public final void onPacketSending(PacketEvent event) { - if (event.getPacket() == null) + public final void onPacketSending(final PacketEvent event) { + if (event.getPacket() == null) { return; + } - if (!event.getPacket().getType().equals(type)) + if (!event.getPacket().getType().equals(type)) { return; + } onSend(event.getPacket()); } + /** + * Register the packet adapter with ProtocolLib. + */ public final void register() { if (!ProtocolLibrary.getProtocolManager().getPacketListeners().contains(this)) { ProtocolLibrary.getProtocolManager().addPacketListener(this); } } - - public final boolean isPostLoad() { - return postLoad; - } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java b/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java index f9eea2bf..400294f0 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java +++ b/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java @@ -41,6 +41,7 @@ import com.willfp.eco.util.integrations.placeholder.plugins.PlaceholderIntegrati import com.willfp.eco.util.optional.Prerequisite; import com.willfp.eco.util.packets.AbstractPacketAdapter; import com.willfp.eco.util.updater.UpdateChecker; +import lombok.Getter; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; @@ -48,6 +49,7 @@ import org.bukkit.event.Listener; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Arrays; @@ -56,25 +58,95 @@ import java.util.Set; import java.util.stream.Collectors; public abstract class AbstractEcoPlugin extends JavaPlugin { - protected static AbstractEcoPlugin instance; + /** + * The instance of the plugin. + */ + @Getter + private static AbstractEcoPlugin instance; - protected final String pluginName; - protected final int resourceId; - protected final int bStatsId; + /** + * The name of the plugin. + */ + @Getter + private final String pluginName; + /** + * The spigot resource ID of the plugin. + */ + @Getter + private final int resourceId; + + /** + * The bStats resource ID of the plugin. + */ + @Getter + private final int bStatsId; + + /** + * Set of external plugin integrations. + */ private final List integrations = new ArrayList<>(); + /** + * The internal plugin logger. + */ + @Getter private final Logger log; + + /** + * The internal plugin scheduler. + */ + @Getter private final Scheduler scheduler; + + /** + * The internal plugin Event Manager. + */ + @Getter private final EventManager eventManager; + + /** + * The internal factory to produce {@link org.bukkit.NamespacedKey}s. + */ + @Getter private final NamespacedKeyFactory namespacedKeyFactory; + + /** + * The internal factory to produce {@link org.bukkit.metadata.FixedMetadataValue}s. + */ + @Getter private final MetadataValueFactory metadataValueFactory; + + /** + * The internal factory to produce {@link com.willfp.eco.util.bukkit.scheduling.EcoBukkitRunnable}s. + */ + @Getter private final RunnableFactory runnableFactory; + + /** + * The loader for all plugin extensions. + * + * @see com.willfp.eco.util.extensions.Extension + */ + @Getter private final ExtensionLoader extensionLoader; - protected boolean outdated = false; + /** + * If the server is running an outdated version of the plugin. + */ + @Getter + private boolean outdated = false; - protected AbstractEcoPlugin(String pluginName, int resourceId, int bStatsId) { + /** + * Create a new plugin. + * + * @param pluginName The name of the plugin. + * @param resourceId The spigot resource ID for the plugin. + * @param bStatsId The bStats resource ID for the plugin. + */ + protected AbstractEcoPlugin(@NotNull final String pluginName, + final int resourceId, + final int bStatsId) { this.pluginName = pluginName; this.resourceId = resourceId; this.bStatsId = bStatsId; @@ -88,12 +160,15 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.extensionLoader = new EcoExtensionLoader(this); if (!Bukkit.getServicesManager().isProvidedFor(TelekinesisTests.class)) { - Bukkit.getServicesManager().register(TelekinesisTests.class, new EcoTelekinesisTests(this), this, ServicePriority.Normal); + Bukkit.getServicesManager().register(TelekinesisTests.class, new EcoTelekinesisTests(), this, ServicePriority.Normal); } TelekinesisUtils.update(); } + /** + * Default code to be executed on plugin enable. + */ @Override public final void onEnable() { super.onLoad(); @@ -113,10 +188,11 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { new FastCollatedDropQueue.CollatedRunnable(this); - new UpdateChecker(this, resourceId).getVersion(version -> { + new UpdateChecker(this).getVersion(version -> { DefaultArtifactVersion currentVersion = new DefaultArtifactVersion(this.getDescription().getVersion()); DefaultArtifactVersion mostRecentVersion = new DefaultArtifactVersion(version); if (!(currentVersion.compareTo(mostRecentVersion) > 0 || currentVersion.equals(mostRecentVersion))) { + this.outdated = true; this.getScheduler().runTimer(() -> { this.getLog().info("&c " + this.pluginName + " is out of date! (Version " + this.getDescription().getVersion() + ")"); this.getLog().info("&cThe newest version is &f" + version); @@ -145,7 +221,9 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { Prerequisite.update(); this.getPacketAdapters().forEach(abstractPacketAdapter -> { - if(!abstractPacketAdapter.isPostLoad()) abstractPacketAdapter.register(); + if (!abstractPacketAdapter.isPostLoad()) { + abstractPacketAdapter.register(); + } }); this.getListeners().forEach(listener -> this.getEventManager().registerListener(listener)); @@ -157,6 +235,9 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.enable(); } + /** + * Default code to be executed on plugin disable. + */ @Override public final void onDisable() { super.onDisable(); @@ -167,6 +248,9 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.disable(); } + /** + * Default code to be executed on plugin load. + */ @Override public final void onLoad() { super.onLoad(); @@ -176,9 +260,12 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.load(); } + /** + * Default code to be executed after the server is up. + */ public final void afterLoad() { this.getPacketAdapters().forEach(abstractPacketAdapter -> { - if(abstractPacketAdapter.isPostLoad()) abstractPacketAdapter.register(); + if (abstractPacketAdapter.isPostLoad()) abstractPacketAdapter.register(); }); if (!Prerequisite.HasPaper.isMet()) { @@ -201,6 +288,9 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.getLog().info("Loaded &a" + this.pluginName); } + /** + * Default code to be executed on plugin reload. + */ public final void onReload() { Configs.update(); DropManager.update(); @@ -210,6 +300,11 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.reload(); } + /** + * Default integrations that exist within {@link AbstractEcoPlugin}. + * + * @return The default integrations. + */ public final List getDefaultIntegrations() { integrations.add(new IntegrationLoader("PlaceholderAPI", () -> PlaceholderManager.addIntegration(new PlaceholderIntegrationPAPI(this)))); @@ -230,57 +325,58 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { return integrations; } + /** + * The plugin-specific code to be executed on enable. + */ public abstract void enable(); + /** + * The plugin-specific code to be executed on disable. + */ public abstract void disable(); + /** + * The plugin-specific code to be executed on load. + */ public abstract void load(); + /** + * The plugin-specific code to be executed on reload. + */ public abstract void reload(); + /** + * The plugin-specific code to be executed after the server is up. + */ public abstract void postLoad(); + /** + * The plugin-specific integrations to be tested and loaded. + * + * @return A list of integrations. + */ public abstract List getIntegrations(); + /** + * The command to be registered. + * + * @return A list of commands. + */ public abstract List getCommands(); + /** + * ProtocolLib packet adapters to be registered. + *

+ * If the plugin does not require ProtocolLib this can be left empty. + * + * @return A list of packet adapters. + */ public abstract List getPacketAdapters(); + /** + * All listeners to be registered. + * + * @return A list of all listeners. + */ public abstract List getListeners(); - - public final Logger getLog() { - return log; - } - - public final Scheduler getScheduler() { - return scheduler; - } - - public final EventManager getEventManager() { - return eventManager; - } - - public final NamespacedKeyFactory getNamespacedKeyFactory() { - return namespacedKeyFactory; - } - - public final MetadataValueFactory getMetadataValueFactory() { - return metadataValueFactory; - } - - public final RunnableFactory getRunnableFactory() { - return runnableFactory; - } - - public final ExtensionLoader getExtensionLoader() { - return extensionLoader; - } - - public final boolean isOutdated() { - return outdated; - } - - public static AbstractEcoPlugin getInstance() { - return instance; - } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/tuplets/Pair.java b/eco-util/src/main/java/com/willfp/eco/util/tuplets/Pair.java index 21c5dc6d..6024e7e7 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/tuplets/Pair.java +++ b/eco-util/src/main/java/com/willfp/eco/util/tuplets/Pair.java @@ -1,64 +1,37 @@ package com.willfp.eco.util.tuplets; -/** - * Spigot doesn't include javafx - */ +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import org.jetbrains.annotations.Nullable; + +@ToString public class Pair { + /** + * The first value in the pair. + */ + @Getter + @Setter + @Nullable private A first; + + /** + * The second value in the pair. + */ + @Getter + @Setter + @Nullable private B second; /** - * Create a pair + * Create a pair of values. * - * @param first The first item in the tuplet - * @param second The second item in the tuplet + * @param first The first item in the pair. + * @param second The second item in the pair. */ - public Pair(A first, B second) { + public Pair(@Nullable final A first, + @Nullable final B second) { this.first = first; this.second = second; } - - /** - * Get the first item in the tuplet - * - * @return The first item - */ - public A getFirst() { - return first; - } - - /** - * Get the second item in the tuplet - * - * @return The second item - */ - public B getSecond() { - return second; - } - - /** - * Set the first item in the tuplet - * - * @param first The value to set the first item to - */ - public void setFirst(A first) { - this.first = first; - } - - /** - * Set the second item in the tuplet - * - * @param second The value to set the second item to - */ - public void setSecond(B second) { - this.second = second; - } - - @Override - public String toString() { - return "Pair{" + - "first=" + first + - ", second=" + second + - '}'; - } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/tuplets/Triplet.java b/eco-util/src/main/java/com/willfp/eco/util/tuplets/Triplet.java index 8a12f8c1..cdb99efe 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/tuplets/Triplet.java +++ b/eco-util/src/main/java/com/willfp/eco/util/tuplets/Triplet.java @@ -1,86 +1,48 @@ package com.willfp.eco.util.tuplets; -/** - * Spigot doesn't include javafx - */ +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import org.jetbrains.annotations.Nullable; + +@ToString public class Triplet { + /** + * The first item in the triplet. + */ + @Getter + @Setter + @Nullable private A first; + + /** + * The second item in the triplet. + */ + @Getter + @Setter + @Nullable private B second; + + /** + * The third item in the triplet. + */ + @Getter + @Setter + @Nullable private C third; /** - * Create a triplet + * Create a triplet. * - * @param first The first item in the tuplet - * @param second The second item in the tuplet - * @param third The third item in the tuplet + * @param first The first item in the triplet. + * @param second The second item in the triplet. + * @param third The third item in the triplet. */ - public Triplet(A first, B second, C third) { + public Triplet(@Nullable final A first, + @Nullable final B second, + @Nullable final C third) { this.first = first; this.second = second; this.third = third; } - - /** - * Get the first item in the tuplet - * - * @return The first item - */ - public A getFirst() { - return first; - } - - /** - * Get the second item in the tuplet - * - * @return The second item - */ - public B getSecond() { - return second; - } - - /** - * Get the third item in the tuplet - * - * @return The third item - */ - public C getThird() { - return third; - } - - /** - * Set the first item in the tuplet - * - * @param first The value to set - */ - public void setFirst(A first) { - this.first = first; - } - - /** - * Set the second item in the tuplet - * - * @param second The value to set - */ - public void setSecond(B second) { - this.second = second; - } - - /** - * Set the third item in the tuplet - * - * @param third The value to set - */ - public void setThird(C third) { - this.third = third; - } - - @Override - public String toString() { - return "Triplet{" + - "first=" + first + - ", second=" + second + - ", third=" + third + - '}'; - } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/updater/UpdateChecker.java b/eco-util/src/main/java/com/willfp/eco/util/updater/UpdateChecker.java index b9ac4c1b..b3a57f58 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/updater/UpdateChecker.java +++ b/eco-util/src/main/java/com/willfp/eco/util/updater/UpdateChecker.java @@ -1,46 +1,46 @@ package com.willfp.eco.util.updater; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import lombok.AccessLevel; +import lombok.Getter; import org.bukkit.util.Consumer; +import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Scanner; -/** - * Checks spigot if a plugin is out of date - */ public class UpdateChecker { + /** + * The instance of the plugin to check updates for. + */ + @Getter(AccessLevel.PRIVATE) private final AbstractEcoPlugin plugin; - private final int resourceId; /** - * Create an update checker for the specified spigot resource id + * Create an update checker for the specified spigot resource id. * - * @param plugin The plugin to check - * @param resourceId The resource ID of the plugin + * @param plugin The plugin to check. */ - public UpdateChecker(AbstractEcoPlugin plugin, int resourceId) { + public UpdateChecker(@NotNull final AbstractEcoPlugin plugin) { this.plugin = plugin; - this.resourceId = resourceId; } /** - * Get the latest version of the plugin + * Get the latest version of the plugin. * - * @param consumer The process to run after checking + * @param consumer The process to run after checking. */ - public void getVersion(final Consumer consumer) { - this.plugin.getScheduler().runAsync(() -> { - try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) { + public void getVersion(@NotNull final Consumer consumer) { + this.getPlugin().getScheduler().runAsync(() -> { + try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.getPlugin().getResourceId()).openStream(); Scanner scanner = new Scanner(inputStream)) { if (scanner.hasNext()) { consumer.accept(scanner.next()); } } catch (IOException exception) { - this.plugin.getLogger().warning("Failed to check for updates: " + exception.getMessage()); + this.getPlugin().getLogger().warning("Failed to check for updates: " + exception.getMessage()); } }); } } - \ No newline at end of file