package com.edicatad.emvi.handlers;

import com.edicatad.emvi.world.storage.VillagerData;

import net.minecraft.world.World;

public class NBTDataHandler {
	private static VillagerData villagerData;
	
	private static final String tagName = "EmVi";
	
	public static void init(World world){
		if(villagerData == null){
			villagerData = (VillagerData) world.getPerWorldStorage().getOrLoadData(VillagerData.class, tagName);
			
			if(villagerData == null){
				villagerData = new VillagerData(tagName);
				world.getPerWorldStorage().setData(tagName, villagerData);
			}
		}
	}
	
	public static int getVillagersSpawnedForChunk(int chunkX, int chunkZ){
		// this returns 0 if no villagers have spawned or if there is no data stored - functionally the same
		return villagerData.getData().getInteger(String.format("x%iz%i", chunkX, chunkZ));
	}
	
	public static void incrementVillagersSpawnedForChunk(int chunkX, int chunkZ){
		villagerData.getData().setInteger(String.format("x%iz%i", chunkX, chunkZ), getVillagersSpawnedForChunk(chunkX, chunkZ) + 1);
	}
	
	public static void decrementVillagersSpawnedForChunk(int chunkX, int chunkZ){
		if(getVillagersSpawnedForChunk(chunkX, chunkZ) > 0){
			villagerData.getData().setInteger(String.format("x%iz%i", chunkX, chunkZ), getVillagersSpawnedForChunk(chunkX, chunkZ) - 1);
		}
	}
}

@@ -30,16 +30,100 @@
 dlers;%0A%0A
+import org.apache.logging.log4j.Level;%0Aimport org.apache.logging.log4j.LogManager;%0A%0A
 import c
@@ -1392,16 +1392,172 @@
 ) - 1);%0A
+%09%09%7D else %7B%0A%09%09%09LogManager.getLogger().log(Level.WARN, String.format(%22Tried to decrement villager spawn count for chunk at x%25iz%25i below 0%22, chunkX, chunkZ));%0A
 %09%09%7D%0A%09%7D%0A%7D

Add warning if trying to decrement villagers spawned below 0