跳到主要内容

23自定义村民交易

代码

public class ModCustomTrades {
public static void registerCustomTrades() {
TradeOfferHelper.registerVillagerOffers(VillagerProfession.FARMER, 1,
factories -> {
factories.add((entity, random) -> new TradeOffer(
new ItemStack(Items.EMERALD, 2),
new ItemStack(ModItems.TOMATO, 7),
6, 5, 0.05f));

factories.add((entity, random) -> new TradeOffer(
new ItemStack(Items.EMERALD, 7),
new ItemStack(ModItems.TOMATO_SEEDS, 1),
2, 7, 0.075f));
});

TradeOfferHelper.registerVillagerOffers(VillagerProfession.FARMER, 2,
factories -> {
factories.add((entity, random) -> new TradeOffer(
new ItemStack(Items.GOLD_INGOT, 16),
new ItemStack(Items.DIAMOND, 12),
new ItemStack(ModItems.CORN_SEEDS, 1),
2, 7, 0.075f));
});

TradeOfferHelper.registerVillagerOffers(VillagerProfession.LIBRARIAN, 1,
factories -> {
factories.add((entity, random) -> new TradeOffer(
new ItemStack(ModItems.RUBY, 32),
EnchantedBookItem.forEnchantment(new EnchantmentLevelEntry(Enchantments.PIERCING, 2)),
3, 12, 0.075f));
});


TradeOfferHelper.registerWanderingTraderOffers(1,
factories -> {
factories.add((entity, random) -> new TradeOffer(
new ItemStack(ModItems.RAW_RUBY, 16),
new ItemStack(ModItems.METAL_DETECTOR, 1),
1, 12, 0.075f));
});

TradeOfferHelper.registerWanderingTraderOffers(2,
factories -> {
factories.add((entity, random) -> new TradeOffer(
new ItemStack(ModItems.RAW_RUBY, 1),
new ItemStack(ModItems.COAL_BRIQUETTE, 1),
1, 12, 0.075f));
});
}
}

启动类TutorialMod:

ModCustomTrades.registerCustomTrades();

效果

图书管理员:

image-20240825091320741

村民:

image-20240825091350217

流浪商人:

image-20240825091412404