From 624f52289dad88a45b437cb947190fa70c5b38e7 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 8 Jul 2023 18:00:43 +0200 Subject: [PATCH] Uses KnarLib for material parsing and tab completion filtering --- pom.xml | 2 +- .../net/knarcraft/launchpad/Launchpad.java | 4 +- .../command/LaunchpadTabCompleter.java | 8 +- .../config/LaunchpadConfiguration.java | 7 +- .../launchpad/config/LaunchpadMessage.java | 5 -- .../launchpad/util/MaterialHelper.java | 90 ------------------- .../launchpad/util/TabCompleteHelper.java | 34 ------- src/main/resources/strings.yml | 1 - 8 files changed, 11 insertions(+), 140 deletions(-) delete mode 100644 src/main/java/net/knarcraft/launchpad/util/MaterialHelper.java delete mode 100644 src/main/java/net/knarcraft/launchpad/util/TabCompleteHelper.java diff --git a/pom.xml b/pom.xml index c648966..d65669b 100644 --- a/pom.xml +++ b/pom.xml @@ -97,7 +97,7 @@ net.knarcraft knarlib - 1.2 + 1.2.2 compile diff --git a/src/main/java/net/knarcraft/launchpad/Launchpad.java b/src/main/java/net/knarcraft/launchpad/Launchpad.java index 9de02d7..fa715ec 100644 --- a/src/main/java/net/knarcraft/launchpad/Launchpad.java +++ b/src/main/java/net/knarcraft/launchpad/Launchpad.java @@ -89,8 +89,8 @@ public final class Launchpad extends JavaPlugin { this.translator = translator; this.stringFormatter = new StringFormatter(this.getDescription().getName(), translator); this.stringFormatter.setColorConversion(ColorConversion.RGB); - this.stringFormatter.setNamePrefix("#FFE34C["); - this.stringFormatter.setNameSuffix("#FFE34C]"); + this.stringFormatter.setNamePrefix("#FFE34C[&r&l"); + this.stringFormatter.setNameSuffix("&r#FFE34C]"); // Register events diff --git a/src/main/java/net/knarcraft/launchpad/command/LaunchpadTabCompleter.java b/src/main/java/net/knarcraft/launchpad/command/LaunchpadTabCompleter.java index f3ebada..3d98980 100644 --- a/src/main/java/net/knarcraft/launchpad/command/LaunchpadTabCompleter.java +++ b/src/main/java/net/knarcraft/launchpad/command/LaunchpadTabCompleter.java @@ -1,7 +1,7 @@ package net.knarcraft.launchpad.command; +import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.launchpad.launchpad.ModificationAction; -import net.knarcraft.launchpad.util.TabCompleteHelper; import org.bukkit.block.BlockFace; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -27,7 +27,7 @@ public class LaunchpadTabCompleter implements TabCompleter { @NotNull String[] arguments) { if (arguments.length == 1) { // Display available sub-commands - return TabCompleteHelper.filterMatchingContains(getModificationActions(), arguments[0]); + return TabCompletionHelper.filterMatchingContains(getModificationActions(), arguments[0]); } else { // If given a valid modification action, and an argument is expected, display possible values ModificationAction action = ModificationAction.getFromCommandName(arguments[0]); @@ -35,9 +35,9 @@ public class LaunchpadTabCompleter implements TabCompleter { return new ArrayList<>(); } if (arguments.length == 2) { - return TabCompleteHelper.filterMatchingContains(getTabCompletions(action), arguments[1]); + return TabCompletionHelper.filterMatchingContains(getTabCompletions(action), arguments[1]); } else if (arguments.length == 3) { - return TabCompleteHelper.filterMatchingContains(getTabCompletions(action), arguments[2]); + return TabCompletionHelper.filterMatchingContains(getTabCompletions(action), arguments[2]); } } return new ArrayList<>(); diff --git a/src/main/java/net/knarcraft/launchpad/config/LaunchpadConfiguration.java b/src/main/java/net/knarcraft/launchpad/config/LaunchpadConfiguration.java index 7241ca5..62aba1a 100644 --- a/src/main/java/net/knarcraft/launchpad/config/LaunchpadConfiguration.java +++ b/src/main/java/net/knarcraft/launchpad/config/LaunchpadConfiguration.java @@ -3,10 +3,10 @@ package net.knarcraft.launchpad.config; import net.knarcraft.knarlib.particle.ParticleConfig; import net.knarcraft.knarlib.particle.ParticleSpawner; import net.knarcraft.knarlib.particle.ParticleTrailSpawner; +import net.knarcraft.knarlib.util.MaterialHelper; import net.knarcraft.knarlib.util.ParticleHelper; import net.knarcraft.launchpad.Launchpad; import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler; -import net.knarcraft.launchpad.util.MaterialHelper; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Particle; @@ -139,7 +139,8 @@ public class LaunchpadConfiguration { Set loadedMaterials = new HashSet<>(); List materialWhitelist = launchpadSection.getList(key); if (materialWhitelist != null) { - loadedMaterials.addAll(MaterialHelper.loadMaterialList(materialWhitelist)); + loadedMaterials.addAll(MaterialHelper.loadMaterialList(materialWhitelist, "+", + Launchpad.getInstance().getLogger())); } // If a non-block material is specified, simply ignore it loadedMaterials.removeIf((item) -> !item.isBlock()); @@ -278,7 +279,7 @@ public class LaunchpadConfiguration { @NotNull ConfigurationSection perMaterialSection) { Map materialConfigs = new HashMap<>(); for (String key : perMaterialSection.getKeys(false)) { - Set materials = MaterialHelper.loadMaterialString(key); + Set materials = MaterialHelper.loadMaterialString(key, "+", Launchpad.getInstance().getLogger()); ConfigurationSection materialSection = perMaterialSection.getConfigurationSection(key); if (materialSection == null) { continue; diff --git a/src/main/java/net/knarcraft/launchpad/config/LaunchpadMessage.java b/src/main/java/net/knarcraft/launchpad/config/LaunchpadMessage.java index e5ba7d6..c3037e5 100644 --- a/src/main/java/net/knarcraft/launchpad/config/LaunchpadMessage.java +++ b/src/main/java/net/knarcraft/launchpad/config/LaunchpadMessage.java @@ -21,11 +21,6 @@ public enum LaunchpadMessage implements TranslatableMessage { */ ERROR_NOT_WHITELISTED, - /** - * The message displayed if an un-parse-able message is given by a user - */ - ERROR_MATERIAL_NOT_PARSE_ABLE, - /* **************** * * Success messages * * **************** */ diff --git a/src/main/java/net/knarcraft/launchpad/util/MaterialHelper.java b/src/main/java/net/knarcraft/launchpad/util/MaterialHelper.java deleted file mode 100644 index b999dee..0000000 --- a/src/main/java/net/knarcraft/launchpad/util/MaterialHelper.java +++ /dev/null @@ -1,90 +0,0 @@ -package net.knarcraft.launchpad.util; - -import net.knarcraft.launchpad.Launchpad; -import net.knarcraft.launchpad.config.LaunchpadMessage; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; -import org.bukkit.Tag; -import org.jetbrains.annotations.NotNull; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * A helper class for dealing with and parsing materials - */ -public final class MaterialHelper { - - private MaterialHelper() { - - } - - /** - * Loads the materials specified in the block whitelist - */ - public static @NotNull Set loadMaterialList(@NotNull List materials) { - Set parsedMaterials = new HashSet<>(); - for (Object value : materials) { - if (!(value instanceof String string)) { - continue; - } - - parsedMaterials.addAll(loadMaterialString(string)); - } - return parsedMaterials; - } - - /** - * Parses a string representing a material or a material tag - * - * @param materialString

The material string to parse

- * @return

The materials defined by the material string, or an empty list if none were found

- */ - public static @NotNull Set loadMaterialString(@NotNull String materialString) { - Set parsedMaterials = new HashSet<>(); - // Try to parse a material tag first - if (parseMaterialTag(parsedMaterials, materialString)) { - return parsedMaterials; - } - - // Try to parse a material name - Material matched = Material.matchMaterial(materialString); - if (matched != null) { - parsedMaterials.add(matched); - } else { - Launchpad.getInstance().getStringFormatter().replacePlaceholder( - LaunchpadMessage.ERROR_MATERIAL_NOT_PARSE_ABLE, "{material}", materialString); - } - return parsedMaterials; - } - - /** - * Tries to parse the material tag in the specified material name - * - * @param targetSet

The set all parsed materials should be added to

- * @param materialName

The material name that might be a material tag

- * @return

True if a tag was found

- */ - private static boolean parseMaterialTag(@NotNull Set targetSet, @NotNull String materialName) { - Pattern pattern = Pattern.compile("^\\+([a-zA-Z_]+)"); - Matcher matcher = pattern.matcher(materialName); - if (matcher.find()) { - // The material is a material tag - Tag tag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, NamespacedKey.minecraft( - matcher.group(1).toLowerCase()), Material.class); - if (tag != null) { - targetSet.addAll(tag.getValues()); - } else { - Launchpad.getInstance().getStringFormatter().replacePlaceholder( - LaunchpadMessage.ERROR_MATERIAL_NOT_PARSE_ABLE, "{material}", materialName); - } - return true; - } - return false; - } - -} diff --git a/src/main/java/net/knarcraft/launchpad/util/TabCompleteHelper.java b/src/main/java/net/knarcraft/launchpad/util/TabCompleteHelper.java deleted file mode 100644 index aa13d13..0000000 --- a/src/main/java/net/knarcraft/launchpad/util/TabCompleteHelper.java +++ /dev/null @@ -1,34 +0,0 @@ -package net.knarcraft.launchpad.util; - -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; - -/** - * A helper-class for common tab-completions - */ -public final class TabCompleteHelper { - - private TabCompleteHelper() { - - } - - /** - * Finds tab complete values that contain the typed text - * - * @param values

The values to filter

- * @param typedText

The text the player has started typing

- * @return

The given string values that contain the player's typed text

- */ - public static List filterMatchingContains(@NotNull List values, @NotNull String typedText) { - List configValues = new ArrayList<>(); - for (String value : values) { - if (value.toLowerCase().contains(typedText.toLowerCase())) { - configValues.add(value); - } - } - return configValues; - } - -} diff --git a/src/main/resources/strings.yml b/src/main/resources/strings.yml index f898245..dedca28 100644 --- a/src/main/resources/strings.yml +++ b/src/main/resources/strings.yml @@ -1,7 +1,6 @@ en: ERROR_PLAYER_ONLY: "&cThis command must be used by a player" ERROR_NOT_WHITELISTED: "&cThe block could not be modified, as it's not whitelisted. If you want to abort changing a launchpad, use \"/launchpad abort\"" - ERROR_MATERIAL_NOT_PARSE_ABLE: "&cUnable to parse material: {material}" SUCCESS_PLUGIN_RELOADED: "&aPlugin reloaded!" SUCCESS_MODIFICATIONS_CLEARED: "&aCleared your launchpad modification queue" SUCCESS_CLICK_BLOCK: "&aClick the launchpad you want to create or modify"