diff --git a/src/main/java/org/dynmap/bukkit/DynmapPlugin.java b/src/main/java/org/dynmap/bukkit/DynmapPlugin.java index 258ae2b3..480bd97c 100644 --- a/src/main/java/org/dynmap/bukkit/DynmapPlugin.java +++ b/src/main/java/org/dynmap/bukkit/DynmapPlugin.java @@ -80,6 +80,7 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { public PlayerList playerList; private MapManager mapManager; public static DynmapPlugin plugin; + public SpoutPluginBlocks spb; public DynmapPlugin() { plugin = this; @@ -339,13 +340,7 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { /* Initialize event processor */ if(bep == null) bep = new BukkitEventProcessor(this); - - /* Check for Spout */ - if(detectSpout()) { - has_spout = true; - Log.info("Detected Spout"); - } - + /* Set up player login/quit event handler */ registerPlayerLoginListener(); @@ -358,6 +353,15 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { File dataDirectory = this.getDataFolder(); if(dataDirectory.exists() == false) dataDirectory.mkdirs(); + + /* Check for Spout */ + if(detectSpout()) { + has_spout = true; + Log.info("Detected Spout"); + spb = new SpoutPluginBlocks(); + spb.processSpoutBlocks(dataDirectory); + } + /* Get MC version */ String bukkitver = getServer().getVersion(); String mcver = "1.0.0"; diff --git a/src/main/java/org/dynmap/bukkit/SpoutPluginBlocks.java b/src/main/java/org/dynmap/bukkit/SpoutPluginBlocks.java new file mode 100644 index 00000000..36f3bf5e --- /dev/null +++ b/src/main/java/org/dynmap/bukkit/SpoutPluginBlocks.java @@ -0,0 +1,118 @@ +package org.dynmap.bukkit; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.imageio.ImageIO; + +import org.dynmap.Log; +import org.getspout.spoutapi.block.design.BlockDesign; +import org.getspout.spoutapi.material.CustomBlock; +import org.getspout.spoutapi.material.MaterialData; + +/** + * Handler for pulling spout-defined custom blocks into dynmap + * + * Generates and maintains custom render data file (renderdata/spout-texture.txt) and + * directory of downloaded image files (texturepacks/standard/spout/plugin.blockid.png) + */ +public class SpoutPluginBlocks { + + /* Process spout blocks - return true if something changed */ + public boolean processSpoutBlocks(File datadir) { + int cnt = 0; + File f = new File(datadir, "texturepacks/standard/spout"); + if(f.exists() == false) + f.mkdirs(); + List blks = new ArrayList(); + CustomBlock[] cb = MaterialData.getCustomBlocks(); + /* Build new texture file as string */ + StringBuilder sb = new StringBuilder(); + /* Loop through blocks - try to freshen files, if needed */ + for(CustomBlock b : cb) { + BlockDesign bd = b.getBlockDesign(); + String blkid = bd.getTexturePlugin() + "." + b.getName(); + File imgfile = new File(f, blkid + ".png"); + BufferedImage img = null; + boolean urlloaded = false; + try { + URL url = new URL(bd.getTexureURL()); + img = ImageIO.read(url); /* Load skin for player */ + urlloaded = true; + } catch (IOException iox) { + Log.severe("Error loading texture for custom block '" + blkid + "' (" + b.getCustomId() + ") from " + bd.getTexureURL() + "(" + iox.getMessage() + ")"); + if(imgfile.exists()) { + try { + img = ImageIO.read(imgfile); /* Load existing */ + Log.info("Loaded cached texture file for " + blkid); + } catch (IOException iox2) { + Log.severe("Error loading cached texture file for " + blkid + " - " + iox2.getMessage()); + } + } + } + if(img != null) { + try { + if(urlloaded) + ImageIO.write(img, "png", imgfile); + blks.add(b); + int w = img.getWidth(); + int h = img.getHeight(); + /* If width >= 6 times height, we're using custom for each side */ + sb.append("texturefile:id=" + blkid + ",filename=spout/" + blkid + ".png,xcount=" + w/h + ",ycount=1\n"); + if(w >= (6*h)) { + sb.append("block:id=" + b.getCustomId() + ",data=*,bottom=0,north=1,south=2,east=3,west=4,top=5,txtid=" + blkid + "\n"); + } + else { + sb.append("block:id=" + b.getCustomId() + ",data=*,allfaces=0,txtid=" + blkid + "\n"); + } + cnt++; + } catch (IOException iox) { + Log.severe("Error writing " + blkid + ".png"); + } finally { + img.flush(); + } + } + } + String rslt = sb.toString(); + /* Now, generate spout texture file - see if changed */ + File st = new File(datadir, "renderdata/spout-texture.txt"); + if(st.exists()) { + FileReader fr = null; + StringBuilder sbold = new StringBuilder(); + try { + fr = new FileReader(st); + int len; + char[] buf = new char[512]; + while((len = fr.read(buf)) > 0) { + sbold.append(buf, 0, len); + } + } catch (IOException iox) { + } finally { + if(fr != null) { try { fr.close(); } catch (IOException iox) {} } + } + /* If same, no changes */ + if(sbold.equals(rslt)) { + Log.info("Loaded " + cnt + " Spout custom blocks"); + return false; + } + } + FileWriter fw = null; + try { + fw = new FileWriter(st); + fw.write(rslt); + } catch (IOException iox) { + Log.severe("Error opening spout texture file - " + st.getPath()); + return false; + } finally { + if(fw != null) { try { fw.close(); } catch (IOException iox) {} } + } + Log.info("Loaded " + cnt + " Spout custom blocks"); + return false; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 93c5a20d..dfd9bbe4 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,7 +2,7 @@ name: dynmap main: org.dynmap.bukkit.DynmapPlugin version: "${project.version}-${BUILD_NUMBER}" authors: [FrozenCow, mikeprimm] -softdepend: [ Permissions, PermissionEx, bPermissions, PermissionsBukkit] +softdepend: [ Permissions, PermissionEx, bPermissions, PermissionsBukkit, SpoutMaterials ] commands: dynmap: description: Controls Dynmap.