Jump to content

Help With Villagers


newrealmcoder

Recommended Posts

Add this into your mod's @init method:

 

 VillagerRegistry.instance().registerVillagerType(villagerNumberID, "path/to/villager/texture.png");

 

villagerNumberID has to be greater than 6 (as all ids below that are already taken), and it can go quite high so it's a good idea to start at 50 or something.

 

Adding trades to this villager is a bit more difficult as Forge's trade adding system is a bit broken at the moment. If I find a quick way to do it i'll let ya know.

Link to comment
Share on other sites

Here's some code I've successfully used to add trades to villagers.

// Called during init
void registerTradeHandlers() {
	VillagerRegistry reg = VillagerRegistry.instance();
	SGTradeHandler handler = new SGTradeHandler();
	reg.registerVillageTradeHandler(tokraVillagerID, handler);
}

// Trade handler class
public class SGTradeHandler implements IVillageTradeHandler {

public void manipulateTradesForVillager(EntityVillager villager, MerchantRecipeList recipes, Random random) {
      // Trade 1 emerald and 3 naquadah ingots for 1 stargate ring block
	recipes.add(new MerchantRecipe(
		new ItemStack(Item.emerald, 4),
		new ItemStack(SGCraft.naquadahIngot, 3),
		new ItemStack(SGCraft.sgRingBlock, 1, 0)));
    }

}

Link to comment
Share on other sites

Thank you so much. ;D

 

And also if anyone is interested after testing around with this if you use a switch for the villager's profession in the Village Handler and put your trades within that under the villagers's ID it will set the trades to specific types of villagers. i.e.

 

@Override

public void manipulateTradesForVillager(EntityVillager villager, MerchantRecipeList recipeList, Random random)

{

switch(villager.getProfession())

{

case 59:

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond), (new ItemStack(Item.emerald, 2)), (new ItemStack(Item.pickaxeDiamond))));

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond), new ItemStack(Item.emerald), new ItemStack(Item.swordDiamond)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond), new ItemStack(Item.emerald,2), new ItemStack(Item.axeDiamond)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond), new ItemStack(Item.emerald,2), new ItemStack(Item.shovelDiamond)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond), new ItemStack(Item.emerald,2), new ItemStack(Item.hoeDiamond)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond,2), new ItemStack(Item.emerald,2), new ItemStack(Item.bootsDiamond)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond,4), new ItemStack(Item.emerald,4), new ItemStack(Item.plateDiamond)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond,2), new ItemStack(Item.emerald,3), new ItemStack(Item.helmetDiamond)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.diamond,3), new ItemStack(Item.emerald,4), new ItemStack(Item.legsDiamond)));

break;

case 60:

recipeList.add(new MerchantRecipe(new ItemStack(Item.carrot),new ItemStack(Item.bread)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.ingotIron), new ItemStack(Item.bread, 5)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.ingotGold),new ItemStack(Item.appleRed, 64)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.potato, 9), new ItemStack(Item.coal), new ItemStack(Item.bakedPotato, 8)));

recipeList.add(new MerchantRecipe(new ItemStack(Item.emerald),new ItemStack(Item.appleGold)));

break;

 

}

 

}

Link to comment
Share on other sites

  • 2 weeks later...

Well to get your villager to spawn, first of all, you'll need some sort of village component to spawn them in. Once you've got that all done, add this line somewhere in the addComponentParts method:

 

this.spawnVillagers(world, box, 3, 16, 3, 1);

 

world is the method's World param, box is the method's StructureBoundingBox param.

The next three integers are the position to spawn the villager, and the last integer is the amount.

 

Add this method somewhere else in your Component class.

 

    protected int getVillagerType(int par1)
    {
        return 0;
    }

 

replace 0 with whatever you set your villager's ID to.

 

Now when a village generates with that component in it, one of your villagers will spawn too :)

Link to comment
Share on other sites

  • 11 months later...

I'd just like to verify that this does indeed work in 1.7.2, besides the fact that the villager ID and skin need to be declared separately, like so.

VillagerRegistry.instance().registerVillagerId(villagerId);
VillagerRegistry.instance().registerVillagerSkin(villagerId, new ResourceLocation("modid:yourdirectory/villagerSkin.png"));

 

The resource location of "modid:" is in src/main/resources/assets/modid with 'modid' being whatever the id of your mod is.

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.