Jump to content

[1.6.4] Changing the chance of a custom villager trade


Da9L

Recommended Posts

Hello.

 

First of all i want to say that this is my entry project into modding with Forge, so my experience is low. However it has been a good experience so far.

 

So just to try it out, i began creating a very simple mod that adds custom trades to villagers. I wanted them to be able to trade emeralds for enchanted books at a higher rate.

 

So far it works well. I have two classes: My main class, and a TradeHandler. Besides loading configs my main class only has this:

 

VillagerRegistry.instance().registerVillageTradeHandler(1, new TradeHandler());
VillagerRegistry.instance().registerVillageTradeHandler(2, new TradeHandler());
VillagerRegistry.instance().registerVillageTradeHandler(3, new TradeHandler());
VillagerRegistry.instance().registerVillageTradeHandler(4, new TradeHandler());

 

My Trade handler looks like this:

 

public class TradeHandler implements IVillageTradeHandler{

@Override
public void manipulateTradesForVillager(EntityVillager villager, MerchantRecipeList recipeList, Random random)
{
	Random rand = new Random();	
	int Price = 64;

	Enchantment enchantment = Enchantment.enchantmentsBookList[random.nextInt(Enchantment.enchantmentsBookList.length)];
	//Enchantment enchantment = Enchantment.enchantmentsBookList[15];
        ItemStack enchantedBook = new ItemStack(Item.enchantedBook, 1);
        int lvl = MathHelper.getRandomIntegerInRange(rand, enchantment.getMinLevel(), enchantment.getMaxLevel());
        enchantedBook.addEnchantment(enchantment, lvl);
        switch (lvl) {
        	case 1 : Price = Main.lvl1Price;
        			 break;
        	case 2 : Price = Main.lvl2Price;
		 		 break;
        	case 3 : Price = Main.lvl3Price;
		 		 break;
        	case 4 : Price = Main.lvl4Price;
        			 break;
        	case 5 : Price = Main.lvl5Price;
		 		 break;
        }
        
        final String[] highCostBooks = convertStringToArraystring(Main.highPriceEnchantments);
        if(stringContainsItemFromList(enchantment.getTranslatedName(lvl), highCostBooks))
        {
        	Price = Main.highPriceEnchantmentsPrice;
        }

        

	recipeList.add(new MerchantRecipe(new ItemStack(Item.emerald, Price, 0), enchantedBook));
}

public static boolean stringContainsItemFromList(String inputString, String[] items)
{
    for(int i =0; i < items.length; i++)
    {
        if(inputString.contains(items[i]))
        {
            return true;
        }
    }
    return false;
}

public static String[] convertStringToArraystring(String input)
{
	String[] split = input.split(",");
	return split;
}

}

 

My problem is that, on a vanilla minecraft installation my custom trades is shown like 50% of the times.. But on a modded installation where other mods also add trades, i only get a 5% chance of my trade showing on a villager. I want this to have a higher chance. So basicly ..

 

Is it possible to set a higher chance of a specifc trade to get shown ?

 

Thanks in advance !

Link to comment
Share on other sites

Could I ask for a clarification of your issue?

 

By "vanilla minecraft installation" do you mean "Forge modded without other mods installed", which would indicate the problem is that your villager trades are getting "crowded out" by villager trades added by other mods?

 

Exactly what i mean :-)

Link to comment
Share on other sites

Well, I don't know that there is a good way of accomplishing that without crowding other mods' trades in return. There are quite a few methods in the VillagerRegistry that your mod can use to fiddle with the trade manifests. You're probably interested in manipulateTradesForVillager, which would let you directly manipulate the MerchantRecipeList of a given villager, or manageVillagerTrades which is a callback you could use for dealing with them in aggregate.

 

If you want a guaranteed way of accessing your custom trades, would you be willing to consider a custom villager? I understand that this isn't in the original scope of your mod's functionality but I feel it would be a better solution (in terms of compatibility, which is the root of your problem here) than just budging other mods' trades to the side. There are handy methods in the VillagerRegistry for this too, including ones that set the skin and whatnot.

 

TL;DR - Poke through VillagerRegistry.class, the answers are therein!

 

EDIT: Also, 1.6.4?

Link to comment
Share on other sites

Well, I don't know that there is a good way of accomplishing that without crowding other mods' trades in return. There are quite a few methods in the VillagerRegistry that your mod can use to fiddle with the trade manifests. You're probably interested in manipulateTradesForVillager, which would let you directly manipulate the MerchantRecipeList of a given villager, or manageVillagerTrades which is a callback you could use for dealing with them in aggregate.

 

If you want a guaranteed way of accessing your custom trades, would you be willing to consider a custom villager? I understand that this isn't in the original scope of your mod's functionality but I feel it would be a better solution (in terms of compatibility, which is the root of your problem here) than just budging other mods' trades to the side. There are handy methods in the VillagerRegistry for this too, including ones that set the skin and whatnot.

 

TL;DR - Poke through VillagerRegistry.class, the answers are therein!

 

EDIT: Also, 1.6.4?

 

Ill take a look at that.. Oh and as for the 1.6.4.. I have no intention of releasing a 1.6.4 version.. I have a 1.7.10 version ready which is the one i want to release.. The 1.6.4 is for my self on a server that runs 1.6.4..

 

I dont want to release the 1.6.4 version because in my opinion, staying at older versions just slows the hole progress of upgrading down for everyone :-)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

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