Added the tree generator for CraftBukkit. Build process is now a bit difficult.

This commit is contained in:
sk89q
2011-01-11 10:21:26 -08:00
parent 8e4558a246
commit 6390e1f180
6 changed files with 307 additions and 101 deletions

View File

@ -19,6 +19,10 @@
package com.sk89q.worldedit.bukkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.ItemStack;
import org.bukkit.Location;
import org.bukkit.World;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalWorld;
@ -26,6 +30,11 @@ import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseItemStack;
public class BukkitWorld extends LocalWorld {
/**
* Logger.
*/
private final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
private World world;
public BukkitWorld(World world) {
@ -100,37 +109,39 @@ public class BukkitWorld extends LocalWorld {
@Override
public boolean generateTree(EditSession editSession, Vector pt) {
// TODO Auto-generated method stub
return false;
try {
return CraftBukkitInterface.generateTree(editSession, pt);
} catch (Throwable t) {
logger.log(Level.SEVERE,
"Failed to create tree (do you need to update WorldEdit " +
"due to a Minecraft update?)", t);
return false;
}
}
@Override
public boolean generateBigTree(EditSession editSession, Vector pt) {
// TODO Auto-generated method stub
return false;
}
@Override
public void dropItem(Vector pt, int type, int count, int times) {
// TODO Auto-generated method stub
try {
return CraftBukkitInterface.generateBigTree(editSession, pt);
} catch (Throwable t) {
logger.log(Level.SEVERE,
"Failed to create tree (do you need to update WorldEdit " +
"due to a Minecraft update?)", t);
return false;
}
}
@Override
public void dropItem(Vector pt, int type, int count) {
// TODO Auto-generated method stub
ItemStack item = new ItemStack(type, count);
world.dropItemNaturally(toLocation(pt), item);
}
@Override
public void dropItem(Vector pt, int type) {
// TODO Auto-generated method stub
}
@Override
public void simulateBlockMine(Vector pt) {
// TODO Auto-generated method stub
ItemStack item = new ItemStack(type, 1);
world.dropItemNaturally(toLocation(pt), item);
}
@ -139,6 +150,10 @@ public class BukkitWorld extends LocalWorld {
// TODO Auto-generated method stub
return 0;
}
private Location toLocation(Vector pt) {
return new Location(world, pt.getX(), pt.getY(), pt.getZ());
}
@Override
public boolean equals(Object other) {

View File

@ -0,0 +1,115 @@
// $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.bukkit;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.Random;
import java.lang.reflect.*;
import net.minecraft.server.WorldGenBigTree;
import net.minecraft.server.WorldGenTrees;
import net.minecraft.server.WorldGenerator;
import sun.reflect.ReflectionFactory;
import com.sk89q.worldedit.*;
/**
*
* @author sk89q
*/
public class CraftBukkitInterface {
/**
* Logger.
*/
private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/**
* Random generator.
*/
private static Random random = new Random();
/**
* Proxy for the tree generator.
*/
private static WorldSetBlockProxy proxy;
/**
* Perform world generation at a location.
*
* @param pt
* @return
*/
private static boolean performWorldGen(EditSession editSession, Vector pt,
WorldGenerator worldGen) {
if (proxy == null) {
try {
proxy = createNoConstructor(WorldSetBlockProxy.class);
} catch (Throwable t) {
logger.log(Level.WARNING, "setBlock() proxy class failed to construct",
t);
return false;
}
}
proxy.setEditSession(editSession);
WorldGenerator gen = worldGen;
return gen.a(proxy, random,
pt.getBlockX(), pt.getBlockY() + 1, pt.getBlockZ());
}
/**
* Generate a tree at a location.
*
* @param pt
* @return
*/
public static boolean generateTree(EditSession editSession, Vector pt) {
return performWorldGen(editSession, pt, new WorldGenTrees());
}
/**
* Generate a big tree at a location.
*
* @param pt
* @return
*/
public static boolean generateBigTree(EditSession editSession, Vector pt) {
return performWorldGen(editSession, pt, new WorldGenBigTree());
}
/**
* Instantiate a class without calling its constructor.
*
* @param <T>
* @param clazz
* @return
* @throws Throwable
*/
@SuppressWarnings("rawtypes")
private static <T> T createNoConstructor(Class<T> clazz) throws Throwable {
try {
ReflectionFactory factory = ReflectionFactory.getReflectionFactory();
Constructor objectConstructor = Object.class.getDeclaredConstructor();
Constructor c = factory.newConstructorForSerialization(
clazz, objectConstructor
);
return clazz.cast(c.newInstance());
} catch (Throwable e) {
throw e;
}
}
}

View File

@ -0,0 +1,92 @@
package com.sk89q.worldedit.bukkit;
// $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/>.
*/
import net.minecraft.server.World;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.blocks.BaseBlock;
/**
* Proxy class to catch calls to set blocks.
*
* @author sk89q
*/
public class WorldSetBlockProxy extends World {
/**
* Edit session.
*/
private EditSession editSession;
/**
* Constructor that should NOT be called.
*
* @param editSession
*/
public WorldSetBlockProxy(EditSession editSession) {
super(null, "", (long)0, null);
throw new IllegalStateException("MinecraftSetBlockProxy constructor called (BAD)");
}
/**
* Called to set a block.
*
* @param x
* @param y
* @param z
* @param blockType
* @return
*/
@Override
public boolean a(int x, int y, int z, int blockType) {
try {
return editSession.setBlock(new Vector(x, y, z), new BaseBlock(blockType));
} catch (MaxChangedBlocksException ex) {
return false;
}
}
/**
* Called to get a block.
*
* @param x
* @param y
* @param z
* @return
*/
@Override
public int a(int x, int y, int z) {
return editSession.getBlock(new Vector(x, y, z)).getID();
}
/**
* @return
*/
public EditSession getEditSession() {
return editSession;
}
/**
* @param editSession
*/
public void setEditSession(EditSession editSession) {
this.editSession = editSession;
}
}