Use modifiable and nullable collections where possible

This commit is contained in:
filoghost 2020-06-14 12:53:25 +02:00
parent e9bbbbdf89
commit c31216f569
6 changed files with 37 additions and 32 deletions

View File

@ -69,8 +69,6 @@ public interface ConfigurableIcon extends Icon {
void removeEnchantment(Enchantment ench); void removeEnchantment(Enchantment ench);
void clearEnchantments();
Color getLeatherColor(); Color getLeatherColor();
void setLeatherColor(Color leatherColor); void setLeatherColor(Color leatherColor);

View File

@ -41,6 +41,7 @@ import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.api.ClickHandler; import me.filoghost.chestcommands.api.ClickHandler;
import me.filoghost.chestcommands.api.ClickResult; import me.filoghost.chestcommands.api.ClickResult;
import me.filoghost.chestcommands.api.ConfigurableIcon; import me.filoghost.chestcommands.api.ConfigurableIcon;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.variable.VariableManager; import me.filoghost.chestcommands.variable.VariableManager;
public class ConfigurableIconImpl implements ConfigurableIcon { 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 private ItemStack cachedItem; // When there are no variables, we don't recreate the item
public ConfigurableIconImpl() { public ConfigurableIconImpl() {
enchantments = new HashMap<>();
closeOnClick = true; closeOnClick = true;
amount = 1; amount = 1;
} }
@ -142,7 +142,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override @Override
public void setLore(List<String> lore) { public void setLore(List<String> lore) {
this.lore = lore; this.lore = Utils.nullableCopy(lore);
this.loreLinesWithVariables = null; this.loreLinesWithVariables = null;
if (lore != null) { if (lore != null) {
@ -159,7 +159,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override @Override
public boolean hasLore() { public boolean hasLore() {
return lore != null && lore.size() > 0; return !Utils.isNullOrEmpty(lore);
} }
@Override @Override
@ -169,16 +169,12 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override @Override
public void setEnchantments(Map<Enchantment, Integer> enchantments) { public void setEnchantments(Map<Enchantment, Integer> enchantments) {
if (enchantments == null) { this.enchantments = Utils.nullableCopy(enchantments);
this.enchantments.clear();
return;
}
this.enchantments = enchantments;
} }
@Override @Override
public Map<Enchantment, Integer> getEnchantments() { public Map<Enchantment, Integer> getEnchantments() {
return new HashMap<>(enchantments); return enchantments;
} }
@Override @Override
@ -188,17 +184,18 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override @Override
public void addEnchantment(Enchantment ench, Integer level) { public void addEnchantment(Enchantment ench, Integer level) {
if (enchantments == null) {
enchantments = new HashMap<>();
}
enchantments.put(ench, level); enchantments.put(ench, level);
} }
@Override @Override
public void removeEnchantment(Enchantment ench) { public void removeEnchantment(Enchantment ench) {
enchantments.remove(ench); if (enchantments == null) {
return;
} }
enchantments.remove(ench);
@Override
public void clearEnchantments() {
enchantments.clear();
} }
@Override @Override
@ -239,7 +236,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override @Override
public void setBannerPatterns(List<Pattern> bannerPatterns) { public void setBannerPatterns(List<Pattern> bannerPatterns) {
this.bannerPatterns = bannerPatterns; this.bannerPatterns = Utils.nullableCopy(bannerPatterns);
} }
@Override @Override
@ -367,7 +364,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
itemStack.setItemMeta(itemMeta); itemStack.setItemMeta(itemMeta);
if (enchantments.size() > 0) { if (enchantments != null) {
for (Entry<Enchantment, Integer> entry : enchantments.entrySet()) { for (Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
itemStack.addUnsafeEnchantment(entry.getKey(), entry.getValue()); itemStack.addUnsafeEnchantment(entry.getKey(), entry.getValue());
} }

View File

@ -26,6 +26,7 @@ import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.Permissions; import me.filoghost.chestcommands.Permissions;
import me.filoghost.chestcommands.action.Action; import me.filoghost.chestcommands.action.Action;
import me.filoghost.chestcommands.menu.icon.AdvancedIcon; import me.filoghost.chestcommands.menu.icon.AdvancedIcon;
import me.filoghost.chestcommands.util.Utils;
public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> { public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
@ -45,12 +46,8 @@ public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
return fileName; return fileName;
} }
public List<Action> getOpenActions() {
return openActions;
}
public void setOpenActions(List<Action> openAction) { public void setOpenActions(List<Action> openAction) {
this.openActions = openAction; this.openActions = Utils.nullableCopy(openAction);
} }
public String getPermission() { public String getPermission() {

View File

@ -19,8 +19,6 @@ import java.util.List;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import com.google.common.collect.ImmutableList;
import me.filoghost.chestcommands.action.Action; import me.filoghost.chestcommands.action.Action;
import me.filoghost.chestcommands.action.OpenMenuAction; import me.filoghost.chestcommands.action.OpenMenuAction;
import me.filoghost.chestcommands.api.impl.ConfigurableIconImpl; import me.filoghost.chestcommands.api.impl.ConfigurableIconImpl;
@ -84,7 +82,7 @@ public class AdvancedIcon extends ConfigurableIconImpl {
} }
public void setClickActions(List<Action> clickActions) { public void setClickActions(List<Action> clickActions) {
this.clickActions = ImmutableList.copyOf(clickActions); this.clickActions = Utils.nullableCopy(clickActions);
} }
@Override @Override

View File

@ -15,7 +15,6 @@
package me.filoghost.chestcommands.menu.settings; package me.filoghost.chestcommands.menu.settings;
import me.filoghost.chestcommands.action.Action; import me.filoghost.chestcommands.action.Action;
import me.filoghost.chestcommands.util.Utils;
import java.util.List; import java.util.List;
@ -45,10 +44,6 @@ public class MenuSettings {
return rows; return rows;
} }
public boolean hasCommands() {
return !Utils.isNullOrEmpty(commands);
}
public void setCommands(List<String> commands) { public void setCommands(List<String> commands) {
this.commands = commands; this.commands = commands;
} }

View File

@ -14,7 +14,11 @@
*/ */
package me.filoghost.chestcommands.util; 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 { public final class Utils {
@ -33,4 +37,20 @@ public final class Utils {
return collection == null || collection.isEmpty(); return collection == null || collection.isEmpty();
} }
public static <E> List<E> nullableCopy(List<E> list) {
if (isNullOrEmpty(list)) {
return null;
} else {
return new ArrayList<>(list);
}
}
public static <K, V> Map<K, V> nullableCopy(Map<K, V> map) {
if (map == null || map.isEmpty()) {
return null;
} else {
return new HashMap<>(map);
}
}
} }