From d34f5cc97eede3e172d21d21b3e65fd49584b9a1 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sun, 10 Nov 2019 18:32:32 +0000 Subject: [PATCH] Config reading/writing --- .../plotsquared/plot/commands/Area.java | 8 +- .../plot/config/Configuration.java | 62 +---- .../plot/generator/ClassicPlotManager.java | 63 ++--- .../plot/generator/ClassicPlotWorld.java | 25 +- .../plotsquared/plot/generator/HybridGen.java | 18 +- .../plot/generator/HybridPlotManager.java | 4 +- .../plotsquared/plot/object/BlockBucket.java | 233 ++++++------------ .../plotsquared/plot/object/Plot.java | 3 +- .../plot/util/world/PatternUtil.java | 16 +- 9 files changed, 148 insertions(+), 284 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Area.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Area.java index 11134d2ca..e21c82f7f 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Area.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Area.java @@ -209,20 +209,20 @@ import java.util.Set; break; case "f": case "floor": - pa.TOP_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]).getPattern(); + pa.TOP_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]); break; case "m": case "main": - pa.MAIN_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]).getPattern(); + pa.MAIN_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]); break; case "w": case "wall": pa.WALL_FILLING = - Configuration.BLOCK_BUCKET.parseString(pair[1]).getPattern(); + Configuration.BLOCK_BUCKET.parseString(pair[1]); break; case "b": case "border": - pa.WALL_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]).getPattern(); + pa.WALL_BLOCK = Configuration.BLOCK_BUCKET.parseString(pair[1]); break; case "terrain": pa.TERRAIN = Integer.parseInt(pair[1]); 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 a1aedc478..aba85f3f8 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 @@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.config; import com.github.intellectualsites.plotsquared.plot.object.BlockBucket; import com.github.intellectualsites.plotsquared.plot.util.StringComparison; import com.github.intellectualsites.plotsquared.plot.util.WorldUtil; +import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.biome.BiomeTypes; import com.sk89q.worldedit.world.block.BlockState; @@ -10,7 +11,6 @@ import lombok.Getter; import lombok.NonNull; import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Main Configuration Utility @@ -62,67 +62,19 @@ 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(); - } - final BlockBucket blockBucket = new BlockBucket(); - final String[] parts = string.split(",(?![^\\(\\[]*[\\]\\)])"); - for (final String part : parts) { - Matcher matcher = pattern.matcher(part); - matcher.find(); - 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) { - throw new UnknownBlockException(block); - } else if (Settings.Enabled_Components.PREVENT_UNSAFE && !value.best.getBlockType().getMaterial().isAir() - && !WorldUtil.IMP.isBlockSolid(value.best)) { - throw new UnsafeBlockException(value.best); - } - blockBucket.addBlock(value.best, chance); - } - blockBucket.compile(); // Pre-compile :D - return blockBucket; + BlockBucket bucket = new BlockBucket(string); + bucket.compile(); + Pattern pattern = bucket.toPattern(); + return pattern != null ? bucket : null; } @Override public boolean validateValue(final String string) { try { - if (string == null || string.isEmpty()) { - return false; - } - final String[] parts = string.split(",(?![^\\(\\[]*[\\]\\)])"); - for (final String part : parts) { - Matcher matcher = pattern.matcher(part); - matcher.find(); - 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); - if (value == null || value.match > 1) { - return false; - } else if (Settings.Enabled_Components.PREVENT_UNSAFE && !value.best.getBlockType().getMaterial().isAir() - && !WorldUtil.IMP.isBlockSolid(value.best)) { - throw new UnsafeBlockException(value.best); - } - } - } catch (final Throwable exception) { + return parseString(string) != null; + } catch (Exception e) { return false; } - return true; } }; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java index a06fbceca..42ebd5c3d 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java @@ -1,6 +1,7 @@ package com.github.intellectualsites.plotsquared.plot.generator; import com.github.intellectualsites.plotsquared.plot.config.Settings; +import com.github.intellectualsites.plotsquared.plot.object.BlockBucket; import com.github.intellectualsites.plotsquared.plot.object.Direction; import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Plot; @@ -52,9 +53,9 @@ public class ClassicPlotManager extends SquarePlotManager { } @Override public boolean unClaimPlot(Plot plot, Runnable whenDone) { - setWallFilling(plot.getId(), classicPlotWorld.WALL_FILLING); - if (!isAir(classicPlotWorld.WALL_BLOCK) || !classicPlotWorld.WALL_BLOCK.equals(classicPlotWorld.CLAIMED_WALL_BLOCK)) { - setWall(plot.getId(), classicPlotWorld.WALL_BLOCK); + setWallFilling(plot.getId(), classicPlotWorld.WALL_FILLING.toPattern()); + if (!classicPlotWorld.WALL_BLOCK.isAir() || !classicPlotWorld.WALL_BLOCK.equals(classicPlotWorld.CLAIMED_WALL_BLOCK)) { + setWall(plot.getId(), classicPlotWorld.WALL_BLOCK.toPattern()); } return GlobalBlockQueue.IMP.addEmptyTask(whenDone); } @@ -292,15 +293,15 @@ public class ClassicPlotManager extends SquarePlotManager { queue.setCuboid(new Location(classicPlotWorld.worldname, sx, 0, sz + 1), new Location(classicPlotWorld.worldname, ex, 0, ez - 1), BlockUtil.get((short) 7, (byte) 0)); queue.setCuboid(new Location(classicPlotWorld.worldname, sx, 1, sz + 1), - new Location(classicPlotWorld.worldname, sx, classicPlotWorld.WALL_HEIGHT, ez - 1), classicPlotWorld.WALL_FILLING); + new Location(classicPlotWorld.worldname, sx, classicPlotWorld.WALL_HEIGHT, ez - 1), classicPlotWorld.WALL_FILLING.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx, classicPlotWorld.WALL_HEIGHT + 1, sz + 1), - new Location(classicPlotWorld.worldname, sx, classicPlotWorld.WALL_HEIGHT + 1, ez - 1), classicPlotWorld.WALL_BLOCK); + new Location(classicPlotWorld.worldname, sx, classicPlotWorld.WALL_HEIGHT + 1, ez - 1), classicPlotWorld.WALL_BLOCK.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, ex, 1, sz + 1), - new Location(classicPlotWorld.worldname, ex, classicPlotWorld.WALL_HEIGHT, ez - 1), classicPlotWorld.WALL_FILLING); + new Location(classicPlotWorld.worldname, ex, classicPlotWorld.WALL_HEIGHT, ez - 1), classicPlotWorld.WALL_FILLING.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, ex, classicPlotWorld.WALL_HEIGHT + 1, sz + 1), - new Location(classicPlotWorld.worldname, ex, classicPlotWorld.WALL_HEIGHT + 1, ez - 1), classicPlotWorld.WALL_BLOCK); + new Location(classicPlotWorld.worldname, ex, classicPlotWorld.WALL_HEIGHT + 1, ez - 1), classicPlotWorld.WALL_BLOCK.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, 1, sz + 1), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.ROAD_HEIGHT, ez - 1), classicPlotWorld.ROAD_BLOCK); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.ROAD_HEIGHT, ez - 1), classicPlotWorld.ROAD_BLOCK.toPattern()); return queue.enqueue(); } @@ -320,15 +321,15 @@ public class ClassicPlotManager extends SquarePlotManager { queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, 0, sz), new Location(classicPlotWorld.worldname, ex - 1, 0, ez), BlockUtil.get((short) 7, (byte) 0)); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, 1, sz), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.WALL_HEIGHT, sz), classicPlotWorld.WALL_FILLING); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.WALL_HEIGHT, sz), classicPlotWorld.WALL_FILLING.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, classicPlotWorld.WALL_HEIGHT + 1, sz), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.WALL_HEIGHT + 1, sz), classicPlotWorld.WALL_BLOCK); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.WALL_HEIGHT + 1, sz), classicPlotWorld.WALL_BLOCK.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, 1, ez), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.WALL_HEIGHT, ez), classicPlotWorld.WALL_FILLING); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.WALL_HEIGHT, ez), classicPlotWorld.WALL_FILLING.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, classicPlotWorld.WALL_HEIGHT + 1, ez), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.WALL_HEIGHT + 1, ez), classicPlotWorld.WALL_BLOCK); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.WALL_HEIGHT + 1, ez), classicPlotWorld.WALL_BLOCK.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, 1, sz + 1), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.ROAD_HEIGHT, ez - 1), classicPlotWorld.ROAD_BLOCK); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.ROAD_HEIGHT, ez - 1), classicPlotWorld.ROAD_BLOCK.toPattern()); return queue.enqueue(); } @@ -346,7 +347,7 @@ public class ClassicPlotManager extends SquarePlotManager { new Location(classicPlotWorld.worldname, ex - 1, 0, ez - 1), BlockUtil.get((short) 7, (byte) 0)); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, 1, sz + 1), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.ROAD_HEIGHT, ez - 1), classicPlotWorld.ROAD_BLOCK); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.ROAD_HEIGHT, ez - 1), classicPlotWorld.ROAD_BLOCK.toPattern()); return queue.enqueue(); } @@ -364,9 +365,9 @@ public class ClassicPlotManager extends SquarePlotManager { new Location(classicPlotWorld.worldname, ex, classicPlotWorld.getPlotManager().getWorldHeight(), ez), BlockUtil.get((short) 0, (byte) 0)); queue.setCuboid(new Location(classicPlotWorld.worldname, sx, 1, sz + 1), - new Location(classicPlotWorld.worldname, ex, classicPlotWorld.PLOT_HEIGHT - 1, ez - 1), classicPlotWorld.MAIN_BLOCK); + new Location(classicPlotWorld.worldname, ex, classicPlotWorld.PLOT_HEIGHT - 1, ez - 1), classicPlotWorld.MAIN_BLOCK.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx, classicPlotWorld.PLOT_HEIGHT, sz + 1), - new Location(classicPlotWorld.worldname, ex, classicPlotWorld.PLOT_HEIGHT, ez - 1), classicPlotWorld.TOP_BLOCK); + new Location(classicPlotWorld.worldname, ex, classicPlotWorld.PLOT_HEIGHT, ez - 1), classicPlotWorld.TOP_BLOCK.toPattern()); return queue.enqueue(); } @@ -384,9 +385,9 @@ public class ClassicPlotManager extends SquarePlotManager { new Location(classicPlotWorld.worldname, ex, classicPlotWorld.getPlotManager().getWorldHeight(), ez), BlockUtil.get((short) 0, (byte) 0)); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, 1, sz), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.PLOT_HEIGHT - 1, ez), classicPlotWorld.MAIN_BLOCK); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.PLOT_HEIGHT - 1, ez), classicPlotWorld.MAIN_BLOCK.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx + 1, classicPlotWorld.PLOT_HEIGHT, sz), - new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.PLOT_HEIGHT, ez), classicPlotWorld.TOP_BLOCK); + new Location(classicPlotWorld.worldname, ex - 1, classicPlotWorld.PLOT_HEIGHT, ez), classicPlotWorld.TOP_BLOCK.toPattern()); return queue.enqueue(); } @@ -401,9 +402,9 @@ public class ClassicPlotManager extends SquarePlotManager { new Location(classicPlotWorld.worldname, ex, classicPlotWorld.getPlotManager().getWorldHeight(), ez), BlockUtil.get((short) 0, (byte) 0)); queue.setCuboid(new Location(classicPlotWorld.worldname, sx, 1, sz), - new Location(classicPlotWorld.worldname, ex, classicPlotWorld.ROAD_HEIGHT - 1, ez), classicPlotWorld.MAIN_BLOCK); + new Location(classicPlotWorld.worldname, ex, classicPlotWorld.ROAD_HEIGHT - 1, ez), classicPlotWorld.MAIN_BLOCK.toPattern()); queue.setCuboid(new Location(classicPlotWorld.worldname, sx, classicPlotWorld.ROAD_HEIGHT, sz), - new Location(classicPlotWorld.worldname, ex, classicPlotWorld.ROAD_HEIGHT, ez), classicPlotWorld.TOP_BLOCK); + new Location(classicPlotWorld.worldname, ex, classicPlotWorld.ROAD_HEIGHT, ez), classicPlotWorld.TOP_BLOCK.toPattern()); return queue.enqueue(); } @@ -413,26 +414,26 @@ public class ClassicPlotManager extends SquarePlotManager { * @return false if part of the merge failed, otherwise true if successful. */ @Override public boolean finishPlotMerge(List plotIds) { - final Pattern claim = classicPlotWorld.CLAIMED_WALL_BLOCK; - if (!isAir(claim) || !claim.equals(classicPlotWorld.WALL_BLOCK)) { + final BlockBucket claim = classicPlotWorld.CLAIMED_WALL_BLOCK; + if (!claim.isAir() || !claim.equals(classicPlotWorld.WALL_BLOCK)) { for (PlotId plotId : plotIds) { - setWall(plotId, claim); + setWall(plotId, claim.toPattern()); } } if (Settings.General.MERGE_REPLACE_WALL) { - final Pattern wallBlock = classicPlotWorld.WALL_FILLING; + final BlockBucket wallBlock = classicPlotWorld.WALL_FILLING; for (PlotId id : plotIds) { - setWallFilling(id, wallBlock); + setWallFilling(id, wallBlock.toPattern()); } } return true; } @Override public boolean finishPlotUnlink(List plotIds) { - final Pattern claim = classicPlotWorld.CLAIMED_WALL_BLOCK; - if (!isAir(claim) || !claim.equals(classicPlotWorld.WALL_BLOCK)) { + final BlockBucket claim = classicPlotWorld.CLAIMED_WALL_BLOCK; + if (!claim.isAir() || !claim.equals(classicPlotWorld.WALL_BLOCK)) { for (PlotId id : plotIds) { - setWall(id, claim); + setWall(id, claim.toPattern()); } } return true; // return false if unlink has been denied @@ -447,9 +448,9 @@ public class ClassicPlotManager extends SquarePlotManager { } @Override public boolean claimPlot(Plot plot) { - final Pattern claim = classicPlotWorld.CLAIMED_WALL_BLOCK; - if (!isAir(claim) || !claim.equals(classicPlotWorld.WALL_BLOCK)) { - return setWall(plot.getId(), claim); + final BlockBucket claim = classicPlotWorld.CLAIMED_WALL_BLOCK; + if (!claim.isAir() || !claim.equals(classicPlotWorld.WALL_BLOCK)) { + return setWall(plot.getId(), claim.toPattern()); } return true; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotWorld.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotWorld.java index 46af0e43c..abb95d99a 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotWorld.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotWorld.java @@ -5,6 +5,7 @@ import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.Configuration; import com.github.intellectualsites.plotsquared.plot.config.ConfigurationNode; import com.github.intellectualsites.plotsquared.plot.config.Settings; +import com.github.intellectualsites.plotsquared.plot.object.BlockBucket; import com.github.intellectualsites.plotsquared.plot.object.PlotId; import com.github.intellectualsites.plotsquared.plot.util.world.BlockUtil; import com.sk89q.worldedit.function.pattern.BlockPattern; @@ -20,17 +21,17 @@ import java.util.Locale; public int ROAD_HEIGHT = 62; public int PLOT_HEIGHT = 62; public int WALL_HEIGHT = 62; - public Pattern MAIN_BLOCK = new BlockPattern(BlockTypes.STONE.getDefaultState()); + public BlockBucket MAIN_BLOCK = new BlockBucket(BlockTypes.STONE); // new BlockState[] {BlockUtil.get("stone")}; - public Pattern TOP_BLOCK = new BlockPattern(BlockTypes.GRASS_BLOCK.getDefaultState()); + public BlockBucket TOP_BLOCK = new BlockBucket(BlockTypes.GRASS_BLOCK); //new BlockState[] {BlockUtil.get("grass")}; - public Pattern WALL_BLOCK = new BlockPattern(BlockTypes.STONE_SLAB.getDefaultState()); + public BlockBucket WALL_BLOCK = new BlockBucket(BlockTypes.STONE_SLAB); // BlockUtil.get((short) 44, (byte) 0); - public Pattern CLAIMED_WALL_BLOCK = new BlockPattern(BlockTypes.SANDSTONE_SLAB.getDefaultState()); + public BlockBucket CLAIMED_WALL_BLOCK = new BlockBucket(BlockTypes.SANDSTONE_SLAB); // BlockUtil.get((short) 44, (byte) 1); - public Pattern WALL_FILLING = new BlockPattern(BlockTypes.STONE.getDefaultState()); + public BlockBucket WALL_FILLING = new BlockBucket(BlockTypes.STONE); //BlockUtil.get((short) 1, (byte) 0); - public Pattern ROAD_BLOCK = new BlockPattern(BlockTypes.QUARTZ_BLOCK.getDefaultState()); + public BlockBucket ROAD_BLOCK = new BlockBucket(BlockTypes.QUARTZ_BLOCK); // BlockUtil.get((short) 155, (byte) 0); public boolean PLOT_BEDROCK = true; @@ -83,16 +84,16 @@ import java.util.Locale; super.loadConfiguration(config); this.PLOT_BEDROCK = config.getBoolean("plot.bedrock"); this.PLOT_HEIGHT = Math.min(255, config.getInt("plot.height")); - this.MAIN_BLOCK = new BlockPattern(BlockUtil.get(config.getString("plot.filling"))); - this.TOP_BLOCK = new BlockPattern(BlockUtil.get(config.getString("plot.floor"))); - this.WALL_BLOCK = new BlockPattern(BlockUtil.get(config.getString("wall.block"))); + this.MAIN_BLOCK = new BlockBucket(BlockUtil.get(config.getString("plot.filling"))); + this.TOP_BLOCK = new BlockBucket(BlockUtil.get(config.getString("plot.floor"))); + this.WALL_BLOCK = new BlockBucket(BlockUtil.get(config.getString("wall.block"))); this.ROAD_HEIGHT = Math.min(255, config.getInt("road.height")); - this.ROAD_BLOCK = new BlockPattern(BlockUtil.get(config.getString("road.block"))); + this.ROAD_BLOCK = new BlockBucket(BlockUtil.get(config.getString("road.block"))); this.WALL_FILLING = - new BlockPattern(BlockUtil.get(config.getString("wall.filling"))); + new BlockBucket(BlockUtil.get(config.getString("wall.filling"))); this.WALL_HEIGHT = Math.min(254, config.getInt("wall.height")); this.CLAIMED_WALL_BLOCK = - new BlockPattern(BlockUtil.get(config.getString("wall.block_claimed"))); + new BlockBucket(BlockUtil.get(config.getString("wall.block_claimed"))); // Dump world settings if (Settings.DEBUG) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java index 1910fc07e..7fbf5c0ae 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridGen.java @@ -96,7 +96,7 @@ public class HybridGen extends IndependentPlotGenerator { for (short z = 0; z < 16; z++) { // Road for (int y = 1; y <= hpw.ROAD_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.ROAD_BLOCK); + result.setBlock(x, y, z, hpw.ROAD_BLOCK.toPattern()); } if (hpw.ROAD_SCHEMATIC_ENABLED) { placeSchem(hpw, result, rx[x], rz[z], x, z, true); @@ -107,7 +107,7 @@ public class HybridGen extends IndependentPlotGenerator { if (gz[z]) { // road for (int y = 1; y <= hpw.ROAD_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.ROAD_BLOCK); + result.setBlock(x, y, z, hpw.ROAD_BLOCK.toPattern()); } if (hpw.ROAD_SCHEMATIC_ENABLED) { placeSchem(hpw, result, rx[x], rz[z], x, z, true); @@ -115,10 +115,10 @@ public class HybridGen extends IndependentPlotGenerator { } else { // wall for (int y = 1; y <= hpw.WALL_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.WALL_FILLING); + result.setBlock(x, y, z, hpw.WALL_FILLING.toPattern()); } if (!hpw.ROAD_SCHEMATIC_ENABLED) { - result.setBlock(x, hpw.WALL_HEIGHT + 1, z, hpw.WALL_BLOCK); + result.setBlock(x, hpw.WALL_HEIGHT + 1, z, hpw.WALL_BLOCK.toPattern()); } else { placeSchem(hpw, result, rx[x], rz[z], x, z, true); } @@ -129,7 +129,7 @@ public class HybridGen extends IndependentPlotGenerator { if (gz[z]) { // road for (int y = 1; y <= hpw.ROAD_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.ROAD_BLOCK); + result.setBlock(x, y, z, hpw.ROAD_BLOCK.toPattern()); } if (hpw.ROAD_SCHEMATIC_ENABLED) { placeSchem(hpw, result, rx[x], rz[z], x, z, true); @@ -137,19 +137,19 @@ public class HybridGen extends IndependentPlotGenerator { } else if (wz[z]) { // wall for (int y = 1; y <= hpw.WALL_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.WALL_FILLING); + result.setBlock(x, y, z, hpw.WALL_FILLING.toPattern()); } if (!hpw.ROAD_SCHEMATIC_ENABLED) { - result.setBlock(x, hpw.WALL_HEIGHT + 1, z, hpw.WALL_BLOCK); + result.setBlock(x, hpw.WALL_HEIGHT + 1, z, hpw.WALL_BLOCK.toPattern()); } else { placeSchem(hpw, result, rx[x], rz[z], x, z, true); } } else { // plot for (int y = 1; y < hpw.PLOT_HEIGHT; y++) { - result.setBlock(x, y, z, hpw.MAIN_BLOCK); + result.setBlock(x, y, z, hpw.MAIN_BLOCK.toPattern()); } - result.setBlock(x, hpw.PLOT_HEIGHT, z, hpw.TOP_BLOCK); + result.setBlock(x, hpw.PLOT_HEIGHT, z, hpw.TOP_BLOCK.toPattern()); if (hpw.PLOT_SCHEMATIC) { placeSchem(hpw, result, rx[x], rz[z], x, z, false); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java index 06550a3cb..e6c165029 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java @@ -174,8 +174,8 @@ public class HybridPlotManager extends ClassicPlotManager { final boolean canRegen = (hybridPlotWorld.TYPE == 0) && (hybridPlotWorld.TERRAIN == 0) && REGENERATIVE_CLEAR; // The component blocks - final Pattern plotfloor = hybridPlotWorld.TOP_BLOCK; - final Pattern filling = hybridPlotWorld.MAIN_BLOCK; + final Pattern plotfloor = hybridPlotWorld.TOP_BLOCK.toPattern(); + final Pattern filling = hybridPlotWorld.MAIN_BLOCK.toPattern(); final BlockState bedrock; if (hybridPlotWorld.PLOT_BEDROCK) { bedrock = BlockUtil.get((short) 7, (byte) 0); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/BlockBucket.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/BlockBucket.java index 8a4e784b3..a74c66317 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/BlockBucket.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/BlockBucket.java @@ -2,54 +2,56 @@ package com.github.intellectualsites.plotsquared.plot.object; import com.github.intellectualsites.plotsquared.configuration.serialization.ConfigurationSerializable; import com.github.intellectualsites.plotsquared.plot.config.Configuration; -import com.github.intellectualsites.plotsquared.plot.object.collection.RandomCollection; +import com.github.intellectualsites.plotsquared.plot.util.MathMan; +import com.github.intellectualsites.plotsquared.plot.util.StringMan; +import com.github.intellectualsites.plotsquared.plot.util.world.BlockUtil; +import com.github.intellectualsites.plotsquared.plot.util.world.PatternUtil; import com.google.common.collect.ImmutableMap; -import com.sk89q.worldedit.extent.NullExtent; -import com.sk89q.worldedit.function.mask.BlockMask; import com.sk89q.worldedit.function.pattern.BlockPattern; import com.sk89q.worldedit.function.pattern.Pattern; -import com.sk89q.worldedit.function.pattern.RandomPattern; -import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BlockState; -import com.sk89q.worldedit.world.block.BlockTypes; +import com.sk89q.worldedit.world.block.BlockType; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import org.jetbrains.annotations.NotNull; import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.Random; -import java.util.stream.Collectors; +import java.util.regex.Matcher; /** * A block bucket is a container of block types, where each block * has a specified chance of being randomly picked */ -@EqualsAndHashCode(of={"blocks"}) @SuppressWarnings({"unused", "WeakerAccess"}) public final class BlockBucket - implements Iterable, ConfigurationSerializable { +@EqualsAndHashCode(of={"input"}) @SuppressWarnings({"unused", "WeakerAccess"}) +public final class BlockBucket implements ConfigurationSerializable { + private boolean compiled; - private final Random random = new Random(); - private final Map blocks; - - private final BucketIterator bucketIterator = new BucketIterator(); - private boolean compiled, singleItem; - private BlockState head; - - private RandomCollection randomBlocks; + private StringBuilder input; private BlockState single; private Pattern pattern; - private BlockMask mask; + + public BlockBucket(BlockType type) { + this(type.getId()); + this.single = type.getDefaultState(); + this.pattern = new BlockPattern(this.single); + this.compiled = true; + } + + public BlockBucket(BlockState state) { + this(state.getAsString()); + this.single = state; + this.pattern = new BlockPattern(this.single); + this.compiled = true; + } + + public BlockBucket(String input) { + this.input = new StringBuilder(input); + } public BlockBucket() { - this.blocks = new HashMap<>(); + this.input = new StringBuilder(); } public static BlockBucket withSingle(@NonNull final BlockState block) { @@ -74,158 +76,75 @@ import java.util.stream.Collectors; } private void addBlock(@NonNull final BlockState block, double chance) { - if (chance == -1) - chance = 1; - this.blocks.put(block, chance); + if (chance == -1) chance = 1; + String prefix = input.length() == 0 ? "" : ","; + input.append(prefix).append(chance + "%" + prefix); this.compiled = false; - if (head == null) { - head = block; - } } public boolean isEmpty() { - return blocks.isEmpty(); + return input == null || input.length() == 0; } - /** - * Get all blocks that are configured in the bucket - * - * @return Immutable collection containing all blocks that can - * be found in the bucket - */ - public Collection getBlocks() { - this.compile(); - return Collections.unmodifiableCollection(this.blocks.keySet()); - } - - /** - * Get a collection containing a specified amount of randomly selected blocks - * - * @param count Number of blocks - * @return Immutable collection containing randomly selected blocks - */ - public Collection getBlocks(final int count) { - return Arrays.asList(getBlockArray(count)); - } - - /** - * Get an array containing a specified amount of randomly selected blocks - * - * @param count Number of blocks - * @return Immutable collection containing randomly selected blocks - */ - public BlockState[] getBlockArray(final int count) { - final BlockState[] blocks = new BlockState[count]; - if (this.singleItem) { - Arrays.fill(blocks, 0, count, getBlock()); - } else { - for (int i = 0; i < count; i++) { - blocks[i] = getBlock(); - } - } - return blocks; - } - - public boolean hasSingleItem() { - return this.singleItem; - } + private static java.util.regex.Pattern regex = java.util.regex.Pattern.compile("((?[A-Za-z_]+):)?(?([A-Za-z_]+(\\[?[\\S\\s]+\\])?))(:(?[0-9]{1,3}))?"); public void compile() { if (isCompiled()) { return; } this.compiled = true; - switch (blocks.size()) { - case 0: - single = null; - this.randomBlocks = null; - this.pattern = null; - this.mask = new BlockMask(new NullExtent()); - break; - case 1: - single = blocks.keySet().iterator().next(); - this.randomBlocks = null; - this.pattern = new BlockPattern(single); - this.mask = new BlockMask(new NullExtent(), single.toBaseBlock()); - break; - default: - single = null; - this.randomBlocks = RandomCollection.of(blocks, random); - RandomPattern randomPattern = new RandomPattern(); - for (Entry entry : this.blocks.entrySet()) { - randomPattern.add(new BlockPattern(entry.getKey()), entry.getValue()); - } - this.pattern = randomPattern; - List baseBlocks = getBlocks().stream().map(BlockState::toBaseBlock) - .collect(Collectors.toList()); - this.mask = new BlockMask(new NullExtent(), baseBlocks); - break; + String string = this.input.toString(); + if (string.isEmpty()) { + this.single = null; + this.pattern = null; + return; } - } - - @NotNull @Override public Iterator iterator() { - return this.bucketIterator; + // Convert legacy format + boolean legacy = false; + String[] blocksStr = string.split(",(?![^\\(\\[]*[\\]\\)])"); + if (blocksStr.length == 1) { + try { + this.single = BlockUtil.get(string); + this.pattern = new BlockPattern(single); + return; + } catch (Exception ignore) {} + } + for (int i = 0; i < blocksStr.length; i++) { + String entry = blocksStr[i]; + Matcher matcher = regex.matcher(entry); + if (matcher.find()) { + String chanceStr = matcher.group("chance"); + if (chanceStr != null && MathMan.isInteger(chanceStr)) { + String[] parts = entry.split(":"); + parts = Arrays.copyOf(parts, parts.length - 1); + entry = chanceStr + "%" + StringMan.join(parts, ":"); + blocksStr[i] = entry; + legacy = true; + } + } + } + if (legacy) { + string = StringMan.join(blocksStr, ","); + } + pattern = PatternUtil.parse(null, string); } public boolean isCompiled() { return this.compiled; } - /** - * Get a random block out of the bucket - * - * @return Randomly picked block (cased on specified rates) - */ - public BlockState getBlock() { - if (!isCompiled()) { - this.compile(); - } - if (single != null) { - return single; - } - if (randomBlocks != null) { - return randomBlocks.next(); - } - return BlockTypes.AIR.getDefaultState(); - } - - public Pattern getPattern() { + public Pattern toPattern() { this.compile(); return this.pattern; } - public BlockMask getMask() { - this.compile(); - return this.mask; - } - @Override public String toString() { - if (!isCompiled()) { - compile(); - } - if (blocks.size() == 1) { - return blocks.entrySet().iterator().next().getKey().toString(); - } - final StringBuilder builder = new StringBuilder(); - Iterator> iterator = blocks.entrySet().iterator(); - while (iterator.hasNext()) { - Entry entry = iterator.next(); - BlockState block = entry.getKey(); - builder.append(block); - Double weight = entry.getValue(); - if (weight != 1) { - builder.append(":").append(weight.intValue()); - } - if (iterator.hasNext()) { - builder.append(","); - } - } - return builder.toString(); + return input.toString(); } public boolean isAir() { compile(); - return blocks.isEmpty() || (single != null && single.getBlockType().getMaterial().isAir()); + return isEmpty() || (single != null && single.getBlockType().getMaterial().isAir()); } @Override public Map serialize() { @@ -249,16 +168,4 @@ import java.util.stream.Collectors; return num <= max && num >= min; } } - - - private final class BucketIterator implements Iterator { - - @Override public boolean hasNext() { - return true; - } - - @Override public BlockState next() { - return getBlock(); - } - } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 65bb23809..f14c21609 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -26,7 +26,6 @@ import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue import com.github.intellectualsites.plotsquared.plot.util.block.LocalBlockQueue; import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager; import com.github.intellectualsites.plotsquared.plot.util.expiry.PlotAnalysis; -import com.github.intellectualsites.plotsquared.plot.util.world.PatternUtil; import com.google.common.collect.BiMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; @@ -1597,7 +1596,7 @@ public class Plot { if (parsed != null && parsed.isEmpty()) { return false; } - return this.setComponent(component, parsed.getPattern()); + return this.setComponent(component, parsed.toPattern()); } /** diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/world/PatternUtil.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/world/PatternUtil.java index 381cfc7f1..4d3107eb0 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/world/PatternUtil.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/world/PatternUtil.java @@ -29,14 +29,18 @@ public class PatternUtil { } public static Pattern parse(PlotPlayer plotPlayer, String input) { - Actor actor = plotPlayer.toActor(); ParserContext context = new ParserContext(); - context.setActor(actor); - if (actor instanceof Player) { - context.setWorld(((Player) actor).getWorld()); + if (plotPlayer != null) { + Actor actor = plotPlayer.toActor(); + context.setActor(actor); + if (actor instanceof Player) { + context.setWorld(((Player) actor).getWorld()); + } + context.setSession(WorldEdit.getInstance().getSessionManager().get(actor)); + context.setRestricted(true); + } else { + context.setRestricted(false); } - context.setSession(WorldEdit.getInstance().getSessionManager().get(actor)); - context.setRestricted(true); context.setPreferringWildcard(false); context.setTryLegacy(true); try {