package com.codeski.dynmap_structures;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.dynmap.DynmapCommonAPI;
import org.dynmap.markers.MarkerAPI;
import org.dynmap.markers.MarkerSet;

import com.codeski.nbt.NBTReader;
import com.codeski.nbt.tags.NBT;
import com.codeski.nbt.tags.NBTByte;
import com.codeski.nbt.tags.NBTCompound;
import com.codeski.nbt.tags.NBTInteger;
import com.codeski.nbt.tags.NBTList;
import com.codeski.nbt.tags.NBTString;

public class DynmapStructures extends JavaPlugin implements Listener
{
	@Override
	public void onDisable() {
		//
	}

	@Override
	public void onEnable() {
		if (Bukkit.getPluginManager().isPluginEnabled("dynmap")) {
			DynmapCommonAPI api = (DynmapCommonAPI) Bukkit.getPluginManager().getPlugin("dynmap");
			MarkerAPI markerapi = api.getMarkerAPI();
			MarkerSet set = markerapi.createMarkerSet("structures", "Structures", null, false);
			InputStream in = this.getClass().getResourceAsStream("/fortress.png");
			markerapi.createMarkerIcon("structures.fortress", "Fortress", in);
			in = this.getClass().getResourceAsStream("/mineshaft.png");
			markerapi.createMarkerIcon("structures.mineshaft", "Mineshaft", in);
			in = this.getClass().getResourceAsStream("/stronghold.png");
			markerapi.createMarkerIcon("structures.stronghold", "Stronghold", in);
			in = this.getClass().getResourceAsStream("/temple.png");
			markerapi.createMarkerIcon("structures.temple", "Temple", in);
			in = this.getClass().getResourceAsStream("/village.png");
			markerapi.createMarkerIcon("structures.village", "Village", in);
			in = this.getClass().getResourceAsStream("/witch.png");
			markerapi.createMarkerIcon("structures.witch", "Witch", in);
			World world = Bukkit.getWorld("world");
			File dataFolder = new File(world.getWorldFolder(), "data");
			for (File f : dataFolder.listFiles())
				if (!f.getName().equals("villages.dat"))
					try {
						NBTReader r = new NBTReader(f);
						NBTCompound root = r.read();
						NBTCompound data = (NBTCompound) root.getPayload()[0];
						NBTCompound features = (NBTCompound) data.getPayload()[0];
						for (NBT temp : features.getPayload()) {
							NBTCompound entries = (NBTCompound) temp;
							String id = "";
							int x = Integer.MIN_VALUE, z = Integer.MIN_VALUE;
							boolean isWitch = false;
							for (NBT entry : entries.getPayload())
								if (entry.getName().equals("ChunkX"))
									x = ((NBTInteger) entry).getPayload();
								else if (entry.getName().equals("ChunkZ"))
									z = ((NBTInteger) entry).getPayload();
								else if (entry.getName().equals("id"))
									id = ((NBTString) entry).getPayload();
								else if (f.getName().equals("Temple.dat") && entry.getName().equals("Children")) {
									NBT[] children = ((NBTList) entry).getPayload();
									if (children.length > 0 && children[0] instanceof NBTCompound) {
										children = ((NBTCompound) children[0]).getPayload();
										for (NBT child : children)
											if (child.getName().equals("Witch"))
												isWitch = ((NBTByte) child).getPayload() > 0;
									}
								}
							if (x != Integer.MIN_VALUE && z != Integer.MIN_VALUE && id != "")
								if (id.equals("Fortress"))
									set.createMarker(id + "," + x + "," + z, id, "world_nether", x * 16, 64, z * 16, markerapi.getMarkerIcon("structures." + id.toLowerCase()), false);
								else if (isWitch)
									set.createMarker(id + "," + x + "," + z, "Witch Hut", "world", x * 16, 64, z * 16, markerapi.getMarkerIcon("structures.witch"), false);
								else
									set.createMarker(id + "," + x + "," + z, id, "world", x * 16, 64, z * 16, markerapi.getMarkerIcon("structures." + id.toLowerCase()), false);
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
		}
	}
}

@@ -2137,20 +2137,24 @@
 ompound 
-data
+features
  = (NBTC
@@ -2166,93 +2166,56 @@
 nd) 
-root.getPayload()%5B0%5D;%0A%09%09%09%09%09%09NBTCompound features = (NBTCompound) 
+((NBTCompound) root.get(%22
 data
+%22))
 .get
-Payload()%5B0%5D
+(%22Features%22)
 ;%0A%09%09
@@ -2974,18 +2974,16 @@
 ompound)
- %7B
 %0A%09%09%09%09%09%09%09
@@ -2985,26 +2985,32 @@
 %09%09%09%09%09%09%09%09
-children =
+for (NBT child :
  ((NBTCo
@@ -3046,45 +3046,8 @@
 ad()
-;%0A%09%09%09%09%09%09%09%09%09%09for (NBT child : children
 )%0A%09%09
@@ -3154,19 +3154,8 @@
  0;%0A
-%09%09%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09

Update to work with latest NBT build