package com.InfinityRaider.AgriCraft.init;

import com.InfinityRaider.AgriCraft.handler.ConfigurationHandler;
import com.InfinityRaider.AgriCraft.handler.VillageCreationHandler;
import com.InfinityRaider.AgriCraft.handler.VillagerTradeHandler;
import com.InfinityRaider.AgriCraft.reference.Reference;
import com.InfinityRaider.AgriCraft.world.StructureGreenhouse;
import com.InfinityRaider.AgriCraft.world.StructureGreenhouseIrrigated;
import cpw.mods.fml.common.registry.VillagerRegistry;
import net.minecraft.world.gen.structure.MapGenStructureIO;

public class WorldGen {
    private static int villagerId;

    public static void init() {
        if (!ConfigurationHandler.disableWorldGen) {
            //register villagers
            if (ConfigurationHandler.villagerEnabled) {
                int id = 0;
                boolean flag = false;
                while (!flag) {
                    try {
                        registerVillager(id);
                        flag = true;
                        villagerId = id;
                    } catch (Exception e) {
                        id++;
                    }
                }
            }

            //add greenhouses to villages
            MapGenStructureIO.func_143031_a(StructureGreenhouse.class, Reference.MOD_ID + ":Greenhouse");
            VillagerRegistry.instance().registerVillageCreationHandler(new VillageCreationHandler.GreenhouseHandler());

            //add irrigated greenhouses to villages
            if (!ConfigurationHandler.disableIrrigation) {
                MapGenStructureIO.func_143031_a(StructureGreenhouseIrrigated.class, Reference.MOD_ID + ":GreenhouseIrrigated");
                VillagerRegistry.instance().registerVillageCreationHandler(new VillageCreationHandler.GreenhouseIrrigatedHandler());
            }
        }
    }

    public static int getVillagerId() {
        return villagerId;
    }

    private static void registerVillager(int id) {
        VillagerRegistry.instance().registerVillagerId(id);
        VillagerRegistry.instance().registerVillageTradeHandler(id, new VillagerTradeHandler());
    }
}

@@ -544,16 +544,46 @@
 ureIO;%0A%0A
+import java.util.Collection;%0A%0A
 public c
@@ -828,156 +828,76 @@
     
-int id = 0;%0A                boolean flag = false;%0A                while (!flag) %7B%0A                    try %7B%0A                        r
+Collection%3CInteger%3E usedIds = VillagerRegistry.getR
 egister
+ed
 Villager
 (id)
@@ -892,19 +892,18 @@
 Villager
+s
 (
-id
 );%0A     
@@ -917,68 +917,18 @@
     
-        flag = true;%0A                        villagerI
+int i
 d = 
-id
+0
 ;%0A  
@@ -945,40 +945,39 @@
     
-    %7D catch (Exception e) %7B%0A    
+while (usedIds.contains(id)) %7B%0A
     
@@ -1006,36 +1006,32 @@
                 
-    
 %7D%0A              
@@ -1024,33 +1024,53 @@
                 
-%7D
+registerVillager(id);
 %0A            %7D%0A%0A

Clean up villager registering