Use regex for block bucket, yay

This commit is contained in:
Jesse Boyd 2019-11-10 13:18:15 +00:00
parent 8c1985f9c0
commit 7dcecf486d
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F

View File

@ -10,6 +10,8 @@ import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* Main Configuration Utility * Main Configuration Utility
@ -61,6 +63,9 @@ public class Configuration {
public static final SettingValue<BlockBucket> BLOCK_BUCKET = public static final SettingValue<BlockBucket> BLOCK_BUCKET =
new SettingValue<BlockBucket>("BLOCK_BUCKET") { new SettingValue<BlockBucket>("BLOCK_BUCKET") {
private Pattern pattern = Pattern.compile("((?<namespace>[A-Za-z_]+):)?(?<block>([A-Za-z_]+(\\[?[\\S\\s]+\\])?))(:(?<chance>[0-9]{1,3}))?");
@Override public BlockBucket parseString(final String string) { @Override public BlockBucket parseString(final String string) {
if (string == null || string.isEmpty()) { if (string == null || string.isEmpty()) {
return new BlockBucket(); return new BlockBucket();
@ -68,18 +73,12 @@ public class Configuration {
final BlockBucket blockBucket = new BlockBucket(); final BlockBucket blockBucket = new BlockBucket();
final String[] parts = string.split(",(?![^\\(\\[]*[\\]\\)])"); final String[] parts = string.split(",(?![^\\(\\[]*[\\]\\)])");
for (final String part : parts) { for (final String part : parts) {
String block = part; Matcher matcher = pattern.matcher(part);
int chance = -1; String namespace = matcher.group("namespace");
if (part.contains(":")) { String block = matcher.group("block");
final String[] innerParts = part.split(":"); String chanceStr = matcher.group("chance");
String chanceStr = innerParts[innerParts.length - 1]; if (namespace == null) namespace = "minecraft";
if (innerParts.length > 1 && MathMan.isInteger(chanceStr)) { int chance = chanceStr == null ? -1 : Integer.parseInt(chanceStr);
chance = Integer.parseInt(chanceStr);
block = StringMan.join(Arrays.copyOf(innerParts, innerParts.length - 1), ":");
}
} else {
block = part;
}
final StringComparison<BlockState>.ComparisonResult value = final StringComparison<BlockState>.ComparisonResult value =
WorldUtil.IMP.getClosestBlock(block); WorldUtil.IMP.getClosestBlock(block);
if (value == null) { if (value == null) {
@ -101,19 +100,15 @@ public class Configuration {
} }
final String[] parts = string.split(",(?![^\\(\\[]*[\\]\\)])"); final String[] parts = string.split(",(?![^\\(\\[]*[\\]\\)])");
for (final String part : parts) { for (final String part : parts) {
String block = part; Matcher matcher = pattern.matcher(part);
if (part.contains(":")) { String namespace = matcher.group("namespace");
final String[] innerParts = part.split(":"); String block = matcher.group("block");
String chanceStr = innerParts[innerParts.length - 1]; String chanceStr = matcher.group("chance");
if (innerParts.length > 1 && MathMan.isInteger(chanceStr)) { if (namespace == null) namespace = "minecraft";
final int chance = Integer.parseInt(chanceStr); int chance = chanceStr == null ? -1 : Integer.parseInt(chanceStr);
if (chance < 1 || chance > 100) {
return false; if ((chance != -1 && (chance < 1 || chance > 100)) || block == null) {
} return false;
block = StringMan.join(Arrays.copyOf(innerParts, innerParts.length - 1), ":");
}
} else {
block = part;
} }
StringComparison<BlockState>.ComparisonResult value = StringComparison<BlockState>.ComparisonResult value =
WorldUtil.IMP.getClosestBlock(block); WorldUtil.IMP.getClosestBlock(block);