mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-16 22:03:53 +00:00
More file moving.
This commit is contained in:
92
src/main/java/com/sk89q/worldedit/util/FileDialogUtil.java
Normal file
92
src/main/java/com/sk89q/worldedit/util/FileDialogUtil.java
Normal file
@ -0,0 +1,92 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* 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.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import com.sk89q.util.StringUtil;
|
||||
|
||||
public class FileDialogUtil {
|
||||
public static File showSaveDialog(String[] exts) {
|
||||
JFileChooser dialog = new JFileChooser();
|
||||
|
||||
if (exts != null) {
|
||||
dialog.setFileFilter(new ExtensionFilter(exts));
|
||||
}
|
||||
|
||||
int returnVal = dialog.showSaveDialog(null);
|
||||
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
return dialog.getSelectedFile();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static File showOpenDialog(String[] exts) {
|
||||
JFileChooser dialog = new JFileChooser();
|
||||
|
||||
if (exts != null) {
|
||||
dialog.setFileFilter(new ExtensionFilter(exts));
|
||||
}
|
||||
|
||||
int returnVal = dialog.showOpenDialog(null);
|
||||
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
return dialog.getSelectedFile();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class ExtensionFilter extends FileFilter {
|
||||
private Set<String> exts;
|
||||
private String desc;
|
||||
|
||||
public ExtensionFilter(String[] exts) {
|
||||
this.exts = new HashSet<String>(Arrays.asList(exts));
|
||||
|
||||
desc = StringUtil.joinString(exts, ",");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(File f) {
|
||||
if (f.isDirectory()) {
|
||||
return true;
|
||||
}
|
||||
String path = f.getPath();
|
||||
int index = path.lastIndexOf('.');
|
||||
if (index == -1 || index == path.length() - 1) {
|
||||
return false;
|
||||
} else {
|
||||
return exts.contains(path.indexOf(index + 1));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,233 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* 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.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import com.sk89q.util.StringUtil;
|
||||
import com.sk89q.worldedit.LocalConfiguration;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.snapshots.SnapshotRepository;
|
||||
|
||||
/**
|
||||
* Simple LocalConfiguration that loads settings using
|
||||
* <code>java.util.Properties</code>.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class PropertiesConfiguration extends LocalConfiguration {
|
||||
protected Properties properties;
|
||||
protected File path;
|
||||
|
||||
/**
|
||||
* Construct the object. The configuration isn't loaded yet.
|
||||
*
|
||||
* @param path
|
||||
*/
|
||||
public PropertiesConfiguration(File path) {
|
||||
this.path = path;
|
||||
|
||||
properties = new Properties();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the configuration file.
|
||||
*/
|
||||
@Override
|
||||
public void load() {
|
||||
InputStream stream = null;
|
||||
try {
|
||||
stream = new FileInputStream(path);
|
||||
properties.load(stream);
|
||||
} catch (FileNotFoundException e) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
profile = getBool("profile", profile);
|
||||
disallowedBlocks = getIntSet("disallowed-blocks", defaultDisallowedBlocks);
|
||||
defaultChangeLimit = getInt("default-max-changed-blocks", defaultChangeLimit);
|
||||
maxChangeLimit = getInt("max-changed-blocks", maxChangeLimit);
|
||||
shellSaveType = getString("shell-save-type", shellSaveType);
|
||||
maxRadius = getInt("max-radius", maxRadius);
|
||||
maxSuperPickaxeSize = getInt("max-super-pickaxe-size", maxSuperPickaxeSize);
|
||||
maxBrushRadius = getInt("max-brush-radius", maxBrushRadius);
|
||||
logCommands = getBool("log-commands", logCommands);
|
||||
registerHelp = getBool("register-help", registerHelp);
|
||||
wandItem = getInt("wand-item", wandItem);
|
||||
superPickaxeDrop = getBool("super-pickaxe-drop-items", superPickaxeDrop);
|
||||
superPickaxeManyDrop = getBool("super-pickaxe-many-drop-items", superPickaxeManyDrop);
|
||||
noDoubleSlash = getBool("no-double-slash", noDoubleSlash);
|
||||
useInventory = getBool("use-inventory", useInventory);
|
||||
useInventoryOverride = getBool("use-inventory-override", useInventoryOverride);
|
||||
navigationWand = getInt("nav-wand-item", navigationWand);
|
||||
navigationWandMaxDistance = getInt("nav-wand-distance", navigationWandMaxDistance);
|
||||
scriptTimeout = getInt("scripting-timeout", scriptTimeout);
|
||||
|
||||
LocalSession.MAX_HISTORY_SIZE = Math.max(15, getInt("history-size", 15));
|
||||
|
||||
String snapshotsDir = getString("snapshots-dir", "");
|
||||
if (!snapshotsDir.trim().equals("")) {
|
||||
snapshotRepo = new SnapshotRepository(snapshotsDir);
|
||||
} else {
|
||||
snapshotRepo = null;
|
||||
}
|
||||
|
||||
OutputStream output = null;
|
||||
path.getParentFile().mkdirs();
|
||||
try {
|
||||
output = new FileOutputStream(path);
|
||||
properties.store(output, "Don't put comments; they get removed");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (output != null) {
|
||||
try {
|
||||
output.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string value.
|
||||
*
|
||||
* @param key
|
||||
* @param def
|
||||
* @return
|
||||
*/
|
||||
protected String getString(String key, String def) {
|
||||
if (def == null) {
|
||||
def = "";
|
||||
}
|
||||
String val = properties.getProperty(key);
|
||||
if (val == null) {
|
||||
properties.setProperty(key, def);
|
||||
return def;
|
||||
} else {
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a boolean value.
|
||||
*
|
||||
* @param key
|
||||
* @param def
|
||||
* @return
|
||||
*/
|
||||
protected boolean getBool(String key, boolean def) {
|
||||
String val = properties.getProperty(key);
|
||||
if (val == null) {
|
||||
properties.setProperty(key, def ? "true" : "false");
|
||||
return def;
|
||||
} else {
|
||||
return val.equalsIgnoreCase("true")
|
||||
|| val.equals("1");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an integer value.
|
||||
*
|
||||
* @param key
|
||||
* @param def
|
||||
* @return
|
||||
*/
|
||||
protected int getInt(String key, int def) {
|
||||
String val = properties.getProperty(key);
|
||||
if (val == null) {
|
||||
properties.setProperty(key, String.valueOf(def));
|
||||
return def;
|
||||
} else {
|
||||
try {
|
||||
return Integer.parseInt(val);
|
||||
} catch (NumberFormatException e) {
|
||||
properties.setProperty(key, String.valueOf(def));
|
||||
return def;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a double value.
|
||||
*
|
||||
* @param key
|
||||
* @param def
|
||||
* @return
|
||||
*/
|
||||
protected double getDouble(String key, double def) {
|
||||
String val = properties.getProperty(key);
|
||||
if (val == null) {
|
||||
properties.setProperty(key, String.valueOf(def));
|
||||
return def;
|
||||
} else {
|
||||
try {
|
||||
return Double.parseDouble(val);
|
||||
} catch (NumberFormatException e) {
|
||||
properties.setProperty(key, String.valueOf(def));
|
||||
return def;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a double value.
|
||||
*
|
||||
* @param key
|
||||
* @param def
|
||||
* @return
|
||||
*/
|
||||
protected Set<Integer> getIntSet(String key, int[] def) {
|
||||
String val = properties.getProperty(key);
|
||||
if (val == null) {
|
||||
properties.setProperty(key, StringUtil.joinString(def, ",", 0));
|
||||
Set<Integer> set = new HashSet<Integer>();
|
||||
for (int i : def) {
|
||||
set.add(i);
|
||||
}
|
||||
return set;
|
||||
} else {
|
||||
Set<Integer> set = new HashSet<Integer>();
|
||||
String[] parts = val.split(",");
|
||||
for (String part : parts) {
|
||||
try {
|
||||
int v = Integer.parseInt(part.trim());
|
||||
set.add(v);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
}
|
||||
return set;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
174
src/main/java/com/sk89q/worldedit/util/TargetBlock.java
Normal file
174
src/main/java/com/sk89q/worldedit/util/TargetBlock.java
Normal file
@ -0,0 +1,174 @@
|
||||
// $Id$
|
||||
/*
|
||||
* Copyright (c) 2011 toi
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.util;
|
||||
|
||||
import com.sk89q.worldedit.BlockWorldVector;
|
||||
import com.sk89q.worldedit.LocalPlayer;
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.blocks.BlockType;
|
||||
|
||||
/**
|
||||
* This class uses an inefficient method to figure out what block a player
|
||||
* is looking towards.
|
||||
*
|
||||
* Originally written by toi. It was ported to WorldEdit and trimmed down by
|
||||
* sk89q. Thanks to Raphfrk for optimization of toi's original class.
|
||||
*
|
||||
* @author toi
|
||||
*/
|
||||
public class TargetBlock {
|
||||
private LocalWorld world;
|
||||
private int maxDistance;
|
||||
private double checkDistance, curDistance;
|
||||
private Vector targetPos = new Vector();
|
||||
private Vector targetPosDouble = new Vector();
|
||||
private Vector prevPos = new Vector();
|
||||
private Vector offset = new Vector();
|
||||
|
||||
/**
|
||||
* Constructor requiring a player, uses default values
|
||||
*
|
||||
* @param player player to work with
|
||||
*/
|
||||
public TargetBlock(LocalPlayer player) {
|
||||
this.world = player.getWorld();
|
||||
this.setValues(player.getPosition(), player.getYaw(), player.getPitch(),
|
||||
300, 1.65, 0.2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor requiring a player, max distance and a checking distance
|
||||
*
|
||||
* @param player LocalPlayer to work with
|
||||
* @param maxDistance how far it checks for blocks
|
||||
* @param checkDistance how often to check for blocks, the smaller the more precise
|
||||
*/
|
||||
public TargetBlock(LocalPlayer player, int maxDistance, double checkDistance) {
|
||||
this.world = player.getWorld();
|
||||
this.setValues(player.getPosition(), player.getYaw(), player.getPitch(),
|
||||
maxDistance, 1.65, checkDistance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the values, all constructors uses this function
|
||||
*
|
||||
* @param loc location of the view
|
||||
* @param xRotation
|
||||
* @param yRotation
|
||||
* @param maxDistance how far it checks for blocks
|
||||
* @param viewPos where the view is positioned in y-axis
|
||||
* @param checkDistance how often to check for blocks, the smaller the more precise
|
||||
*/
|
||||
private void setValues(Vector loc, double xRotation, double yRotation,
|
||||
int maxDistance, double viewHeight, double checkDistance) {
|
||||
this.maxDistance = maxDistance;
|
||||
this.checkDistance = checkDistance;
|
||||
this.curDistance = 0;
|
||||
xRotation = (xRotation + 90) % 360;
|
||||
yRotation = yRotation * -1;
|
||||
|
||||
double h = (checkDistance * Math.cos(Math.toRadians(yRotation)));
|
||||
|
||||
offset = new Vector((h * Math.cos(Math.toRadians(xRotation))),
|
||||
(checkDistance * Math.sin(Math.toRadians(yRotation))),
|
||||
(h * Math.sin(Math.toRadians(xRotation))));
|
||||
|
||||
targetPosDouble = loc.add(0, viewHeight, 0);
|
||||
targetPos = targetPosDouble.toBlockPoint();
|
||||
prevPos = targetPos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the block at the sight. Returns null if out of range or if no
|
||||
* viable target was found
|
||||
*
|
||||
* @return Block
|
||||
*/
|
||||
public BlockWorldVector getTargetBlock() {
|
||||
while ((getNextBlock() != null)
|
||||
&& (world.getBlockType(getCurrentBlock()) == 0));
|
||||
return getCurrentBlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the block at the sight. Returns null if out of range or if no
|
||||
* viable target was found
|
||||
*
|
||||
* @return Block
|
||||
*/
|
||||
public BlockWorldVector getSolidTargetBlock() {
|
||||
while ((getNextBlock() != null)
|
||||
&& BlockType.canPassThrough(world.getBlockType(getCurrentBlock())));
|
||||
return getCurrentBlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next block
|
||||
*
|
||||
* @return next block position
|
||||
*/
|
||||
public BlockWorldVector getNextBlock() {
|
||||
prevPos = targetPos;
|
||||
do {
|
||||
curDistance += checkDistance;
|
||||
|
||||
targetPosDouble = offset.add(targetPosDouble.getX(),
|
||||
targetPosDouble.getY(),
|
||||
targetPosDouble.getZ());
|
||||
targetPos = targetPosDouble.toBlockPoint();
|
||||
} while (curDistance <= maxDistance
|
||||
&& targetPos.getBlockX() == prevPos.getBlockX()
|
||||
&& targetPos.getBlockY() == prevPos.getBlockY()
|
||||
&& targetPos.getBlockZ() == prevPos.getBlockZ());
|
||||
|
||||
if (curDistance > maxDistance) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new BlockWorldVector(world, targetPos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current block along the line of vision
|
||||
*
|
||||
* @return block position
|
||||
*/
|
||||
public BlockWorldVector getCurrentBlock() {
|
||||
if (curDistance > maxDistance) {
|
||||
return null;
|
||||
} else {
|
||||
return new BlockWorldVector(world, targetPos);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the previous block in the aimed path
|
||||
*
|
||||
* @return block position
|
||||
*/
|
||||
public BlockWorldVector getPreviousBlock() {
|
||||
return new BlockWorldVector(world, prevPos);
|
||||
}
|
||||
}
|
239
src/main/java/com/sk89q/worldedit/util/TreeGenerator.java
Normal file
239
src/main/java/com/sk89q/worldedit/util/TreeGenerator.java
Normal file
@ -0,0 +1,239 @@
|
||||
// $Id$
|
||||
/*
|
||||
* WorldEdit
|
||||
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
|
||||
*
|
||||
* 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.util;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.LocalWorld;
|
||||
import com.sk89q.worldedit.MaxChangedBlocksException;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||
|
||||
/**
|
||||
* Tree generator.
|
||||
*
|
||||
* @author sk89q
|
||||
*/
|
||||
public class TreeGenerator {
|
||||
public enum TreeType {
|
||||
TREE("Regular tree", new String[] {"tree", "regular"}),
|
||||
BIG_TREE("Big tree", new String[] {"big", "bigtree"}),
|
||||
REDWOOD("Redwood", new String[] {"redwood", "sequoia", "sequoioideae"}),
|
||||
TALL_REDWOOD("Tall redwood", new String[] {"tallredwood", "tallsequoia", "tallsequoioideae"}),
|
||||
BIRCH("Birch", new String[] {"birch", "white", "whitebark"}),
|
||||
PINE("Pine", new String[] {"pine"}),
|
||||
RANDOM_REDWOOD("Random redwood", new String[] {"randredwood", "randomredwood", "anyredwood"}),
|
||||
RANDOM("Random", new String[] {"rand", "random"});
|
||||
|
||||
/**
|
||||
* Stores a map of the names for fast access.
|
||||
*/
|
||||
private static final Map<String,TreeType> lookup = new HashMap<String,TreeType>();
|
||||
|
||||
private final String name;
|
||||
private final String[] lookupKeys;
|
||||
|
||||
static {
|
||||
for (TreeType type : EnumSet.allOf(TreeType.class)) {
|
||||
for (String key : type.lookupKeys) {
|
||||
lookup.put(key, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TreeType(String name, String lookupKey) {
|
||||
this.name = name;
|
||||
this.lookupKeys = new String[]{ lookupKey };
|
||||
}
|
||||
|
||||
TreeType(String name, String[] lookupKeys) {
|
||||
this.name = name;
|
||||
this.lookupKeys = lookupKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user-friendly tree type name.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return type from name. May return null.
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public static TreeType lookup(String name) {
|
||||
return lookup.get(name.toLowerCase());
|
||||
}
|
||||
};
|
||||
|
||||
private static Random rand = new Random();
|
||||
|
||||
private TreeType type;
|
||||
|
||||
/**
|
||||
* Construct the tree generator with a tree type.
|
||||
*
|
||||
* @param type
|
||||
*/
|
||||
public TreeGenerator(TreeType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a tree.
|
||||
*
|
||||
* @param editSession
|
||||
* @param pos
|
||||
* @return
|
||||
* @throws MaxChangedBlocksException
|
||||
*/
|
||||
public boolean generate(EditSession editSession, Vector pos)
|
||||
throws MaxChangedBlocksException {
|
||||
return generate(type, editSession, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a tree.
|
||||
*
|
||||
* @param world
|
||||
* @param editSession
|
||||
* @param pos
|
||||
* @return
|
||||
* @throws MaxChangedBlocksException
|
||||
*/
|
||||
private boolean generate(TreeType type, EditSession editSession, Vector pos)
|
||||
throws MaxChangedBlocksException {
|
||||
LocalWorld world = editSession.getWorld();
|
||||
|
||||
TreeType[] choices;
|
||||
TreeType realType;
|
||||
|
||||
switch (type) {
|
||||
case TREE:
|
||||
return world.generateTree(editSession, pos);
|
||||
case BIG_TREE:
|
||||
return world.generateBigTree(editSession, pos);
|
||||
case BIRCH:
|
||||
return world.generateBirchTree(editSession, pos);
|
||||
case REDWOOD:
|
||||
return world.generateRedwoodTree(editSession, pos);
|
||||
case TALL_REDWOOD:
|
||||
return world.generateTallRedwoodTree(editSession, pos);
|
||||
case PINE:
|
||||
makePineTree(editSession, pos);
|
||||
return true;
|
||||
case RANDOM_REDWOOD:
|
||||
choices =
|
||||
new TreeType[] {
|
||||
TreeType.REDWOOD, TreeType.TALL_REDWOOD
|
||||
};
|
||||
realType = choices[rand.nextInt(choices.length)];
|
||||
return generate(realType, editSession, pos);
|
||||
case RANDOM:
|
||||
choices =
|
||||
new TreeType[] {
|
||||
TreeType.TREE, TreeType.BIG_TREE, TreeType.BIRCH,
|
||||
TreeType.REDWOOD, TreeType.TALL_REDWOOD, TreeType.PINE
|
||||
};
|
||||
realType = choices[rand.nextInt(choices.length)];
|
||||
return generate(realType, editSession, pos);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a terrible looking pine tree.
|
||||
*
|
||||
* @param basePos
|
||||
*/
|
||||
private static void makePineTree(EditSession editSession, Vector basePos)
|
||||
throws MaxChangedBlocksException {
|
||||
int trunkHeight = (int) Math.floor(Math.random() * 2) + 3;
|
||||
int height = (int) Math.floor(Math.random() * 5) + 8;
|
||||
|
||||
BaseBlock logBlock = new BaseBlock(17);
|
||||
BaseBlock leavesBlock = new BaseBlock(18);
|
||||
|
||||
// Create trunk
|
||||
for (int i = 0; i < trunkHeight; i++) {
|
||||
if (!editSession.setBlockIfAir(basePos.add(0, i, 0), logBlock)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Move up
|
||||
basePos = basePos.add(0, trunkHeight, 0);
|
||||
|
||||
// Create tree + leaves
|
||||
for (int i = 0; i < height; i++) {
|
||||
editSession.setBlockIfAir(basePos.add(0, i, 0), logBlock);
|
||||
|
||||
// Less leaves at these levels
|
||||
double chance = ((i == 0 || i == height - 1) ? 0.6 : 1);
|
||||
|
||||
// Inner leaves
|
||||
editSession.setChanceBlockIfAir(basePos.add(-1, i, 0), leavesBlock, chance);
|
||||
editSession.setChanceBlockIfAir(basePos.add(1, i, 0), leavesBlock, chance);
|
||||
editSession.setChanceBlockIfAir(basePos.add(0, i, -1), leavesBlock, chance);
|
||||
editSession.setChanceBlockIfAir(basePos.add(0, i, 1), leavesBlock, chance);
|
||||
editSession.setChanceBlockIfAir(basePos.add(1, i, 1), leavesBlock, chance);
|
||||
editSession.setChanceBlockIfAir(basePos.add(-1, i, 1), leavesBlock, chance);
|
||||
editSession.setChanceBlockIfAir(basePos.add(1, i, -1), leavesBlock, chance);
|
||||
editSession.setChanceBlockIfAir(basePos.add(-1, i, -1), leavesBlock, chance);
|
||||
|
||||
if (!(i == 0 || i == height - 1)) {
|
||||
for (int j = -2; j <= 2; j++) {
|
||||
editSession.setChanceBlockIfAir(basePos.add(-2, i, j), leavesBlock, 0.6);
|
||||
}
|
||||
for (int j = -2; j <= 2; j++) {
|
||||
editSession.setChanceBlockIfAir(basePos.add(2, i, j), leavesBlock, 0.6);
|
||||
}
|
||||
for (int j = -2; j <= 2; j++) {
|
||||
editSession.setChanceBlockIfAir(basePos.add(j, i, -2), leavesBlock, 0.6);
|
||||
}
|
||||
for (int j = -2; j <= 2; j++) {
|
||||
editSession.setChanceBlockIfAir(basePos.add(j, i, 2), leavesBlock, 0.6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
editSession.setBlockIfAir(basePos.add(0, height, 0), leavesBlock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks up a tree type. May return null if a tree type by that
|
||||
* name is not found.
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
public static TreeType lookup(String type) {
|
||||
return TreeType.lookup(type);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user