Config reading/writing

This commit is contained in:
Jesse Boyd 2019-11-10 18:32:32 +00:00
parent d254633b77
commit d34f5cc97e
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
9 changed files with 148 additions and 284 deletions

View File

@ -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]);

View File

@ -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<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) {
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<BlockState>.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<BlockState>.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;
}
};

View File

@ -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<PlotId> 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<PlotId> 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;
}

View File

@ -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) {

View File

@ -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);
}

View File

@ -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);

View File

@ -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<BlockState>, 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<BlockState, Double> blocks;
private final BucketIterator bucketIterator = new BucketIterator();
private boolean compiled, singleItem;
private BlockState head;
private RandomCollection<BlockState> 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<BlockState> 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<BlockState> 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("((?<namespace>[A-Za-z_]+):)?(?<block>([A-Za-z_]+(\\[?[\\S\\s]+\\])?))(:(?<chance>[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<BlockState, Double> entry : this.blocks.entrySet()) {
randomPattern.add(new BlockPattern(entry.getKey()), entry.getValue());
}
this.pattern = randomPattern;
List<BaseBlock> 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<BlockState> 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<Entry<BlockState, Double>> iterator = blocks.entrySet().iterator();
while (iterator.hasNext()) {
Entry<BlockState, Double> 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<String, Object> serialize() {
@ -249,16 +168,4 @@ import java.util.stream.Collectors;
return num <= max && num >= min;
}
}
private final class BucketIterator implements Iterator<BlockState> {
@Override public boolean hasNext() {
return true;
}
@Override public BlockState next() {
return getBlock();
}
}
}

View File

@ -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());
}
/**

View File

@ -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 {