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 features = (NBTCompound) ((NBTCompound) root.get("data")).get("Features");
						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)
										for (NBT child : ((NBTCompound) children[0]).getPayload())
											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();
					}
		}
	}
}

@@ -109,16 +109,39 @@
 tStream;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -2881,13 +2881,17 @@
 %09%09%09%09
+List%3C
 NBT
-%5B%5D
+%3E
  chi
@@ -2956,14 +2956,14 @@
 ren.
-length
+size()
  %3E 0
@@ -2974,19 +2974,23 @@
 children
-%5B0%5D
+.get(0)
  instanc
@@ -3060,11 +3060,15 @@
 dren
-%5B0%5D
+.get(0)
 ).ge

Update NBTList to match implementation in NBT project