From 130274de86c07e06c67d47690b8b00de875551e4 Mon Sep 17 00:00:00 2001 From: sauilitired Date: Fri, 21 Dec 2018 03:31:08 +0100 Subject: [PATCH] Fix multiverse setup and don't allow unsafe blocks in setup --- .../plotsquared/plot/PlotSquared.java | 14 ++- .../plotsquared/plot/commands/Setup.java | 9 +- .../plot/config/Configuration.java | 96 ++++--------------- .../plot/config/ConfigurationNode.java | 5 +- 4 files changed, 35 insertions(+), 89 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java index 44edfd4b7..ab83748bf 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/PlotSquared.java @@ -1380,25 +1380,23 @@ import java.util.zip.ZipInputStream; break; case "f": case "floor": - this.worlds.set(base + "plot.floor", new ArrayList<>(Arrays.asList( - StringMan.join(Configuration.BLOCKLIST.parseString(value), ",") - .split(",")))); + this.worlds.set(base + "plot.floor", + Configuration.BLOCK_BUCKET.parseString(value).toString()); break; case "m": case "main": - this.worlds.set(base + "plot.filling", new ArrayList<>(Arrays.asList( - StringMan.join(Configuration.BLOCKLIST.parseString(value), ",") - .split(",")))); + this.worlds.set(base + "plot.filling", + Configuration.BLOCK_BUCKET.parseString(value).toString()); break; case "w": case "wall": this.worlds.set(base + "wall.filling", - Configuration.BLOCK.parseString(value).toString()); + Configuration.BLOCK_BUCKET.parseString(value).toString()); break; case "b": case "border": this.worlds.set(base + "wall.block", - Configuration.BLOCK.parseString(value).toString()); + Configuration.BLOCK_BUCKET.parseString(value).toString()); break; default: PlotSquared.log("&cKey not found: &7" + element); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Setup.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Setup.java index 2b79fd8af..bdcbbaaab 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Setup.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Setup.java @@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.commands; import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.C; +import com.github.intellectualsites.plotsquared.plot.config.Configuration; import com.github.intellectualsites.plotsquared.plot.config.ConfigurationNode; import com.github.intellectualsites.plotsquared.plot.generator.GeneratorWrapper; import com.github.intellectualsites.plotsquared.plot.object.PlotArea; @@ -246,7 +247,13 @@ import java.util.Map.Entry; step.getType().getType(), String.valueOf(step.getDefaultValue())); return false; } - boolean valid = step.isValid(args[0]); + + boolean valid = false; + try { + step.isValid(args[0]); + } catch (final Configuration.UnsafeBlockException e) { + C.NOT_ALLOWED_BLOCK.send(player, e.getUnsafeBlock().toString()); + } if (valid) { sendMessage(player, C.SETUP_VALID_ARG, step.getConstant(), args[0]); step.setValue(args[0]); 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 a87377043..3ed154f25 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 @@ -7,8 +7,6 @@ import com.github.intellectualsites.plotsquared.plot.util.WorldUtil; import lombok.Getter; import lombok.NonNull; -import java.util.ArrayList; - /** * Main Configuration Utility */ @@ -88,22 +86,7 @@ public class Configuration { return "FOREST"; } }; - public static final SettingValue BLOCK = new SettingValue("BLOCK") { - @Override public boolean validateValue(String string) { - StringComparison.ComparisonResult value = - WorldUtil.IMP.getClosestBlock(string); - return !(value == null || value.match > 1); - } - @Override public PlotBlock parseString(String string) { - StringComparison.ComparisonResult value = - WorldUtil.IMP.getClosestBlock(string); - if (value == null || value.match > 1) { - return null; - } - return value.best; - } - }; public static final SettingValue BLOCK_BUCKET = new SettingValue("BLOCK_BUCKET") { @Override public BlockBucket parseString(final String string) { @@ -129,6 +112,8 @@ public class Configuration { WorldUtil.IMP.getClosestBlock(block); if (value == null) { throw new UnknownBlockException(block); + } else if (!value.best.isAir() && !WorldUtil.IMP.isBlockSolid(value.best)) { + throw new UnsafeBlockException(value.best); } blockBucket.addBlock(value.best, chance); } @@ -160,6 +145,8 @@ public class Configuration { WorldUtil.IMP.getClosestBlock(block); if (value == null || value.match > 1) { return false; + } else if (!value.best.isAir() && !WorldUtil.IMP.isBlockSolid(value.best)) { + throw new UnsafeBlockException(value.best); } } } catch (final Throwable exception) { @@ -168,68 +155,6 @@ public class Configuration { return true; } }; - public static final SettingValue BLOCKLIST = - new SettingValue("BLOCKLIST") { - @Override public boolean validateValue(String string) { - try { - for (String block : string.split(",")) { - if (block.contains("%")) { - String[] split = block.split("%"); - Integer.parseInt(split[0]); - block = split[1]; - } - StringComparison.ComparisonResult value = - WorldUtil.IMP.getClosestBlock(block); - if (value == null || value.match > 1) { - return false; - } - } - return true; - } catch (NumberFormatException ignored) { - return false; - } - } - - @Override public PlotBlock[] parseString(String string) { - String[] blocks = string.split(","); - ArrayList parsedvalues = new ArrayList<>(); - PlotBlock[] values = new PlotBlock[blocks.length]; - int[] counts = new int[blocks.length]; - int min = 100; - for (int i = 0; i < blocks.length; i++) { - try { - if (blocks[i].contains("%")) { - String[] split = blocks[i].split("%"); - blocks[i] = split[1]; - int value = Integer.parseInt(split[0]); - counts[i] = value; - if (value < min) { - min = value; - } - } else { - counts[i] = 1; - if (1 < min) { - min = 1; - } - } - StringComparison.ComparisonResult result = - WorldUtil.IMP.getClosestBlock(blocks[i]); - if (result != null && result.match < 2) { - values[i] = result.best; - } - } catch (NumberFormatException ignored) { - } - } - int gcd = gcd(counts); - for (int i = 0; i < counts.length; i++) { - int num = counts[i]; - for (int j = 0; j < num / gcd; j++) { - parsedvalues.add(values[i]); - } - } - return parsedvalues.toArray(new PlotBlock[parsedvalues.size()]); - } - }; public static int gcd(int a, int b) { if (b == 0) { @@ -278,4 +203,17 @@ public class Configuration { public abstract boolean validateValue(String string); } + + public static final class UnsafeBlockException extends IllegalArgumentException { + + @Getter + private final PlotBlock unsafeBlock; + + public UnsafeBlockException(@NonNull final PlotBlock unsafeBlock) { + super(String.format("%s is not a valid block", unsafeBlock)); + this.unsafeBlock = unsafeBlock; + } + + } + } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/ConfigurationNode.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/ConfigurationNode.java index f44929d0f..31539d5c2 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/ConfigurationNode.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/config/ConfigurationNode.java @@ -36,7 +36,10 @@ public class ConfigurationNode { try { Object result = this.type.parseString(string); return result != null; - } catch (Exception ignored) { + } catch (Exception e) { + if (e instanceof Configuration.UnknownBlockException) { + throw e; + } return false; } }