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

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