Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!)

Expand Down
93 changes: 53 additions & 40 deletions src/main/java/de/minebench/plotgenerator/PlotChunkGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
61 changes: 32 additions & 29 deletions src/main/java/de/minebench/plotgenerator/PlotGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,38 +152,40 @@ public Economy getEconomy() {
return economy;
}

public PlotSchematic loadSchematic(String schematicName) {
if (schematicName == null || schematicName.isEmpty()) {
return null;
}
public CompletableFuture<PlotSchematic> 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 {
Expand Down
35 changes: 21 additions & 14 deletions src/main/java/de/minebench/plotgenerator/PlotGeneratorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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<PlotSchematic> schematic;
private final BlockVector3 center;
private final int overlap;
private final String regionId;
Expand All @@ -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<PlotSchematic> 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;
Expand Down Expand Up @@ -176,7 +178,7 @@ public static PlotGeneratorConfig fromConfig(PlotGenerator plugin, Configuration
}

public PlotSchematic getSchematic() {
return schematic;
return schematic.get();
}

public BlockVector3 getCenter() {
Expand Down Expand Up @@ -218,7 +220,7 @@ public String getId() {
public static class Builder {

private String id;
private PlotSchematic schematic = null;
private Supplier<PlotSchematic> schematic = Suppliers.ofInstance(null);
private BlockVector3 center = BlockVector3.at(0, 0, 0);
private int overlap = 0;
private String regionId = null;
Expand All @@ -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<PlotSchematic> 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;
}

Expand Down Expand Up @@ -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();
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/de/minebench/plotgenerator/PlotSchematic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down