From c47d4e340c535e8e1320d8ff457ac65a2bc0c29d Mon Sep 17 00:00:00 2001 From: Jakub <53441451+kuba6000@users.noreply.github.com> Date: Mon, 8 Jul 2024 01:17:44 +0200 Subject: [PATCH] Catch custom villager recipes when adding vanilla villagers (#85) * Catch custom villager recipes when adding vanilla villagers * Smith -> Blacksmith --- .../mobsinfo/loader/VillagerTradesLoader.java | 45 ++++++++++++++++++- .../mobsinfo/nei/VillagerTradesHandler.java | 2 +- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/kuba6000/mobsinfo/loader/VillagerTradesLoader.java b/src/main/java/com/kuba6000/mobsinfo/loader/VillagerTradesLoader.java index e436729..aec16c1 100644 --- a/src/main/java/com/kuba6000/mobsinfo/loader/VillagerTradesLoader.java +++ b/src/main/java/com/kuba6000/mobsinfo/loader/VillagerTradesLoader.java @@ -26,6 +26,7 @@ import com.kuba6000.mobsinfo.api.VillagerRecipe; import com.kuba6000.mobsinfo.api.VillagerTrade; import com.kuba6000.mobsinfo.api.utils.ItemID; +import com.kuba6000.mobsinfo.api.utils.ModUtils; import com.kuba6000.mobsinfo.config.Config; import com.kuba6000.mobsinfo.nei.VillagerTradesHandler; import com.kuba6000.mobsinfo.network.LoadConfigPacket; @@ -53,6 +54,38 @@ public static void generateVillagerTrades() { // vanilla recipes? + ModUtils.TriConsumer, EntityVillager, Integer> detectCustomRecipes = (recipes, + villager, villagerID) -> { + try { + frand.newRound(); + + TradeList trades = new TradeList(); + TradeCollector collector = new TradeCollector(); + boolean second = false; + do { + MerchantRecipeList list = new MerchantRecipeList(); + VillagerRegistry.manageVillagerTrades(list, villager, villagerID, world.rand); + collector.collectTrades(trades, list, frand.chance); + + if (second && frand.chance < 0.0000001d) { + LOG.warn("Skipping " + villagerID + "(CustomHandler) because it's too randomized"); + break; + } + second = true; + } while (frand.nextRound()); + frand.newRound(); + collector.newRound(); + + if (!trades.itemsToTrade.isEmpty()) { + for (TradeInstance value : trades.itemsToTrade.values()) { + recipes.add(new VillagerTrade(value.i1, value.i2, value.o, value.chance)); + } + } + } catch (Throwable e) { + e.printStackTrace(); + } + }; + { // profession 0 EntityVillager entity = new EntityVillager(world); @@ -80,6 +113,8 @@ public static void generateVillagerTrades() { new ItemStack(Items.flint, 5, 0), 0.5d)); + detectCustomRecipes.accept(recipes, entity, 0); + VillagerRecipe.recipes.put(0, new VillagerRecipe(recipes, 0, entity)); } @@ -104,6 +139,8 @@ public static void generateVillagerTrades() { new VillagerTrade.TradeItem(new ItemStack(Items.book), null, 1), 0.07d)); + detectCustomRecipes.accept(recipes, entity, 1); + VillagerRecipe.recipes.put(1, new VillagerRecipe(recipes, 1, entity)); } @@ -130,6 +167,8 @@ public static void generateVillagerTrades() { 0.05d)); } + detectCustomRecipes.accept(recipes, entity, 2); + VillagerRecipe.recipes.put(2, new VillagerRecipe(recipes, 2, entity)); } @@ -166,6 +205,8 @@ public static void generateVillagerTrades() { recipes.add(new VillagerTrade(Items.emerald, null, Items.chainmail_chestplate, 0.1d)); recipes.add(new VillagerTrade(Items.emerald, null, Items.chainmail_leggings, 0.1d)); + detectCustomRecipes.accept(recipes, entity, 3); + VillagerRecipe.recipes.put(3, new VillagerRecipe(recipes, 3, entity)); } @@ -186,10 +227,12 @@ public static void generateVillagerTrades() { recipes.add(new VillagerTrade(Items.emerald, null, Items.cooked_porkchop, 0.3d)); recipes.add(new VillagerTrade(Items.emerald, null, Items.cooked_beef, 0.3d)); + detectCustomRecipes.accept(recipes, entity, 4); + VillagerRecipe.recipes.put(4, new VillagerRecipe(recipes, 4, entity)); } - // custom handlers + // custom villagers MobRecipeLoader.isInGenerationProcess = true; for (Integer id : VillagerRegistry.getRegisteredVillagers()) { diff --git a/src/main/java/com/kuba6000/mobsinfo/nei/VillagerTradesHandler.java b/src/main/java/com/kuba6000/mobsinfo/nei/VillagerTradesHandler.java index a1a2ee3..7fca13b 100644 --- a/src/main/java/com/kuba6000/mobsinfo/nei/VillagerTradesHandler.java +++ b/src/main/java/com/kuba6000/mobsinfo/nei/VillagerTradesHandler.java @@ -505,7 +505,7 @@ public VillagerCachedRecipe( this.mod = "Minecraft"; break; case 3: - this.profession = "Smith"; + this.profession = "Blacksmith"; this.mod = "Minecraft"; break; case 4: