diff --git a/src/tconstruct/TConstruct.java b/src/tconstruct/TConstruct.java index d397eb8d..2355311d 100644 --- a/src/tconstruct/TConstruct.java +++ b/src/tconstruct/TConstruct.java @@ -108,9 +108,12 @@ public class TConstruct GameRegistry.registerCraftingHandler(new TCraftingHandler()); NetworkRegistry.instance().registerGuiHandler(instance, proxy); - VillagerRegistry.instance().registerVillageTradeHandler(78943, new TVillageTrades()); if (PHConstruct.addToVillages) { + // adds to the villager spawner egg + VillagerRegistry.instance().registerVillagerId(78943); + // moved down, not needed if 'addToVillages' is false + VillagerRegistry.instance().registerVillageTradeHandler(78943, new TVillageTrades()); VillagerRegistry.instance().registerVillageCreationHandler(new VillageToolStationHandler()); VillagerRegistry.instance().registerVillageCreationHandler(new VillageSmelteryHandler()); try diff --git a/src/tconstruct/worldgen/village/TVillageTrades.java b/src/tconstruct/worldgen/village/TVillageTrades.java index 818f58ab..1591564d 100644 --- a/src/tconstruct/worldgen/village/TVillageTrades.java +++ b/src/tconstruct/worldgen/village/TVillageTrades.java @@ -1,5 +1,7 @@ package tconstruct.worldgen.village; +import java.util.ArrayList; +import java.util.List; import java.util.Random; import net.minecraft.block.Block; @@ -13,22 +15,98 @@ import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler; public class TVillageTrades implements IVillageTradeHandler { + private final List allowedIngredients = new ArrayList(); + private final int max = 17; + private final int min = 7; + + public TVillageTrades() { + super(); + + // vanilla blocks + allowedIngredients.add(new ItemStack(Block.pistonBase, 64)); + allowedIngredients.add(new ItemStack(Block.pistonStickyBase, 64)); + + // tconstruct blocks + allowedIngredients.add(new ItemStack(TContent.barricadeBirch, 64)); + allowedIngredients.add(new ItemStack(TContent.barricadeJungle, 64)); + allowedIngredients.add(new ItemStack(TContent.barricadeOak, 64)); + allowedIngredients.add(new ItemStack(TContent.barricadeSpruce, 64)); + allowedIngredients.add(new ItemStack(TContent.punji, 64)); + allowedIngredients.add(new ItemStack(TContent.toolStationWood, 3, 0)); + allowedIngredients.add(new ItemStack(TContent.toolStationWood, 3, 1)); + allowedIngredients.add(new ItemStack(TContent.toolStationWood, 3, 5)); + allowedIngredients.add(new ItemStack(TContent.toolStationWood, 3, 10)); + for (int sc = 0; sc < 4; sc++) { + allowedIngredients.add(new ItemStack(TContent.landmine, 64, sc)); + } + } + @Override public void manipulateTradesForVillager (EntityVillager villager, MerchantRecipeList recipeList, Random random) { if (villager.getProfession() == 78943) { - for (int num = 14; num < 18; num++) - recipeList.addToListWithCheck(new MerchantRecipe(new ItemStack(Block.pistonBase, num), new ItemStack(TContent.oreBerrySecond, 1, 0))); - for (int num = 23; num < 26; num++) - recipeList.addToListWithCheck(new MerchantRecipe(new ItemStack(Block.pistonBase, num), new ItemStack(TContent.oreBerry, 1, 0))); - for (int num = 23; num < 26; num++) - recipeList.addToListWithCheck(new MerchantRecipe(new ItemStack(Block.pistonBase, num), new ItemStack(TContent.oreBerry, 1, 2))); - for (int num = 23; num < 26; num++) - recipeList.addToListWithCheck(new MerchantRecipe(new ItemStack(Block.pistonBase, num), new ItemStack(TContent.oreBerry, 1, 3))); - for (int num = 30; num < 34; num++) - recipeList.addToListWithCheck(new MerchantRecipe(new ItemStack(Block.pistonBase, num), new ItemStack(TContent.oreBerry, 1, 1))); + ItemStack ingredient; + ItemStack ingredient2; + ItemStack result; + + for (int sc = 8; sc < 12; sc++) { + int num = getNextInt(random, min, max); + + ingredient = getIngredient(random, num); + if (ingredient.stackSize < 13) { + ingredient2 = getIngredient(random, ingredient); + } else { + ingredient2 = null; + } + result = new ItemStack(TContent.oreBerry, calcStackSize(ingredient, ingredient2), sc); + // adds iron, gold, copper & tin orebushes to the recipe list + recipeList.addToListWithCheck(new MerchantRecipe(ingredient, ingredient2, result)); + + if (sc == 8) { + // adds alumine orebush to the recipe list + result = new ItemStack(TContent.oreBerrySecond, calcStackSize(ingredient, ingredient2), sc); + recipeList.addToListWithCheck(new MerchantRecipe(ingredient, ingredient2, result)); + } + } } } + private int calcStackSize(ItemStack ingredient, ItemStack ingredient2) { + if (ingredient == null) return 1; + int num = ingredient.stackSize; + if (ingredient2 != null) + num += ingredient2.stackSize; + + return Math.max(1, Math.round((num - 5) / 4)); + } + + private ItemStack getIngredient(Random random, ItemStack ingredient) { + int sc; + ItemStack is; + int tries = 0; + while (true) { + sc = getNextInt(random, 0, allowedIngredients.size() - 1); + is = allowedIngredients.get(sc); + + if (is.itemID != ingredient.itemID || is.getItemDamage() != ingredient.getItemDamage()) + break; + + tries++; + if (tries == 5) + return null; + } + int num = getNextInt(random, 0, Math.min(is.stackSize , max - ingredient.stackSize)); + return is.copy().splitStack(num); + } + + private ItemStack getIngredient(Random random, int num) { + int sc = getNextInt(random, 0, allowedIngredients.size() - 1); + ItemStack item = allowedIngredients.get(sc); + return item.copy().splitStack(Math.min(num, item.stackSize)); + } + + private int getNextInt(Random random, int min, int max) { + return random.nextInt(Math.max(1, (max - min) + 1)) + min; + } }