Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce nullability in all modules #41

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

Expand All @@ -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<String> 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);
}

Expand All @@ -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<String> predicate, @Nonnull Consumer<String> 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
Expand Down Expand Up @@ -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<Player, String> 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);
}

Expand All @@ -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<String> predicate, @Nonnull BiConsumer<Player, String> 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
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;

/**
Expand All @@ -24,6 +25,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);
}

Expand All @@ -40,6 +42,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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
/**
* A utility {@link Logger} implementation which automatically sets the
* {@link Server} as its parent {@link Logger}.
*
*
* This allows us to properly log messages and warnings without the need
* for a {@link PluginLogger}.
*
*
* @author TheBusyBiscuit
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.annotation.Nonnull;

import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -38,6 +39,7 @@ private PlayerList() {}
* @return An Optional describing the player (or an empty Optional)
*/
public static @Nonnull Optional<Player> findByName(@Nonnull String name) {
Validate.notNull(name, "The name cannot be null");
return stream().filter(p -> p.getName().equalsIgnoreCase(name)).findAny();
}

Expand All @@ -50,6 +52,7 @@ private PlayerList() {}
* @return A Set of Players
*/
public static @Nonnull Set<Player> findPermitted(@Nonnull String permission) {
Validate.notNull(permission, "The permission cannot be null");
return stream().filter(p -> p.hasPermission(permission)).collect(Collectors.toSet());
}

Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -222,13 +235,15 @@ 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);
}
}

@SuppressWarnings("unchecked")
public <T> T getOrSetDefault(@Nonnull String path, T value) {
Validate.notNull(path, "The path cannot be null");
Object val = getValue(path);

if (value.getClass().isInstance(val)) {
Expand All @@ -247,6 +262,7 @@ public <T> 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);
}

Expand All @@ -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 <T> Optional<T> getValueAs(@Nonnull Class<T> 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();
}
Expand All @@ -279,6 +299,7 @@ public <T> Optional<T> getValueAs(@Nonnull Class<T> c, @Nonnull String path) {
*/
@Nullable
public ItemStack getItem(@Nonnull String path) {
Validate.notNull(path, "The path cannot be null");
return fileConfig.getItemStack(path);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -329,6 +353,7 @@ public boolean getBoolean(@Nonnull String path) {
*/
@Nonnull
public List<String> getStringList(@Nonnull String path) {
Validate.notNull(path, "The path cannot be null");
return fileConfig.getStringList(path);
}

Expand All @@ -342,6 +367,7 @@ public List<String> getStringList(@Nonnull String path) {
*/
@Nonnull
public List<Integer> getIntList(@Nonnull String path) {
Validate.notNull(path, "The path cannot be null");
return fileConfig.getIntegerList(path);
}

Expand All @@ -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)));
}

Expand All @@ -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)));
}

Expand All @@ -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));
}

Expand All @@ -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"));
}

Expand All @@ -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;
}
Expand All @@ -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));
}

Expand All @@ -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);
}

Expand All @@ -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"));
}

Expand All @@ -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));
Expand All @@ -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));

Expand Down Expand Up @@ -520,6 +560,7 @@ public Set<String> getKeys() {
*/
@Nonnull
public Set<String> 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);
}
Expand Down
Loading