Added new Pattern interface.

This commit is contained in:
sk89q 2014-03-30 12:59:39 -07:00
parent e7f7d17f25
commit f0d97c5231
14 changed files with 321 additions and 63 deletions

View File

@ -39,6 +39,7 @@ import com.sk89q.worldedit.function.mask.*;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy; import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.OperationHelper; import com.sk89q.worldedit.function.operation.OperationHelper;
import com.sk89q.worldedit.function.operation.OperationQueue; import com.sk89q.worldedit.function.operation.OperationQueue;
import com.sk89q.worldedit.function.pattern.Patterns;
import com.sk89q.worldedit.function.util.RegionOffset; import com.sk89q.worldedit.function.util.RegionOffset;
import com.sk89q.worldedit.function.visitor.DownwardVisitor; import com.sk89q.worldedit.function.visitor.DownwardVisitor;
import com.sk89q.worldedit.function.visitor.LayerVisitor; import com.sk89q.worldedit.function.visitor.LayerVisitor;
@ -831,7 +832,7 @@ public class EditSession implements Extent {
Masks.negate(new ExistingBlockMask(this))); Masks.negate(new ExistingBlockMask(this)));
// Want to replace blocks // Want to replace blocks
BlockReplace replace = new BlockReplace(this, pattern); BlockReplace replace = new BlockReplace(this, Patterns.wrap(pattern));
// Pick how we're going to visit blocks // Pick how we're going to visit blocks
RecursiveVisitor visitor; RecursiveVisitor visitor;
@ -941,7 +942,7 @@ public class EditSession implements Extent {
checkNotNull(region); checkNotNull(region);
checkNotNull(pattern); checkNotNull(pattern);
BlockReplace replace = new BlockReplace(this, pattern); BlockReplace replace = new BlockReplace(this, Patterns.wrap(pattern));
RegionVisitor visitor = new RegionVisitor(region, replace); RegionVisitor visitor = new RegionVisitor(region, replace);
OperationHelper.completeLegacy(visitor); OperationHelper.completeLegacy(visitor);
return visitor.getAffected(); return visitor.getAffected();
@ -991,7 +992,7 @@ public class EditSession implements Extent {
checkNotNull(mask); checkNotNull(mask);
checkNotNull(pattern); checkNotNull(pattern);
BlockReplace replace = new BlockReplace(this, pattern); BlockReplace replace = new BlockReplace(this, Patterns.wrap(pattern));
RegionMaskingFilter filter = new RegionMaskingFilter(Masks.wrap(this, mask), replace); RegionMaskingFilter filter = new RegionMaskingFilter(Masks.wrap(this, mask), replace);
RegionVisitor visitor = new RegionVisitor(region, filter); RegionVisitor visitor = new RegionVisitor(region, filter);
OperationHelper.completeLegacy(visitor); OperationHelper.completeLegacy(visitor);
@ -1164,7 +1165,7 @@ public class EditSession implements Extent {
checkNotNull(region); checkNotNull(region);
checkNotNull(pattern); checkNotNull(pattern);
BlockReplace replace = new BlockReplace(this, pattern); BlockReplace replace = new BlockReplace(this, Patterns.wrap(pattern));
RegionOffset offset = new RegionOffset(new Vector(0, 1, 0), replace); RegionOffset offset = new RegionOffset(new Vector(0, 1, 0), replace);
GroundFunction ground = new GroundFunction(this, offset); GroundFunction ground = new GroundFunction(this, offset);
LayerVisitor visitor = new LayerVisitor( LayerVisitor visitor = new LayerVisitor(
@ -1241,7 +1242,7 @@ public class EditSession implements Extent {
Pattern pattern = replacement != null ? Pattern pattern = replacement != null ?
new SingleBlockPattern(replacement) : new SingleBlockPattern(replacement) :
new SingleBlockPattern(new BaseBlock(BlockID.AIR)); new SingleBlockPattern(new BaseBlock(BlockID.AIR));
BlockReplace remove = new BlockReplace(this, pattern); BlockReplace remove = new BlockReplace(this, Patterns.wrap(pattern));
// Copy to a buffer so we don't destroy our original before we can copy all the blocks from it // Copy to a buffer so we don't destroy our original before we can copy all the blocks from it
ExtentBuffer buffer = new ExtentBuffer(this, new RegionMask(region)); ExtentBuffer buffer = new ExtentBuffer(this, new RegionMask(region));

View File

@ -26,7 +26,7 @@ import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.function.mask.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Masks; import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.regions.AbstractRegion; import com.sk89q.worldedit.regions.AbstractRegion;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionOperationException; import com.sk89q.worldedit.regions.RegionOperationException;
@ -115,7 +115,7 @@ public class ExtentBuffer implements Extent, Pattern {
} }
@Override @Override
public BaseBlock next(Vector pos) { public BaseBlock apply(Vector pos) {
BaseBlock block = buffer.get(pos.toBlockVector()); BaseBlock block = buffer.get(pos.toBlockVector());
if (block != null) { if (block != null) {
return block; return block;
@ -124,11 +124,6 @@ public class ExtentBuffer implements Extent, Pattern {
} }
} }
@Override
public BaseBlock next(int x, int y, int z) {
return next(new Vector(x, y, z));
}
/** /**
* Return a region representation of this buffer. * Return a region representation of this buffer.
* *

View File

@ -23,7 +23,7 @@ import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -50,7 +50,7 @@ public class BlockReplace implements RegionFunction {
@Override @Override
public boolean apply(Vector position) throws WorldEditException { public boolean apply(Vector position) throws WorldEditException {
return extent.setBlock(position, pattern.next(position), true); return extent.setBlock(position, pattern.apply(position), true);
} }
} }

View File

@ -25,12 +25,9 @@ import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.patterns.BlockChance; import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.patterns.RandomFillPattern; import com.sk89q.worldedit.function.pattern.RandomPattern;
import java.util.ArrayList;
import java.util.List;
/** /**
* Generates flora (which may include tall grass, flowers, etc.). * Generates flora (which may include tall grass, flowers, etc.).
@ -85,11 +82,11 @@ public class FloraGenerator implements RegionFunction {
* @return a pattern that places flora * @return a pattern that places flora
*/ */
public static Pattern getDesertPattern() { public static Pattern getDesertPattern() {
List<BlockChance> chance = new ArrayList<BlockChance>(); RandomPattern pattern = new RandomPattern();
chance.add(new BlockChance(new BaseBlock(BlockID.DEAD_BUSH), 30)); pattern.add(new BlockPattern(new BaseBlock(BlockID.DEAD_BUSH)), 30);
chance.add(new BlockChance(new BaseBlock(BlockID.CACTUS), 20)); pattern.add(new BlockPattern(new BaseBlock(BlockID.CACTUS)), 20);
chance.add(new BlockChance(new BaseBlock(BlockID.AIR), 300)); pattern.add(new BlockPattern(new BaseBlock(BlockID.AIR)), 300);
return new RandomFillPattern(chance); return pattern;
} }
/** /**
@ -98,11 +95,11 @@ public class FloraGenerator implements RegionFunction {
* @return a pattern that places flora * @return a pattern that places flora
*/ */
public static Pattern getTemperatePattern() { public static Pattern getTemperatePattern() {
List<BlockChance> chance = new ArrayList<BlockChance>(); RandomPattern pattern = new RandomPattern();
chance.add(new BlockChance(new BaseBlock(BlockID.LONG_GRASS, 1), 300)); pattern.add(new BlockPattern(new BaseBlock(BlockID.LONG_GRASS, 1)), 300);
chance.add(new BlockChance(new BaseBlock(BlockID.RED_FLOWER), 5)); pattern.add(new BlockPattern(new BaseBlock(BlockID.RED_FLOWER)), 5);
chance.add(new BlockChance(new BaseBlock(BlockID.YELLOW_FLOWER), 5)); pattern.add(new BlockPattern(new BaseBlock(BlockID.YELLOW_FLOWER)), 5);
return new RandomFillPattern(chance); return pattern;
} }
@Override @Override
@ -110,10 +107,10 @@ public class FloraGenerator implements RegionFunction {
BaseBlock block = editSession.getBlock(position); BaseBlock block = editSession.getBlock(position);
if (block.getType() == BlockID.GRASS) { if (block.getType() == BlockID.GRASS) {
editSession.setBlock(position.add(0, 1, 0), temperatePattern.next(position)); editSession.setBlock(position.add(0, 1, 0), temperatePattern.apply(position));
return true; return true;
} else if (block.getType() == BlockID.SAND) { } else if (block.getType() == BlockID.SAND) {
editSession.setBlock(position.add(0, 1, 0), desertPattern.next(position)); editSession.setBlock(position.add(0, 1, 0), desertPattern.apply(position));
return true; return true;
} }

View File

@ -26,13 +26,10 @@ import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.patterns.BlockChance; import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.patterns.RandomFillPattern; import com.sk89q.worldedit.function.pattern.RandomPattern;
import com.sk89q.worldedit.patterns.SingleBlockPattern;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; import java.util.Random;
public class GardenPatchGenerator implements RegionFunction { public class GardenPatchGenerator implements RegionFunction {
@ -115,7 +112,7 @@ public class GardenPatchGenerator implements RegionFunction {
editSession.setBlockIfAir(pos.add(1, h, -1), log); editSession.setBlockIfAir(pos.add(1, h, -1), log);
affected++; affected++;
} }
editSession.setBlockIfAir(p = pos.add(0, 0, -1), plant.next(p)); editSession.setBlockIfAir(p = pos.add(0, 0, -1), plant.apply(p));
affected++; affected++;
break; break;
@ -127,7 +124,7 @@ public class GardenPatchGenerator implements RegionFunction {
editSession.setBlockIfAir(pos.add(1, h, 0), log); editSession.setBlockIfAir(pos.add(1, h, 0), log);
affected++; affected++;
} }
editSession.setBlockIfAir(p = pos.add(1, 0, 1), plant.next(p)); editSession.setBlockIfAir(p = pos.add(1, 0, 1), plant.apply(p));
affected++; affected++;
break; break;
@ -139,7 +136,7 @@ public class GardenPatchGenerator implements RegionFunction {
editSession.setBlockIfAir(pos.add(-1, h, 0), log); editSession.setBlockIfAir(pos.add(-1, h, 0), log);
affected++; affected++;
} }
editSession.setBlockIfAir(p = pos.add(-1, 0, 1), plant.next(p)); editSession.setBlockIfAir(p = pos.add(-1, 0, 1), plant.apply(p));
affected++; affected++;
break; break;
@ -151,7 +148,7 @@ public class GardenPatchGenerator implements RegionFunction {
editSession.setBlockIfAir(pos.add(-1, h, -1), log); editSession.setBlockIfAir(pos.add(-1, h, -1), log);
affected++; affected++;
} }
editSession.setBlockIfAir(p = pos.add(-1, 0, -1), plant.next(p)); editSession.setBlockIfAir(p = pos.add(-1, 0, -1), plant.apply(p));
affected++; affected++;
break; break;
} }
@ -185,11 +182,11 @@ public class GardenPatchGenerator implements RegionFunction {
* @return a pumpkin pattern * @return a pumpkin pattern
*/ */
public static Pattern getPumpkinPattern() { public static Pattern getPumpkinPattern() {
List<BlockChance> chance = new ArrayList<BlockChance>(); RandomPattern pattern = new RandomPattern();
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
chance.add(new BlockChance(new BaseBlock(BlockID.PUMPKIN, i), 100)); pattern.add(new BlockPattern(new BaseBlock(BlockID.PUMPKIN, i)), 100);
} }
return new RandomFillPattern(chance); return pattern;
} }
/** /**
@ -198,6 +195,6 @@ public class GardenPatchGenerator implements RegionFunction {
* @return a melon pattern * @return a melon pattern
*/ */
public static Pattern getMelonPattern() { public static Pattern getMelonPattern() {
return new SingleBlockPattern(new BaseBlock(BlockID.MELON_BLOCK)); return new BlockPattern(new BaseBlock(BlockID.MELON_BLOCK));
} }
} }

View File

@ -0,0 +1,26 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.pattern;
/**
* An abstract implementation for {@link Pattern}s.
*/
public abstract class AbstractPattern implements Pattern {
}

View File

@ -0,0 +1,67 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.pattern;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A pattern that returns the same {@link BaseBlock} each time.
*/
public class BlockPattern extends AbstractPattern {
private BaseBlock block;
/**
* Create a new pattern with the given block.
*
* @param block the block
*/
public BlockPattern(BaseBlock block) {
setBlock(block);
}
/**
* Get the block.
*
* @return the block that is always returned
*/
public BaseBlock getBlock() {
return block;
}
/**
* Set the block that is returned.
*
* @param block the block
*/
public void setBlock(BaseBlock block) {
checkNotNull(block);
this.block = block;
}
@Override
public BaseBlock apply(Vector position) {
return block;
}
}

View File

@ -0,0 +1,38 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.pattern;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
/**
* Returns a {@link BaseBlock} for a given position.
*/
public interface Pattern {
/**
* Return a {@link BaseBlock} for the given position.
*
* @param position the position
* @return a block
*/
BaseBlock apply(Vector position);
}

View File

@ -0,0 +1,51 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.pattern;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Utility methods related to {@link Pattern}s.
*/
public final class Patterns {
private Patterns() {
}
/**
* Wrap an old-style pattern and return a new pattern.
*
* @param pattern the pattern
* @return a new-style pattern
*/
public static Pattern wrap(final com.sk89q.worldedit.patterns.Pattern pattern) {
checkNotNull(pattern);
return new Pattern() {
@Override
public BaseBlock apply(Vector position) {
return pattern.next(position);
}
};
}
}

View File

@ -0,0 +1,88 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.pattern;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Uses a random pattern of a weighted list of patterns.
*/
public class RandomPattern extends AbstractPattern {
private final Random random = new Random();
private List<Chance> patterns = new ArrayList<Chance>();
private double max = 0;
/**
* Add a pattern to the weight list of patterns.
* </p>
* The probability for the pattern added is chance / max where max is the sum
* of the probabilities of all added patterns.
*
* @param pattern the pattern
* @param chance the chance, which can be any positive number
*/
public void add(Pattern pattern, double chance) {
checkNotNull(pattern);
patterns.add(new Chance(pattern, chance));
max += chance;
}
@Override
public BaseBlock apply(Vector position) {
double r = random.nextDouble();
double offset = 0;
for (Chance chance : patterns) {
if (r <= (offset + chance.getChance()) / max) {
return chance.getPattern().apply(position);
}
offset += chance.getChance();
}
throw new RuntimeException("ProportionalFillPattern");
}
private static class Chance {
private Pattern pattern;
private double chance;
private Chance(Pattern pattern, double chance) {
this.pattern = pattern;
this.chance = chance;
}
public Pattern getPattern() {
return pattern;
}
public double getChance() {
return chance;
}
}
}

View File

@ -22,10 +22,9 @@ package com.sk89q.worldedit.patterns;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
/** /**
* Gives a block a chance. * @deprecated Will be removed in the future -- there is no replacement
*
* @author sk89q
*/ */
@Deprecated
public class BlockChance { public class BlockChance {
/** /**
* Block. * Block.

View File

@ -23,11 +23,9 @@ import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
/** /**
* Used to get the block to set. This can be used to implement a pattern * @deprecated See {@link com.sk89q.worldedit.function.pattern.Pattern}
* for when setting a region with blocks.
*
* @author sk89q
*/ */
@Deprecated
public interface Pattern { public interface Pattern {
/** /**
* Get a block for a position. This return value of this method does * Get a block for a position. This return value of this method does

View File

@ -19,17 +19,18 @@
package com.sk89q.worldedit.patterns; package com.sk89q.worldedit.patterns;
import java.util.Random; import com.sk89q.worldedit.Vector;
import java.util.List;
import java.util.ArrayList;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.function.pattern.RandomPattern;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/** /**
* Pattern proportionally fills. * @deprecated See {@link RandomPattern}
*
* @author sk89q
*/ */
@Deprecated
public class RandomFillPattern implements Pattern { public class RandomFillPattern implements Pattern {
/** /**
* Random number generator. * Random number generator.

View File

@ -21,12 +21,12 @@ package com.sk89q.worldedit.patterns;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.function.pattern.BlockPattern;
/** /**
* Always returns the same block type. * @deprecated See {@link BlockPattern}
*
* @author sk89q
*/ */
@Deprecated
public class SingleBlockPattern implements Pattern { public class SingleBlockPattern implements Pattern {
/** /**
* Block type. * Block type.