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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • that happens every time I enter a new dimension.
    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } } my entire project:https://github.com/kevin051606/DERP-Mod/tree/Derp-1.0-1.20
  • Topics

×
×
  • Create New...

Important Information

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