diff --git a/README.md b/README.md index 47520f7..94cc5a9 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ You can define all settings of the generator directly via the id string of the g To create the same worlds as displayed below in the config section you would use the following commands (in Multiverse): -`/mv create test NORMAL -g PlotGenerator:testschematic,overlap=1,centerX=100,centerZ=0,regionId=%world%_plot_%number%,regionInset=5,regionMinY=40,regionMaxY=100,regionPrice=50` +`/mv create test NORMAL -g PlotGenerator:testschematic,overlap=1,x=100,y=64,z=0,regionId=%world%_plot_%number%,regionInset=5,regionMinY=40,regionMaxY=100,regionPrice=50` `/mv create mb_plotworld NORMAL -g PlotGenerator:config=test,schem=plot` (You need to have the test section of the config defined, it wont use the generator of the test world!) diff --git a/src/main/java/de/minebench/plotgenerator/PlotChunkGenerator.java b/src/main/java/de/minebench/plotgenerator/PlotChunkGenerator.java index eeab3a1..5f50789 100644 --- a/src/main/java/de/minebench/plotgenerator/PlotChunkGenerator.java +++ b/src/main/java/de/minebench/plotgenerator/PlotChunkGenerator.java @@ -46,54 +46,67 @@ public PlotChunkGenerator(PlotGenerator plugin, String id) { @Override public ChunkData generateChunkData(World world, Random random, int x, int z, BiomeGrid biome) { ChunkData data = createChunkData(world); + PlotGeneratorConfig config = getConfig(world); - if (config != null && config.getSchematic() != null && !BlockVector3.ZERO.equals(config.getSchematic().getSize())) { - PlotSchematic schematic = config.getSchematic(); - BlockVector3 center = config.getCenter(); - int width = schematic.getWidth() - config.getOverlap(); - int startX = (x * 16 - center.getBlockX()) % width; - while (startX < 0) { - startX = width + startX; - } - int length = schematic.getLength() - config.getOverlap(); - int startZ = (z * 16 - center.getBlockZ()) % length; - while (startZ < 0) { - startZ = length + startZ; - } + if (config == null) { + return data; + } + + PlotSchematic schematic = config.getSchematic(); + if (schematic == null || BlockVector3.ZERO.equals(schematic.getSize())) { + return data; + } + + BlockVector3 center = config.getCenter(); - BlockVector3 sign = null; - for (int chunkX = 0; chunkX < 16; chunkX++) { - int schemX = (startX + chunkX) % width; - for (int chunkZ = 0; chunkZ < 16; chunkZ++) { - int schemZ = (startZ + chunkZ) % length; - for (int chunkY = 0; chunkY < schematic.getHeight(); chunkY++) { - BlockData block = schematic.getBlock(schemX, chunkY, schemZ); - data.setBlock(chunkX, chunkY, chunkZ, block); - if (sign == null && (block instanceof Sign || block instanceof WallSign)) { - sign = BlockVector3.at(x * 16 + chunkX, chunkY, z * 16 + chunkZ); - } + int width = schematic.getWidth() - config.getOverlap(); + int startX = (x * 16 - center.getBlockX()) % width; + while (startX < 0) { + startX = width + startX; + } + + int length = schematic.getLength() - config.getOverlap(); + int startZ = (z * 16 - center.getBlockZ()) % length; + while (startZ < 0) { + startZ = length + startZ; + } + + int startY = center.getBlockY(); + int height = startY + schematic.getHeight(); + + BlockVector3 sign = null; + for (int chunkX = 0; chunkX < 16; chunkX++) { + int schemX = (startX + chunkX) % width; + for (int chunkZ = 0; chunkZ < 16; chunkZ++) { + int schemZ = (startZ + chunkZ) % length; + for (int chunkY = startY; chunkY < height; chunkY++) { + BlockData block = schematic.getBlock(schemX, chunkY - startY, schemZ); + data.setBlock(chunkX, chunkY, chunkZ, block); + if (sign == null && (block instanceof Sign || block instanceof WallSign)) { + sign = BlockVector3.at(x * 16 + chunkX, chunkY, z * 16 + chunkZ); } } } + } - if (plugin.getWorldGuard() != null && config.getRegionId() != null) { - BlockVector3 minPoint = BlockVector3.at( - x * 16 - startX + config.getRegionInset(), - config.getRegionMinY(), - z * 16 - startZ + config.getRegionInset() - ); - BlockVector3 maxPoint = BlockVector3.at( - minPoint.getBlockX() + width - 2 * config.getRegionInset(), - config.getRegionMaxY(), - minPoint.getBlockZ() + length - 2 * config.getRegionInset() - ); - RegionIntent intent = new RegionIntent(world, config, minPoint, maxPoint); - if (sign != null ) { - intent.setSign(sign); - } - plugin.registerRegionIntent(intent); + if (plugin.getWorldGuard() != null && config.getRegionId() != null) { + BlockVector3 minPoint = BlockVector3.at( + x * 16 - startX + config.getRegionInset(), + config.getRegionMinY(), + z * 16 - startZ + config.getRegionInset() + ); + BlockVector3 maxPoint = BlockVector3.at( + minPoint.getBlockX() + width - 2 * config.getRegionInset(), + config.getRegionMaxY(), + minPoint.getBlockZ() + length - 2 * config.getRegionInset() + ); + RegionIntent intent = new RegionIntent(world, config, minPoint, maxPoint); + if (sign != null ) { + intent.setSign(sign); } + plugin.registerRegionIntent(intent); } + return data; } diff --git a/src/main/java/de/minebench/plotgenerator/PlotGenerator.java b/src/main/java/de/minebench/plotgenerator/PlotGenerator.java index 5ab8228..d2cc073 100644 --- a/src/main/java/de/minebench/plotgenerator/PlotGenerator.java +++ b/src/main/java/de/minebench/plotgenerator/PlotGenerator.java @@ -59,6 +59,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.regex.Pattern; @@ -151,38 +152,40 @@ public Economy getEconomy() { return economy; } - public PlotSchematic loadSchematic(String schematicName) { - if (schematicName == null || schematicName.isEmpty()) { - return null; - } + public CompletableFuture loadSchematic(String schematicName) { + return CompletableFuture.supplyAsync(() -> { + if (schematicName == null || schematicName.isEmpty()) { + return null; + } - File file = new File(getDataFolder(), schematicName + ".schem"); - if (!file.exists()){ - file = new File(weSchemDir, schematicName + ".schem"); - } - if (!file.exists()){ - file = new File(getDataFolder(), schematicName + ".schematic"); - } - if (!file.exists()){ - file = new File(weSchemDir, schematicName + ".schematic"); - } - if (!file.exists()) { - getLogger().log(Level.SEVERE, "No schematic found with the name " + schematicName + "!"); - return null; - } + File file = new File(getDataFolder(), schematicName + ".schem"); + if (!file.exists()){ + file = new File(weSchemDir, schematicName + ".schem"); + } + if (!file.exists()){ + file = new File(getDataFolder(), schematicName + ".schematic"); + } + if (!file.exists()){ + file = new File(weSchemDir, schematicName + ".schematic"); + } + if (!file.exists()) { + getLogger().log(Level.SEVERE, "No schematic found with the name " + schematicName + "!"); + return null; + } - ClipboardFormat format = ClipboardFormats.findByFile(file); - if (format == null) { - getLogger().log(Level.SEVERE, "Could not load schematic format from file " + file.getAbsolutePath() + "!"); - return null; - } + ClipboardFormat format = ClipboardFormats.findByFile(file); + if (format == null) { + getLogger().log(Level.SEVERE, "Could not load schematic format from file " + file.getAbsolutePath() + "!"); + return null; + } - try { - return new PlotSchematic(loadSchematicFromFile(file, format)); - } catch (Exception e) { - getLogger().log(Level.SEVERE, "Error loading file " + file.getAbsolutePath(), e); - return null; - } + try { + return new PlotSchematic(loadSchematicFromFile(file, format)); + } catch (Exception e) { + getLogger().log(Level.SEVERE, "Error loading file " + file.getAbsolutePath(), e); + return null; + } + }); } private Clipboard loadSchematicFromFile(File file, ClipboardFormat format) throws IOException { diff --git a/src/main/java/de/minebench/plotgenerator/PlotGeneratorConfig.java b/src/main/java/de/minebench/plotgenerator/PlotGeneratorConfig.java index 98a69a1..a65c838 100644 --- a/src/main/java/de/minebench/plotgenerator/PlotGeneratorConfig.java +++ b/src/main/java/de/minebench/plotgenerator/PlotGeneratorConfig.java @@ -18,15 +18,17 @@ * along with this program. If not, see . */ +import com.google.common.base.Suppliers; import com.sk89q.worldedit.math.BlockVector3; import org.bukkit.configuration.ConfigurationSection; +import java.util.function.Supplier; import java.util.logging.Level; public class PlotGeneratorConfig { private final String id; - private final PlotSchematic schematic; + private final Supplier schematic; private final BlockVector3 center; private final int overlap; private final String regionId; @@ -36,9 +38,9 @@ public class PlotGeneratorConfig { private final double regionPrice; private final String plotType; - public PlotGeneratorConfig(String id, PlotSchematic schematic, BlockVector3 center, int overlap, String regionId, int regionInset, int regionMinY, int regionMaxY, double regionPrice, String plotType) { + public PlotGeneratorConfig(String id, Supplier schematic, BlockVector3 center, int overlap, String regionId, int regionInset, int regionMinY, int regionMaxY, double regionPrice, String plotType) { this.id = id; - this.schematic = schematic; + this.schematic = Suppliers.memoize(schematic::get); this.center = center; this.overlap = overlap; this.regionId = regionId; @@ -176,7 +178,7 @@ public static PlotGeneratorConfig fromConfig(PlotGenerator plugin, Configuration } public PlotSchematic getSchematic() { - return schematic; + return schematic.get(); } public BlockVector3 getCenter() { @@ -218,7 +220,7 @@ public String getId() { public static class Builder { private String id; - private PlotSchematic schematic = null; + private Supplier schematic = Suppliers.ofInstance(null); private BlockVector3 center = BlockVector3.at(0, 0, 0); private int overlap = 0; private String regionId = null; @@ -237,16 +239,21 @@ public Builder(PlotGenerator plugin, String id) { } public Builder schematic(String name) { - return this.schematic(name, plugin.loadSchematic(name)); + return this.schematic(name, plugin.loadSchematic(name)::join); } - public Builder schematic(String name, PlotSchematic schematic) { - if (schematic == null) { - plugin.getLogger().log(Level.WARNING, "Schematic " + name + "not found?"); - return this; - } - this.schematic = schematic; - plugin.getLogger().log(Level.INFO, "Schematic: " + name + " (size: " + (schematic == null ? "null" : schematic.getSize()) + ")"); + public Builder schematic(String name, Supplier schematicSupplier) { + this.schematic = () -> { + PlotSchematic schematic = schematicSupplier.get(); + if (schematic == null) { + plugin.getLogger().log(Level.WARNING, "Schematic " + name + "not found?"); + return null; + } + + plugin.getLogger().log(Level.INFO, "Schematic: " + name + " (size: " + schematic.getSize() + ")"); + return schematic; + }; + return this; } @@ -333,7 +340,7 @@ public PlotGeneratorConfig build() { } public Builder copy(PlotGeneratorConfig config) { - schematic = config.getSchematic(); + schematic = config::getSchematic; center = config.getCenter(); overlap = config.getOverlap(); regionId = config.getRegionId(); diff --git a/src/main/java/de/minebench/plotgenerator/PlotSchematic.java b/src/main/java/de/minebench/plotgenerator/PlotSchematic.java index ab2e52d..9e9d19f 100644 --- a/src/main/java/de/minebench/plotgenerator/PlotSchematic.java +++ b/src/main/java/de/minebench/plotgenerator/PlotSchematic.java @@ -35,10 +35,17 @@ public PlotSchematic(Clipboard clipboard) { blocks = new BlockData[getWidth()][getHeight()][getLength()]; - for (int x = 0; x < getWidth(); x++) { - for (int z = 0; z < getLength(); z++) { - for (int y = 0; y < getHeight(); y++) { - setBlock(x, y, z, BukkitAdapter.adapt(clipboard.getBlock(BlockVector3.at(x, y, z)))); + BlockVector3 minimumPoint = clipboard.getMinimumPoint(); + BlockVector3 maximumPoint = clipboard.getMaximumPoint(); + + for (int x = minimumPoint.getBlockX(); x < maximumPoint.getBlockX() + 1; x++) { + for (int z = minimumPoint.getBlockZ(); z < maximumPoint.getBlockZ() + 1; z++) { + for (int y = minimumPoint.getBlockY(); y < maximumPoint.getBlockY() + 1; y++) { + BlockVector3 pos = BlockVector3.at(x, y, z); + BlockData adapt = BukkitAdapter.adapt(clipboard.getBlock(pos)); + + BlockVector3 relPos = pos.subtract(minimumPoint); + setBlock(relPos.getBlockX(), relPos.getBlockY(), relPos.getBlockZ(), adapt); } } }