Started by
tbroski
on
Topic category: User side tutorials
How to add Trades to Vanilla Villagers!
You don't really need java or forge knowledge.
First, make a class called VillagerTradeAdder
It should look similar to;
package net.mcreator.testmod;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
@TestmodModElements.ModElement.Tag
public class VillagerTradeAdder extends TestmodModElements.ModElement {
/**
* Do not remove this constructor
*/
public VillagerTradeAdder(TestmodModElements instance) {
super(instance, 9);
}
@Override
public void initElements() {
}
@Override
public void init(FMLCommonSetupEvent event) {
}
@Override
public void serverLoad(FMLServerStartingEvent event) {
}
}
*Important* You need to register this class by placing;
MinecraftForge.EVENT_BUS.register(this);
in;
@Override
public void initElements() {
}
so it looks like;
@Override
public void initElements() {
MinecraftForge.EVENT_BUS.register(this);
}
Now it's registered 😁.
Now add the following line at the bottom, but still inside the last }.
@SubscribeEvent
public void addNewTrade(VillagerTradesEvent event) {
if (event.getType() == VillagerProfession.BUTCHER) {
List<VillagerTrades.ITrade> trades = event.getTrades().get(1);
trades.add(new BasicTrade(new ItemStack([BuyingItem]), new ItemStack([SellingItem]), 1, 0, 0));
// ...
}
}
And add the following imports;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.village.VillagerTradesEvent;
import net.minecraft.entity.merchant.villager.VillagerProfession;
import java.util.List;
import net.minecraft.entity.merchant.villager.VillagerTrades;
import net.minecraft.item.MerchantOffer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import javax.annotation.Nullable;
import net.minecraftforge.common.BasicTrade;
Now your class should look similar too;
package net.mcreator.testmod;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.village.VillagerTradesEvent;
import net.minecraft.entity.merchant.villager.VillagerProfession;
import java.util.List;
import net.minecraft.entity.merchant.villager.VillagerTrades;
import net.minecraft.item.MerchantOffer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import javax.annotation.Nullable;
import net.minecraftforge.common.BasicTrade;
@TestmodModElements.ModElement.Tag
public class VillagerTradeAdder extends TestmodModElements.ModElement {
/**
* Do not remove this constructor
*/
public VillagerTradeAdder(TestmodModElements instance) {
super(instance, 9);
}
@Override
public void initElements() {
MinecraftForge.EVENT_BUS.register(this);
}
@Override
public void init(FMLCommonSetupEvent event) {
}
@Override
public void serverLoad(FMLServerStartingEvent event) {
}
@SubscribeEvent
public void addNewTrade(VillagerTradesEvent event) {
if (event.getType() == VillagerProfession.[TargetedProfessionNameAllCaps]) {
List<VillagerTrades.ITrade> trades = event.getTrades().get(1);
trades.add(new BasicTrade(new ItemStack([BuyingItem]), new ItemStack([SellingItem]), [maxTrades], [xp], [priceMulitplier]));
// ...
}
}
}
Congrats 🤯 😳, all of the code is implemented. Now all you need to do is replace [BuyingItem], [SellingItem], [maxTrades], [xp], [priceMultiplier] with the respected items/numbers. As well as changing [TargetedProfessionNameAllCaps] with the targeted profession. An example may be;
@SubscribeEvent
public void addNewTrade(VillagerTradesEvent event) {
if (event.getType() == VillagerProfession.BUTCHER) {
List<VillagerTrades.ITrade> trades = event.getTrades().get(1);
trades.add(new BasicTrade(new ItemStack(Items.GOLD_INGOT), new ItemStack(Items.CARROT), 1, 0, 0));
trades.add(new BasicTrade(new ItemStack(Items.GOLD_INGOT), new ItemStack(Items.PORKCHOP), 1, 0, 0));
trades.add(new BasicTrade(new ItemStack(Items.GOLD_INGOT), new ItemStack(Items.POWERED_RAIL), 1, 0, 0));
trades.add(new BasicTrade(new ItemStack(Items.GOLD_INGOT), new ItemStack(Items.POISONOUS_POTATO), 1, 0, 0));
}
}
Now, this snippet adds 4 trades. As you can see, to add more trades all you need to do is copy the trades.add(new BasicTrade ...)
Now you should be done 😎😝.
If you get any errors 😡 🤬 or it doesn't work, drop a comment, or add my discord (in profile).
Edited by tbroski on Wed, 08/12/2020 - 05:17
Yayy, another tutorial by tborski! Well written and clear-to-understand tutorial ;)
Haha
Wonderful Tutorial! Thanks Tbroski!
Amazing.
What. I can now make a farming mod that works with villagers!!!!!!!
I found that this works with vanilla items, but when I put in the name of a modded item (all caps and underscores), it says that the item is not valid.
But is this because I did it with armor? I put the in game display name of each armor piece in this way:
List<VillagerTrades.ITrade> trades = event.getTrades().get(1);
trades.add(new BasicTrade(new ItemStack(Items.EMERALD), new ItemStack(Items.STEEL_HELMET), 1, 0, 0));
trades.add(new BasicTrade(new ItemStack(Items.EMERALD), new ItemStack(Items.STEEL_CHESTPLATE), 1, 0, 0));
trades.add(new BasicTrade(new ItemStack(Items.EMERALD), new ItemStack(Items.STEEL_LEGGINGS), 1, 0, 0));
trades.add(new BasicTrade(new ItemStack(Items.EMERALD), new ItemStack(Items.STEEL_BOOTS), 1, 0, 0));
// ...
Yo, i know what im going to say have nothing to do with this topic but, im making a mod and i need to change the player model in my mod. i saw that 1.15.2 - Editing Player Model? | MCreator. can you do a tutorial about that. i only know coding with Python, and not java. in my case, my mod is about Dragon Ball. there is 4 race and each race have their own model.
Now how do i make new villager job?
How to specify the amount of emeralds and sold items per trade?
Nevermind, it doesn't work.
How do I replace emeralds in trades with another item?
I'm having trouble getting this to work. It says the package Testmod doesn't exist, so I put in the name of my mod. It still doesn't work. I made sure the capitalization was right and tried different variations, but I can't seem to stop it giving me errors. Help?
Also, the @Override parts aren't working