From 7dcecf486dcde089751876ee0dadcd28916e64d3 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sun, 10 Nov 2019 13:18:15 +0000 Subject: [PATCH] Use regex for block bucket, yay --- .../plot/config/Configuration.java | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Configuration.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Configuration.java index dc143f1e4..ec2e1dd34 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Configuration.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/Configuration.java @@ -10,6 +10,8 @@ import lombok.Getter; import lombok.NonNull; import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Main Configuration Utility @@ -61,6 +63,9 @@ public class Configuration { public static final SettingValue BLOCK_BUCKET = new SettingValue("BLOCK_BUCKET") { + + private Pattern pattern = Pattern.compile("((?[A-Za-z_]+):)?(?([A-Za-z_]+(\\[?[\\S\\s]+\\])?))(:(?[0-9]{1,3}))?"); + @Override public BlockBucket parseString(final String string) { if (string == null || string.isEmpty()) { return new BlockBucket(); @@ -68,18 +73,12 @@ public class Configuration { final BlockBucket blockBucket = new BlockBucket(); final String[] parts = string.split(",(?![^\\(\\[]*[\\]\\)])"); for (final String part : parts) { - String block = part; - int chance = -1; - if (part.contains(":")) { - final String[] innerParts = part.split(":"); - String chanceStr = innerParts[innerParts.length - 1]; - if (innerParts.length > 1 && MathMan.isInteger(chanceStr)) { - chance = Integer.parseInt(chanceStr); - block = StringMan.join(Arrays.copyOf(innerParts, innerParts.length - 1), ":"); - } - } else { - block = part; - } + Matcher matcher = pattern.matcher(part); + String namespace = matcher.group("namespace"); + String block = matcher.group("block"); + String chanceStr = matcher.group("chance"); + if (namespace == null) namespace = "minecraft"; + int chance = chanceStr == null ? -1 : Integer.parseInt(chanceStr); final StringComparison.ComparisonResult value = WorldUtil.IMP.getClosestBlock(block); if (value == null) { @@ -101,19 +100,15 @@ public class Configuration { } final String[] parts = string.split(",(?![^\\(\\[]*[\\]\\)])"); for (final String part : parts) { - String block = part; - if (part.contains(":")) { - final String[] innerParts = part.split(":"); - String chanceStr = innerParts[innerParts.length - 1]; - if (innerParts.length > 1 && MathMan.isInteger(chanceStr)) { - final int chance = Integer.parseInt(chanceStr); - if (chance < 1 || chance > 100) { - return false; - } - block = StringMan.join(Arrays.copyOf(innerParts, innerParts.length - 1), ":"); - } - } else { - block = part; + Matcher matcher = pattern.matcher(part); + String namespace = matcher.group("namespace"); + String block = matcher.group("block"); + String chanceStr = matcher.group("chance"); + if (namespace == null) namespace = "minecraft"; + int chance = chanceStr == null ? -1 : Integer.parseInt(chanceStr); + + if ((chance != -1 && (chance < 1 || chance > 100)) || block == null) { + return false; } StringComparison.ComparisonResult value = WorldUtil.IMP.getClosestBlock(block);