Skip to content

Commit

Permalink
chore: 移除了许多异常判断
Browse files Browse the repository at this point in the history
事实上这些异常不大会发生,追求性能,无所畏惧
  • Loading branch information
mcchampions committed Nov 22, 2024
1 parent 9dc2d46 commit d61af6c
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public ItemStack getItem() {
* being the {@link ItemStack} you want to change the item to.
*/
public void setItem(ItemStack output) {
if (output == null || output.getType() == Material.AIR) {
throw new IllegalArgumentException("An Ancient Altar cannot drop 'null' items");
}

this.output = output;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ public class ItemSetting<T> {
/**
* This creates a new {@link ItemSetting} with the given key and default value
*
* @param item
* The {@link SlimefunItem} this {@link ItemSetting} belongs to
* @param key
* The key under which this setting will be stored (relative to the {@link SlimefunItem})
* @param defaultValue
* The default value for this {@link ItemSetting}
* @param item The {@link SlimefunItem} this {@link ItemSetting} belongs to
* @param key The key under which this setting will be stored (relative to the {@link SlimefunItem})
* @param defaultValue The default value for this {@link ItemSetting}
*/
public ItemSetting(SlimefunItem item, String key, T defaultValue) {
this.item = item;
Expand All @@ -45,9 +42,7 @@ public ItemSetting(SlimefunItem item, String key, T defaultValue) {
* This method checks if a given input would be valid as a value for this
* {@link ItemSetting}. You can override this method to implement your own checks.
*
* @param input
* The input value to validate
*
* @param input The input value to validate
* @return Whether the given input was valid
*/
public boolean validateInput(T input) {
Expand All @@ -59,17 +54,10 @@ public boolean validateInput(T input) {
* Override this method to catch changes of a value.
* A value may never be null.
*
* @param newValue
* The new value for this {@link ItemSetting}
* @param newValue The new value for this {@link ItemSetting}
*/
public void update(T newValue) {
if (validateInput(newValue)) {
this.value = newValue;
} else {
throw new IllegalArgumentException("The passed value was not valid. (Maybe null?)");
}

// Feel free to override this as necessary.
this.value = newValue;
}

/**
Expand Down Expand Up @@ -105,9 +93,7 @@ public T getValue() {
/**
* This method checks if this {@link ItemSetting} stores the given data type.
*
* @param c
* The class of data type you want to compare
*
* @param c The class of data type you want to compare
* @return Whether this {@link ItemSetting} stores the given type
*/
public boolean isType(Class<?> c) {
Expand All @@ -127,7 +113,6 @@ protected String getErrorMessage() {
/**
* This method is called by a {@link SlimefunItem} which wants to load its {@link ItemSetting}
* from the {@link Config} file.
*
*/
@SuppressWarnings("unchecked")
public void reload() {
Expand All @@ -143,16 +128,16 @@ public void reload() {
this.value = newValue;
} else {
item.warn("发现在 Items.yml 中有无效的物品设置!"
+ "\n\""
+ item.getId()
+ "."
+ getKey()
+ "\""
+ "\n "
+ configuredValue
+ " 不是一个有效值!"
+ "\n"
+ getErrorMessage());
+ "\n\""
+ item.getId()
+ "."
+ getKey()
+ "\""
+ "\n "
+ configuredValue
+ " 不是一个有效值!"
+ "\n"
+ getErrorMessage());

}
} else {
Expand All @@ -162,17 +147,17 @@ public void reload() {
: configuredValue.getClass().getSimpleName();

item.warn("发现在 Items.yml 中有无效的物品设置!"
+ "\n请只设置有效的值."
+ "\n\""
+ item.getId()
+ "."
+ getKey()
+ "\""
+ "\n 期望值为 \""
+ defaultValue.getClass().getSimpleName()
+ "\" 但填写了: \""
+ found
+ "\"");
+ "\n请只设置有效的值."
+ "\n\""
+ item.getId()
+ "."
+ getKey()
+ "\""
+ "\n 期望值为 \""
+ defaultValue.getClass().getSimpleName()
+ "\" 但填写了: \""
+ found
+ "\"");

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,6 @@ public boolean isDisabledIn(World world) {
* @return The {@link SlimefunAddon} that registered this {@link SlimefunItem}
*/
public final SlimefunAddon getAddon() {
if (addon == null) {
throw new UnregisteredItemException(this);
}

return addon;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ public void register(SlimefunAddon addon) {
* @see #removeParent(ItemGroup)
*/
public void addParent(ItemGroup group) {
if (group == this || group == null) {
throw new IllegalArgumentException("ItemGroup '"
+ item.getItemMeta().getDisplayName()
+ "' cannot be a parent of itself or have a 'null' parent.");
}

parents.add(group);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static void bindItem(ItemStack item, PlayerBackpack bp) {
}

public static void setItemDisplayInfo(ItemStack item, PlayerBackpack bp) {
var meta = item.getItemMeta();
ItemMeta meta = item.getItemMeta();
setItem(meta, bp);
item.setItemMeta(meta);
}
Expand Down Expand Up @@ -210,10 +210,6 @@ private static void setItem(ItemMeta meta, PlayerBackpack bp) {

public PlayerBackpack(
OfflinePlayer owner, UUID uuid, String name, int id, int size, @Nullable ItemStack[] contents) {
if (size < 9 || size > 54 || size % 9 != 0) {
throw new IllegalArgumentException("Invalid size! Size must be one of: [9, 18, 27, 36, 45, 54]");
}

this.owner = owner;
this.uuid = uuid;
this.name = name;
Expand All @@ -225,9 +221,6 @@ public PlayerBackpack(
return;
}

if (size != contents.length) {
throw new IllegalArgumentException("Invalid contents: size mismatched!");
}
inventory.setContents(contents);
}

Expand Down Expand Up @@ -263,10 +256,6 @@ public void open(Player p) {
* The new size for this Backpack
*/
public void setSize(int size) {
if (size < 9 || size > 54 || size % 9 != 0) {
throw new IllegalArgumentException("Invalid size! Size must be one of: [9, 18, 27, 36, 45, 54]");
}

this.size = size;
updateInv();
Slimefun.getDatabaseManager().getProfileDataController().saveBackpackInfo(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class Research implements Keyed {
private final int id;
private final String name;
private boolean enabled = true;
/**
* -- SETTER --
* Sets the cost in XP levels to unlock this
*/
@Setter
@Getter
private int levelCost;
@Setter
Expand Down Expand Up @@ -161,26 +166,9 @@ public int getCost() {
*/
@Deprecated
public void setCost(int cost) {
if (levelCost < 0) {
throw new IllegalArgumentException("Research cost must be zero or greater!");
}

levelCost = cost;
}

/**
* Sets the cost in XP levels to unlock this {@link Research}.
*
* @param levelCost The cost in XP levels
*/
public void setLevelCost(int levelCost) {
if (levelCost < 0) {
throw new IllegalArgumentException("Research cost must be zero or greater!");
}

this.levelCost = levelCost;
}

/**
* Bind the specified {@link SlimefunItem SlimefunItems} to this {@link Research}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,8 @@ public interface Rechargeable extends ItemAttribute {
* The amount of charge to store
*/
default void setItemCharge(ItemStack item, float charge) {
if (item == null || item.getType() == Material.AIR) {
throw new IllegalArgumentException("Cannot set Item charge for null or AIR");
}

float maximum = getMaxItemCharge(item);

if (charge < 0 || charge > maximum) {
throw new IllegalArgumentException("Charge must be between zero and " + maximum + ".");
}

ItemMeta meta = item.getItemMeta();
ChargeUtils.setCharge(meta, charge, maximum);
item.setItemMeta(meta);
Expand All @@ -68,10 +60,6 @@ default void setItemCharge(ItemStack item, float charge) {
* @return The charge stored on this {@link ItemStack}
*/
default float getItemCharge(ItemStack item) {
if (item == null || item.getType() == Material.AIR) {
throw new IllegalArgumentException("Cannot get Item charge for null or AIR");
}

return ChargeUtils.getCharge(item.getItemMeta());
}

Expand All @@ -96,10 +84,6 @@ default float getDistanceToMaxCharge(ItemStack item) {
* @return Whether the given charge could be added successfully
*/
default boolean addItemCharge(ItemStack item, float charge) {
if (item == null || item.getType() == Material.AIR) {
throw new IllegalArgumentException("Cannot add Item charge for null or AIR");
}

ItemMeta meta = item.getItemMeta();
float currentCharge = ChargeUtils.getCharge(meta);
float maximum = getMaxItemCharge(item);
Expand Down Expand Up @@ -128,10 +112,6 @@ default boolean addItemCharge(ItemStack item, float charge) {
* @return Whether the given charge could be removed successfully
*/
default boolean removeItemCharge(ItemStack item, float charge) {
if (item == null || item.getType() == Material.AIR) {
throw new IllegalArgumentException("Cannot remove Item charge for null or AIR");
}

ItemMeta meta = item.getItemMeta();
float currentCharge = ChargeUtils.getCharge(meta);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ public static Set<Tag<Material>> getSupportedTags() {
private final boolean isSymmetric;

public MultiBlock(SlimefunItem item, Material[] build, BlockFace trigger) {
if (build == null || build.length != 9) {
throw new IllegalArgumentException("MultiBlocks must have a length of 9!");
}

if (trigger != BlockFace.SELF && trigger != BlockFace.UP && trigger != BlockFace.DOWN) {
throw new IllegalArgumentException("Multiblock Blockface must be either UP, DOWN or SELF");
}

this.item = item;
this.blocks = build;
this.trigger = trigger;
Expand Down
Loading

0 comments on commit d61af6c

Please sign in to comment.