From fb634ef95b0e59aa076db16ce4c0f79e4d166c9d Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 1 Mar 2014 14:37:08 -0800 Subject: [PATCH] Delegate flora creation to new FloraGenerator. Sadly, no biome-specific flowers because the biome API in WorldEdit apparently needs some improvement. --- .../worldedit/generator/FloraGenerator.java | 123 ++++++++++++++++++ .../worldedit/generator/FloraPlacer.java | 46 ++----- .../worldedit/operation/RegionFunction.java | 39 ++++++ 3 files changed, 173 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/sk89q/worldedit/generator/FloraGenerator.java create mode 100644 src/main/java/com/sk89q/worldedit/operation/RegionFunction.java diff --git a/src/main/java/com/sk89q/worldedit/generator/FloraGenerator.java b/src/main/java/com/sk89q/worldedit/generator/FloraGenerator.java new file mode 100644 index 000000000..7ddb06678 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/generator/FloraGenerator.java @@ -0,0 +1,123 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.generator; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.blocks.BaseBlock; +import com.sk89q.worldedit.blocks.BlockID; +import com.sk89q.worldedit.operation.RegionFunction; +import com.sk89q.worldedit.patterns.BlockChance; +import com.sk89q.worldedit.patterns.Pattern; +import com.sk89q.worldedit.patterns.RandomFillPattern; + +import java.util.ArrayList; +import java.util.List; + +/** + * Generates flora (which may include tall grass, flowers, etc.). + *

+ * The current implementation is not biome-aware, but it may become so in + * the future. + */ +public class FloraGenerator implements RegionFunction { + + private final EditSession editSession; + private boolean biomeAware = false; + private final Pattern desertPattern = getDesertPattern(); + private final Pattern temperatePattern = getTemperatePattern(); + + /** + * Create a new flora generator. + * + * @param editSession the edit session + */ + public FloraGenerator(EditSession editSession) { + this.editSession = editSession; + } + + /** + * Return whether the flora generator is set to be biome-aware. + *

+ * By default, it is currently disabled by default, but this may change. + * + * @return true if biome aware + */ + public boolean isBiomeAware() { + return biomeAware; + } + + /** + * Set whether the generator is biome aware. + *

+ * It is currently not possible to make the generator biome-aware. + * + * @param biomeAware must always be false + */ + public void setBiomeAware(boolean biomeAware) { + if (biomeAware) { + throw new IllegalArgumentException("Cannot enable biome-aware mode; not yet implemented"); + } + this.biomeAware = biomeAware; + } + + /** + * Get a pattern for plants to place inside a desert environment. + * + * @return a pattern that places flora + */ + public static Pattern getDesertPattern() { + List chance = new ArrayList(); + chance.add(new BlockChance(new BaseBlock(BlockID.DEAD_BUSH), 30)); + chance.add(new BlockChance(new BaseBlock(BlockID.CACTUS), 20)); + chance.add(new BlockChance(new BaseBlock(BlockID.AIR), 300)); + return new RandomFillPattern(chance); + } + + /** + * Get a pattern for plants to place inside a temperate environment. + * + * @return a pattern that places flora + */ + public static Pattern getTemperatePattern() { + List chance = new ArrayList(); + chance.add(new BlockChance(new BaseBlock(BlockID.LONG_GRASS, 1), 300)); + chance.add(new BlockChance(new BaseBlock(BlockID.RED_FLOWER), 5)); + chance.add(new BlockChance(new BaseBlock(BlockID.YELLOW_FLOWER), 5)); + return new RandomFillPattern(chance); + } + + @Override + public boolean apply(Vector position) throws WorldEditException { + BaseBlock block = editSession.getBlock(position); + + if (block.getType() == BlockID.GRASS) { + editSession.setBlock(position.add(0, 1, 0), temperatePattern.next(position)); + return true; + } else if (block.getType() == BlockID.SAND) { + editSession.setBlock(position.add(0, 1, 0), desertPattern.next(position)); + return true; + } + + return false; + } + +} diff --git a/src/main/java/com/sk89q/worldedit/generator/FloraPlacer.java b/src/main/java/com/sk89q/worldedit/generator/FloraPlacer.java index be878e326..0f4662ccc 100644 --- a/src/main/java/com/sk89q/worldedit/generator/FloraPlacer.java +++ b/src/main/java/com/sk89q/worldedit/generator/FloraPlacer.java @@ -23,13 +23,6 @@ import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.blocks.BaseBlock; -import com.sk89q.worldedit.blocks.BlockID; -import com.sk89q.worldedit.patterns.BlockChance; -import com.sk89q.worldedit.patterns.Pattern; -import com.sk89q.worldedit.patterns.RandomFillPattern; - -import java.util.ArrayList; -import java.util.List; /** * Generates flora over an applied area. @@ -37,8 +30,7 @@ import java.util.List; public class FloraPlacer extends GroundGenerator { private final EditSession editSession; - private final Pattern desertPattern = getDesertPattern(); - private final Pattern temperatePattern = getTemperatePattern(); + private FloraGenerator floraGenerator; /** * Create a new instance. @@ -48,46 +40,30 @@ public class FloraPlacer extends GroundGenerator { public FloraPlacer(EditSession editSession) { super(editSession); this.editSession = editSession; + this.floraGenerator = new FloraGenerator(editSession); } /** - * Get a pattern for plants to place inside a desert environment. + * Get the flora generator. * - * @return a pattern that places flora + * @return the flora generator */ - public static Pattern getDesertPattern() { - List chance = new ArrayList(); - chance.add(new BlockChance(new BaseBlock(BlockID.DEAD_BUSH), 30)); - chance.add(new BlockChance(new BaseBlock(BlockID.CACTUS), 20)); - chance.add(new BlockChance(new BaseBlock(BlockID.AIR), 300)); - return new RandomFillPattern(chance); + public FloraGenerator getFloraGenerator() { + return floraGenerator; } /** - * Get a pattern for plants to place inside a temperate environment. + * Set the flora generator. * - * @return a pattern that places flora + * @param floraGenerator the flora generator */ - public static Pattern getTemperatePattern() { - List chance = new ArrayList(); - chance.add(new BlockChance(new BaseBlock(BlockID.LONG_GRASS, 1), 300)); - chance.add(new BlockChance(new BaseBlock(BlockID.RED_FLOWER), 5)); - chance.add(new BlockChance(new BaseBlock(BlockID.RED_FLOWER, 1), 1)); - chance.add(new BlockChance(new BaseBlock(BlockID.YELLOW_FLOWER), 5)); - return new RandomFillPattern(chance); + public void setFloraGenerator(FloraGenerator floraGenerator) { + this.floraGenerator = floraGenerator; } @Override protected boolean apply(Vector pt, BaseBlock block) throws WorldEditException { - if (block.getType() == BlockID.GRASS) { - editSession.setBlock(pt.add(0, 1, 0), temperatePattern.next(pt)); - return true; - } else if (block.getType() == BlockID.SAND) { - editSession.setBlock(pt.add(0, 1, 0), desertPattern.next(pt)); - return true; - } - - return false; + return floraGenerator.apply(pt); } } diff --git a/src/main/java/com/sk89q/worldedit/operation/RegionFunction.java b/src/main/java/com/sk89q/worldedit/operation/RegionFunction.java new file mode 100644 index 000000000..8707d7c4f --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/operation/RegionFunction.java @@ -0,0 +1,39 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.operation; + +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldEditException; + +/** + * Performs a function on points in a region. + */ +public interface RegionFunction { + + /** + * Apply the function to the given position. + * + * @param position the position + * @return true if something was changed + * @throws WorldEditException thrown on an error + */ + public boolean apply(Vector position) throws WorldEditException; + +}