Jump to content

  • Curse Sites
Become a Premium Member! Help
Latest News Article

Information on adding a new villager? [FORGE]


  • Please log in to reply
8 replies to this topic

#1

Mark719
    Mark719

    Lapis Lazuli Collector

  • Members
  • 1071 posts
  • Minecraft: Eurkes

Posted 12 February 2013 - 11:16 PM

Hey was just wondering have you any information on adding a new villager to Minecraft, similar to what is on Thaumcraft where he only sales a specific set of items. So if there any tutorials or some information I can read to help me along with the process.

Thank you.

Posted Image

I take in all suggestions and comments on my mods, even if I don't reply I have logged it somewhere. :)


Register or log in to remove.

#2

dmillerw
    dmillerw

    Nether Resident

  • Curse Premium
  • Curse Premium
  • 2160 posts
  • Minecraft: dmillerw

Posted 13 February 2013 - 02:01 PM

FML comes with a bunch of code for manipulating villages and villagers. Look at VillagerRegistry.

#3

Crumpetxxix
    Crumpetxxix

    Lapis Lazuli Collector

  • Members
  • 1111 posts
  • Location: Where it is obnoxiously hot.
  • Minecraft: Crump3txxix

Posted 13 February 2013 - 03:26 PM

Adding a new villager is pretty easy.. as long as your not making them do anythign crazy. I actually made my own over the past couple days, i dont use forge however but this is what i did. (The reason why mine took a couple days is because i made him an archer so there was a lot more modding that i had to do rather than just making a new guy to buy stuff from)

In the EntityVillager file theres a few fields you need to change.  To start off in the function "public String getTexture()" you need to add a new case for your new villager type so it can find the texture for your villager, remember that case number bcuz thats the number that will your new villagers proffession number throughout the rest of the code.
Next in the function "private void addDefaultEquipmentAndRecipies(int par1)".  This is where you will set what your new villager sells, go down to where the switch statement starts "switch (this.getProfession())"  and using the same number you did in getTexture() add a new case for yours and add the merchant items like it does above (i havent quite looked at exactly how the addMerchantItem and addBlacksmithItem functions work but i think it explains them in a comment above the actual function within the code.. and its pretty self explanitory if you just read over the code.  At the very bottom of the file there is also a villager stock list / blacksmith selling list, you also have to put the items you used down here (if theyre not already there) to work in conjunction with the addDefaultEquipmentAndRecipes, addMerchantItem, and addBlacksmithItem.
Finally in the "public void initCreature()" function you should just see one line "this.setProffession(this.worldObj.rand.nextInt(5));  This function sets a random proffession when a new villager is spawned.  Just to be clear the rand.nextInt(5) means its picking a random number between 0-4, not 0-5 or 1-5.  So if you made your new villager proffession to be number 5 then you would need the number 6 in rand.nextInt(6)
If you wanna take a look at my LP series, check it out on my channel at http://www.youtube.c...ser/Crump3txxix.  If not, my journal (which the series is based on) is here http://www.minecraft...ournal-for-172/

#4

Bobiking6
  • Minecraft: Bobiking6

Posted 17 February 2013 - 01:48 PM

View PostCrumpetxxix, on 13 February 2013 - 03:26 PM, said:

Adding a new villager is pretty easy.. as long as your not making them do anythign crazy. I actually made my own over the past couple days, i dont use forge however but this is what i did. (The reason why mine took a couple days is because i made him an archer so there was a lot more modding that i had to do rather than just making a new guy to buy stuff from)

In the EntityVillager file theres a few fields you need to change.  To start off in the function "public String getTexture()" you need to add a new case for your new villager type so it can find the texture for your villager, remember that case number bcuz thats the number that will your new villagers proffession number throughout the rest of the code.
Next in the function "private void addDefaultEquipmentAndRecipies(int par1)".  This is where you will set what your new villager sells, go down to where the switch statement starts "switch (this.getProfession())"  and using the same number you did in getTexture() add a new case for yours and add the merchant items like it does above (i havent quite looked at exactly how the addMerchantItem and addBlacksmithItem functions work but i think it explains them in a comment above the actual function within the code.. and its pretty self explanitory if you just read over the code.  At the very bottom of the file there is also a villager stock list / blacksmith selling list, you also have to put the items you used down here (if theyre not already there) to work in conjunction with the addDefaultEquipmentAndRecipes, addMerchantItem, and addBlacksmithItem.
Finally in the "public void initCreature()" function you should just see one line "this.setProffession(this.worldObj.rand.nextInt(5));  This function sets a random proffession when a new villager is spawned.  Just to be clear the rand.nextInt(5) means its picking a random number between 0-4, not 0-5 or 1-5.  So if you made your new villager proffession to be number 5 then you would need the number 6 in rand.nextInt(6)

I still dont get it, what to add to the addmerchantitem and addblacksmithitem cases?
I mean, does blacksmithitem mean selling? and merchant mean buying?
Posted Image
Tameable Chickens Mod - http://www.minecraft...__fromsearch__1

#5

Bobiking6
  • Minecraft: Bobiking6

Posted 17 February 2013 - 02:02 PM

View PostCrumpetxxix, on 13 February 2013 - 03:26 PM, said:

Adding a new villager is pretty easy.. as long as your not making them do anythign crazy. I actually made my own over the past couple days, i dont use forge however but this is what i did. (The reason why mine took a couple days is because i made him an archer so there was a lot more modding that i had to do rather than just making a new guy to buy stuff from)

In the EntityVillager file theres a few fields you need to change.  To start off in the function "public String getTexture()" you need to add a new case for your new villager type so it can find the texture for your villager, remember that case number bcuz thats the number that will your new villagers proffession number throughout the rest of the code.
Next in the function "private void addDefaultEquipmentAndRecipies(int par1)".  This is where you will set what your new villager sells, go down to where the switch statement starts "switch (this.getProfession())"  and using the same number you did in getTexture() add a new case for yours and add the merchant items like it does above (i havent quite looked at exactly how the addMerchantItem and addBlacksmithItem functions work but i think it explains them in a comment above the actual function within the code.. and its pretty self explanitory if you just read over the code.  At the very bottom of the file there is also a villager stock list / blacksmith selling list, you also have to put the items you used down here (if theyre not already there) to work in conjunction with the addDefaultEquipmentAndRecipes, addMerchantItem, and addBlacksmithItem.
Finally in the "public void initCreature()" function you should just see one line "this.setProffession(this.worldObj.rand.nextInt(5));  This function sets a random proffession when a new villager is spawned.  Just to be clear the rand.nextInt(5) means its picking a random number between 0-4, not 0-5 or 1-5.  So if you made your new villager proffession to be number 5 then you would need the number 6 in rand.nextInt(6)

Never mind! i found out blacksmith means buying and merchant means selling.
Posted Image
Tameable Chickens Mod - http://www.minecraft...__fromsearch__1

#6

Crumpetxxix
    Crumpetxxix

    Lapis Lazuli Collector

  • Members
  • 1111 posts
  • Location: Where it is obnoxiously hot.
  • Minecraft: Crump3txxix

Posted 17 February 2013 - 03:49 PM

Are you sure? In the code comments I could have sworn if you made the emerald price negative it meant it was buying, not selling.  It seemed to me the only difference when I glanced over that code is that blacksmith seemed to have a slightly random factor on the amount of items he would buy or sell for the emerald.  However I'm not at my PC to confirm this right now.

Anyways glad it worked for you
If you wanna take a look at my LP series, check it out on my channel at http://www.youtube.c...ser/Crump3txxix.  If not, my journal (which the series is based on) is here http://www.minecraft...ournal-for-172/

#7

Lucid_Star
  • Location: Portland, OR
  • Minecraft: Lucidstargames
  • Xbox:The Wise Baker

Posted 21 February 2013 - 03:40 AM

I've been searching around for a couple days now using this forum, forge and the general internet and no one seems to have any information on how to add villagers with a mod. Specifically on the purpose and use of the forge VillageRegistry class. It appears to be used for this exact thing but there's no simple information on how to actually use it in my mod. I know I could just alter the base files and easily add in the information but it sounds like it would conflict with other mods.
Or would it not? It just seems like it would be simpler to be able to add a line or a method or something to the main mod class registering the villagerid (case?) and type (reference name?) and texture (string /nameGFX/customvillager.png) and of course what it trades with blacksmith and merchant recipes.
Maybe I'm way off base but I thought I'd ask just in case I can get some helpful information.

#8

Lucid_Star
  • Location: Portland, OR
  • Minecraft: Lucidstargames
  • Xbox:The Wise Baker

Posted 22 February 2013 - 02:33 AM

Alright I found some info on the forge forum. By adding some code to the init method you can call the villager in. By adding a tradehandler class implementing the IVillageTradeHandler you can set a trade to the new type. So far it doesn't seem to spawn at default but I did get them to birth one.
The init code is:
VillagerRegistry.instance().registerVillagerType(villager id), "/yourmodGFX/newvillager.png"); //id must be greater than 6
newvillagerTradeHandler newTradeHandler = new newvillagerTradeHandler();
VillagerRegistry.instance().registerVillageTradeHandler(villager id, newTradeHandler);
VillagerRegistry.instance().getRegisteredVillagers(); \\not sure if this is required. But I didn't see a new one born until i added it

The newvillagerTradeHandler class should be:

package name.modname.common.core.handlers;

import java.util.Random;

import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.village.MerchantRecipe;
import net.minecraft.village.MerchantRecipeList;
import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;

public class newvillagerTradeHandler implements IVillageTradeHandler {

  public void manipulateTradesForVillager(EntityVillager villager, MerchantRecipeList recipes, Random random) {
     // Trade 4 emeralds and 3 apples for a diamond axe
     recipes.add(new MerchantRecipe(
        new ItemStack(Item.emerald, 4),
        new ItemStack(Item.appleRed, 3),
        new ItemStack(Item.axeDiamond, 1, 0)));  
}

Still working on how to add more recipes and give them random chance like the other villagers. But it all looks like its in villageregistry. Note that for every villager you will need a different tradehandler to give them different trades.

#9

MrOliverHsj
  • Location: Have you checked your closet?
  • Minecraft: oliv2560

Posted 05 June 2013 - 12:16 PM

View PostCrumpetxxix, on 13 February 2013 - 03:26 PM, said:

Adding a new villager is pretty easy.. as long as your not making them do anythign crazy. I actually made my own over the past couple days, i dont use forge however but this is what i did. (The reason why mine took a couple days is because i made him an archer so there was a lot more modding that i had to do rather than just making a new guy to buy stuff from)

In the EntityVillager file theres a few fields you need to change.  To start off in the function "public String getTexture()" you need to add a new case for your new villager type so it can find the texture for your villager, remember that case number bcuz thats the number that will your new villagers proffession number throughout the rest of the code.
Next in the function "private void addDefaultEquipmentAndRecipies(int par1)".  This is where you will set what your new villager sells, go down to where the switch statement starts "switch (this.getProfession())"  and using the same number you did in getTexture() add a new case for yours and add the merchant items like it does above (i havent quite looked at exactly how the addMerchantItem and addBlacksmithItem functions work but i think it explains them in a comment above the actual function within the code.. and its pretty self explanitory if you just read over the code.  At the very bottom of the file there is also a villager stock list / blacksmith selling list, you also have to put the items you used down here (if theyre not already there) to work in conjunction with the addDefaultEquipmentAndRecipes, addMerchantItem, and addBlacksmithItem.
Finally in the "public void initCreature()" function you should just see one line "this.setProffession(this.worldObj.rand.nextInt(5));  This function sets a random proffession when a new villager is spawned.  Just to be clear the rand.nextInt(5) means its picking a random number between 0-4, not 0-5 or 1-5.  So if you made your new villager proffession to be number 5 then you would need the number 6 in rand.nextInt(6)

Tryed you method and it worked :D Thanks, but the only problem is the villagers wont naturally spawn. They need a spawn egg. I dont know if im the only one with this problem. :D