Add Villager Trades to Vanilla Villagers

Started by tbroski on

Topic category: User side tutorials

Last seen on 04:35, 17. Jan 2021
Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Add Villager Trades to Vanilla Villagers
Wed, 08/12/2020 - 05:17 (edited)

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…
Wed, 08/12/2020 - 05:46

Yayy, another tutorial by tborski! Well written and clear-to-understand tutorial ;)

Last seen on 04:35, 17. Jan 2021
Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Haha
Wed, 08/12/2020 - 16:09

Haha

Last seen on 02:50, 30. Aug 2022
Joined Jun 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Wonderful Tutorial! Thanks…
Sat, 08/15/2020 - 03:01

Wonderful Tutorial! Thanks Tbroski!

Last seen on 17:32, 27. Mar 2024
Joined Sep 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Amazing.
Mon, 08/31/2020 - 12:58

Amazing.

Last seen on 00:42, 10. Oct 2023
Joined Mar 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
What. I can now make a…
Sat, 09/05/2020 - 00:45

What. I can now make a farming mod that works with villagers!!!!!!!

Last seen on 00:24, 9. Oct 2021
Joined Apr 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I found that this works with…
Tue, 10/20/2020 - 00:37

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.

Last seen on 00:24, 9. Oct 2021
Joined Apr 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
But is this because I did it…
Tue, 10/20/2020 - 00:40

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));
            // ...

Last seen on 17:26, 28. Mar 2024
Joined Mar 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Yo, i know what im going to…
Mon, 12/14/2020 - 21:02

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.

Last seen on 04:38, 27. Jul 2023
Joined Jan 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Now how do i make new…
Wed, 02/24/2021 - 08:46

Now how do i make new villager job?

Last seen on 22:55, 24. Feb 2021
Joined Jun 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to specify the amount of…
Wed, 02/24/2021 - 08:50

How to specify the amount of emeralds and sold items per trade?

Last seen on 22:55, 24. Feb 2021
Joined Jun 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Nevermind, it doesn't work.
Wed, 02/24/2021 - 09:03

Nevermind, it doesn't work.

Last seen on 17:14, 28. Mar 2024
Joined Jul 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How do I replace emeralds in…
Mon, 03/08/2021 - 11:14

How do I replace emeralds in trades with another item?

Last seen on 00:17, 31. Dec 2021
Joined Apr 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I'm having trouble getting…
Fri, 04/30/2021 - 20:04

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?

Last seen on 00:17, 31. Dec 2021
Joined Apr 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Also, the @Override parts…
Fri, 04/30/2021 - 20:09

Also, the @Override parts aren't working