From c31216f56939bc78054f3386655f8c1831399b21 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sun, 14 Jun 2020 12:53:25 +0200 Subject: [PATCH] Use modifiable and nullable collections where possible --- .../chestcommands/api/ConfigurableIcon.java | 2 -- .../api/impl/ConfigurableIconImpl.java | 29 +++++++++---------- .../chestcommands/menu/AdvancedIconMenu.java | 7 ++--- .../chestcommands/menu/icon/AdvancedIcon.java | 4 +-- .../menu/settings/MenuSettings.java | 5 ---- .../filoghost/chestcommands/util/Utils.java | 22 +++++++++++++- 6 files changed, 37 insertions(+), 32 deletions(-) diff --git a/API/src/main/java/me/filoghost/chestcommands/api/ConfigurableIcon.java b/API/src/main/java/me/filoghost/chestcommands/api/ConfigurableIcon.java index 5d2dbcb..5f6429d 100644 --- a/API/src/main/java/me/filoghost/chestcommands/api/ConfigurableIcon.java +++ b/API/src/main/java/me/filoghost/chestcommands/api/ConfigurableIcon.java @@ -69,8 +69,6 @@ public interface ConfigurableIcon extends Icon { void removeEnchantment(Enchantment ench); - void clearEnchantments(); - Color getLeatherColor(); void setLeatherColor(Color leatherColor); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/api/impl/ConfigurableIconImpl.java b/Plugin/src/main/java/me/filoghost/chestcommands/api/impl/ConfigurableIconImpl.java index 446a143..1d39611 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/api/impl/ConfigurableIconImpl.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/api/impl/ConfigurableIconImpl.java @@ -41,6 +41,7 @@ import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.api.ClickHandler; import me.filoghost.chestcommands.api.ClickResult; import me.filoghost.chestcommands.api.ConfigurableIcon; +import me.filoghost.chestcommands.util.Utils; import me.filoghost.chestcommands.variable.VariableManager; public class ConfigurableIconImpl implements ConfigurableIcon { @@ -67,7 +68,6 @@ public class ConfigurableIconImpl implements ConfigurableIcon { private ItemStack cachedItem; // When there are no variables, we don't recreate the item public ConfigurableIconImpl() { - enchantments = new HashMap<>(); closeOnClick = true; amount = 1; } @@ -142,7 +142,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon { @Override public void setLore(List lore) { - this.lore = lore; + this.lore = Utils.nullableCopy(lore); this.loreLinesWithVariables = null; if (lore != null) { @@ -159,7 +159,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon { @Override public boolean hasLore() { - return lore != null && lore.size() > 0; + return !Utils.isNullOrEmpty(lore); } @Override @@ -169,16 +169,12 @@ public class ConfigurableIconImpl implements ConfigurableIcon { @Override public void setEnchantments(Map enchantments) { - if (enchantments == null) { - this.enchantments.clear(); - return; - } - this.enchantments = enchantments; + this.enchantments = Utils.nullableCopy(enchantments); } @Override public Map getEnchantments() { - return new HashMap<>(enchantments); + return enchantments; } @Override @@ -188,19 +184,20 @@ public class ConfigurableIconImpl implements ConfigurableIcon { @Override public void addEnchantment(Enchantment ench, Integer level) { + if (enchantments == null) { + enchantments = new HashMap<>(); + } enchantments.put(ench, level); } @Override public void removeEnchantment(Enchantment ench) { + if (enchantments == null) { + return; + } enchantments.remove(ench); } - @Override - public void clearEnchantments() { - enchantments.clear(); - } - @Override public Color getLeatherColor() { return leatherColor; @@ -239,7 +236,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon { @Override public void setBannerPatterns(List bannerPatterns) { - this.bannerPatterns = bannerPatterns; + this.bannerPatterns = Utils.nullableCopy(bannerPatterns); } @Override @@ -367,7 +364,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon { itemStack.setItemMeta(itemMeta); - if (enchantments.size() > 0) { + if (enchantments != null) { for (Entry entry : enchantments.entrySet()) { itemStack.addUnsafeEnchantment(entry.getKey(), entry.getValue()); } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/AdvancedIconMenu.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/AdvancedIconMenu.java index 33639af..c8ccd32 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/AdvancedIconMenu.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/AdvancedIconMenu.java @@ -26,6 +26,7 @@ import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.Permissions; import me.filoghost.chestcommands.action.Action; import me.filoghost.chestcommands.menu.icon.AdvancedIcon; +import me.filoghost.chestcommands.util.Utils; public class AdvancedIconMenu extends BaseIconMenu { @@ -45,12 +46,8 @@ public class AdvancedIconMenu extends BaseIconMenu { return fileName; } - public List getOpenActions() { - return openActions; - } - public void setOpenActions(List openAction) { - this.openActions = openAction; + this.openActions = Utils.nullableCopy(openAction); } public String getPermission() { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/AdvancedIcon.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/AdvancedIcon.java index e2a797e..f29c8a6 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/AdvancedIcon.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/AdvancedIcon.java @@ -19,8 +19,6 @@ import java.util.List; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; -import com.google.common.collect.ImmutableList; - import me.filoghost.chestcommands.action.Action; import me.filoghost.chestcommands.action.OpenMenuAction; import me.filoghost.chestcommands.api.impl.ConfigurableIconImpl; @@ -84,7 +82,7 @@ public class AdvancedIcon extends ConfigurableIconImpl { } public void setClickActions(List clickActions) { - this.clickActions = ImmutableList.copyOf(clickActions); + this.clickActions = Utils.nullableCopy(clickActions); } @Override diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/settings/MenuSettings.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/settings/MenuSettings.java index d527035..3058679 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/settings/MenuSettings.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/settings/MenuSettings.java @@ -15,7 +15,6 @@ package me.filoghost.chestcommands.menu.settings; import me.filoghost.chestcommands.action.Action; -import me.filoghost.chestcommands.util.Utils; import java.util.List; @@ -45,10 +44,6 @@ public class MenuSettings { return rows; } - public boolean hasCommands() { - return !Utils.isNullOrEmpty(commands); - } - public void setCommands(List commands) { this.commands = commands; } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/util/Utils.java b/Plugin/src/main/java/me/filoghost/chestcommands/util/Utils.java index c300d0c..bc1d3e1 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/util/Utils.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/util/Utils.java @@ -14,7 +14,11 @@ */ package me.filoghost.chestcommands.util; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public final class Utils { @@ -33,4 +37,20 @@ public final class Utils { return collection == null || collection.isEmpty(); } + public static List nullableCopy(List list) { + if (isNullOrEmpty(list)) { + return null; + } else { + return new ArrayList<>(list); + } + } + + public static Map nullableCopy(Map map) { + if (map == null || map.isEmpty()) { + return null; + } else { + return new HashMap<>(map); + } + } + }