Move ItemConfig loading code

This commit is contained in:
GeorgH93 2021-07-11 13:38:22 +02:00
parent c39574fc02
commit 7a75d0d3c8
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
3 changed files with 48 additions and 26 deletions

View File

@ -1,7 +1,7 @@
Defaults:
# The item that should be used as a shortcut by default
BackpackItem: BackpackHeadBlue
BlockedSlotsMaterial: Barrier
BlockedSlots: Blocked
# Options: top_left, top_right, bottom_left, bottom_right
ControlsPosition: bottom_right
ControlsLayout: None
@ -37,7 +37,7 @@ Backpacks:
1:
Pages: 1
SlotsPerPage: 9
BlockedSlotsMaterial: Barrier
BlockedSlots: Blocked
2:
Slots: 18
3:
@ -105,6 +105,10 @@ Buttons:
Material: LIGHT_GRAY_STAINED_GLASS_PANE
DisplayName: ""
Action: nothing
Blocked:
Material: Barrier
DisplayName: "&e{BlockedSlot}"
Action: nothing

View File

@ -40,7 +40,7 @@
public class BackpacksConfig extends Configuration
{
private static final int CONFIG_VERSION = 1;
private static final Pattern ITEM_TEXT_PLACEHOLDER_PATTERN = Pattern.compile("\\{(?<placeholder>[\\w-.]+)}");
private static final Pattern ITEM_TEXT_PLACEHOLDER_PATTERN = Pattern.compile("\\{(?<placeholder>[\\w_-]+)}");
@Getter private static BackpacksConfig instance;
private final @NotNull Minepacks plugin;
@ -107,33 +107,16 @@ private void loadItemConfigs(final @NotNull String parentKey, final @NotNull Map
{
getYamlE().getKeysFiltered(parentKey + "\\.[^.]*\\.Material").forEach(materialKey -> {
final String key = materialKey.substring(0, materialKey.length() - ".Material".length());
try
final ItemConfig itemConfig = ItemConfig.fromConfig(this, key, this::translateItemText);
if(itemConfig != null)
{
if(!getConfigE().getBoolean(key + "Enabled", true)) return;
final String name = key.substring(parentKey.length() + 1);
if(parentKey.equals("Items") && name.equals(MagicValues.BACKPACK_STYLE_NAME_DISABLED)) return;
final List<String> lore = getConfigE().getStringList(key + ".Lore", new ArrayList<>(0));
final List<String> loreFinal;
if(lore.size() == 0) loreFinal = null;
else
{
loreFinal = new ArrayList<>(lore.size());
lore.forEach(loreEntry -> loreFinal.add(translateItemData(loreEntry)));
}
final String displayName = translateItemData(getConfigE().getString(key + ".DisplayName", "&kBackpack"));
final String material = getYamlE().getString(key + ".Material");
final int model = getYamlE().getInt(key + ".Model", 0);
final int amount = getYamlE().getInt(key + ".Amount", 1);
itemConfigs.put(name, new ItemConfig(name, material, amount, displayName, loreFinal, model, getConfigE().getString(key + ".HeadValue", null)));
}
catch(Exception e)
{
plugin.getLogger().warning("Failed to load item definition for '" + key + "'! Error: " + e.getMessage());
if(parentKey.equals("Items") && itemConfig.getName().equals(MagicValues.BACKPACK_STYLE_NAME_DISABLED)) return;
itemConfigs.put(itemConfig.getName(), itemConfig);
}
});
}
private @NotNull String translateItemData(@NotNull String text)
private @NotNull String translateItemText(@NotNull String text)
{
text = ChatColor.translateAlternateColorCodes('&', text);
Matcher matcher = ITEM_TEXT_PLACEHOLDER_PATTERN.matcher(text);
@ -154,6 +137,6 @@ private void loadItemConfigs(final @NotNull String parentKey, final @NotNull Map
public @NotNull List<ItemConfig> getBackpackItems()
{
return backpackStylesMap.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
return new ArrayList<>(backpackStylesMap.values());
}
}

View File

@ -19,6 +19,7 @@
import at.pcgamingfreaks.Bukkit.MCVersion;
import at.pcgamingfreaks.Bukkit.Util.HeadUtils;
import at.pcgamingfreaks.Config.IConfig;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@ -28,8 +29,10 @@
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
@Getter
public final class ItemConfig
@ -43,6 +46,38 @@ public final class ItemConfig
private final @NotNull IItemProducer producer;
@Getter @Setter private @Nullable Object databaseKey;
public static ItemConfig fromConfig(final @NotNull IConfig config, final @NotNull String key, final @Nullable Function<String, String> translatePlaceholdersFunction)
{
try
{
if(!config.getConfigE().getBoolean(key + ".Enabled", true)) return null;
int nameStartAt = key.lastIndexOf('.');
final String name = key.substring(Math.max(0, nameStartAt));
final List<String> lore = config.getConfigE().getStringList(key + ".Lore", new ArrayList<>(0));
final List<String> loreFinal;
if(lore.size() == 0) loreFinal = null;
else
{
loreFinal = new ArrayList<>(lore.size());
lore.forEach(loreEntry -> loreFinal.add(translatePlaceholdersFunction != null ? translatePlaceholdersFunction.apply(loreEntry) : loreEntry));
}
String displayName = config.getConfigE().getString(key + ".DisplayName", "&kBackpack");
if(translatePlaceholdersFunction != null)
{
displayName = translatePlaceholdersFunction.apply(displayName);
}
final String material = config.getConfigE().getString(key + ".Material");
final int model = config.getConfigE().getInt(key + ".Model", 0);
final int amount = config.getConfigE().getInt(key + ".Amount", 1);
return new ItemConfig(name, material, amount, displayName, loreFinal, model, config.getConfigE().getString(key + ".HeadValue", null));
}
catch(Exception e)
{
config.getLogger().warning("Failed to load item definition for '" + key + "'! Error: " + e.getMessage());
}
return null;
}
public ItemConfig(final @NotNull String name, final @NotNull String material, final int amount, final @NotNull String displayName, final @Nullable List<String> lore, int model, final @Nullable String value) throws IllegalArgumentException
{
assert model < 0;