mirror of
https://github.com/filoghost/ChestCommands.git
synced 2024-11-22 18:16:14 +01:00
Refactor Validate class, rename to Preconditions
This commit is contained in:
parent
e5591ed97c
commit
80f1cca3d4
@ -21,7 +21,7 @@ import org.bukkit.inventory.Inventory;
|
||||
import me.filoghost.chestcommands.internal.MenuInventoryHolder;
|
||||
import me.filoghost.chestcommands.util.ItemUtils;
|
||||
import me.filoghost.chestcommands.util.Utils;
|
||||
import me.filoghost.chestcommands.util.Validate;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@ -87,7 +87,7 @@ public class IconMenu {
|
||||
}
|
||||
|
||||
public void open(Player player) {
|
||||
Validate.notNull(player, "Player cannot be null");
|
||||
Preconditions.notNull(player, "player");
|
||||
|
||||
Inventory inventory = Bukkit.createInventory(new MenuInventoryHolder(this), icons.length, title);
|
||||
|
||||
|
@ -15,6 +15,8 @@
|
||||
package me.filoghost.chestcommands.bridge;
|
||||
|
||||
import me.confuser.barapi.BarAPI;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@ -40,8 +42,8 @@ public class BarAPIBridge {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void setMessage(Player player, String message, int seconds) {
|
||||
if (!hasValidPlugin()) throw new IllegalStateException("BarAPI plugin was not found!");
|
||||
|
||||
Preconditions.checkState(hasValidPlugin(), "BarAPI plugin not found");
|
||||
|
||||
BarAPI.setMessage(player, message, seconds);
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
import me.filoghost.chestcommands.MenuManager;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import net.milkbowl.vault.economy.EconomyResponse;
|
||||
|
||||
@ -30,11 +31,11 @@ public class EconomyBridge {
|
||||
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
|
||||
return false;
|
||||
}
|
||||
RegisteredServiceProvider<Economy> rsp = Bukkit.getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null) {
|
||||
RegisteredServiceProvider<Economy> economyServiceProvider = Bukkit.getServicesManager().getRegistration(Economy.class);
|
||||
if (economyServiceProvider == null) {
|
||||
return false;
|
||||
}
|
||||
economy = rsp.getProvider();
|
||||
economy = economyServiceProvider.getProvider();
|
||||
return economy != null;
|
||||
}
|
||||
|
||||
@ -88,15 +89,11 @@ public class EconomyBridge {
|
||||
}
|
||||
|
||||
private static void checkValidEconomy() {
|
||||
if (!hasValidEconomy()) {
|
||||
throw new IllegalStateException("Economy plugin was not found!");
|
||||
}
|
||||
Preconditions.checkState(hasValidEconomy(), "economy plugin not found");
|
||||
}
|
||||
|
||||
private static void checkPositiveAmount(double amount) {
|
||||
if (amount < 0.0) {
|
||||
throw new IllegalArgumentException("Invalid amount of money: " + amount);
|
||||
}
|
||||
Preconditions.checkArgument(amount >= 0.0, "amount cannot be negative");
|
||||
}
|
||||
|
||||
public static String formatMoney(double amount) {
|
||||
|
@ -1,44 +1,42 @@
|
||||
package me.filoghost.chestcommands.bridge;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class PlaceholderAPIBridge {
|
||||
|
||||
private static PlaceholderAPIPlugin placeholderAPI;
|
||||
|
||||
public static boolean setupPlugin() {
|
||||
Plugin placeholderPlugin = Bukkit.getPluginManager().getPlugin("PlaceholderAPI");
|
||||
|
||||
if (placeholderPlugin == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
placeholderAPI = (PlaceholderAPIPlugin) placeholderPlugin;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean hasValidPlugin() {
|
||||
return placeholderAPI != null;
|
||||
}
|
||||
|
||||
public static boolean hasPlaceholders(String message) {
|
||||
if (!hasValidPlugin()) {
|
||||
throw new IllegalStateException("PlaceholderAPI plugin was not found!");
|
||||
}
|
||||
|
||||
return PlaceholderAPI.containsPlaceholders(message);
|
||||
}
|
||||
|
||||
public static String setPlaceholders(String message, Player executor) {
|
||||
if (!hasValidPlugin()) {
|
||||
throw new IllegalStateException("PlaceholderAPI plugin was not found!");
|
||||
}
|
||||
|
||||
return PlaceholderAPI.setPlaceholders(executor, message);
|
||||
}
|
||||
|
||||
}
|
||||
package me.filoghost.chestcommands.bridge;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class PlaceholderAPIBridge {
|
||||
|
||||
private static PlaceholderAPIPlugin placeholderAPI;
|
||||
|
||||
public static boolean setupPlugin() {
|
||||
Plugin placeholderPlugin = Bukkit.getPluginManager().getPlugin("PlaceholderAPI");
|
||||
|
||||
if (placeholderPlugin == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
placeholderAPI = (PlaceholderAPIPlugin) placeholderPlugin;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean hasValidPlugin() {
|
||||
return placeholderAPI != null;
|
||||
}
|
||||
|
||||
public static boolean hasPlaceholders(String message) {
|
||||
Preconditions.checkState(hasValidPlugin(), "PlaceholderAPI plugin not found");
|
||||
|
||||
return PlaceholderAPI.containsPlaceholders(message);
|
||||
}
|
||||
|
||||
public static String setPlaceholders(String message, Player executor) {
|
||||
Preconditions.checkState(hasValidPlugin(), "PlaceholderAPI plugin not found");
|
||||
|
||||
return PlaceholderAPI.setPlaceholders(executor, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.filoghost.chestcommands.util.Validate;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
|
||||
public class OpenTrigger {
|
||||
|
||||
@ -28,10 +28,9 @@ public class OpenTrigger {
|
||||
private boolean isRestrictiveDurability;
|
||||
|
||||
public OpenTrigger(Material material, ClickType clickType) {
|
||||
Validate.notNull(material, "Material cannot be null");
|
||||
Validate.notNull(material, "ClickType cannot be null");
|
||||
Validate.isTrue(material != Material.AIR, "Material cannot be AIR");
|
||||
|
||||
Preconditions.checkArgumentNotAir(material, "material");
|
||||
Preconditions.notNull(clickType, "clickType");
|
||||
|
||||
this.material = material;
|
||||
this.clickType = clickType;
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import me.filoghost.chestcommands.util.Validate;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
|
||||
public class RequiredItem {
|
||||
|
||||
@ -28,8 +28,7 @@ public class RequiredItem {
|
||||
private boolean isDurabilityRestrictive = false;
|
||||
|
||||
public RequiredItem(Material material, int amount) {
|
||||
Validate.notNull(material, "Material cannot be null");
|
||||
Validate.isTrue(material != Material.AIR, "Material cannot be air");
|
||||
Preconditions.checkArgumentNotAir(material, "material");
|
||||
|
||||
this.material = material;
|
||||
this.amount = amount;
|
||||
@ -48,7 +47,7 @@ public class RequiredItem {
|
||||
}
|
||||
|
||||
public void setRestrictiveDataValue(short data) {
|
||||
Validate.isTrue(data >= 0, "Data value cannot be negative");
|
||||
Preconditions.checkArgument(data >= 0, "Data value cannot be negative");
|
||||
|
||||
this.dataValue = data;
|
||||
isDurabilityRestrictive = true;
|
||||
|
@ -35,7 +35,7 @@ import me.filoghost.chestcommands.parser.EnchantmentParser.EnchantmentDetails;
|
||||
import me.filoghost.chestcommands.util.ErrorCollector;
|
||||
import me.filoghost.chestcommands.util.FormatUtils;
|
||||
import me.filoghost.chestcommands.util.ItemUtils;
|
||||
import me.filoghost.chestcommands.util.Validate;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
import me.filoghost.chestcommands.util.nbt.parser.MojangsonParseException;
|
||||
import me.filoghost.chestcommands.util.nbt.parser.MojangsonParser;
|
||||
|
||||
@ -94,7 +94,7 @@ public class IconParser {
|
||||
|
||||
|
||||
public static Icon loadIconFromSection(ConfigurationSection section, String iconName, String menuFileName, ErrorCollector errorCollector) {
|
||||
Validate.notNull(section, "ConfigurationSection cannot be null");
|
||||
Preconditions.notNull(section, "section");
|
||||
|
||||
// The icon is valid even without a Material
|
||||
ExtendedIcon icon = new ExtendedIcon();
|
||||
@ -245,7 +245,7 @@ public class IconParser {
|
||||
|
||||
|
||||
public static Coords loadCoordsFromSection(ConfigurationSection section) {
|
||||
Validate.notNull(section, "ConfigurationSection cannot be null");
|
||||
Preconditions.notNull(section, "section");
|
||||
|
||||
Integer x = null;
|
||||
Integer y = null;
|
||||
|
@ -20,7 +20,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
import me.filoghost.chestcommands.exception.FormatException;
|
||||
import me.filoghost.chestcommands.util.MaterialsHelper;
|
||||
import me.filoghost.chestcommands.util.Utils;
|
||||
import me.filoghost.chestcommands.util.Validate;
|
||||
import me.filoghost.chestcommands.util.Preconditions;
|
||||
|
||||
public class ItemStackParser {
|
||||
|
||||
@ -35,7 +35,7 @@ public class ItemStackParser {
|
||||
* for example wool:5, 3 is a valid input.
|
||||
*/
|
||||
public ItemStackParser(String input, boolean parseAmount) throws FormatException {
|
||||
Validate.notNull(input, "input cannot be null");
|
||||
Preconditions.notNull(input, "input");
|
||||
|
||||
// Remove spaces, they're not needed
|
||||
input = input.replace(" ", "");
|
||||
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package me.filoghost.chestcommands.util;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
public final class Preconditions {
|
||||
|
||||
private Preconditions() {}
|
||||
|
||||
public static void notNull(Object object, String objectName) {
|
||||
if (object == null) {
|
||||
throw new NullPointerException(objectName + " cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkArgument(boolean expression, String errorMessage) {
|
||||
if (!expression) {
|
||||
throw new IllegalArgumentException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkState(boolean expression, String errorMessage) {
|
||||
if (!expression) {
|
||||
throw new IllegalStateException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkArgumentNotAir(Material material, String objectName) {
|
||||
notNull(material, objectName);
|
||||
if (MaterialsHelper.isAir(material)) {
|
||||
throw new IllegalArgumentException(objectName + " cannot be " + material);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -55,9 +55,7 @@ public class Registry<T> {
|
||||
}
|
||||
|
||||
public void putIfEnumExists(String key, String enumValueName) {
|
||||
if (!valuesType.isEnum()) {
|
||||
throw new IllegalArgumentException("Value type is not an enum");
|
||||
}
|
||||
Preconditions.checkState(valuesType.isEnum(), "value type is not an enum");
|
||||
|
||||
try {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
|
@ -1,34 +0,0 @@
|
||||
/*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package me.filoghost.chestcommands.util;
|
||||
|
||||
public final class Validate {
|
||||
|
||||
private Validate() {
|
||||
}
|
||||
|
||||
public static void notNull(Object object, String error) {
|
||||
if (object == null) {
|
||||
throw new NullPointerException(error);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isTrue(boolean statement, String error) {
|
||||
if (!statement) {
|
||||
throw new IllegalArgumentException(error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user