Rename and move classes and methods

This commit is contained in:
filoghost 2020-07-01 08:59:16 +02:00
parent 36d74db7ec
commit decb0eb039
36 changed files with 677 additions and 672 deletions

View File

@ -20,7 +20,7 @@ import me.filoghost.chestcommands.config.ConfigLoader;
import me.filoghost.chestcommands.config.ConfigManager;
import me.filoghost.chestcommands.config.files.CustomPlaceholders;
import me.filoghost.chestcommands.config.files.Lang;
import me.filoghost.chestcommands.config.files.LoadedMenu;
import me.filoghost.chestcommands.parsing.menu.LoadedMenu;
import me.filoghost.chestcommands.config.files.Settings;
import me.filoghost.chestcommands.hook.BarAPIHook;
import me.filoghost.chestcommands.hook.BungeeCordHook;
@ -34,9 +34,9 @@ import me.filoghost.chestcommands.listener.JoinListener;
import me.filoghost.chestcommands.listener.SignListener;
import me.filoghost.chestcommands.menu.MenuManager;
import me.filoghost.chestcommands.task.RefreshMenusTask;
import me.filoghost.chestcommands.util.ErrorCollector;
import me.filoghost.chestcommands.util.Log;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.util.collection.ErrorCollector;
import me.filoghost.chestcommands.util.Log;
import me.filoghost.updatechecker.UpdateChecker;
import org.bstats.bukkit.MetricsLite;
import org.bukkit.Bukkit;

View File

@ -17,7 +17,7 @@ package me.filoghost.chestcommands.action;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.variable.RelativeString;
public class BroadcastAction extends Action {
@ -25,7 +25,7 @@ public class BroadcastAction extends Action {
private final RelativeString message;
public BroadcastAction(String action) {
message = RelativeString.of(FormatUtils.addColors(action));
message = RelativeString.of(Colors.addColors(action));
}
@Override

View File

@ -20,7 +20,7 @@ import org.bukkit.entity.Player;
import me.filoghost.chestcommands.parsing.ParseException;
import me.filoghost.chestcommands.hook.BarAPIHook;
import me.filoghost.chestcommands.parsing.NumberParser;
import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.util.Strings;
import me.filoghost.chestcommands.variable.RelativeString;
@ -44,7 +44,7 @@ public class DragonBarAction extends Action {
}
}
this.message = RelativeString.of(FormatUtils.addColors(message));
this.message = RelativeString.of(Colors.addColors(message));
}
@Override

View File

@ -18,7 +18,7 @@ import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import me.filoghost.chestcommands.util.Registry;
import me.filoghost.chestcommands.util.collection.Registry;
import me.filoghost.chestcommands.util.Strings;
import java.util.Optional;

View File

@ -16,7 +16,7 @@ package me.filoghost.chestcommands.action;
import org.bukkit.entity.Player;
import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.variable.RelativeString;
public class SendMessageAction extends Action {
@ -24,7 +24,7 @@ public class SendMessageAction extends Action {
private final RelativeString message;
public SendMessageAction(String action) {
message = RelativeString.of(FormatUtils.addColors(action));
message = RelativeString.of(Colors.addColors(action));
}
@Override

View File

@ -19,7 +19,7 @@ import me.filoghost.chestcommands.api.ClickResult;
import me.filoghost.chestcommands.api.ConfigurableIcon;
import me.filoghost.chestcommands.util.Log;
import me.filoghost.chestcommands.util.Preconditions;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.util.collection.CollectionUtils;
import me.filoghost.chestcommands.variable.RelativeString;
import me.filoghost.chestcommands.variable.RelativeStringList;
import org.bukkit.Bukkit;
@ -148,7 +148,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override
public void setLore(List<String> lore) {
if (!Utils.isNullOrEmpty(lore)) {
if (!CollectionUtils.isNullOrEmpty(lore)) {
this.lore = new RelativeStringList(lore);
} else {
this.lore = null;
@ -171,12 +171,12 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override
public void setEnchantments(Map<Enchantment, Integer> enchantments) {
this.enchantments = Utils.nullableCopy(enchantments);
this.enchantments = CollectionUtils.nullableCopy(enchantments);
}
@Override
public Map<Enchantment, Integer> getEnchantments() {
return Utils.nullableCopy(enchantments);
return CollectionUtils.nullableCopy(enchantments);
}
@Override
@ -236,12 +236,12 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
@Override
public List<Pattern> getBannerPatterns() {
return Utils.nullableCopy(bannerPatterns);
return CollectionUtils.nullableCopy(bannerPatterns);
}
@Override
public void setBannerPatterns(List<Pattern> bannerPatterns) {
this.bannerPatterns = Utils.nullableCopy(bannerPatterns);
this.bannerPatterns = CollectionUtils.nullableCopy(bannerPatterns);
}
@Override
@ -332,7 +332,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon {
}
// Hide all text details (damage, enchantments, potions, etc,)
if (Utils.isNullOrEmpty(itemMeta.getItemFlags())) {
if (CollectionUtils.isNullOrEmpty(itemMeta.getItemFlags())) {
itemMeta.addItemFlags(ItemFlag.values());
}

View File

@ -26,7 +26,7 @@ import me.filoghost.chestcommands.command.framework.CommandFramework;
import me.filoghost.chestcommands.command.framework.CommandValidate;
import me.filoghost.chestcommands.menu.AdvancedIconMenu;
import me.filoghost.chestcommands.menu.MenuManager;
import me.filoghost.chestcommands.util.ErrorCollector;
import me.filoghost.chestcommands.util.collection.ErrorCollector;
public class CommandHandler extends CommandFramework {

View File

@ -16,10 +16,10 @@ package me.filoghost.chestcommands.config;
import me.filoghost.chestcommands.config.files.CustomPlaceholders;
import me.filoghost.chestcommands.config.files.Lang;
import me.filoghost.chestcommands.config.files.LoadedMenu;
import me.filoghost.chestcommands.parsing.menu.LoadedMenu;
import me.filoghost.chestcommands.config.files.Settings;
import me.filoghost.chestcommands.parsing.menu.MenuParser;
import me.filoghost.chestcommands.util.ErrorCollector;
import me.filoghost.chestcommands.util.collection.ErrorCollector;
import me.filoghost.chestcommands.util.Log;
import org.bukkit.configuration.InvalidConfigurationException;

View File

@ -14,7 +14,7 @@
*/
package me.filoghost.chestcommands.config;
import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.util.Log;
import org.bukkit.configuration.InvalidConfigurationException;
@ -103,7 +103,7 @@ public abstract class SpecialConfig {
field.set(this, config.getDouble(configNode));
} else if (type == String.class) {
field.set(this, FormatUtils.addColors(config.getString(configNode))); // Always add colors
field.set(this, Colors.addColors(config.getString(configNode))); // Always add colors
} else {
Log.warning("Unknown field type: " + field.getType().getName() + " (" + field.getName() + "). Please inform the developer.");

View File

@ -15,9 +15,9 @@
package me.filoghost.chestcommands.config.files;
import me.filoghost.chestcommands.config.Config;
import me.filoghost.chestcommands.util.ErrorCollector;
import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.util.collection.ErrorCollector;
import me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.util.collection.CollectionUtils;
import java.util.HashMap;
import java.util.List;
@ -33,7 +33,7 @@ public class CustomPlaceholders {
for (String key : config.getKeys(false)) {
String placeholder = key;
String replacement = FormatUtils.addColors(config.getString(key));
String replacement = Colors.addColors(config.getString(key));
if (placeholder.length() == 0) {
errorCollector.addError("Error in " + config.getFileName() + ": placeholder cannot be empty (skipped).");
@ -53,7 +53,7 @@ public class CustomPlaceholders {
if (input == null) {
return null;
}
return Utils.transform(input, this::replaceAll);
return CollectionUtils.transform(input, this::replaceAll);
}
public String replaceAll(String input) {

View File

@ -21,7 +21,7 @@ import me.filoghost.chestcommands.legacy.upgrades.MenuUpgrade;
import me.filoghost.chestcommands.legacy.upgrades.PlaceholdersUpgrade;
import me.filoghost.chestcommands.legacy.upgrades.SettingsUpgrade;
import me.filoghost.chestcommands.util.Log;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.util.collection.CollectionUtils;
import java.io.IOException;
import java.nio.file.Path;
@ -65,7 +65,7 @@ public class UpgradesExecutor {
runIfNecessary(UpgradeID.V4_PLACEHOLDERS, placeholdersUpgrade);
try {
List<MenuUpgrade> menuUpgrades = Utils.transform(
List<MenuUpgrade> menuUpgrades = CollectionUtils.transform(
configManager.getMenusPathList(),
menuPath -> new MenuUpgrade(new ConfigLoader(menuPath), legacyCommandSeparator));
runIfNecessary(UpgradeID.V4_MENUS, menuUpgrades);

View File

@ -20,7 +20,7 @@ import me.filoghost.chestcommands.config.ConfigSection;
import me.filoghost.chestcommands.legacy.Upgrade;
import me.filoghost.chestcommands.legacy.UpgradeException;
import me.filoghost.chestcommands.parsing.menu.MenuSettingsNode;
import me.filoghost.chestcommands.parsing.icon.IconNode;
import me.filoghost.chestcommands.parsing.icon.IconSettingsNode;
import me.filoghost.chestcommands.util.Strings;
import java.io.IOException;
@ -89,53 +89,53 @@ public class MenuUpgrade extends Upgrade {
}
private void upgradeIcon(ConfigSection section) {
renameNode(section, "ID", IconNode.MATERIAL);
renameNode(section, "DATA-VALUE", IconNode.DURABILITY);
renameNode(section, "NBT", IconNode.NBT_DATA);
renameNode(section, "ENCHANTMENT", IconNode.ENCHANTMENTS);
renameNode(section, "COMMAND", IconNode.ACTIONS);
renameNode(section, "COMMANDS", IconNode.ACTIONS);
renameNode(section, "REQUIRED-ITEM", IconNode.REQUIRED_ITEMS);
renameNode(section, "ID", IconSettingsNode.MATERIAL);
renameNode(section, "DATA-VALUE", IconSettingsNode.DURABILITY);
renameNode(section, "NBT", IconSettingsNode.NBT_DATA);
renameNode(section, "ENCHANTMENT", IconSettingsNode.ENCHANTMENTS);
renameNode(section, "COMMAND", IconSettingsNode.ACTIONS);
renameNode(section, "COMMANDS", IconSettingsNode.ACTIONS);
renameNode(section, "REQUIRED-ITEM", IconSettingsNode.REQUIRED_ITEMS);
expandInlineList(section, IconNode.ACTIONS, legacyCommandSeparator);
expandInlineList(section, IconNode.ENCHANTMENTS, ";");
expandInlineList(section, IconSettingsNode.ACTIONS, legacyCommandSeparator);
expandInlineList(section, IconSettingsNode.ENCHANTMENTS, ";");
expandSingletonList(section, IconNode.REQUIRED_ITEMS);
expandSingletonList(section, IconSettingsNode.REQUIRED_ITEMS);
expandInlineItemstack(section);
}
private void expandInlineItemstack(ConfigSection section) {
String material = section.getString(IconNode.MATERIAL);
String material = section.getString(IconSettingsNode.MATERIAL);
if (material == null) {
return;
}
if (material.contains(",")) {
String[] parts = Strings.trimmedSplit(material, ",", 2);
if (!section.isSet(IconNode.AMOUNT)) {
if (!section.isSet(IconSettingsNode.AMOUNT)) {
try {
section.set(IconNode.AMOUNT, Integer.parseInt(parts[1]));
section.set(IconSettingsNode.AMOUNT, Integer.parseInt(parts[1]));
} catch (NumberFormatException e) {
section.set(IconNode.AMOUNT, parts[1]);
section.set(IconSettingsNode.AMOUNT, parts[1]);
}
}
material = parts[0];
section.set(IconNode.MATERIAL, material);
section.set(IconSettingsNode.MATERIAL, material);
setModified();
}
if (material.contains(":")) {
String[] parts = Strings.trimmedSplit(material, ":", 2);
if (!section.isSet(IconNode.DURABILITY)) {
if (!section.isSet(IconSettingsNode.DURABILITY)) {
try {
section.set(IconNode.DURABILITY, Integer.parseInt(parts[1]));
section.set(IconSettingsNode.DURABILITY, Integer.parseInt(parts[1]));
} catch (NumberFormatException e) {
section.set(IconNode.DURABILITY, parts[1]);
section.set(IconSettingsNode.DURABILITY, parts[1]);
}
}
material = parts[0];
section.set(IconNode.MATERIAL, material);
section.set(IconSettingsNode.MATERIAL, material);
setModified();
}
}

View File

@ -25,7 +25,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;
import me.filoghost.chestcommands.util.collection.CollectionUtils;
public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
@ -46,7 +46,7 @@ public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
}
public void setOpenActions(List<Action> openAction) {
this.openActions = Utils.nullableCopy(openAction);
this.openActions = CollectionUtils.nullableCopy(openAction);
}
public String getOpenPermission() {

View File

@ -14,11 +14,11 @@
*/
package me.filoghost.chestcommands.menu;
import me.filoghost.chestcommands.config.files.LoadedMenu;
import me.filoghost.chestcommands.parsing.menu.LoadedMenu;
import me.filoghost.chestcommands.menu.inventory.MenuInventoryHolder;
import me.filoghost.chestcommands.menu.settings.OpenTrigger;
import me.filoghost.chestcommands.util.CaseInsensitiveMap;
import me.filoghost.chestcommands.util.ErrorCollector;
import me.filoghost.chestcommands.parsing.menu.OpenTrigger;
import me.filoghost.chestcommands.util.collection.CaseInsensitiveMap;
import me.filoghost.chestcommands.util.collection.ErrorCollector;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.inventory.Inventory;

View File

@ -28,7 +28,7 @@ import me.filoghost.chestcommands.api.impl.ConfigurableIconImpl;
import me.filoghost.chestcommands.menu.AdvancedIconMenu;
import me.filoghost.chestcommands.menu.BaseIconMenu;
import me.filoghost.chestcommands.menu.MenuManager;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.util.collection.CollectionUtils;
public class AdvancedIcon extends ConfigurableIconImpl {
@ -81,7 +81,7 @@ public class AdvancedIcon extends ConfigurableIconImpl {
}
public void setRequiredItems(List<RequiredItem> requiredItems) {
if (!Utils.isNullOrEmpty(requiredItems)) {
if (!CollectionUtils.isNullOrEmpty(requiredItems)) {
this.requiredItems = new RequiredItems(requiredItems);
} else {
this.requiredItems = null;
@ -89,7 +89,7 @@ public class AdvancedIcon extends ConfigurableIconImpl {
}
public void setClickActions(List<Action> clickActions) {
this.clickActions = Utils.nullableCopy(clickActions);
this.clickActions = CollectionUtils.nullableCopy(clickActions);
}

View File

@ -16,12 +16,12 @@ package me.filoghost.chestcommands.menu.icon;
import java.util.List;
import me.filoghost.chestcommands.util.Utils;
import org.bukkit.entity.Player;
import com.google.common.collect.ImmutableList;
import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.util.MaterialsHelper;
import me.filoghost.chestcommands.util.Preconditions;
public class RequiredItems implements Requirement {
@ -45,7 +45,7 @@ public class RequiredItems implements Requirement {
if (!item.isItemContainedIn(player.getInventory())) {
missingItems = true;
player.sendMessage(ChestCommands.getLang().no_required_item
.replace("{material}", MaterialsHelper.formatMaterial(item.getMaterial()))
.replace("{material}", Utils.formatEnum(item.getMaterial()))
.replace("{amount}", Integer.toString(item.getAmount()))
.replace("{durability}", item.hasRestrictiveDurability() ? Short.toString(item.getDurability()) : ChestCommands.getLang().any)
);

View File

@ -14,7 +14,7 @@
*/
package me.filoghost.chestcommands.parsing;
import me.filoghost.chestcommands.util.Registry;
import me.filoghost.chestcommands.util.collection.Registry;
import me.filoghost.chestcommands.util.Strings;
import org.bukkit.enchantments.Enchantment;

View File

@ -14,7 +14,7 @@
*/
package me.filoghost.chestcommands.parsing;
import me.filoghost.chestcommands.util.Registry;
import me.filoghost.chestcommands.util.collection.Registry;
import me.filoghost.chestcommands.util.Strings;
import org.bukkit.Color;
import org.bukkit.DyeColor;

View File

@ -1,7 +1,7 @@
package me.filoghost.chestcommands.parsing.icon;
import me.filoghost.chestcommands.parsing.ErrorFormat;
import me.filoghost.chestcommands.util.ErrorCollector;
import me.filoghost.chestcommands.util.collection.ErrorCollector;
public class AttributeErrorCollector {

View File

@ -38,7 +38,7 @@ import me.filoghost.chestcommands.parsing.icon.attributes.PriceAttribute;
import me.filoghost.chestcommands.parsing.icon.attributes.RequiredItemsAttribute;
import me.filoghost.chestcommands.parsing.icon.attributes.SkullOwnerAttribute;
import me.filoghost.chestcommands.parsing.icon.attributes.ViewPermissionAttribute;
import me.filoghost.chestcommands.util.ErrorCollector;
import me.filoghost.chestcommands.util.collection.ErrorCollector;
import me.filoghost.chestcommands.util.Preconditions;
import java.util.ArrayList;
@ -58,28 +58,28 @@ public class IconSettings {
public static final Map<String, IconNodeHandler> iconNodeHandlers = new HashMap<>();
static {
addIconNodeHandler(IconNode.POSITION_X, ValueExtractor.INT, PositionAttribute::new, IconSettings::setPositionX);
addIconNodeHandler(IconNode.POSITION_Y, ValueExtractor.INT, PositionAttribute::new, IconSettings::setPositionY);
addIconNodeHandler(IconNode.MATERIAL, ValueExtractor.STRING, MaterialAttribute::new, IconSettings::setMaterialAttribute);
addIconNodeHandler(IconSettingsNode.POSITION_X, ValueExtractor.INT, PositionAttribute::new, IconSettings::setPositionX);
addIconNodeHandler(IconSettingsNode.POSITION_Y, ValueExtractor.INT, PositionAttribute::new, IconSettings::setPositionY);
addIconNodeHandler(IconSettingsNode.MATERIAL, ValueExtractor.STRING, MaterialAttribute::new, IconSettings::setMaterialAttribute);
addApplicableIconNodeHandler(IconNode.DURABILITY, ValueExtractor.SHORT, DurabilityAttribute::new);
addApplicableIconNodeHandler(IconNode.AMOUNT, ValueExtractor.INT, AmountAttribute::new);
addApplicableIconNodeHandler(IconNode.NAME, ValueExtractor.STRING, NameAttribute::new);
addApplicableIconNodeHandler(IconNode.LORE, ValueExtractor.STRING_LIST, LoreAttribute::new);
addApplicableIconNodeHandler(IconNode.NBT_DATA, ValueExtractor.STRING, NBTDataAttribute::new);
addApplicableIconNodeHandler(IconNode.LEATHER_COLOR, ValueExtractor.STRING, LeatherColorAttribute::new);
addApplicableIconNodeHandler(IconNode.SKULL_OWNER, ValueExtractor.STRING, SkullOwnerAttribute::new);
addApplicableIconNodeHandler(IconNode.BANNER_COLOR, ValueExtractor.STRING, BannerColorAttribute::new);
addApplicableIconNodeHandler(IconNode.BANNER_PATTERNS, ValueExtractor.STRING_LIST, BannerPatternsAttribute::new);
addApplicableIconNodeHandler(IconNode.PRICE, ValueExtractor.DOUBLE, PriceAttribute::new);
addApplicableIconNodeHandler(IconNode.EXP_LEVELS, ValueExtractor.INT, ExpLevelsAttribute::new);
addApplicableIconNodeHandler(IconNode.CLICK_PERMISSION, ValueExtractor.STRING, ClickPermissionAttribute::new);
addApplicableIconNodeHandler(IconNode.CLICK_PERMISSION_MESSAGE, ValueExtractor.STRING, ClickPermissionMessageAttribute::new);
addApplicableIconNodeHandler(IconNode.VIEW_PERMISSION, ValueExtractor.STRING, ViewPermissionAttribute::new);
addApplicableIconNodeHandler(IconNode.KEEP_OPEN, ValueExtractor.BOOLEAN, KeepOpenAttribute::new);
addApplicableIconNodeHandler(IconNode.ACTIONS, ValueExtractor.STRING_LIST, ActionsAttribute::new);
addApplicableIconNodeHandler(IconNode.ENCHANTMENTS, ValueExtractor.STRING_LIST, EnchantmentsAttribute::new);
addApplicableIconNodeHandler(IconNode.REQUIRED_ITEMS, ValueExtractor.STRING_LIST, RequiredItemsAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.DURABILITY, ValueExtractor.SHORT, DurabilityAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.AMOUNT, ValueExtractor.INT, AmountAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.NAME, ValueExtractor.STRING, NameAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.LORE, ValueExtractor.STRING_LIST, LoreAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.NBT_DATA, ValueExtractor.STRING, NBTDataAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.LEATHER_COLOR, ValueExtractor.STRING, LeatherColorAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.SKULL_OWNER, ValueExtractor.STRING, SkullOwnerAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.BANNER_COLOR, ValueExtractor.STRING, BannerColorAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.BANNER_PATTERNS, ValueExtractor.STRING_LIST, BannerPatternsAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.PRICE, ValueExtractor.DOUBLE, PriceAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.EXP_LEVELS, ValueExtractor.INT, ExpLevelsAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.CLICK_PERMISSION, ValueExtractor.STRING, ClickPermissionAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.CLICK_PERMISSION_MESSAGE, ValueExtractor.STRING, ClickPermissionMessageAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.VIEW_PERMISSION, ValueExtractor.STRING, ViewPermissionAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.KEEP_OPEN, ValueExtractor.BOOLEAN, KeepOpenAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.ACTIONS, ValueExtractor.STRING_LIST, ActionsAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.ENCHANTMENTS, ValueExtractor.STRING_LIST, EnchantmentsAttribute::new);
addApplicableIconNodeHandler(IconSettingsNode.REQUIRED_ITEMS, ValueExtractor.STRING_LIST, RequiredItemsAttribute::new);
}
public String getMenuName() {

View File

@ -14,7 +14,7 @@
*/
package me.filoghost.chestcommands.parsing.icon;
public class IconNode {
public class IconSettingsNode {
public static final String
POSITION_X = "POSITION-X",

View File

@ -18,7 +18,7 @@ import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.menu.icon.AdvancedIcon;
import me.filoghost.chestcommands.parsing.icon.ApplicableIconAttribute;
import me.filoghost.chestcommands.parsing.icon.AttributeErrorCollector;
import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.Colors;
import java.util.List;
@ -27,7 +27,7 @@ public class LoreAttribute implements ApplicableIconAttribute {
private final List<String> lore;
public LoreAttribute(List<String> lore, AttributeErrorCollector attributeErrorCollector) {
this.lore = ChestCommands.getCustomPlaceholders().replaceAll(FormatUtils.colorizeLore(lore));
this.lore = ChestCommands.getCustomPlaceholders().replaceAll(Colors.colorLore(lore));
}
@Override

View File

@ -18,14 +18,14 @@ import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.menu.icon.AdvancedIcon;
import me.filoghost.chestcommands.parsing.icon.ApplicableIconAttribute;
import me.filoghost.chestcommands.parsing.icon.AttributeErrorCollector;
import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.Colors;
public class NameAttribute implements ApplicableIconAttribute {
private final String name;
public NameAttribute(String name, AttributeErrorCollector attributeErrorCollector) {
this.name = ChestCommands.getCustomPlaceholders().replaceAll(FormatUtils.colorizeName(name));
this.name = ChestCommands.getCustomPlaceholders().replaceAll(Colors.colorName(name));
}
@Override

View File

@ -1,47 +1,47 @@
/*
* 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.menu.settings;
import org.bukkit.event.block.Action;
public enum ClickType {
LEFT,
RIGHT,
BOTH;
public static ClickType fromOptions(boolean left, boolean right) {
if (left && right) {
return BOTH;
} else if (left) {
return LEFT;
} else if (right) {
return RIGHT;
} else {
return null;
}
}
public boolean isValidInteract(Action action) {
if (action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK) {
return this == LEFT || this == BOTH;
} else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
return this == RIGHT || this == BOTH;
} else {
return false;
}
}
}
/*
* 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.parsing.menu;
import org.bukkit.event.block.Action;
public enum ClickType {
LEFT,
RIGHT,
BOTH;
public static ClickType fromOptions(boolean left, boolean right) {
if (left && right) {
return BOTH;
} else if (left) {
return LEFT;
} else if (right) {
return RIGHT;
} else {
return null;
}
}
public boolean isValidInteract(Action action) {
if (action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK) {
return this == LEFT || this == BOTH;
} else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
return this == RIGHT || this == BOTH;
} else {
return false;
}
}
}

View File

@ -1,7 +1,6 @@
package me.filoghost.chestcommands.config.files;
package me.filoghost.chestcommands.parsing.menu;
import me.filoghost.chestcommands.menu.AdvancedIconMenu;
import me.filoghost.chestcommands.menu.settings.OpenTrigger;
import java.util.List;

View File

@ -18,20 +18,16 @@ import me.filoghost.chestcommands.action.Action;
import me.filoghost.chestcommands.config.Config;
import me.filoghost.chestcommands.config.ConfigSection;
import me.filoghost.chestcommands.config.ConfigValueException;
import me.filoghost.chestcommands.config.files.LoadedMenu;
import me.filoghost.chestcommands.menu.AdvancedIconMenu;
import me.filoghost.chestcommands.menu.icon.AdvancedIcon;
import me.filoghost.chestcommands.menu.settings.ClickType;
import me.filoghost.chestcommands.menu.settings.MenuSettings;
import me.filoghost.chestcommands.menu.settings.OpenTrigger;
import me.filoghost.chestcommands.parsing.ActionParser;
import me.filoghost.chestcommands.parsing.ErrorFormat;
import me.filoghost.chestcommands.parsing.ItemStackParser;
import me.filoghost.chestcommands.parsing.ParseException;
import me.filoghost.chestcommands.parsing.icon.IconNode;
import me.filoghost.chestcommands.parsing.icon.IconSettingsNode;
import me.filoghost.chestcommands.parsing.icon.IconSettings;
import me.filoghost.chestcommands.util.ErrorCollector;
import me.filoghost.chestcommands.util.FormatUtils;
import me.filoghost.chestcommands.util.collection.ErrorCollector;
import me.filoghost.chestcommands.util.Colors;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -64,22 +60,22 @@ public class MenuParser {
private static void addIconToMenu(AdvancedIconMenu iconMenu, IconSettings iconSettings, ErrorCollector errorCollector) throws IconAddException {
if (iconSettings.getPositionX() == null) {
throw new IconAddException(ErrorFormat.missingAttribute(iconSettings, IconNode.POSITION_X));
throw new IconAddException(ErrorFormat.missingAttribute(iconSettings, IconSettingsNode.POSITION_X));
}
if (iconSettings.getPositionY() == null) {
throw new IconAddException(ErrorFormat.missingAttribute(iconSettings, IconNode.POSITION_Y));
throw new IconAddException(ErrorFormat.missingAttribute(iconSettings, IconSettingsNode.POSITION_Y));
}
int actualX = iconSettings.getPositionX().getPosition() - 1;
int actualY = iconSettings.getPositionY().getPosition() - 1;
if (actualX < 0 || actualX >= iconMenu.getColumnCount()) {
throw new IconAddException(ErrorFormat.invalidAttribute(iconSettings, IconNode.POSITION_X,
throw new IconAddException(ErrorFormat.invalidAttribute(iconSettings, IconSettingsNode.POSITION_X,
"it must be between 1 and " + iconMenu.getColumnCount()));
}
if (actualY < 0 || actualY >= iconMenu.getRowCount()) {
throw new IconAddException(ErrorFormat.invalidAttribute(iconSettings, IconNode.POSITION_Y,
throw new IconAddException(ErrorFormat.invalidAttribute(iconSettings, IconSettingsNode.POSITION_Y,
"it must be between 1 and " + iconMenu.getRowCount()));
}
@ -89,7 +85,7 @@ public class MenuParser {
}
if (iconSettings.getMaterialAttribute() == null) {
errorCollector.addError(ErrorFormat.missingAttribute(iconSettings, IconNode.MATERIAL));
errorCollector.addError(ErrorFormat.missingAttribute(iconSettings, IconSettingsNode.MATERIAL));
}
AdvancedIcon icon = new AdvancedIcon(Material.BEDROCK);
@ -103,7 +99,7 @@ public class MenuParser {
String title;
try {
title = FormatUtils.addColors(settingsSection.getRequiredString(MenuSettingsNode.NAME));
title = Colors.addColors(settingsSection.getRequiredString(MenuSettingsNode.NAME));
if (title.length() > 32) {
title = title.substring(0, 32);
}

View File

@ -1,79 +1,79 @@
/*
* 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.menu.settings;
import me.filoghost.chestcommands.action.Action;
import java.util.List;
public class MenuSettings {
// Required settings
private final String title;
private final int rows;
// Optional settings
private List<String> commands;
private List<Action> openActions;
private int refreshTenths;
private OpenTrigger openTrigger;
public MenuSettings(String title, int rows) {
this.title = title;
this.rows = rows;
}
public String getTitle() {
return title;
}
public int getRows() {
return rows;
}
public void setCommands(List<String> commands) {
this.commands = commands;
}
public List<String> getCommands() {
return commands;
}
public List<Action> getOpenActions() {
return openActions;
}
public void setOpenActions(List<Action> openAction) {
this.openActions = openAction;
}
public int getRefreshTenths() {
return refreshTenths;
}
public void setRefreshTenths(int refreshTenths) {
this.refreshTenths = refreshTenths;
}
public OpenTrigger getOpenTrigger() {
return openTrigger;
}
public void setOpenTrigger(OpenTrigger openTrigger) {
this.openTrigger = openTrigger;
}
}
/*
* 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.parsing.menu;
import me.filoghost.chestcommands.action.Action;
import java.util.List;
public class MenuSettings {
// Required settings
private final String title;
private final int rows;
// Optional settings
private List<String> commands;
private List<Action> openActions;
private int refreshTenths;
private OpenTrigger openTrigger;
public MenuSettings(String title, int rows) {
this.title = title;
this.rows = rows;
}
public String getTitle() {
return title;
}
public int getRows() {
return rows;
}
public void setCommands(List<String> commands) {
this.commands = commands;
}
public List<String> getCommands() {
return commands;
}
public List<Action> getOpenActions() {
return openActions;
}
public void setOpenActions(List<Action> openAction) {
this.openActions = openAction;
}
public int getRefreshTenths() {
return refreshTenths;
}
public void setRefreshTenths(int refreshTenths) {
this.refreshTenths = refreshTenths;
}
public OpenTrigger getOpenTrigger() {
return openTrigger;
}
public void setOpenTrigger(OpenTrigger openTrigger) {
this.openTrigger = openTrigger;
}
}

View File

@ -1,62 +1,58 @@
/*
* 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.menu.settings;
import org.bukkit.Material;
import org.bukkit.event.block.Action;
import org.bukkit.inventory.ItemStack;
import me.filoghost.chestcommands.util.Preconditions;
public class OpenTrigger {
private final Material material;
private final ClickType clickType;
private short durability;
private boolean isRestrictiveDurability;
public OpenTrigger(Material material, ClickType clickType) {
Preconditions.checkArgumentNotAir(material, "material");
Preconditions.notNull(clickType, "clickType");
this.material = material;
this.clickType = clickType;
}
public void setRestrictiveDurability(short durability) {
this.durability = durability;
this.isRestrictiveDurability = true;
}
public boolean matches(ItemStack item, Action action) {
if (item == null) {
return false;
}
if (this.material != item.getType()) {
return false;
}
if (isRestrictiveDurability && this.durability != item.getDurability()) {
return false;
}
if (!clickType.isValidInteract(action)) {
return false;
}
return true;
}
}
/*
* 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.parsing.menu;
import org.bukkit.Material;
import org.bukkit.event.block.Action;
import org.bukkit.inventory.ItemStack;
import me.filoghost.chestcommands.util.Preconditions;
public class OpenTrigger {
private final Material material;
private final ClickType clickType;
private short durability;
private boolean isRestrictiveDurability;
public OpenTrigger(Material material, ClickType clickType) {
Preconditions.checkArgumentNotAir(material, "material");
Preconditions.notNull(clickType, "clickType");
this.material = material;
this.clickType = clickType;
}
public void setRestrictiveDurability(short durability) {
this.durability = durability;
this.isRestrictiveDurability = true;
}
public boolean matches(ItemStack item, Action action) {
if (item == null) {
return false;
}
if (this.material != item.getType()) {
return false;
}
if (isRestrictiveDurability && this.durability != item.getDurability()) {
return false;
}
return clickType.isValidInteract(action);
}
}

View File

@ -1,41 +1,42 @@
package me.filoghost.chestcommands.util;
import org.bukkit.ChatColor;
import me.filoghost.chestcommands.ChestCommands;
import java.util.List;
public final class FormatUtils {
public static String addColors(String input) {
if (input == null || input.isEmpty()) {
return input;
}
return ChatColor.translateAlternateColorCodes('&', input);
}
public static String colorizeName(String input) {
if (input == null || input.isEmpty()) {
return input;
}
if (input.charAt(0) != ChatColor.COLOR_CHAR) {
return ChestCommands.getSettings().default_color__name + addColors(input);
} else {
return addColors(input);
}
}
public static List<String> colorizeLore(List<String> input) {
return Utils.transform(input, line -> {
if (line.isEmpty()) {
return line;
} else if (line.charAt(0) != ChatColor.COLOR_CHAR) {
return ChestCommands.getSettings().default_color__lore + addColors(line);
} else {
return addColors(line);
}
});
}
}
package me.filoghost.chestcommands.util;
import me.filoghost.chestcommands.util.collection.CollectionUtils;
import org.bukkit.ChatColor;
import me.filoghost.chestcommands.ChestCommands;
import java.util.List;
public final class Colors {
public static String addColors(String input) {
if (input == null || input.isEmpty()) {
return input;
}
return ChatColor.translateAlternateColorCodes('&', input);
}
public static String colorName(String input) {
if (input == null || input.isEmpty()) {
return input;
}
if (input.charAt(0) != ChatColor.COLOR_CHAR) {
return ChestCommands.getSettings().default_color__name + addColors(input);
} else {
return addColors(input);
}
}
public static List<String> colorLore(List<String> input) {
return CollectionUtils.transform(input, line -> {
if (line.isEmpty()) {
return line;
} else if (line.charAt(0) != ChatColor.COLOR_CHAR) {
return ChestCommands.getSettings().default_color__lore + addColors(line);
} else {
return addColors(line);
}
});
}
}

View File

@ -14,6 +14,7 @@
*/
package me.filoghost.chestcommands.util;
import me.filoghost.chestcommands.util.collection.Registry;
import org.bukkit.Material;
import java.util.Collection;
import java.util.HashSet;
@ -103,10 +104,6 @@ public final class MaterialsHelper {
return MATERIALS_REGISTRY.find(materialName);
}
public static String formatMaterial(Material material) {
return Strings.capitalizeFully(material.toString().replace("_", " "));
}
private static Collection<Material> getExistingMaterials(String... materialEnumNames) {
Collection<Material> existingMaterials = new HashSet<>();

View File

@ -1,68 +1,20 @@
/*
* 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 java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public final class Utils {
private Utils() {}
public static boolean isClassLoaded(String name) {
try {
Class.forName(name);
return true;
} catch (Throwable t) {
return false;
}
}
public static boolean isNullOrEmpty(Collection<?> collection) {
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);
}
}
public static <A, B> List<B> transform(List<A> list, Function<A, B> transformFunction) {
if (list == null) {
return null;
}
List<B> result = new ArrayList<>(list.size());
for (A element : list) {
result.add(transformFunction.apply(element));
}
return result;
}
}
package me.filoghost.chestcommands.util;
import org.bukkit.Material;
public class Utils {
public static boolean isClassLoaded(String name) {
try {
Class.forName(name);
return true;
} catch (Throwable t) {
return false;
}
}
public static String formatEnum(Enum<?> enumValue) {
return Strings.capitalizeFully(enumValue.name().replace("_", " "));
}
}

View File

@ -1,97 +1,99 @@
/*
* 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 java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CaseInsensitiveMap<V> implements Map<String, V> {
private final Map<String, V> delegate;
public CaseInsensitiveMap() {
this.delegate = new HashMap<>();
}
@Override
public V put(String key, V value) {
return delegate.put(getLowercaseKey(key), value);
}
@Override
public V get(Object key) {
return delegate.get(getLowercaseKey(key));
}
@Override
public boolean containsKey(Object key) {
return delegate.containsKey(getLowercaseKey(key));
}
@Override
public V remove(Object key) {
return delegate.remove(getLowercaseKey(key));
}
@Override
public int size() {
return delegate.size();
}
@Override
public boolean isEmpty() {
return delegate.isEmpty();
}
@Override
public boolean containsValue(Object value) {
return delegate.containsValue(value);
}
@Override
public void putAll(Map<? extends String, ? extends V> map) {
map.forEach(this::put);
}
@Override
public void clear() {
delegate.clear();
}
@Override
public Set<String> keySet() {
return Collections.unmodifiableSet(delegate.keySet());
}
@Override
public Collection<V> values() {
return Collections.unmodifiableCollection(delegate.values());
}
@Override
public Set<Entry<String, V>> entrySet() {
return Collections.unmodifiableSet(delegate.entrySet());
}
private String getLowercaseKey(Object key) {
Preconditions.notNull(key, "key");
Preconditions.checkArgument(key instanceof String, "key must be a string");
return ((String) key).toLowerCase();
}
}
/*
* 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.collection;
import me.filoghost.chestcommands.util.Preconditions;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CaseInsensitiveMap<V> implements Map<String, V> {
private final Map<String, V> delegate;
public CaseInsensitiveMap() {
this.delegate = new HashMap<>();
}
@Override
public V put(String key, V value) {
return delegate.put(getLowercaseKey(key), value);
}
@Override
public V get(Object key) {
return delegate.get(getLowercaseKey(key));
}
@Override
public boolean containsKey(Object key) {
return delegate.containsKey(getLowercaseKey(key));
}
@Override
public V remove(Object key) {
return delegate.remove(getLowercaseKey(key));
}
@Override
public int size() {
return delegate.size();
}
@Override
public boolean isEmpty() {
return delegate.isEmpty();
}
@Override
public boolean containsValue(Object value) {
return delegate.containsValue(value);
}
@Override
public void putAll(Map<? extends String, ? extends V> map) {
map.forEach(this::put);
}
@Override
public void clear() {
delegate.clear();
}
@Override
public Set<String> keySet() {
return Collections.unmodifiableSet(delegate.keySet());
}
@Override
public Collection<V> values() {
return Collections.unmodifiableCollection(delegate.values());
}
@Override
public Set<Entry<String, V>> entrySet() {
return Collections.unmodifiableSet(delegate.entrySet());
}
private String getLowercaseKey(Object key) {
Preconditions.notNull(key, "key");
Preconditions.checkArgument(key instanceof String, "key must be a string");
return ((String) key).toLowerCase();
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public final class CollectionUtils {
private CollectionUtils() {}
public static boolean isNullOrEmpty(Collection<?> collection) {
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);
}
}
public static <A, B> List<B> transform(List<A> list, Function<A, B> transformFunction) {
if (list == null) {
return null;
}
List<B> result = new ArrayList<>(list.size());
for (A element : list) {
result.add(transformFunction.apply(element));
}
return result;
}
}

View File

@ -1,77 +1,77 @@
/*
* 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 java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
/**
* This is a class to collect all the errors found while loading the plugin.
*/
public class ErrorCollector {
private final List<String> errors = new ArrayList<>();
private final List<String> warnings = new ArrayList<>();
public void addError(String error) {
errors.add(error);
}
public void addWarning(String warning) {
warnings.add(warning);
}
public int getErrorsCount() {
return errors.size();
}
public int getWarningsCount() {
return warnings.size();
}
public boolean hasWarningsOrErrors() {
return errors.size() > 0 || warnings.size() > 0;
}
public void logToConsole() {
StringBuilder msg = new StringBuilder(200);
msg.append(" \n \n");
if (errors.size() > 0) {
msg.append(ChatColor.RED + "[ChestCommands] Encountered " + errors.size() + " error(s) on load:\n");
appendNumberedList(msg, errors);
}
if (warnings.size() > 0) {
msg.append(ChatColor.YELLOW + "[ChestCommands] Encountered " + warnings.size() + " warnings(s) on load:\n");
appendNumberedList(msg, warnings);
}
Bukkit.getConsoleSender().sendMessage(msg.toString());
}
private void appendNumberedList(StringBuilder output, List<String> lines) {
int count = 1;
for (String line : lines) {
output.append(ChatColor.WHITE).append(count).append(") ").append(line);
output.append("\n");
count++;
}
}
}
/*
* 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.collection;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
/**
* This is a class to collect all the errors found while loading the plugin.
*/
public class ErrorCollector {
private final List<String> errors = new ArrayList<>();
private final List<String> warnings = new ArrayList<>();
public void addError(String error) {
errors.add(error);
}
public void addWarning(String warning) {
warnings.add(warning);
}
public int getErrorsCount() {
return errors.size();
}
public int getWarningsCount() {
return warnings.size();
}
public boolean hasWarningsOrErrors() {
return errors.size() > 0 || warnings.size() > 0;
}
public void logToConsole() {
StringBuilder msg = new StringBuilder(200);
msg.append(" \n \n");
if (errors.size() > 0) {
msg.append(ChatColor.RED + "[ChestCommands] Encountered " + errors.size() + " error(s) on load:\n");
appendNumberedList(msg, errors);
}
if (warnings.size() > 0) {
msg.append(ChatColor.YELLOW + "[ChestCommands] Encountered " + warnings.size() + " warnings(s) on load:\n");
appendNumberedList(msg, warnings);
}
Bukkit.getConsoleSender().sendMessage(msg.toString());
}
private void appendNumberedList(StringBuilder output, List<String> lines) {
int count = 1;
for (String line : lines) {
output.append(ChatColor.WHITE).append(count).append(") ").append(line);
output.append("\n");
count++;
}
}
}

View File

@ -1,89 +1,92 @@
/*
* 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 java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
public class Registry<T> {
// Characters to ignore when searching enums by name
private static final char[] KEY_IGNORE_CHARS = {'-', '_', ' '};
private final Class<T> valuesType;
private final Map<String, T> valuesMap;
public static <T extends Enum<T>> Registry<T> fromEnumValues(Class<T> enumClass) {
Registry<T> registry = new Registry<>(enumClass);
registry.putAll(enumClass.getEnumConstants(), Enum::name);
return registry;
}
public static <T> Registry<T> fromValues(T[] values, Function<T, String> toKeyFunction) {
Registry<T> registry = new Registry<>(null);
registry.putAll(values, toKeyFunction);
return registry;
}
private Registry(Class<T> valuesType) {
this.valuesType = valuesType;
this.valuesMap = new HashMap<>();
}
public Optional<T> find(String key) {
if (key == null) {
return Optional.empty();
}
return Optional.ofNullable(valuesMap.get(toKeyFormat(key)));
}
public void putIfEnumExists(String key, String enumValueName) {
Preconditions.checkState(valuesType.isEnum(), "value type is not an enum");
try {
@SuppressWarnings({ "unchecked", "rawtypes" })
T enumValue = (T) Enum.valueOf((Class<Enum>) valuesType, enumValueName);
put(key, enumValue);
} catch (IllegalArgumentException e) {
// Ignore, enum value doesn't exist
}
}
private void putAll(T[] enumValues, Function<T, String> toKeyFunction) {
for (T enumValue : enumValues) {
valuesMap.put(toKeyFormat(toKeyFunction.apply(enumValue)), enumValue);
}
}
public void put(String key, T enumValue) {
valuesMap.put(toKeyFormat(key), enumValue);
}
private String toKeyFormat(String enumValueName) {
return Strings.stripChars(enumValueName, KEY_IGNORE_CHARS).toLowerCase();
}
@Override
public String toString() {
return "Registry [type=" + valuesType + ", values=" + valuesMap + "]";
}
}
/*
* 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.collection;
import me.filoghost.chestcommands.util.Preconditions;
import me.filoghost.chestcommands.util.Strings;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
public class Registry<T> {
// Characters to ignore when searching enums by name
private static final char[] KEY_IGNORE_CHARS = {'-', '_', ' '};
private final Class<T> valuesType;
private final Map<String, T> valuesMap;
public static <T extends Enum<T>> Registry<T> fromEnumValues(Class<T> enumClass) {
Registry<T> registry = new Registry<>(enumClass);
registry.putAll(enumClass.getEnumConstants(), Enum::name);
return registry;
}
public static <T> Registry<T> fromValues(T[] values, Function<T, String> toKeyFunction) {
Registry<T> registry = new Registry<>(null);
registry.putAll(values, toKeyFunction);
return registry;
}
private Registry(Class<T> valuesType) {
this.valuesType = valuesType;
this.valuesMap = new HashMap<>();
}
public Optional<T> find(String key) {
if (key == null) {
return Optional.empty();
}
return Optional.ofNullable(valuesMap.get(toKeyFormat(key)));
}
public void putIfEnumExists(String key, String enumValueName) {
Preconditions.checkState(valuesType.isEnum(), "value type is not an enum");
try {
@SuppressWarnings({ "unchecked", "rawtypes" })
T enumValue = (T) Enum.valueOf((Class<Enum>) valuesType, enumValueName);
put(key, enumValue);
} catch (IllegalArgumentException e) {
// Ignore, enum value doesn't exist
}
}
private void putAll(T[] enumValues, Function<T, String> toKeyFunction) {
for (T enumValue : enumValues) {
valuesMap.put(toKeyFormat(toKeyFunction.apply(enumValue)), enumValue);
}
}
public void put(String key, T enumValue) {
valuesMap.put(toKeyFormat(key), enumValue);
}
private String toKeyFormat(String enumValueName) {
return Strings.stripChars(enumValueName, KEY_IGNORE_CHARS).toLowerCase();
}
@Override
public String toString() {
return "Registry [type=" + valuesType + ", values=" + valuesMap + "]";
}
}

View File

@ -6,7 +6,7 @@ import org.bukkit.entity.Player;
import com.google.common.collect.ImmutableList;
import me.filoghost.chestcommands.util.Utils;
import me.filoghost.chestcommands.util.collection.CollectionUtils;
public class RelativeStringList {
@ -17,7 +17,7 @@ public class RelativeStringList {
public RelativeStringList(List<String> list) {
if (list != null) {
this.originalList = ImmutableList.copyOf(list);
this.relativeList = Utils.transform(list, RelativeString::of);
this.relativeList = CollectionUtils.transform(list, RelativeString::of);
this.hasVariables = this.relativeList.stream().anyMatch(RelativeString::hasVariables);
} else {
this.originalList = null;
@ -32,7 +32,7 @@ public class RelativeStringList {
public List<String> getValue(Player player) {
if (hasVariables) {
return Utils.transform(relativeList, element -> element.getValue(player));
return CollectionUtils.transform(relativeList, element -> element.getValue(player));
} else {
return originalList;
}