Jump to content

[1.12.2] How to disable specific villager trades?


Recommended Posts

Posted

My mod adds new enchantments, and I gave them special ways to be obtained (drop from certain mob). I want to disable ANY other way to obtain the enchanted book. Making them "treasure" enchantments fixed the enchanting table part, but even treasure enchantments can still be found in some chest loot and in villager trades. I can edit the loot tables for the chest part, but how do I disable those specific trades? Even if I disabled village generation, players could still just cure a zombie villager and breed them, and I don't want to remove villagers. I just want to remove some specific trades.

Posted

The problem with villager trades is that they are not fixed, see EntityVillager.ListEnchantedBookForEmeralds for example - it just gets a random enchantment from the registry, and you can't control it in any way. You could iterate the profession registry, then iterate the careers, iterate the list-in-a-list in each career and remove those kind of recipes but it won't include custom modded trades and you will be deleting all trades like that, not just your specific enchantment.

You could make a system that checks whether the enchantment is applicable for the trade, say add Enchantment#canAppearInTrade or something and submit a PR to forge.

Posted
1 hour ago, V0idWa1k3r said:

The problem with villager trades is that they are not fixed, see EntityVillager.ListEnchantedBookForEmeralds for example - it just gets a random enchantment from the registry, and you can't control it in any way. You could iterate the profession registry, then iterate the careers, iterate the list-in-a-list in each career and remove those kind of recipes but it won't include custom modded trades and you will be deleting all trades like that, not just your specific enchantment.

You could make a system that checks whether the enchantment is applicable for the trade, say add Enchantment#canAppearInTrade or something and submit a PR to forge.

 

Thank you for the answer. I will try to do the PR, it seems much easier.

Posted (edited)

How can I see the source code, though? I went ahead and try to add a PR, but the code in MinecraftForge/patches/minecraft/net/minecraft/enchantment/EntityVillager.java.patch is way different than the one I see at my own eclipse when I go  to net.minecraft.enchantment.EntityVillager.java

 

so I don't know exactly where to edit ListEnchantedBookForEmeralds

Edited by Jiro7
Posted (edited)

Is there really no easy way to do this without having to do the PR, though?

 

Like, creating a custom villager class that extends from the original one but overrides the trade for books method, and then replace villagers with the new ones. The only problem I see is that ListEnchantedBookForEmeralds is a static class and not a method, but I feel like there must be a way.

 

I literally just want to replace

Enchantment enchantment = (Enchantment)Enchantment.REGISTRY.getRandomObject(random);

by

Enchantment enchantment = (Enchantment)Enchantment.REGISTRY.getRandomObject(random);
while (enchantment instanceof EnchantmentMyEnchant){
  enchantment = (Enchantment)Enchantment.REGISTRY.getRandomObject(random);
}

in ListEnchantedBookForEmeralds

Edited by Jiro7
Posted
5 hours ago, diesieben07 said:

Loop through ForgeRegistries.VILLAGER_PROFESSIONS, these are all villager professions that exist. From there you unfortunately have to use a bit of reflection to first obtain all careers for each profession (VillagerProfession#careers) and then for every career you can find the trades (VillagerCareer#trades, again through reflection). You can then replace the ITradeList entry you want to replace.

 

Not sure if this is what you meant by reflection, as I've never done that yet (I know basic Java, but not that much), but this is what I did and got a crash.

 

In my main.java:

 

@EventHandler
	public static void init(FMLInitializationEvent event){
		Collection<VillagerProfession> profs = ForgeRegistries.VILLAGER_PROFESSIONS.getValuesCollection();
		for (VillagerProfession pr : profs){
			if(pr.getSkin().toString().equals("minecraft:textures/entity/villager/librarian.png")){
				pr.getCareer(0).getTrades(0).remove(1);
				pr.getCareer(0).getTrades(3).remove(0);
				pr.getCareer(0).getTrades(4).remove(0);
				pr.getCareer(0).getTrades(0).add(1, new ListEnchantedBookNew());
				pr.getCareer(0).getTrades(3).add(0, new ListEnchantedBookNew());
				pr.getCareer(0).getTrades(4).add(0, new ListEnchantedBookNew());
			}
		}
	}

 

The new class:

 

public class ListEnchantedBookNew implements EntityVillager.ITradeList {

	@Override
	public void addMerchantRecipe(IMerchant merchant, MerchantRecipeList recipeList, Random random)
    {
        Enchantment enchantment = (Enchantment)Enchantment.REGISTRY.getRandomObject(random);
        while (!(enchantment instanceof EnchantmentSlimy || enchantment instanceof EnchantmentShieldBoost || enchantment instanceof EnchantmentAbsorb)){
        	enchantment = (Enchantment)Enchantment.REGISTRY.getRandomObject(random);
        }
        int i = MathHelper.getInt(random, enchantment.getMinLevel(), enchantment.getMaxLevel());
        ItemStack itemstack = ItemEnchantedBook.getEnchantedItemStack(new EnchantmentData(enchantment, i));
        int j = 2 + random.nextInt(5 + i * 10) + 3 * i;

        if (enchantment.isTreasureEnchantment())
        {
            j *= 2;
        }

        if (j > 64)
        {
            j = 64;
        }

        recipeList.add(new MerchantRecipe(new ItemStack(Items.BOOK), new ItemStack(Items.EMERALD, j), itemstack));
    }

}

 

And the crash:

Spoiler

 

escription: There was a severe problem during mod loading that has caused the game to fail

net.minecraftforge.fml.common.LoaderExceptionModCrash: Caught exception from Shield Enchantments (shieldenchants)
Caused by: java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableList.add(Unknown Source)
    at jiro.shieldenchantments.Main.init(Main.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:626)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91)
    at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150)
    at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399)
    at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71)
    at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116)
    at com.google.common.eventbus.EventBus.post(EventBus.java:217)
    at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:218)
    at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:196)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.common.eventbus.Subscriber.invokeSubscriberMethod(Subscriber.java:91)
    at com.google.common.eventbus.Subscriber$SynchronizedSubscriber.invokeSubscriberMethod(Subscriber.java:150)
    at com.google.common.eventbus.Subscriber$1.run(Subscriber.java:76)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399)
    at com.google.common.eventbus.Subscriber.dispatchEvent(Subscriber.java:71)
    at com.google.common.eventbus.Dispatcher$PerThreadQueuedDispatcher.dispatch(Dispatcher.java:116)
    at com.google.common.eventbus.EventBus.post(EventBus.java:217)
    at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:135)
    at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:744)
    at net.minecraftforge.fml.client.FMLClientHandler.finishMinecraftLoading(FMLClientHandler.java:336)
    at net.minecraft.client.Minecraft.init(Minecraft.java:581)
    at net.minecraft.client.Minecraft.run(Minecraft.java:421)
    at net.minecraft.client.main.Main.main(Main.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
    at GradleStart.main(GradleStart.java:25)

 

 

  • 1 year later...
Posted

1.12 is no longer supported on this forum.

Please update to a modern version of Minecraft to receive support.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.