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
Nevermind, I got it working. I was capitalizing it incorrectly- I didn't know you only capitalize the first letter and not the others in some of the code.
Good tutorial! I think you should also add part how to make different levels of recipes.
would this work with the wandering trader?
i have an error
My code
package net.mcreator.galaxycraft;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.api.distmarker.Dist;
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;
@GalaxyCraftModElements.ModElement.Tag
public class VillagerTrades extends GalaxyCraftModElements.ModElement {
/**
* Do not remove this constructor
*/
public VillagerTrades(GalaxyCraftModElements instance) {
super(instance, 154);
}
@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.CARTOGRAPH) {
List<VillagerTrades.ITrade> trades = event.getTrades().get(1);
trades.add(new BasicTrade(new ItemStack(Items.EMERALD), new ItemStack(Items.CARROT), 15, 5, 3));
// ...
}
}
}
My Build
> Task :compileJava FAILED
error: VillagerTrades is already defined in this compilation unit import net.minecraft.entity.merchant.villager.VillagerTrades;
^
error: cannot find symbol if (event.getType() == VillagerProfession.CARTOGRAPH) {
^
symbol: variable CARTOGRAPH
location: class VillagerProfession
: error: cannot find symbol List<VillagerTrades.ITrade> trades = event.getTrades().get(1);
^
symbol: class ITrade
location: class VillagerTrades
3 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
1 actionable task: 1 executed
BUILD FAILED
Task completed in 3 seconds
Can make a tutorial with custom villagers instead?