I'm wondering how to go about altering the drops of a sheep when they are sheared, so they drop a custom item instead (i.e. fleece).
Doesn't matter if the drop ignores the sheep's wool colour (dyeing sheep is a bit silly anyway).
However, unlike standard drops which are handled by the LivingDropsEvent, the shearing drops work differently.
My understanding is:
The drop data is within the sheep class itself; the onSheared function determines what should be dropped when the sheep is sheared (within ItemStack, I think). However this is not an Event, so obviously I can't subscribe to it, but I'm not sure how to override sheep's drops without altering the sheep's base class (which I want to avoid).
Altering the shears doesn't seem to achieve much either (itemInteractionForEntity), as they simply spawn the drops in the world based on the entity's onSheared drop data.
My understanding may be wrong - feel free to correct me - I'm fairly new to modding and in the process of learning Java.
One possible solution is making my own sheep class which extends vanilla, and overriding its drop data, but then I'm not sure about how to go about replacing all vanilla sheep with my modded sheep.
Another solution is replacing the wool for fleece when the player picks it up (but once again, I'm not sure how).
Neither solution is particularly elegant, so if anyone can provide a simpler solution, I would greatly appreciate it.
Thanks!
Rollback Post to RevisionRollBack
Github now up for my WIP mod - CalebRatedCore, the core mod of a modpack I'll be making.
As far as I can see, the shearing is handled in the shear's item (ItemShears class) and then it calls a method in the sheep entity's class (EntitySheep class). So you could replace the vanilla shears with your own item which changes how the drops work. I think it's easier than trying to replace an entity.
Thanks for the tip Bright_Spark! With your advice, I've set up my own modded shears, and I've replaced the entity handling code within my custom shears Class (changes in bold, the rest is part of a vanilla shears class).
//Checks if the entity can be sheared. Normally sheep. If so, item is dropped.
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, net.minecraft.entity.player.EntityPlayer player, EntityLivingBase entity, net.minecraft.util.EnumHand hand)
{
if (entity.worldObj.isRemote)
{
return false;
}
if (entity instanceof net.minecraftforge.common.IShearable)
{
net.minecraftforge.common.IShearable target = (net.minecraftforge.common.IShearable)entity;
BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);
if (target.isShearable(itemstack, entity.worldObj, pos))
{
java.util.List<ItemStack> drops = target.onSheared(itemstack, entity.worldObj, pos,
net.minecraft.enchantment.EnchantmentHelper.getEnchantmentLevel(net.minecraft.init.Enchantments.FORTUNE, itemstack));
Works like a charm. Not 100% sure if it will play nice with other modded shearable entities (i.e. Shear Madness sheep), although I do have that if statement there to try and prevent mod conflict. The if statement could be replaced with a case to check for specific coloured wools and drop accordingly, if need be.
The only concern I have with my code is that I used .getDisplayName() as my means of extracting the item data from the ItemStack, which I imagine is not the correct way. If someone can offer a more correct solution, then by all means. Otherwise, this works fine for my purposes, and I'm leaving it at that.
Rollback Post to RevisionRollBack
Github now up for my WIP mod - CalebRatedCore, the core mod of a modpack I'll be making.
The only concern I have with my code is that I used .getDisplayName() as my means of extracting the item data from the ItemStack, which I imagine is not the correct way. If someone can offer a more correct solution, then by all means. Otherwise, this works fine for my purposes, and I'm leaving it at that.
Use ItemStack#getItem to get the ItemStack's Item, then use the equality operator (==) to check if it's equal to the desired Item. Since you're checking for a block rather than an item, use Item.getItemFromBlock to get the Item form of the Block.
The vanilla Blocks are stored in the Blocks class.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
Hi all,
I'm wondering how to go about altering the drops of a sheep when they are sheared, so they drop a custom item instead (i.e. fleece).
Doesn't matter if the drop ignores the sheep's wool colour (dyeing sheep is a bit silly anyway).
However, unlike standard drops which are handled by the LivingDropsEvent, the shearing drops work differently.
My understanding is:
The drop data is within the sheep class itself; the onSheared function determines what should be dropped when the sheep is sheared (within ItemStack, I think). However this is not an Event, so obviously I can't subscribe to it, but I'm not sure how to override sheep's drops without altering the sheep's base class (which I want to avoid).
Altering the shears doesn't seem to achieve much either (itemInteractionForEntity), as they simply spawn the drops in the world based on the entity's onSheared drop data.
My understanding may be wrong - feel free to correct me - I'm fairly new to modding and in the process of learning Java.
One possible solution is making my own sheep class which extends vanilla, and overriding its drop data, but then I'm not sure about how to go about replacing all vanilla sheep with my modded sheep.
Another solution is replacing the wool for fleece when the player picks it up (but once again, I'm not sure how).
Neither solution is particularly elegant, so if anyone can provide a simpler solution, I would greatly appreciate it.
Thanks!
Github now up for my WIP mod - CalebRatedCore, the core mod of a modpack I'll be making.
Not much at present, but will expand with time.
https://github.com/CalebR-300/CalebRatedCore
As far as I can see, the shearing is handled in the shear's item (ItemShears class) and then it calls a method in the sheep entity's class (EntitySheep class). So you could replace the vanilla shears with your own item which changes how the drops work. I think it's easier than trying to replace an entity.
Thanks for the tip Bright_Spark! With your advice, I've set up my own modded shears, and I've replaced the entity handling code within my custom shears Class (changes in bold, the rest is part of a vanilla shears class).
//Checks if the entity can be sheared. Normally sheep. If so, item is dropped.
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, net.minecraft.entity.player.EntityPlayer player, EntityLivingBase entity, net.minecraft.util.EnumHand hand)
{
if (entity.worldObj.isRemote)
{
return false;
}
if (entity instanceof net.minecraftforge.common.IShearable)
{
net.minecraftforge.common.IShearable target = (net.minecraftforge.common.IShearable)entity;
BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);
if (target.isShearable(itemstack, entity.worldObj, pos))
{
java.util.List<ItemStack> drops = target.onSheared(itemstack, entity.worldObj, pos,
net.minecraft.enchantment.EnchantmentHelper.getEnchantmentLevel(net.minecraft.init.Enchantments.FORTUNE, itemstack));
java.util.Random rand = new java.util.Random();
for(ItemStack stack : drops)
{
if(stack.getDisplayName().contains("Wool")){
net.minecraft.entity.item.EntityItem ent = entity.dropItem(calebR.tutorial.init.ModItems.itemFleece, 1);
ent.motionY += rand.nextFloat() * 0.05F;
ent.motionX += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
ent.motionZ += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
}else{
net.minecraft.entity.item.EntityItem ent = entity.entityDropItem(stack, 1.0F);
ent.motionY += rand.nextFloat() * 0.05F;
ent.motionX += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
ent.motionZ += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
}
System.out.println(stack.getDisplayName());
}
itemstack.damageItem(1, entity);
}
return true;
}
return false;
}
Works like a charm. Not 100% sure if it will play nice with other modded shearable entities (i.e. Shear Madness sheep), although I do have that if statement there to try and prevent mod conflict. The if statement could be replaced with a case to check for specific coloured wools and drop accordingly, if need be.
The only concern I have with my code is that I used .getDisplayName() as my means of extracting the item data from the ItemStack, which I imagine is not the correct way. If someone can offer a more correct solution, then by all means. Otherwise, this works fine for my purposes, and I'm leaving it at that.
Github now up for my WIP mod - CalebRatedCore, the core mod of a modpack I'll be making.
Not much at present, but will expand with time.
https://github.com/CalebR-300/CalebRatedCore
Use ItemStack#getItem to get the ItemStack's Item, then use the equality operator (==) to check if it's equal to the desired Item. Since you're checking for a block rather than an item, use Item.getItemFromBlock to get the Item form of the Block.
The vanilla Blocks are stored in the Blocks class.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
Cheers Choonster - little explanations like that make a big difference to newbies like me. Patched the code accordingly.
For reference, the if statement condition would look like:
Thanks for the help guys.
BTW, I've set up a github if anyone wants to check out the code in detail. Link in sig.
Github now up for my WIP mod - CalebRatedCore, the core mod of a modpack I'll be making.
Not much at present, but will expand with time.
https://github.com/CalebR-300/CalebRatedCore