diff --git a/Common/src/main/java/net/potionstudios/biomeswevegone/world/entity/npc/BWGVillagerTrades.java b/Common/src/main/java/net/potionstudios/biomeswevegone/world/entity/npc/BWGVillagerTrades.java index ffc038569..54bc5a60d 100644 --- a/Common/src/main/java/net/potionstudios/biomeswevegone/world/entity/npc/BWGVillagerTrades.java +++ b/Common/src/main/java/net/potionstudios/biomeswevegone/world/entity/npc/BWGVillagerTrades.java @@ -1,6 +1,9 @@ package net.potionstudios.biomeswevegone.world.entity.npc; -import com.mojang.datafixers.util.Pair; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import net.minecraft.world.entity.npc.VillagerProfession; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; @@ -17,43 +20,65 @@ public class BWGVillagerTrades { - public static final Map>> TRADES = new HashMap<>(); + public static final Map>> TRADES = new HashMap<>(); public static void makeTrades() { - TRADES.put(BWGVillagerProfessions.FORAGER.get(), List.of( - Pair.of(1, createEmeraldForItemsOffer(Items.RED_MUSHROOM, 10, 12, 2)), - Pair.of(1, createEmeraldForItemsOffer(Items.BROWN_MUSHROOM, 10, 12, 2)), - Pair.of(1, createEmeraldForItemsOffer(BWGBlocks.GREEN_MUSHROOM.get(), 10, 12, 2)), - Pair.of(2, createEmeraldForItemsOffer(BWGBlocks.WOOD_BLEWIT.get(), 8, 12, 3)), - Pair.of(2, createItemsForEmeraldsOffer(BWGItems.WHITE_PUFFBALL_CAP.get(), 4, 5, 4, 2, 0.05f)), - Pair.of(3, createEmeraldForItemsOffer(BWGItems.WHITE_PUFFBALL_SPORES.get(), 4, 5, 4)), - Pair.of(4, createItemsForEmeraldsOffer(BWGBlocks.WITCH_HAZEL_BRANCH.get(),4, 9, 4, 3, 0.05f)), - Pair.of(4, createItemsForEmeraldsOffer(BWGBlocks.WITCH_HAZEL_BLOSSOM.get(), 10, 1, 10, 3, 0.05f)), - Pair.of(5, createItemsForEmeraldsOffer(BWGBlocks.SHELF_FUNGI.get(), 3, 9, 4, 4, 0.05f)), - Pair.of(5, createEmeraldForItemsOffer(Items.SWEET_BERRIES, 16, 4, 2)), - Pair.of(5, createEmeraldForItemsOffer(BWGItems.BLUEBERRIES.get(), 16, 4, 2)) - )); + TRADES.put(BWGVillagerProfessions.FORAGER.get(), toIntMap(ImmutableMap.of( + 1, ImmutableList.of( + createEmeraldForItemsOffer(Items.RED_MUSHROOM, 10, 12, 2), + createEmeraldForItemsOffer(Items.BROWN_MUSHROOM, 10, 12, 2), + createEmeraldForItemsOffer(BWGBlocks.GREEN_MUSHROOM.get(), 10, 12, 2) + ), + 2, ImmutableList.of( + createEmeraldForItemsOffer(BWGBlocks.WOOD_BLEWIT.get(), 8, 12, 3), + createItemsForEmeraldsOffer(BWGItems.WHITE_PUFFBALL_CAP.get(), 4, 5, 4, 2, 0.05f) + ), + 3, ImmutableList.of( + createEmeraldForItemsOffer(BWGItems.WHITE_PUFFBALL_SPORES.get(), 4, 5, 4) + ), + 4, ImmutableList.of( + createItemsForEmeraldsOffer(BWGBlocks.WITCH_HAZEL_BRANCH.get(), 4, 9, 4, 3, 0.05f), + createItemsForEmeraldsOffer(BWGBlocks.WITCH_HAZEL_BLOSSOM.get(), 10, 1, 10, 3, 0.05f) + ), + 5, ImmutableList.of( + createItemsForEmeraldsOffer(BWGBlocks.SHELF_FUNGI.get(), 3, 9, 4, 4, 0.05f), + createEmeraldForItemsOffer(Items.SWEET_BERRIES, 16, 4, 2), + createEmeraldForItemsOffer(BWGItems.BLUEBERRIES.get(), 16, 4, 2) + ) + ))); if (!BWGTradesConfig.INSTANCE.get().enableVanillaTradeAdditions()) return; - TRADES.put(VillagerProfession.BUTCHER, List.of( - Pair.of(2, createEmeraldForItemsOffer(BWGItems.BLUEBERRIES.get(), 10, 12, 2)) - )); - TRADES.put(VillagerProfession.FARMER, List.of( - Pair.of(1, createEmeraldForItemsOffer(BWGItems.CATTAIL_SPROUT.get(), 24, 12, 2)), - Pair.of(2, createEmeraldForItemsOffer(BWGItems.BAOBAB_FRUIT.get(), 10, 12, 2)), - Pair.of(2, createEmeraldForItemsOffer(BWGItems.GREEN_APPLE.get(), 24, 12, 2)), - Pair.of(2, createEmeraldForItemsOffer(BWGBlocks.ALOE_VERA.get(), 16, 12, 2)), - Pair.of(3, createEmeraldForItemsOffer(BWGItems.YUCCA_FRUIT.get(), 10, 12, 2)) - )); - TRADES.put(VillagerProfession.MASON, List.of( - Pair.of(3, createEmeraldForItemsOffer(BWGBlocks.ROCKY_STONE_SET.getBase(), 12, 12, 20)), - Pair.of(3, createItemsForEmeraldsOffer(BWGBlocks.ROCKY_STONE_SET.getBase(), 1, 1, 12, 10, 0.05f)), - Pair.of(3, createEmeraldForItemsOffer(BWGBlocks.MOSSY_STONE_SET.getBase(), 12, 12, 20)), - Pair.of(3, createItemsForEmeraldsOffer(BWGBlocks.MOSSY_STONE_SET.getBase(), 1, 1, 12, 10, 0.05f)), - Pair.of(4, createEmeraldForItemsOffer(BWGBlocks.DACITE_SET.getBase(), 12, 12, 30)), - Pair.of(4, createItemsForEmeraldsOffer(BWGBlocks.DACITE_SET.getBase(), 1, 1, 12, 15, 0.05f)), - Pair.of(4, createEmeraldForItemsOffer(BWGBlocks.RED_ROCK_SET.getBase(), 12, 12, 30)), - Pair.of(4, createItemsForEmeraldsOffer(BWGBlocks.RED_ROCK_SET.getBase(), 1, 1, 12, 15, 0.05f)) - )); + TRADES.put(VillagerProfession.BUTCHER, toIntMap(ImmutableMap.of( + 2, ImmutableList.of( + createEmeraldForItemsOffer(BWGItems.BLUEBERRIES.get(), 10, 12, 2) + ) + ))); + TRADES.put(VillagerProfession.FARMER, toIntMap(ImmutableMap.of( + 1, ImmutableList.of( + createEmeraldForItemsOffer(BWGItems.CATTAIL_SPROUT.get(), 24, 12, 2) + ), + 2, ImmutableList.of( + createEmeraldForItemsOffer(BWGItems.BAOBAB_FRUIT.get(), 10, 12, 2), + createEmeraldForItemsOffer(BWGItems.GREEN_APPLE.get(), 24, 12, 2), + createEmeraldForItemsOffer(BWGBlocks.ALOE_VERA.get(), 16, 12, 2) + ), + 3, ImmutableList.of( + createEmeraldForItemsOffer(BWGItems.YUCCA_FRUIT.get(), 10, 12, 2) + ) + ))); + TRADES.put(VillagerProfession.MASON, toIntMap(ImmutableMap.of( + 3, ImmutableList.of( + createEmeraldForItemsOffer(BWGBlocks.ROCKY_STONE_SET.getBase(), 12, 12, 20), + createItemsForEmeraldsOffer(BWGBlocks.ROCKY_STONE_SET.getBase(), 1, 1, 12, 10, 0.05f), + createEmeraldForItemsOffer(BWGBlocks.MOSSY_STONE_SET.getBase(), 12, 12, 20), + createItemsForEmeraldsOffer(BWGBlocks.MOSSY_STONE_SET.getBase(), 1, 1, 12, 10, 0.05f) + ), + 4, ImmutableList.of( + createEmeraldForItemsOffer(BWGBlocks.DACITE_SET.getBase(), 12, 12, 30), + createItemsForEmeraldsOffer(BWGBlocks.DACITE_SET.getBase(), 1, 1, 12, 15, 0.05f), + createEmeraldForItemsOffer(BWGBlocks.RED_ROCK_SET.getBase(), 12, 12, 30), + createItemsForEmeraldsOffer(BWGBlocks.RED_ROCK_SET.getBase(), 1, 1, 12, 15, 0.05f) + ) + ))); } private static MerchantOffer createEmeraldForItemsOffer(ItemLike item, int cost, int maxUses, int villagerXp) { @@ -63,4 +88,8 @@ private static MerchantOffer createEmeraldForItemsOffer(ItemLike item, int cost, private static MerchantOffer createItemsForEmeraldsOffer(ItemLike item, int emeraldCost, int numberOfItems, int maxUses, int villagerXp, float priceMultiplier) { return new MerchantOffer(new ItemCost(Items.EMERALD, emeraldCost), new ItemStack(item, numberOfItems), maxUses, villagerXp, priceMultiplier); } + + private static Int2ObjectMap> toIntMap(ImmutableMap> map) { + return new Int2ObjectOpenHashMap<>(map); + } } diff --git a/Fabric/src/main/java/net/potionstudios/biomeswevegone/fabric/VanillaCompatFabric.java b/Fabric/src/main/java/net/potionstudios/biomeswevegone/fabric/VanillaCompatFabric.java index ba5a029f4..f84efabe3 100644 --- a/Fabric/src/main/java/net/potionstudios/biomeswevegone/fabric/VanillaCompatFabric.java +++ b/Fabric/src/main/java/net/potionstudios/biomeswevegone/fabric/VanillaCompatFabric.java @@ -5,6 +5,7 @@ import net.fabricmc.fabric.api.loot.v3.LootTableEvents; import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper; import net.fabricmc.fabric.api.registry.*; +import net.minecraft.world.item.trading.MerchantOffer; import net.minecraft.world.level.storage.loot.BuiltInLootTables; import net.minecraft.world.level.storage.loot.LootPool; import net.minecraft.world.level.storage.loot.entries.LootItem; @@ -69,7 +70,12 @@ private static void registerLootModifiers() { private static void registerTrades() { if (!BWGTradesConfig.INSTANCE.get().enableTrades()) return; BWGVillagerTrades.makeTrades(); - BWGVillagerTrades.TRADES.forEach(((villagerProfession, pairs) -> pairs.forEach(pair -> - TradeOfferHelper.registerVillagerOffers(villagerProfession, pair.getFirst(), factory -> factory.add(((trader, random) -> pair.getSecond())))))); + BWGVillagerTrades.TRADES.forEach((villagerProfession, offersMap) -> + offersMap.forEach((level, offers) -> + TradeOfferHelper.registerVillagerOffers(villagerProfession, level, factory -> { + for (MerchantOffer offer : offers) factory.add((trader, random) -> offer); + }) + ) + ); } } diff --git a/Forge/src/main/java/net/potionstudios/biomeswevegone/forge/VanillaCompatForge.java b/Forge/src/main/java/net/potionstudios/biomeswevegone/forge/VanillaCompatForge.java index f2c46df29..e51f23e88 100644 --- a/Forge/src/main/java/net/potionstudios/biomeswevegone/forge/VanillaCompatForge.java +++ b/Forge/src/main/java/net/potionstudios/biomeswevegone/forge/VanillaCompatForge.java @@ -8,6 +8,7 @@ import net.minecraft.world.entity.npc.VillagerTrades; import net.minecraft.world.item.AxeItem; import net.minecraft.world.item.ShovelItem; +import net.minecraft.world.item.trading.MerchantOffer; import net.minecraft.world.level.block.*; import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.common.ToolActions; @@ -85,7 +86,10 @@ private static void onVillagerTrade(final VillagerTradesEvent event) { if (BWGVillagerTrades.TRADES.containsKey(event.getType())) { Int2ObjectMap> trades = event.getTrades(); BWGVillagerTrades.TRADES.get(event.getType()) - .forEach(pair -> trades.get(pair.getFirst().intValue()).add((trader, random) -> pair.getSecond())); + .forEach((level, offers) -> { + List tradeList = trades.get(level.intValue()); + for (MerchantOffer offer : offers) tradeList.add((trader, random) -> offer); + }); } } diff --git a/NeoForge/src/main/java/net/potionstudios/biomeswevegone/neoforge/VanillaCompatNeoForge.java b/NeoForge/src/main/java/net/potionstudios/biomeswevegone/neoforge/VanillaCompatNeoForge.java index c281c1763..a797713ae 100644 --- a/NeoForge/src/main/java/net/potionstudios/biomeswevegone/neoforge/VanillaCompatNeoForge.java +++ b/NeoForge/src/main/java/net/potionstudios/biomeswevegone/neoforge/VanillaCompatNeoForge.java @@ -8,6 +8,7 @@ import net.minecraft.world.entity.npc.VillagerTrades; import net.minecraft.world.item.AxeItem; import net.minecraft.world.item.ShovelItem; +import net.minecraft.world.item.trading.MerchantOffer; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.FireBlock; import net.minecraft.world.level.block.state.BlockState; @@ -65,15 +66,14 @@ else if (state.is(BWGBlocks.PEAT.get())) } } - /** - * Register villager trades. - * @see VillagerTradesEvent - */ private static void onVillagerTrade(final VillagerTradesEvent event) { if (BWGVillagerTrades.TRADES.containsKey(event.getType())) { Int2ObjectMap> trades = event.getTrades(); BWGVillagerTrades.TRADES.get(event.getType()) - .forEach(pair -> trades.get(pair.getFirst().intValue()).add((trader, random) -> pair.getSecond())); + .forEach((level, offers) -> { + List tradeList = trades.get(level.intValue()); + for (MerchantOffer offer : offers) tradeList.add((trader, random) -> offer); + }); } }