Tie up lose ends with NmsBlock class loading.

This commit is contained in:
Wizjany
2013-01-19 18:33:23 -05:00
parent d64a16da48
commit 89bdd8d9ba
10 changed files with 59 additions and 459 deletions

View File

@ -122,6 +122,8 @@ public class BukkitWorld extends LocalWorld {
this.world = world;
// check if we have a class we can use for nms access
// only run once per server startup
if (nmsBlockType != null) return;
Plugin plugin = Bukkit.getPluginManager().getPlugin("WorldEdit");
if (!(plugin instanceof WorldEditPlugin)) return; // hopefully never happens
@ -132,12 +134,20 @@ public class BukkitWorld extends LocalWorld {
return;
}
try {
// make a classloader that can handle our blocks
NmsBlockClassLoader loader = new NmsBlockClassLoader(BukkitWorld.class.getClassLoader(), nmsBlocksDir);
String filename;
for (File f : nmsBlocksDir.listFiles()) {
if (!f.isFile()) continue;
filename = f.getName();
Class<?> testBlock = loader.loadClass("CL-NMS" + filename);
// load class using magic keyword
Class<?> testBlock = null;
try {
testBlock = loader.loadClass("CL-NMS" + filename);
} catch (Throwable e) {
// someone is putting things where they don't belong
continue;
}
filename = filename.replaceFirst(".class$", ""); // get rid of extension
if (NmsBlock.class.isAssignableFrom(testBlock)) {
// got a NmsBlock, test it now
@ -160,7 +170,7 @@ public class BukkitWorld extends LocalWorld {
}
}
if (nmsBlockType != null) {
// logger.info("Found nms block class, using: " + nmsBlockType);
logger.info("[WorldEdit] Using external NmsBlock for this version: " + nmsBlockType.getName());
} else {
// try our default
try {
@ -172,7 +182,7 @@ public class BukkitWorld extends LocalWorld {
nmsGetMethod = nmsBlockType.getMethod("get", World.class, Vector.class, int.class, int.class);
nmsSetSafeMethod = nmsBlockType.getMethod("setSafely",
BukkitWorld.class, Vector.class, com.sk89q.worldedit.foundation.Block.class, boolean.class);
logger.info("[WorldEdit] Using inbuilt NmsBlock for this version of WorldEdit.");
logger.info("[WorldEdit] Using inbuilt NmsBlock for this version.");
}
} catch (Throwable e) {
// OMG DEVS WAI U NO SUPPORT <xyz> SERVER

View File

@ -250,14 +250,17 @@ public class DefaultNmsBlock extends NmsBlock {
int z = position.getBlockZ();
CraftWorld craftWorld = ((CraftWorld) world.getWorld());
// TileEntity te = craftWorld.getHandle().getTileEntity(x, y, z);
// craftWorld.getHandle().tileEntityList.remove(te);
boolean changed = craftWorld.getHandle().setRawTypeIdAndData(
x, y, z, block.getId(), block.getData());
boolean changed = craftWorld.getHandle().setRawTypeId(x, y, z, block.getId());
if (block instanceof BaseBlock) {
world.copyToWorld(position, (BaseBlock) block);
}
changed = craftWorld.getHandle().setRawData(x, y, z, block.getData()) || changed;
if (changed) {
if (notifyAdjacent) {
craftWorld.getHandle().update(x, y, z, block.getId());

View File

@ -24,6 +24,8 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Handler;
import java.util.zip.ZipEntry;
@ -96,7 +98,9 @@ public class WorldEditPlugin extends JavaPlugin {
// Make the data folders that WorldEdit uses
getDataFolder().mkdirs();
new File(getDataFolder() + File.separator + "nmsblocks").mkdir();
File targetDir = new File(getDataFolder() + File.separator + "nmsblocks");
targetDir.mkdir();
copyNmsBlockClasses(targetDir);
// Create the default configuration file
createDefaultConfiguration("config.yml");
@ -121,7 +125,32 @@ public class WorldEditPlugin extends JavaPlugin {
getServer().getScheduler().scheduleAsyncRepeatingTask(this,
new SessionTimer(controller, getServer()), 120, 120);
}
private void copyNmsBlockClasses(File target) {
try {
JarFile jar = new JarFile(getFile());
Enumeration entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) entries.nextElement();
if (!jarEntry.getName().startsWith("nmsblocks") || jarEntry.isDirectory()) continue;
File file = new File(target + File.separator + jarEntry.getName().replace("nmsblocks", ""));
if (file.exists()) continue;
InputStream is = jar.getInputStream(jarEntry);
FileOutputStream fos = new FileOutputStream(file);
fos = new FileOutputStream(file);
byte[] buf = new byte[8192];
int length = 0;
while ((length = is.read(buf)) > 0) {
fos.write(buf, 0, length);
}
fos.close();
is.close();
}
} catch (Throwable e) {}
}
/**