From 60685cdcf4a659c1e30f3b6e6248d25c284b4c44 Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:12:59 +0200 Subject: [PATCH 01/12] Enforce nullability in dough-common --- .../io/github/thebusybiscuit/dough/common/ChatColors.java | 3 +++ .../io/github/thebusybiscuit/dough/common/DoughLogger.java | 2 ++ .../io/github/thebusybiscuit/dough/common/PlayerList.java | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/ChatColors.java b/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/ChatColors.java index 077ce5bf..aa660d5a 100644 --- a/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/ChatColors.java +++ b/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/ChatColors.java @@ -2,6 +2,7 @@ import javax.annotation.Nonnull; +import org.apache.commons.lang.Validate; import org.bukkit.ChatColor; public final class ChatColors { @@ -18,6 +19,7 @@ private ChatColors() {} * @return The colored String */ public static @Nonnull String color(@Nonnull String input) { + Validate.notNull(input, "The input cannot be null"); return ChatColor.translateAlternateColorCodes('&', input); } @@ -34,6 +36,7 @@ private ChatColors() {} * @return The colored String */ public static @Nonnull String alternating(@Nonnull String text, ChatColor... colors) { + Validate.notNull(text, "The text cannot be null"); int i = 0; StringBuilder builder = new StringBuilder(text.length() * 3); diff --git a/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/DoughLogger.java b/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/DoughLogger.java index 737dad0c..a30e37f3 100644 --- a/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/DoughLogger.java +++ b/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/DoughLogger.java @@ -6,6 +6,7 @@ import javax.annotation.Nonnull; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.Server; import org.bukkit.plugin.PluginLogger; @@ -35,6 +36,7 @@ public DoughLogger(@Nonnull String name) { @Override public void log(@Nonnull LogRecord logRecord) { + Validate.notNull(logRecord, "The log record cannot be null"); logRecord.setMessage(logRecord.getMessage()); super.log(logRecord); } diff --git a/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/PlayerList.java b/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/PlayerList.java index 1cc05bbe..7665c067 100644 --- a/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/PlayerList.java +++ b/dough-common/src/main/java/io/github/thebusybiscuit/dough/common/PlayerList.java @@ -7,6 +7,7 @@ import javax.annotation.Nonnull; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -38,6 +39,7 @@ private PlayerList() {} * @return An Optional describing the player (or an empty Optional) */ public static @Nonnull Optional findByName(@Nonnull String name) { + Validate.notNull(name, "The name cannot be null"); return stream().filter(p -> p.getName().equalsIgnoreCase(name)).findAny(); } @@ -50,6 +52,7 @@ private PlayerList() {} * @return A Set of Players */ public static @Nonnull Set findPermitted(@Nonnull String permission) { + Validate.notNull(permission, "The permission cannot be null"); return stream().filter(p -> p.hasPermission(permission)).collect(Collectors.toSet()); } @@ -62,6 +65,7 @@ private PlayerList() {} * @return Whether the Player is online */ public static boolean isOnline(@Nonnull String name) { + Validate.notNull(name, "The name cannot be null"); return findByName(name).isPresent(); } From 581b0af5d0785e2bd5665739a4a4e3281eb6764d Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:13:15 +0200 Subject: [PATCH 02/12] Enforce nullability in dough-chat --- .../thebusybiscuit/dough/chat/ChatInput.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dough-chat/src/main/java/io/github/thebusybiscuit/dough/chat/ChatInput.java b/dough-chat/src/main/java/io/github/thebusybiscuit/dough/chat/ChatInput.java index bd1596ae..11910fac 100644 --- a/dough-chat/src/main/java/io/github/thebusybiscuit/dough/chat/ChatInput.java +++ b/dough-chat/src/main/java/io/github/thebusybiscuit/dough/chat/ChatInput.java @@ -6,6 +6,7 @@ import javax.annotation.Nonnull; +import org.apache.commons.lang.Validate; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -27,6 +28,9 @@ private ChatInput() {} * A callback to invoke when the Player has entered some text */ public static void waitForPlayer(@Nonnull Plugin plugin, @Nonnull Player p, @Nonnull Consumer handler) { + Validate.notNull(plugin, "The plugin cannot be null"); + Validate.notNull(p, "The player cannot be null"); + Validate.notNull(handler, "The handler cannot be null"); waitForPlayer(plugin, p, s -> true, handler); } @@ -46,6 +50,11 @@ public static void waitForPlayer(@Nonnull Plugin plugin, @Nonnull Player p, @Non * A callback to invoke when the Player has entered some text */ public static void waitForPlayer(@Nonnull Plugin plugin, @Nonnull Player p, @Nonnull Predicate predicate, @Nonnull Consumer handler) { + Validate.notNull(plugin, "The plugin cannot be null"); + Validate.notNull(p, "The player cannot be null"); + Validate.notNull(handler, "The handler cannot be null"); + Validate.notNull(predicate, "The predicate cannot be null"); + queue(plugin, p, new ChatInputHandler() { @Override @@ -73,6 +82,9 @@ public void onChat(Player p, String msg) { * A callback to invoke when the Player has entered some text */ public static void waitForPlayer(@Nonnull Plugin plugin, @Nonnull Player p, @Nonnull BiConsumer handler) { + Validate.notNull(plugin, "The plugin cannot be null"); + Validate.notNull(p, "The player cannot be null"); + Validate.notNull(handler, "The handler cannot be null"); waitForPlayer(plugin, p, s -> true, handler); } @@ -92,6 +104,11 @@ public static void waitForPlayer(@Nonnull Plugin plugin, @Nonnull Player p, @Non * A callback to invoke when the Player has entered some text */ public static void waitForPlayer(@Nonnull Plugin plugin, @Nonnull Player p, @Nonnull Predicate predicate, @Nonnull BiConsumer handler) { + Validate.notNull(plugin, "The plugin cannot be null"); + Validate.notNull(p, "The player cannot be null"); + Validate.notNull(handler, "The handler cannot be null"); + Validate.notNull(predicate, "The predicate cannot be null"); + queue(plugin, p, new ChatInputHandler() { @Override @@ -108,6 +125,10 @@ public void onChat(Player p, String msg) { } public static void queue(@Nonnull Plugin plugin, @Nonnull Player p, @Nonnull ChatInputHandler callback) { + Validate.notNull(plugin, "The plugin cannot be null"); + Validate.notNull(p, "The player cannot be null"); + Validate.notNull(callback, "The callback cannot be null"); + if (listener == null) { listener = new ChatInputListener(plugin); } From ef6e259a6ad0b75fba0ba5b3d7560aeb1c1d5ef6 Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:13:24 +0200 Subject: [PATCH 03/12] Enforce nullability in dough-reflection --- .../reflection/PrimitiveTypeConversion.java | 4 ++ .../dough/reflection/ReflectionUtils.java | 42 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/dough-reflection/src/main/java/io/github/thebusybiscuit/dough/reflection/PrimitiveTypeConversion.java b/dough-reflection/src/main/java/io/github/thebusybiscuit/dough/reflection/PrimitiveTypeConversion.java index 571eade5..3ca8092d 100644 --- a/dough-reflection/src/main/java/io/github/thebusybiscuit/dough/reflection/PrimitiveTypeConversion.java +++ b/dough-reflection/src/main/java/io/github/thebusybiscuit/dough/reflection/PrimitiveTypeConversion.java @@ -1,5 +1,7 @@ package io.github.thebusybiscuit.dough.reflection; +import org.apache.commons.lang.Validate; + import java.util.HashMap; import java.util.Map; @@ -24,10 +26,12 @@ final class PrimitiveTypeConversion { private PrimitiveTypeConversion() {} static @Nullable Class convert(@Nonnull Class boxedClassType) { + Validate.notNull(boxedClassType, "The boxed class type cannot be null"); return primitiveTypes.get(boxedClassType); } static @Nonnull Class convertIfNecessary(@Nonnull Class boxedClassType) { + Validate.notNull(boxedClassType, "The boxed class type cannot be null"); Class primitiveType = convert(boxedClassType); return primitiveType != null ? primitiveType : boxedClassType; } diff --git a/dough-reflection/src/main/java/io/github/thebusybiscuit/dough/reflection/ReflectionUtils.java b/dough-reflection/src/main/java/io/github/thebusybiscuit/dough/reflection/ReflectionUtils.java index 4cfd18fe..f1886db6 100644 --- a/dough-reflection/src/main/java/io/github/thebusybiscuit/dough/reflection/ReflectionUtils.java +++ b/dough-reflection/src/main/java/io/github/thebusybiscuit/dough/reflection/ReflectionUtils.java @@ -11,6 +11,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; /** @@ -52,6 +53,8 @@ private ReflectionUtils() {} */ @Nullable public static Method getMethod(@Nonnull Class c, @Nonnull String method) { + Validate.notNull(c, "The class cannot be null"); + Validate.notNull(method, "The method cannot be null"); for (Method m : c.getMethods()) { if (m.getName().equals(method)) return m; @@ -73,6 +76,9 @@ public static Method getMethod(@Nonnull Class c, @Nonnull String method) { */ @Nullable public static Method getMethod(@Nonnull Class c, @Nonnull String method, Class... paramTypes) { + Validate.notNull(c, "The class cannot be null"); + Validate.notNull(method, "The method cannot be null"); + Class[] t = toPrimitiveTypeArray(paramTypes); for (Method m : c.getMethods()) { @@ -100,6 +106,9 @@ public static Method getMethod(@Nonnull Class c, @Nonnull String method, Clas */ @Nonnull public static Field getField(@Nonnull Class c, @Nonnull String field) throws NoSuchFieldException { + Validate.notNull(c, "The class cannot be null"); + Validate.notNull(field, "The field cannot be null"); + return c.getDeclaredField(field); } @@ -123,6 +132,10 @@ public static Field getField(@Nonnull Class c, @Nonnull String field) throws * If the field could not be modified. */ public static void setFieldValue(@Nonnull T object, @Nonnull Class c, @Nonnull String field, @Nullable Object value) throws NoSuchFieldException, IllegalAccessException { + Validate.notNull(object, "The object cannot be null"); + Validate.notNull(c, "The class cannot be null"); + Validate.notNull(field, "The field cannot be null"); + Field f = getField(c, field); f.setAccessible(true); f.set(object, value); @@ -146,6 +159,9 @@ public static void setFieldValue(@Nonnull T object, @Nonnull Class c, @No * If the field could not be modified. */ public static void setFieldValue(@Nonnull T object, @Nonnull String field, @Nullable Object value) throws NoSuchFieldException, IllegalAccessException { + Validate.notNull(object, "The object cannot be null"); + Validate.notNull(field, "The field cannot be null"); + Field f = getField(object.getClass(), field); f.setAccessible(true); f.set(object, value); @@ -168,6 +184,10 @@ public static void setFieldValue(@Nonnull T object, @Nonnull String field, @ */ @Nullable public static T getFieldValue(@Nonnull Object object, @Nonnull Class fieldType, @Nonnull String field) throws NoSuchFieldException, IllegalAccessException { + Validate.notNull(object, "The object cannot be null"); + Validate.notNull(fieldType, "The field type cannot be null"); + Validate.notNull(field, "The field cannot be null"); + Field f = getField(object.getClass(), field); f.setAccessible(true); return fieldType.cast(f.get(object)); @@ -184,6 +204,8 @@ public static T getFieldValue(@Nonnull Object object, @Nonnull Class fiel */ @Nonnull public static Class[] toPrimitiveTypeArray(@Nonnull Class[] classes) { + Validate.notNull(classes, "The classes cannot be null"); + Validate.noNullElements(classes, "The class array cannot contain null elements"); int size = classes.length; Class[] types = new Class[size]; @@ -207,6 +229,8 @@ public static Class[] toPrimitiveTypeArray(@Nonnull Class[] classes) { */ @Nonnull public static Class[] toPrimitiveTypeArray(@Nonnull Object[] objects) { + Validate.notNull(objects, "The objects cannot be null"); + Validate.noNullElements(objects, "The object array cannot contain null elements"); int size = objects.length; Class[] types = new Class[size]; @@ -232,6 +256,7 @@ public static Class[] toPrimitiveTypeArray(@Nonnull Object[] objects) { @Nullable @SuppressWarnings("unchecked") public static Constructor getConstructor(@Nonnull Class c, Class... paramTypes) { + Validate.notNull(c, "The class cannot be null"); Class[] t = toPrimitiveTypeArray(paramTypes); for (Constructor constructor : c.getConstructors()) { @@ -260,6 +285,8 @@ public static Constructor getConstructor(@Nonnull Class c, Class... */ @Nonnull public static Class getInnerNMSClass(@Nonnull String name, @Nonnull String subname) throws ClassNotFoundException { + Validate.notNull(name, "The name cannot be null"); + Validate.notNull(subname, "The subname cannot be null"); return getNMSClass(name + '$' + subname); } @@ -271,6 +298,7 @@ public static Class getInnerNMSClass(@Nonnull String name, @Nonnull String su * @throws ClassNotFoundException If the class does not exist */ public static Class getNetMinecraftClass(@Nonnull String name) throws ClassNotFoundException { + Validate.notNull(name, "The name cannot be null"); return Class.forName("net.minecraft." + (MAJOR_VERSION <= 16 ? getVersion() + '.' : "") + name); } @@ -287,6 +315,7 @@ public static Class getNetMinecraftClass(@Nonnull String name) throws ClassNo */ @Nonnull public static Class getNMSClass(@Nonnull String name) throws ClassNotFoundException { + Validate.notNull(name, "The name cannot be null"); return Class.forName("net.minecraft.server." + (MAJOR_VERSION <= 16 ? getVersion() + '.' : "") + name); } @@ -305,6 +334,8 @@ public static Class getNMSClass(@Nonnull String name) throws ClassNotFoundExc */ @Nonnull public static Class getInnerOBCClass(@Nonnull String name, @Nonnull String subname) throws ClassNotFoundException { + Validate.notNull(name, "The name cannot be null"); + Validate.notNull(subname, "The subname cannot be null"); return getOBCClass(name + '$' + subname); } @@ -321,6 +352,7 @@ public static Class getInnerOBCClass(@Nonnull String name, @Nonnull String su */ @Nonnull public static Class getOBCClass(@Nonnull String name) throws ClassNotFoundException { + Validate.notNull(name, "The name cannot be null"); return Class.forName("org.bukkit.craftbukkit." + getVersion() + '.' + name); } @@ -364,7 +396,11 @@ public static boolean isUnitTestEnvironment() { * All following Arrays you want to compare * @return Whether they equal each other */ - private static boolean equalsTypeArray(@Nonnull Class[] a, Class... o) { + private static boolean equalsTypeArray(@Nonnull Class[] a, @Nonnull Class... o) { + Validate.notNull(a, "The first array cannot be null"); + Validate.notNull(o, "The second array cannot be null"); + Validate.noNullElements(a, "The first array cannot contain null elements"); + Validate.noNullElements(o, "The second array cannot contain null elements"); if (a.length != o.length) { return false; } @@ -389,6 +425,7 @@ private static boolean equalsTypeArray(@Nonnull Class[] a, Class... o) { */ @Nonnull public static List getEnumConstants(@Nonnull Class c) { + Validate.notNull(c, "The class cannot be null"); return Arrays.asList(c.getEnumConstants()); } @@ -405,6 +442,9 @@ public static List getEnumConstants(@Nonnull Class c) { */ @Nullable public static T getEnumConstant(@Nonnull Class c, @Nonnull String name) { + Validate.notNull(c, "The class cannot be null"); + Validate.notNull(name, "The name cannot be null"); + for (T field : c.getEnumConstants()) { if (field.toString().equals(name)) { return field; From e31e59e35be1007016fe8560c93f940ed12cc771 Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:13:40 +0200 Subject: [PATCH 04/12] Enforce nullability in dough-config --- .../thebusybiscuit/dough/config/Config.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/dough-config/src/main/java/io/github/thebusybiscuit/dough/config/Config.java b/dough-config/src/main/java/io/github/thebusybiscuit/dough/config/Config.java index 56d87bde..34232fef 100644 --- a/dough-config/src/main/java/io/github/thebusybiscuit/dough/config/Config.java +++ b/dough-config/src/main/java/io/github/thebusybiscuit/dough/config/Config.java @@ -14,6 +14,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Chunk; @@ -45,6 +46,8 @@ public class Config { * The Instance of the Plugin, the config.yml is referring to */ public Config(@Nonnull Plugin plugin) { + Validate.notNull(plugin, "Plugin cannot be null"); + plugin.getConfig().options().copyDefaults(true); plugin.saveConfig(); @@ -56,6 +59,9 @@ public Config(@Nonnull Plugin plugin) { } public Config(@Nonnull Plugin plugin, @Nonnull String name) { + Validate.notNull(plugin, "Plugin cannot be null"); + Validate.notNull(name, "Name cannot be null"); + this.logger = plugin.getLogger(); this.file = new File("plugins/" + plugin.getName().replace(" ", "_"), name); this.fileConfig = YamlConfiguration.loadConfiguration(this.file); @@ -72,6 +78,9 @@ public Config(@Nonnull Plugin plugin, @Nonnull String name) { * The FileConfiguration */ public Config(@Nonnull File file, @Nonnull FileConfiguration config) { + Validate.notNull(file, "File cannot be null"); + Validate.notNull(config, "FileConfiguration cannot be null"); + this.logger = new DoughLogger("config"); this.file = file; @@ -128,6 +137,7 @@ public void setHeader(@Nullable String header) { * Your {@link Logger} instance */ public void setLogger(@Nonnull Logger logger) { + Validate.notNull(logger, "The logger cannot be null"); this.logger = logger; } @@ -138,6 +148,7 @@ public void clear() { } protected void store(@Nonnull String path, Object value) { + Validate.notNull(path, "The path cannot be null"); this.fileConfig.set(path, value); } @@ -150,6 +161,7 @@ protected void store(@Nonnull String path, Object value) { * The Value for that path */ public void setValue(@Nonnull String path, Object value) { + Validate.notNull(path, "THe path cannot be null"); if (value == null) { this.store(path, value); } else if (value instanceof Optional) { @@ -198,6 +210,7 @@ public void save() { * The {@link File} you are saving this {@link Config} to */ public void save(@Nonnull File file) { + Validate.notNull(file, "The file cannot be null"); try { if (header != null) { fileConfig.options().copyHeader(true); @@ -222,6 +235,7 @@ public void save(@Nonnull File file) { * The Value for that path */ public void setDefaultValue(@Nonnull String path, @Nullable Object value) { + Validate.notNull(path, "The path cannot be null"); if (!contains(path)) { setValue(path, value); } @@ -229,6 +243,7 @@ public void setDefaultValue(@Nonnull String path, @Nullable Object value) { @SuppressWarnings("unchecked") public T getOrSetDefault(@Nonnull String path, T value) { + Validate.notNull(path, "The path cannot be null"); Object val = getValue(path); if (value.getClass().isInstance(val)) { @@ -247,6 +262,7 @@ public T getOrSetDefault(@Nonnull String path, T value) { * @return True/false */ public boolean contains(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.contains(path); } @@ -260,11 +276,15 @@ public boolean contains(@Nonnull String path) { */ @Nullable public Object getValue(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.get(path); } @Nonnull public Optional getValueAs(@Nonnull Class c, @Nonnull String path) { + Validate.notNull(c, "The class cannot be null"); + Validate.notNull(path, "The path cannot be null"); + Object obj = getValue(path); return c.isInstance(obj) ? Optional.of(c.cast(obj)) : Optional.empty(); } @@ -279,6 +299,7 @@ public Optional getValueAs(@Nonnull Class c, @Nonnull String path) { */ @Nullable public ItemStack getItem(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.getItemStack(path); } @@ -292,6 +313,7 @@ public ItemStack getItem(@Nonnull String path) { */ @Nullable public String getString(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.getString(path); } @@ -304,6 +326,7 @@ public String getString(@Nonnull String path) { * @return The Integer at that path */ public int getInt(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.getInt(path); } @@ -316,6 +339,7 @@ public int getInt(@Nonnull String path) { * @return The Boolean at that path */ public boolean getBoolean(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.getBoolean(path); } @@ -329,6 +353,7 @@ public boolean getBoolean(@Nonnull String path) { */ @Nonnull public List getStringList(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.getStringList(path); } @@ -342,6 +367,7 @@ public List getStringList(@Nonnull String path) { */ @Nonnull public List getIntList(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.getIntegerList(path); } @@ -368,6 +394,7 @@ public boolean createFile() { * @return The Float at that path */ public float getFloat(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return Float.valueOf(String.valueOf(getValue(path))); } @@ -380,6 +407,7 @@ public float getFloat(@Nonnull String path) { * @return The Long at that path */ public long getLong(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return Long.valueOf(String.valueOf(getValue(path))); } @@ -392,6 +420,7 @@ public long getLong(@Nonnull String path) { * @return The Date at that path */ public Date getDate(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return new Date(getLong(path)); } @@ -404,6 +433,7 @@ public Date getDate(@Nonnull String path) { * @return The Chunk at that path */ public Chunk getChunk(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return Bukkit.getWorld(getString(path + ".world")).getChunkAt(getInt(path + ".x"), getInt(path + ".z")); } @@ -416,6 +446,7 @@ public Chunk getChunk(@Nonnull String path) { * @return The UUID at that path */ public UUID getUUID(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); String value = getString(path); return value != null ? UUID.fromString(value) : null; } @@ -429,6 +460,7 @@ public UUID getUUID(@Nonnull String path) { * @return The World at that path */ public World getWorld(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return Bukkit.getWorld(getString(path)); } @@ -441,6 +473,7 @@ public World getWorld(@Nonnull String path) { * @return The Double at that path */ public double getDouble(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return fileConfig.getDouble(path); } @@ -454,6 +487,7 @@ public double getDouble(@Nonnull String path) { */ @Nonnull public Location getLocation(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); return new Location(Bukkit.getWorld(getString(path + ".world")), getDouble(path + ".x"), getDouble(path + ".y"), getDouble(path + ".z"), getFloat(path + ".yaw"), getFloat(path + ".pitch")); } @@ -471,6 +505,9 @@ public Location getLocation(@Nonnull String path) { */ @Nonnull public Inventory getInventory(@Nonnull String path, int size, @Nonnull String title) { + Validate.notNull(path, "The path cannot be null"); + Validate.notNull(title, "The title cannot be null"); + Inventory inventory = Bukkit.createInventory(null, size, ChatColor.translateAlternateColorCodes('&', title)); for (int i = 0; i < size; i++) { inventory.setItem(i, getItem(path + "." + i)); @@ -490,6 +527,9 @@ public Inventory getInventory(@Nonnull String path, int size, @Nonnull String ti */ @Nonnull public Inventory getInventory(@Nonnull String path, @Nonnull String title) { + Validate.notNull(path, "The path cannot be null"); + Validate.notNull(title, "The title cannot be null"); + int size = getInt(path + ".size"); Inventory inventory = Bukkit.createInventory(null, size, ChatColor.translateAlternateColorCodes('&', title)); @@ -520,6 +560,7 @@ public Set getKeys() { */ @Nonnull public Set getKeys(@Nonnull String path) { + Validate.notNull(path, "The path cannot be null"); ConfigurationSection section = fileConfig.getConfigurationSection(path); return section == null ? new HashSet<>() : section.getKeys(false); } From 8c787ba1a352dc8252df7538c308c0f041845f0d Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:13:48 +0200 Subject: [PATCH 05/12] Enforce nullability in dough-inventories --- .../dough/inventory/InvUtils.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/dough-inventories/src/main/java/io/github/thebusybiscuit/dough/inventory/InvUtils.java b/dough-inventories/src/main/java/io/github/thebusybiscuit/dough/inventory/InvUtils.java index 1a351cd5..d58160c7 100644 --- a/dough-inventories/src/main/java/io/github/thebusybiscuit/dough/inventory/InvUtils.java +++ b/dough-inventories/src/main/java/io/github/thebusybiscuit/dough/inventory/InvUtils.java @@ -5,6 +5,7 @@ import java.util.function.Predicate; import java.util.stream.IntStream; +import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; @@ -32,6 +33,7 @@ private InvUtils() {} * @return Whether an empty slot exists */ public static boolean hasEmptySlot(@Nonnull Inventory inv) { + Validate.notNull(inv, "The inventory cannot be null"); return inv.firstEmpty() != 1; } @@ -44,6 +46,7 @@ public static boolean hasEmptySlot(@Nonnull Inventory inv) { * @return Whether that {@link Inventory} is empty */ public static boolean isEmpty(@Nonnull Inventory inv) { + Validate.notNull(inv, "The inventory cannot be null"); // Sadly Inventory#isEmpty() is not available everywhere for (ItemStack item : inv) { @@ -71,6 +74,10 @@ public static boolean isEmpty(@Nonnull Inventory inv) { * @return Whether the maxStackSizes allow for these items to stack */ public static boolean isValidStackSize(@Nonnull ItemStack stack, @Nonnull ItemStack item, @Nonnull Inventory inv) { + Validate.notNull(stack, "The stack cannot be null"); + Validate.notNull(item, "The item cannot be null"); + Validate.notNull(inv, "The inventory cannot be null"); + int newStackSize = stack.getAmount() + item.getAmount(); return newStackSize <= stack.getMaxStackSize() && newStackSize <= inv.getMaxStackSize(); } @@ -86,6 +93,9 @@ public static boolean isValidStackSize(@Nonnull ItemStack stack, @Nonnull ItemSt * @return Whether the given {@link InventoryType} allows this {@link Material} to be stored within */ public static boolean isItemAllowed(@Nonnull Material itemType, @Nonnull InventoryType inventoryType) { + Validate.notNull(itemType, "The item type cannot be null"); + Validate.notNull(inventoryType, "The inventory type cannot be null"); + switch (inventoryType) { case LECTERN: // Lecterns only allow written books or writable books @@ -114,6 +124,9 @@ public static boolean isItemAllowed(@Nonnull Material itemType, @Nonnull Invento * @return Whether the slots have space for the {@link ItemStack} */ public static boolean fits(@Nonnull Inventory inv, @Nonnull ItemStack item, int... slots) { + Validate.notNull(item, "The item type cannot be null"); + Validate.notNull(inv, "The inventory cannot be null"); + if (!isItemAllowed(item.getType(), inv.getType())) { return false; } @@ -153,6 +166,10 @@ public static boolean fits(@Nonnull Inventory inv, @Nonnull ItemStack item, int. * @return Whether the slots have space for the given {@link ItemStack ItemStacks} */ public static boolean fitAll(@Nonnull Inventory inv, @Nonnull ItemStack[] items, int... slots) { + Validate.notNull(inv, "The inventory cannot be null"); + Validate.notNull(items, "The items cannot be null"); + Validate.noNullElements(items, "The items array cannot have null elements"); + if (slots.length == 0) { slots = IntStream.range(0, inv.getSize()).toArray(); } @@ -210,6 +227,9 @@ public static boolean fitAll(@Nonnull Inventory inv, @Nonnull ItemStack[] items, * @return Whether the operation was successful */ public static boolean removeItem(@Nonnull Inventory inv, int amount, boolean replaceConsumables, @Nonnull Predicate predicate) { + Validate.notNull(inv, "The inventory cannot be null"); + Validate.notNull(predicate, "The predicate cannot be null"); + int removed = 0; for (int slot = 0; slot < inv.getSize(); slot++) { ItemStack item = inv.getItem(slot); From 4815a8bc34d8eaeb8bbb7be24a4a1e5ab4460618 Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:13:57 +0200 Subject: [PATCH 06/12] Enforce nullability in dough-items --- .../thebusybiscuit/dough/items/ItemMetaSnapshot.java | 7 +++++++ .../io/github/thebusybiscuit/dough/items/ItemUtils.java | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/dough-items/src/main/java/io/github/thebusybiscuit/dough/items/ItemMetaSnapshot.java b/dough-items/src/main/java/io/github/thebusybiscuit/dough/items/ItemMetaSnapshot.java index ac4da10b..445fd2a9 100644 --- a/dough-items/src/main/java/io/github/thebusybiscuit/dough/items/ItemMetaSnapshot.java +++ b/dough-items/src/main/java/io/github/thebusybiscuit/dough/items/ItemMetaSnapshot.java @@ -10,6 +10,7 @@ import javax.annotation.Nonnull; +import org.apache.commons.lang.Validate; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; @@ -44,6 +45,8 @@ public ItemMetaSnapshot(@Nonnull Supplier supplier) { } public ItemMetaSnapshot(@Nonnull ItemMeta meta) { + Validate.notNull(meta, "The ItemMeta cannot be null"); + this.displayName = meta.hasDisplayName() ? Optional.of(meta.getDisplayName()) : Optional.empty(); this.lore = meta.hasLore() ? Optional.of(Collections.unmodifiableList(meta.getLore())) : Optional.empty(); this.customModelData = meta.hasCustomModelData() ? OptionalInt.of(meta.getCustomModelData()) : OptionalInt.empty(); @@ -73,6 +76,8 @@ public ItemMetaSnapshot(@Nonnull ItemMeta meta) { } public boolean isSimilar(@Nonnull ItemMetaSnapshot snapshot) { + Validate.notNull(snapshot, "The snapshot cannot be null"); + if (snapshot.displayName.isPresent() != displayName.isPresent()) { return false; } else if (snapshot.displayName.isPresent() && displayName.isPresent() && !snapshot.displayName.get().equals(displayName.get())) { @@ -85,6 +90,8 @@ public boolean isSimilar(@Nonnull ItemMetaSnapshot snapshot) { } public boolean isSimilar(@Nonnull ItemMeta meta) { + Validate.notNull(meta, "The ItemMeta cannot be null"); + boolean hasDisplayName = meta.hasDisplayName(); if (hasDisplayName != displayName.isPresent()) { diff --git a/dough-items/src/main/java/io/github/thebusybiscuit/dough/items/ItemUtils.java b/dough-items/src/main/java/io/github/thebusybiscuit/dough/items/ItemUtils.java index 6d2664cf..00314c0a 100644 --- a/dough-items/src/main/java/io/github/thebusybiscuit/dough/items/ItemUtils.java +++ b/dough-items/src/main/java/io/github/thebusybiscuit/dough/items/ItemUtils.java @@ -7,6 +7,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; @@ -175,6 +176,7 @@ public static boolean canStack(@Nullable ItemStack a, @Nullable ItemStack b) { * Whether the Unbreaking Enchantment should be ignored */ public static void damageItem(@Nonnull ItemStack item, boolean ignoreEnchantments) { + Validate.notNull(item, "The item cannot be null"); damageItem(item, 1, ignoreEnchantments); } @@ -190,6 +192,8 @@ public static void damageItem(@Nonnull ItemStack item, boolean ignoreEnchantment * Whether the Unbreaking Enchantment should be ignored */ public static void damageItem(@Nonnull ItemStack item, int damage, boolean ignoreEnchantments) { + Validate.notNull(item, "The item cannot be null"); + if (item.getType() != Material.AIR && item.getAmount() > 0) { int remove = damage; @@ -226,6 +230,7 @@ public static void damageItem(@Nonnull ItemStack item, int damage, boolean ignor * {@link ItemUtils#consumeItem(ItemStack, int, boolean)} */ public static void consumeItem(@Nonnull ItemStack item, boolean replaceConsumables) { + Validate.notNull(item, "The item cannot be null"); consumeItem(item, 1, replaceConsumables); } @@ -250,6 +255,8 @@ public static void consumeItem(@Nonnull ItemStack item, boolean replaceConsumabl * Whether Items should be replaced with their "empty" version */ public static void consumeItem(@Nonnull ItemStack item, int amount, boolean replaceConsumables) { + Validate.notNull(item, "The item cannot be null"); + if (item.getType() != Material.AIR && item.getAmount() > 0) { if (replaceConsumables) { switch (item.getType()) { From 050d50717eae284b061a6cb01f90120ade099ddf Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:14:05 +0200 Subject: [PATCH 07/12] Enforce nullability in dough-data --- .../io/github/thebusybiscuit/dough/blocks/BlockPosition.java | 3 +++ .../io/github/thebusybiscuit/dough/blocks/ChunkPosition.java | 3 +++ 2 files changed, 6 insertions(+) diff --git a/dough-data/src/main/java/io/github/thebusybiscuit/dough/blocks/BlockPosition.java b/dough-data/src/main/java/io/github/thebusybiscuit/dough/blocks/BlockPosition.java index 12084799..1987e46b 100644 --- a/dough-data/src/main/java/io/github/thebusybiscuit/dough/blocks/BlockPosition.java +++ b/dough-data/src/main/java/io/github/thebusybiscuit/dough/blocks/BlockPosition.java @@ -4,6 +4,7 @@ import javax.annotation.Nonnull; +import org.apache.commons.lang.Validate; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; @@ -23,11 +24,13 @@ public final class BlockPosition { private final long position; public BlockPosition(@Nonnull World world, long position) { + Validate.notNull(world, "The world cannot be null"); this.world = new WeakReference<>(world); this.position = position; } public BlockPosition(@Nonnull World world, int x, int y, int z) { + Validate.notNull(world, "The world cannot be null"); this.world = new WeakReference<>(world); this.position = getAsLong(x, y, z); } diff --git a/dough-data/src/main/java/io/github/thebusybiscuit/dough/blocks/ChunkPosition.java b/dough-data/src/main/java/io/github/thebusybiscuit/dough/blocks/ChunkPosition.java index 9e429c85..78eb7e59 100644 --- a/dough-data/src/main/java/io/github/thebusybiscuit/dough/blocks/ChunkPosition.java +++ b/dough-data/src/main/java/io/github/thebusybiscuit/dough/blocks/ChunkPosition.java @@ -5,6 +5,7 @@ import javax.annotation.Nonnull; +import org.apache.commons.lang.Validate; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; @@ -25,11 +26,13 @@ public final class ChunkPosition { private final long position; public ChunkPosition(@Nonnull World world, long position) { + Validate.notNull(world, "The world cannot be null"); this.world = new WeakReference<>(world); this.position = position; } public ChunkPosition(@Nonnull World world, int x, int z) { + Validate.notNull(world, "The world cannot be null"); this.world = new WeakReference<>(world); this.position = getAsLong(x, z); } From c634c58b9cf71940a508645606e1663d874451c7 Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:14:16 +0200 Subject: [PATCH 08/12] Enforce nullability in dough-protection --- .../dough/protection/ProtectionManager.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/dough-protection/src/main/java/io/github/thebusybiscuit/dough/protection/ProtectionManager.java b/dough-protection/src/main/java/io/github/thebusybiscuit/dough/protection/ProtectionManager.java index 3b14feee..a72fe839 100644 --- a/dough-protection/src/main/java/io/github/thebusybiscuit/dough/protection/ProtectionManager.java +++ b/dough-protection/src/main/java/io/github/thebusybiscuit/dough/protection/ProtectionManager.java @@ -23,6 +23,7 @@ import io.github.thebusybiscuit.dough.protection.modules.RedProtectProtectionModule; import io.github.thebusybiscuit.dough.protection.modules.TownyProtectionModule; import io.github.thebusybiscuit.dough.protection.modules.WorldGuardProtectionModule; +import org.apache.commons.lang.Validate; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.Server; @@ -54,6 +55,8 @@ public final class ProtectionManager { */ @SuppressWarnings("java:S1612") public ProtectionManager(@Nonnull Server server) { + Validate.notNull(server, "The server cannot be null"); + logger = new DoughLogger(server, "protection"); logger.log(Level.INFO, "Loading Protection Modules..."); @@ -112,11 +115,16 @@ public ProtectionManager(@Nonnull Server server) { } public void registerLogger(@Nonnull String name, @Nonnull ProtectionLogger module) { + Validate.notNull(name, "The name cannot be null"); + Validate.notNull(module, "The module cannot be null"); protectionLoggers.add(module); loadModuleMSG(name); } public void registerModule(@Nonnull Server server, @Nonnull String pluginName, @Nonnull Function constructor) { + Validate.notNull(server, "The server cannot be null"); + Validate.notNull(pluginName, "The plugin name cannot be null"); + Validate.notNull(constructor, "The constructor cannot be null"); Plugin plugin = server.getPluginManager().getPlugin(pluginName); if (plugin != null && plugin.isEnabled()) { @@ -137,6 +145,8 @@ private void registerModule(@Nonnull Plugin plugin, @Nonnull Function Date: Wed, 30 Jun 2021 13:14:31 +0200 Subject: [PATCH 09/12] Enforce nullability in dough-recipes --- .../dough/recipes/MinecraftRecipe.java | 4 +++- .../dough/recipes/RecipeSnapshot.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/dough-recipes/src/main/java/io/github/thebusybiscuit/dough/recipes/MinecraftRecipe.java b/dough-recipes/src/main/java/io/github/thebusybiscuit/dough/recipes/MinecraftRecipe.java index 3b49d8a8..a6970fdc 100644 --- a/dough-recipes/src/main/java/io/github/thebusybiscuit/dough/recipes/MinecraftRecipe.java +++ b/dough-recipes/src/main/java/io/github/thebusybiscuit/dough/recipes/MinecraftRecipe.java @@ -16,6 +16,7 @@ import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; +import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.inventory.BlastingRecipe; import org.bukkit.inventory.CampfireRecipe; @@ -109,7 +110,7 @@ public class MinecraftRecipe { } @ParametersAreNonnullByDefault - private static @Nullable MinecraftRecipe findRecipeType(Logger logger, String type, Function> supplier) { + private static @Nullable MinecraftRecipe findRecipeType(Logger logger, String type, Function> supplier) { try { return supplier.apply(type); } catch (Exception | LinkageError x) { @@ -162,6 +163,7 @@ protected boolean validate(@Nonnull ItemStack[] inputs) { @SuppressWarnings("unchecked") public static Optional> of(@Nonnull T recipe) { + Validate.notNull(recipe, "The recipe cannot be null"); Class recipeClass = recipe.getClass(); return recipeTypes.stream().filter(type -> type.getRecipeClass().isAssignableFrom(recipeClass)).findAny().map(type -> (MinecraftRecipe) type); diff --git a/dough-recipes/src/main/java/io/github/thebusybiscuit/dough/recipes/RecipeSnapshot.java b/dough-recipes/src/main/java/io/github/thebusybiscuit/dough/recipes/RecipeSnapshot.java index 7f1b1799..61fa7210 100644 --- a/dough-recipes/src/main/java/io/github/thebusybiscuit/dough/recipes/RecipeSnapshot.java +++ b/dough-recipes/src/main/java/io/github/thebusybiscuit/dough/recipes/RecipeSnapshot.java @@ -15,6 +15,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.Keyed; import org.bukkit.Material; @@ -43,6 +44,8 @@ public class RecipeSnapshot { * The Plugin running on the Server that serves as the Snapshot's source. */ public RecipeSnapshot(@Nonnull Plugin plugin) { + Validate.notNull(plugin, "The plugin cannot be null"); + Iterator iterator = plugin.getServer().recipeIterator(); plugin.getLogger().log(Level.INFO, "Collecting Snapshots of all Recipes..."); @@ -88,6 +91,7 @@ public Stream streamAllRecipes() { */ @Nonnull public Set getRecipes(@Nonnull Class recipeClass) { + Validate.notNull(recipeClass, "The recipe class cannot be null"); return stream(recipeClass).collect(Collectors.toSet()); } @@ -104,6 +108,7 @@ public Set getRecipes(@Nonnull Class recipeClass) { */ @Nonnull public Stream stream(@Nonnull Class recipeClass) { + Validate.notNull(recipeClass, "The recipe class cannot be null"); return recipes.entrySet().stream().filter(entry -> recipeClass.isAssignableFrom(entry.getKey())).flatMap(entry -> entry.getValue().stream()).map(recipeClass::cast); } @@ -122,6 +127,8 @@ public Stream stream(@Nonnull Class recipeClass) { */ @Nonnull public RecipeChoice[] getRecipeInput(@Nonnull MinecraftRecipe recipeType, @Nonnull T recipe) { + Validate.notNull(recipeType, "The recipe type cannot be null"); + Validate.notNull(recipe, "The recipe cannot be null"); return recipeType.getInputs(recipe); } @@ -142,6 +149,8 @@ public RecipeChoice[] getRecipeInput(@Nonnull MinecraftRecipe */ @Nonnull public RecipeChoice[] getRecipeInput(@Nonnull T recipe) { + Validate.notNull(recipe, "The recipe cannot be null"); + Optional> type = MinecraftRecipe.of(recipe); if (type.isPresent()) { @@ -168,6 +177,8 @@ public RecipeChoice[] getRecipeInput(@Nonnull T recipe) { */ @Nonnull public Optional getRecipeOutput(@Nonnull MinecraftRecipe recipeType, ItemStack... inputs) { + Validate.notNull(recipeType, "The recipe type cannot be null"); + if (recipeType.validate(inputs)) { return recipeType.getOutput(stream(recipeType.getRecipeClass()), inputs); } else { @@ -185,6 +196,7 @@ public Optional getRecipeOutput(@Nonnull Minecraft */ @Nonnull public Set getRecipes(@Nonnull Predicate predicate) { + Validate.notNull(predicate, "The predicate cannot be null"); return streamAllRecipes().filter(predicate).collect(Collectors.toSet()); } @@ -199,6 +211,7 @@ public Set getRecipes(@Nonnull Predicate predicate) { */ @Nonnull public Set getRecipesFor(@Nonnull Material type) { + Validate.notNull(type, "The type cannot be null"); return getRecipes(recipe -> recipe.getResult().getType() == type); } @@ -212,6 +225,7 @@ public Set getRecipesFor(@Nonnull Material type) { */ @Nonnull public Set getRecipesFor(@Nonnull ItemStack item) { + Validate.notNull(item, "The item cannot be null"); return getRecipes(recipe -> recipe.getResult().isSimilar(item)); } @@ -227,6 +241,7 @@ public Set getRecipesFor(@Nonnull ItemStack item) { */ @Nonnull public Set getRecipesWith(@Nonnull ItemStack item) { + Validate.notNull(item, "The item cannot be null"); return getRecipes(recipe -> Arrays.stream(getRecipeInput(recipe)).anyMatch(choice -> choice.test(item))); } @@ -244,6 +259,7 @@ public Set getRecipesWith(@Nonnull ItemStack item) { */ @Nullable public Recipe getRecipe(@Nonnull NamespacedKey key) { + Validate.notNull(key, "The key cannot be null"); return keyedRecipes.get(key); } From d146133b3b547f586f152c89df7268ce8db63e05 Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:14:42 +0200 Subject: [PATCH 10/12] Enforce nullability in dough-scheduling --- .../dough/scheduling/TaskNode.java | 6 ++++- .../dough/scheduling/TaskQueue.java | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/dough-scheduling/src/main/java/io/github/thebusybiscuit/dough/scheduling/TaskNode.java b/dough-scheduling/src/main/java/io/github/thebusybiscuit/dough/scheduling/TaskNode.java index 62476b4f..f1c7e85b 100644 --- a/dough-scheduling/src/main/java/io/github/thebusybiscuit/dough/scheduling/TaskNode.java +++ b/dough-scheduling/src/main/java/io/github/thebusybiscuit/dough/scheduling/TaskNode.java @@ -16,11 +16,15 @@ class TaskNode { private TaskNode nextNode; protected TaskNode(@Nonnull IntConsumer consumer, boolean async) { + Validate.notNull(consumer, "The consumer cannot be null"); + this.runnable = consumer; this.asynchronous = async; } protected TaskNode(@Nonnull IntConsumer consumer, int delay, boolean async) { + Validate.notNull(consumer, "The consumer cannot be null"); + this.runnable = consumer; this.delay = delay; this.asynchronous = async; @@ -56,4 +60,4 @@ public void setDelay(int delay) { this.delay = delay; } -} \ No newline at end of file +} diff --git a/dough-scheduling/src/main/java/io/github/thebusybiscuit/dough/scheduling/TaskQueue.java b/dough-scheduling/src/main/java/io/github/thebusybiscuit/dough/scheduling/TaskQueue.java index 8a40637b..5e3cfad0 100644 --- a/dough-scheduling/src/main/java/io/github/thebusybiscuit/dough/scheduling/TaskQueue.java +++ b/dough-scheduling/src/main/java/io/github/thebusybiscuit/dough/scheduling/TaskQueue.java @@ -5,6 +5,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; @@ -32,6 +33,7 @@ public class TaskQueue { * The plugin that is performing this execution */ public void execute(@Nonnull Plugin plugin) { + Validate.notNull(plugin, "Plugin cannot be null"); if (head == null) { throw new IllegalStateException("Cannot execute TaskQueue, no head was found"); } @@ -40,6 +42,7 @@ public void execute(@Nonnull Plugin plugin) { } private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { + Validate.notNull(plugin, "Plugin cannot be null"); if (node == null) { return; } @@ -74,6 +77,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRun(@Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); return append(new TaskNode(consumer, false)); } @@ -86,6 +90,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRun(@Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); return thenRun(i -> runnable.run()); } @@ -99,6 +104,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRunAsynchronously(@Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); return append(new TaskNode(consumer, true)); } @@ -111,6 +117,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRunAsynchronously(@Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); return thenRunAsynchronously(i -> runnable.run()); } @@ -126,6 +133,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRun(int ticks, @Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); if (ticks < 1) { throw new IllegalArgumentException("thenAfter() must be given a time that is greater than zero!"); } @@ -144,6 +152,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRun(int ticks, @Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); return thenRun(ticks, i -> runnable.run()); } @@ -159,6 +168,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRunAsynchronously(int ticks, @Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); if (ticks < 1) { throw new IllegalArgumentException("thenAfter() must be given a time that is greater than zero!"); } @@ -177,6 +187,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRunAsynchronously(int ticks, @Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); return thenRunAsynchronously(ticks, i -> runnable.run()); } @@ -193,6 +204,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRepeat(int iterations, @Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); for (int i = 0; i < iterations; i++) { append(new TaskNode(consumer, false)); } @@ -212,6 +224,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRepeat(int iterations, @Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); return thenRepeat(iterations, i -> runnable.run()); } @@ -228,6 +241,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRepeatAsynchronously(int iterations, @Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); for (int i = 0; i < iterations; i++) { append(new TaskNode(consumer, true)); } @@ -247,6 +261,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRepeatAsynchronously(int iterations, @Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); return thenRepeatAsynchronously(iterations, i -> runnable.run()); } @@ -290,6 +305,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRepeatEvery(int ticks, int iterations, @Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); return thenRepeatEvery(ticks, iterations, i -> runnable.run()); } @@ -308,6 +324,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRepeatEveryAsynchronously(int ticks, int iterations, @Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); if (ticks < 1) { throw new IllegalArgumentException("thenRepeatEveryAsynchronously() must be given a time that is greater than zero!"); } @@ -333,6 +350,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * @return The current instance of {@link TaskQueue} */ public @Nonnull TaskQueue thenRepeatEveryAsynchronously(int ticks, int iterations, @Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); return thenRepeatEveryAsynchronously(ticks, iterations, i -> runnable.run()); } @@ -345,6 +363,7 @@ private void run(@Nonnull Plugin plugin, @Nullable TaskNode node, int index) { * The callback to run */ public void thenLoop(@Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); TaskNode node = new TaskNode(consumer, false); node.setNextNode(node); append(node); @@ -359,6 +378,7 @@ public void thenLoop(@Nonnull IntConsumer consumer) { * The callback to run */ public void thenLoop(@Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); thenLoop(i -> runnable.run()); } @@ -371,6 +391,7 @@ public void thenLoop(@Nonnull Runnable runnable) { * The callback to run */ public void thenLoopAsynchronously(@Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); TaskNode node = new TaskNode(consumer, true); node.setNextNode(node); append(node); @@ -385,6 +406,7 @@ public void thenLoopAsynchronously(@Nonnull IntConsumer consumer) { * The callback to run */ public void thenLoopAsynchronously(@Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); thenLoopAsynchronously(i -> runnable.run()); } @@ -399,6 +421,7 @@ public void thenLoopAsynchronously(@Nonnull Runnable runnable) { * The callback to run */ public void thenLoopEvery(int ticks, @Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); if (ticks < 1) { throw new IllegalArgumentException("thenLoopEvery() must be given a time that is greater than zero!"); } @@ -419,6 +442,7 @@ public void thenLoopEvery(int ticks, @Nonnull IntConsumer consumer) { * The callback to run */ public void thenLoopEvery(int ticks, @Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); thenLoopEvery(ticks, i -> runnable.run()); } @@ -433,6 +457,8 @@ public void thenLoopEvery(int ticks, @Nonnull Runnable runnable) { * The callback to run */ public void thenLoopEveryAsynchronously(int ticks, @Nonnull IntConsumer consumer) { + Validate.notNull(consumer, "The consumer cannot be null"); + if (ticks < 1) { throw new IllegalArgumentException("thenLoopEveryAsynchronously() must be given a time that is greater than zero!"); } @@ -453,6 +479,7 @@ public void thenLoopEveryAsynchronously(int ticks, @Nonnull IntConsumer consumer * The callback to run */ public void thenLoopEveryAsynchronously(int ticks, @Nonnull Runnable runnable) { + Validate.notNull(runnable, "The runnable cannot be null"); thenLoopEveryAsynchronously(ticks, i -> runnable.run()); } From 5657d0d23164381c6ce3194611e1b5cdea78c36c Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:15:07 +0200 Subject: [PATCH 11/12] Enforce nullability in dough-tags --- .../thebusybiscuit/dough/tags/CustomMaterialTag.java | 7 +++++++ .../io/github/thebusybiscuit/dough/tags/TagParser.java | 1 + 2 files changed, 8 insertions(+) diff --git a/dough-tags/src/main/java/io/github/thebusybiscuit/dough/tags/CustomMaterialTag.java b/dough-tags/src/main/java/io/github/thebusybiscuit/dough/tags/CustomMaterialTag.java index d8d47531..41497a1f 100644 --- a/dough-tags/src/main/java/io/github/thebusybiscuit/dough/tags/CustomMaterialTag.java +++ b/dough-tags/src/main/java/io/github/thebusybiscuit/dough/tags/CustomMaterialTag.java @@ -10,6 +10,7 @@ import javax.annotation.Nonnull; +import org.apache.commons.lang.Validate; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Tag; @@ -41,22 +42,27 @@ public class CustomMaterialTag implements Tag { * {@link Slimefun} and {@link #name()}. */ public CustomMaterialTag(@Nonnull NamespacedKey key) { + Validate.notNull(key, "The key cannot be null"); this.key = key; } public void loadFromStream(@Nonnull InputStream inputStream) throws TagMisconfigurationException { + Validate.notNull(inputStream, "The InputStream cannot be null"); load(new TagParser(key).parse(inputStream)); } public void loadFromJson(@Nonnull JsonObject json) throws TagMisconfigurationException { + Validate.notNull(json, "The JsonObject cannot be null"); load(new TagParser(key).parse(json)); } public void loadFromString(@Nonnull String json) throws TagMisconfigurationException { + Validate.notNull(json, "The json string cannot be null"); load(new TagParser(key).parse(json)); } private void load(@Nonnull CompletableFuture future) { + Validate.notNull(future, "The future cannot be null"); future.thenAccept(data -> { this.includedMaterials.clear(); this.includedMaterials.addAll(data.getMaterials()); @@ -73,6 +79,7 @@ private void load(@Nonnull CompletableFuture future) { @Override public boolean isTagged(@Nonnull Material item) { + Validate.notNull(item, "The item cannot be null"); if (includedMaterials.contains(item)) { return true; } else { diff --git a/dough-tags/src/main/java/io/github/thebusybiscuit/dough/tags/TagParser.java b/dough-tags/src/main/java/io/github/thebusybiscuit/dough/tags/TagParser.java index d7c9b6f2..8c997fa3 100644 --- a/dough-tags/src/main/java/io/github/thebusybiscuit/dough/tags/TagParser.java +++ b/dough-tags/src/main/java/io/github/thebusybiscuit/dough/tags/TagParser.java @@ -53,6 +53,7 @@ public class TagParser implements Keyed { * The {@link NamespacedKey} of the resulting {@link CustomMaterialTag} */ public TagParser(@Nonnull NamespacedKey key) { + Validate.notNull(key, "The key cannot be null"); this.key = key; } From 58cc58956cd63d3e5d4fe9fcee91d9926ce63ede Mon Sep 17 00:00:00 2001 From: Andrew Wong <42793301+md5sha256@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:15:17 +0200 Subject: [PATCH 12/12] Enforce nullability in dough-updater --- .../io/github/thebusybiscuit/dough/updater/UpdateInfo.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dough-updater/src/main/java/io/github/thebusybiscuit/dough/updater/UpdateInfo.java b/dough-updater/src/main/java/io/github/thebusybiscuit/dough/updater/UpdateInfo.java index 10dce157..e31e3ecb 100644 --- a/dough-updater/src/main/java/io/github/thebusybiscuit/dough/updater/UpdateInfo.java +++ b/dough-updater/src/main/java/io/github/thebusybiscuit/dough/updater/UpdateInfo.java @@ -5,6 +5,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import io.github.thebusybiscuit.dough.versions.Version; +import org.apache.commons.lang.Validate; // TODO: Convert to Java 16 record class UpdateInfo { @@ -14,6 +15,9 @@ class UpdateInfo { @ParametersAreNonnullByDefault UpdateInfo(URL url, Version version) { + Validate.notNull(url, "The URL cannot be null"); + Validate.notNull(version, "The version cannot be null"); + this.url = url; this.version = version; }