Skip to content

Commit

Permalink
Update BWGVillagerTrades to use a Int2ObjectMap instead of a List of …
Browse files Browse the repository at this point in the history
…Pairs

Signed-off-by: Joseph T. McQuigg <[email protected]>
  • Loading branch information
JT122406 committed Dec 19, 2024
1 parent 2f5ae94 commit 72a79e4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,43 +19,65 @@

public class BWGVillagerTrades {

public static final Map<VillagerProfession, List<Pair<Integer, MerchantOffer>>> TRADES = new HashMap<>();
public static final Map<VillagerProfession, Int2ObjectMap<List<MerchantOffer>>> 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) {
Expand All @@ -62,4 +87,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 ItemStack(Items.EMERALD, emeraldCost), new ItemStack(item, numberOfItems), maxUses, villagerXp, priceMultiplier);
}

private static Int2ObjectMap<List<MerchantOffer>> toIntMap(ImmutableMap<Integer, List<MerchantOffer>> map) {
return new Int2ObjectOpenHashMap<>(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.fabricmc.fabric.api.loot.v2.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;
Expand Down Expand Up @@ -62,7 +63,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);
})
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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;
Expand Down Expand Up @@ -79,7 +80,10 @@ private static void onVillagerTrade(final VillagerTradesEvent event) {
if (BWGVillagerTrades.TRADES.containsKey(event.getType())) {
Int2ObjectMap<List<VillagerTrades.ItemListing>> trades = event.getTrades();
BWGVillagerTrades.TRADES.get(event.getType())
.forEach(pair -> trades.get(pair.getFirst().intValue()).add((trader, random) -> pair.getSecond()));
.forEach((level, offers) -> {
List<VillagerTrades.ItemListing> tradeList = trades.get(level.intValue());
for (MerchantOffer offer : offers) tradeList.add((trader, random) -> offer);
});
}
}

Expand Down

0 comments on commit 72a79e4

Please sign in to comment.