Uses KnarLib for material parsing and tab completion filtering

This commit is contained in:
Kristian Knarvik 2023-07-08 18:00:43 +02:00
parent b6a2c9d3ab
commit 624f52289d
8 changed files with 11 additions and 140 deletions

View File

@ -97,7 +97,7 @@
<dependency> <dependency>
<groupId>net.knarcraft</groupId> <groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId> <artifactId>knarlib</artifactId>
<version>1.2</version> <version>1.2.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -89,8 +89,8 @@ public final class Launchpad extends JavaPlugin {
this.translator = translator; this.translator = translator;
this.stringFormatter = new StringFormatter(this.getDescription().getName(), translator); this.stringFormatter = new StringFormatter(this.getDescription().getName(), translator);
this.stringFormatter.setColorConversion(ColorConversion.RGB); this.stringFormatter.setColorConversion(ColorConversion.RGB);
this.stringFormatter.setNamePrefix("#FFE34C["); this.stringFormatter.setNamePrefix("#FFE34C[&r&l");
this.stringFormatter.setNameSuffix("#FFE34C]"); this.stringFormatter.setNameSuffix("&r#FFE34C]");
// Register events // Register events

View File

@ -1,7 +1,7 @@
package net.knarcraft.launchpad.command; package net.knarcraft.launchpad.command;
import net.knarcraft.knarlib.util.TabCompletionHelper;
import net.knarcraft.launchpad.launchpad.ModificationAction; import net.knarcraft.launchpad.launchpad.ModificationAction;
import net.knarcraft.launchpad.util.TabCompleteHelper;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -27,7 +27,7 @@ public class LaunchpadTabCompleter implements TabCompleter {
@NotNull String[] arguments) { @NotNull String[] arguments) {
if (arguments.length == 1) { if (arguments.length == 1) {
// Display available sub-commands // Display available sub-commands
return TabCompleteHelper.filterMatchingContains(getModificationActions(), arguments[0]); return TabCompletionHelper.filterMatchingContains(getModificationActions(), arguments[0]);
} else { } else {
// If given a valid modification action, and an argument is expected, display possible values // If given a valid modification action, and an argument is expected, display possible values
ModificationAction action = ModificationAction.getFromCommandName(arguments[0]); ModificationAction action = ModificationAction.getFromCommandName(arguments[0]);
@ -35,9 +35,9 @@ public class LaunchpadTabCompleter implements TabCompleter {
return new ArrayList<>(); return new ArrayList<>();
} }
if (arguments.length == 2) { if (arguments.length == 2) {
return TabCompleteHelper.filterMatchingContains(getTabCompletions(action), arguments[1]); return TabCompletionHelper.filterMatchingContains(getTabCompletions(action), arguments[1]);
} else if (arguments.length == 3) { } else if (arguments.length == 3) {
return TabCompleteHelper.filterMatchingContains(getTabCompletions(action), arguments[2]); return TabCompletionHelper.filterMatchingContains(getTabCompletions(action), arguments[2]);
} }
} }
return new ArrayList<>(); return new ArrayList<>();

View File

@ -3,10 +3,10 @@ package net.knarcraft.launchpad.config;
import net.knarcraft.knarlib.particle.ParticleConfig; import net.knarcraft.knarlib.particle.ParticleConfig;
import net.knarcraft.knarlib.particle.ParticleSpawner; import net.knarcraft.knarlib.particle.ParticleSpawner;
import net.knarcraft.knarlib.particle.ParticleTrailSpawner; import net.knarcraft.knarlib.particle.ParticleTrailSpawner;
import net.knarcraft.knarlib.util.MaterialHelper;
import net.knarcraft.knarlib.util.ParticleHelper; import net.knarcraft.knarlib.util.ParticleHelper;
import net.knarcraft.launchpad.Launchpad; import net.knarcraft.launchpad.Launchpad;
import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler; import net.knarcraft.launchpad.launchpad.LaunchpadBlockHandler;
import net.knarcraft.launchpad.util.MaterialHelper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Particle; import org.bukkit.Particle;
@ -139,7 +139,8 @@ public class LaunchpadConfiguration {
Set<Material> loadedMaterials = new HashSet<>(); Set<Material> loadedMaterials = new HashSet<>();
List<?> materialWhitelist = launchpadSection.getList(key); List<?> materialWhitelist = launchpadSection.getList(key);
if (materialWhitelist != null) { 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 // If a non-block material is specified, simply ignore it
loadedMaterials.removeIf((item) -> !item.isBlock()); loadedMaterials.removeIf((item) -> !item.isBlock());
@ -278,7 +279,7 @@ public class LaunchpadConfiguration {
@NotNull ConfigurationSection perMaterialSection) { @NotNull ConfigurationSection perMaterialSection) {
Map<Material, ParticleConfig> materialConfigs = new HashMap<>(); Map<Material, ParticleConfig> materialConfigs = new HashMap<>();
for (String key : perMaterialSection.getKeys(false)) { for (String key : perMaterialSection.getKeys(false)) {
Set<Material> materials = MaterialHelper.loadMaterialString(key); Set<Material> materials = MaterialHelper.loadMaterialString(key, "+", Launchpad.getInstance().getLogger());
ConfigurationSection materialSection = perMaterialSection.getConfigurationSection(key); ConfigurationSection materialSection = perMaterialSection.getConfigurationSection(key);
if (materialSection == null) { if (materialSection == null) {
continue; continue;

View File

@ -21,11 +21,6 @@ public enum LaunchpadMessage implements TranslatableMessage {
*/ */
ERROR_NOT_WHITELISTED, ERROR_NOT_WHITELISTED,
/**
* The message displayed if an un-parse-able message is given by a user
*/
ERROR_MATERIAL_NOT_PARSE_ABLE,
/* **************** * /* **************** *
* Success messages * * Success messages *
* **************** */ * **************** */

View File

@ -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<Material> loadMaterialList(@NotNull List<?> materials) {
Set<Material> 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 <p>The material string to parse</p>
* @return <p>The materials defined by the material string, or an empty list if none were found</p>
*/
public static @NotNull Set<Material> loadMaterialString(@NotNull String materialString) {
Set<Material> 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 <p>The set all parsed materials should be added to</p>
* @param materialName <p>The material name that might be a material tag</p>
* @return <p>True if a tag was found</p>
*/
private static boolean parseMaterialTag(@NotNull Set<Material> 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<Material> 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;
}
}

View File

@ -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 <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that contain the player's typed text</p>
*/
public static List<String> filterMatchingContains(@NotNull List<String> values, @NotNull String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().contains(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
}

View File

@ -1,7 +1,6 @@
en: en:
ERROR_PLAYER_ONLY: "&cThis command must be used by a player" 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_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_PLUGIN_RELOADED: "&aPlugin reloaded!"
SUCCESS_MODIFICATIONS_CLEARED: "&aCleared your launchpad modification queue" SUCCESS_MODIFICATIONS_CLEARED: "&aCleared your launchpad modification queue"
SUCCESS_CLICK_BLOCK: "&aClick the launchpad you want to create or modify" SUCCESS_CLICK_BLOCK: "&aClick the launchpad you want to create or modify"