I'm working on trying to make the player able to milk sheep using the vanilla bucket. I figured it would need to be done using the onEntityInteractEvent in an EventHandler. What method would I need to use inside of this to achieve this effect?
My SheepMilkEvent.java:
package net.sargeant.rapad.eventhandler;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.EntityInteractEvent;
import net.sargeant.rapad.items.ModItems;
public class SheepMilkEvent
{
public EntityPlayer player;
public void onEntityActivated(EntityInteractEvent event)
{
if (event.entityLiving instanceof EntitySheep)
{
}
}
}
Look at EntityCow#interact to see how vanilla handles milking cows. You should be able to use the same logic, though I'd recommend creating the milk bucket ItemStack once, storing it in a local variable and then passing it to InventoryPlayer#setInventorySlotContents, InventoryPlayer#addItemStackToInventory and EntityPlayer#dropPlayerItemWithRandomChoice instead of passing it directly to the methods.
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.
Look at EntityCow#interact to see how vanilla handles milking cows. You should be able to use the same logic, though I'd recommend creating the milk bucket ItemStack once, storing it in a local variable and then passing it to InventoryPlayer#setInventorySlotContents, InventoryPlayer#addItemStackToInventory and EntityPlayer#dropPlayerItemWithRandomChoice instead of passing it directly to the methods.
Okay, I set it to my mod's item called sheepmilk. The only problem now is the return at the end. EntityCow has an else return super.interact(EntityPlayer); statement. How can I add a return statement that mimics this?
package net.sargeant.rapad.eventhandler;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.EntityInteractEvent;
import net.sargeant.rapad.items.ModItems;
public class SheepMilkEvent
{
public EntityPlayer player;
public EntityAnimal animal;
public void onEntityActivated(EntityInteractEvent event)
{
if (event.entityLiving instanceof EntitySheep)
{
this.interact(player);
}
}
public boolean interact(EntityPlayer player)
{
ItemStack itemstack = player.inventory.getCurrentItem();
if (itemstack != null && itemstack.getItem() == Items.bucket && !player.capabilities.isCreativeMode)
{
if (itemstack.stackSize-- == 1)
{
player.inventory.setInventorySlotContents(player.inventory.currentItem, new ItemStack(ModItems.sheepmilk));
}
else if (!player.inventory.addItemStackToInventory(new ItemStack(ModItems.sheepmilk)))
{
player.dropPlayerItemWithRandomChoice(new ItemStack(ModItems.sheepmilk, 1, 0), false);
}
return true;
}
else
{
return animal.interact(player);
}
}
}
@Cancelable
public class EntityInteractEvent extends PlayerEvent
{
public final Entity target;
public EntityInteractEvent(EntityPlayer player, Entity target)
{
super(player);
this.target = target;
}
}
Do you see what it does? Every time the player right-clicks on an entity, this method is called. Note that it would be used like so:
public class EventHandler
{
@ForgeSubscribe
public void onEntityInteract(EntityInteractEvent event)
{
if (event.entity instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) event.entity;
if (event.target != null && event.target instanceof EntityCow && player.getHeldItem() != null && player.getHeldItem().itemID == YourMod.yourBucket.itemID)
{
// fill your bucket with milk
}
}
}
}
CoolAlias, how would this work in 1.7.10 for the vanilla bucket on sheep?
You can't just create fields and expect them to have proper values, you need to extract the values from the event object you receive as an argument of the handler method. You shouldn't store values in fields of your event handler classes either (unless it's something like a Random object where you only need one instance that never changes), each handler method should be self-contained.
EntityInteractEvent extends PlayerEvent, so the entityPlayer field contains the player and the target field contains the entity that the player interacted with.
You don't need the boolean return value here, that's just used by EntityPlayer#interactWith to determine whether to call ItemStack#interactWithEntity for the player's held item.
Rollback Post to RevisionRollBack
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.
You can't just create fields and expect them to have proper values, you need to extract the values from the event object you receive as an argument of the handler method. You shouldn't store values in fields of your event handler classes either (unless it's something like a Random object where you only need one instance that never changes), each handler method should be self-contained.
EntityInteractEvent extends PlayerEvent, so the entityPlayer field contains the player and the target field contains the entity that the player interacted with.
You don't need the boolean return value here, that's just used by EntityPlayer#interactWith to determine whether to call ItemStack#interactWithEntity for the player's held item.
I found code that works:
package net.sargeant.rapad.eventhandler;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.EntityInteractEvent;
import net.sargeant.rapad.items.ModItems;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class SheepMilkEvent
{
@SubscribeEvent
public void onEntityInteract(EntityInteractEvent event)
{
if (event.entity instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) event.entity;
ItemStack itemstack = player.inventory.getCurrentItem();
if (event.target != null && event.target instanceof EntitySheep && itemstack != null && itemstack.getItem() == Items.bucket && !player.capabilities.isCreativeMode)
{
if (player.getHeldItem().stackSize-- == 1)
{
player.inventory.setInventorySlotContents(player.inventory.currentItem, new ItemStack(ModItems.sheepmilk));
}
}
}
}
}
Rollback Post to RevisionRollBack
I just took the Minecraft Noob test! Check out what I scored. Think you can beat me?!
I'm working on trying to make the player able to milk sheep using the vanilla bucket. I figured it would need to be done using the onEntityInteractEvent in an EventHandler. What method would I need to use inside of this to achieve this effect?
My SheepMilkEvent.java:
I just took the Minecraft Noob test! Check out what I scored. Think you can beat me?!
To take the test, check out
https://minecraftnoobtest.com/test.php
Don't click this link, HE is haunting it...
Look at EntityCow#interact to see how vanilla handles milking cows. You should be able to use the same logic, though I'd recommend creating the milk bucket ItemStack once, storing it in a local variable and then passing it to InventoryPlayer#setInventorySlotContents, InventoryPlayer#addItemStackToInventory and EntityPlayer#dropPlayerItemWithRandomChoice instead of passing it directly to the methods.
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.
Okay, I set it to my mod's item called sheepmilk. The only problem now is the return at the end. EntityCow has an else return super.interact(EntityPlayer); statement. How can I add a return statement that mimics this?
CoolAlias, how would this work in 1.7.10 for the vanilla bucket on sheep?
Edit: Nvm, got it to work.
I just took the Minecraft Noob test! Check out what I scored. Think you can beat me?!
To take the test, check out
https://minecraftnoobtest.com/test.php
Don't click this link, HE is haunting it...
You can't just create fields and expect them to have proper values, you need to extract the values from the event object you receive as an argument of the handler method. You shouldn't store values in fields of your event handler classes either (unless it's something like a Random object where you only need one instance that never changes), each handler method should be self-contained.
EntityInteractEvent extends PlayerEvent, so the entityPlayer field contains the player and the target field contains the entity that the player interacted with.
You don't need the boolean return value here, that's just used by EntityPlayer#interactWith to determine whether to call ItemStack#interactWithEntity for the player's held item.
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.
I found code that works:
I just took the Minecraft Noob test! Check out what I scored. Think you can beat me?!
To take the test, check out
https://minecraftnoobtest.com/test.php
Don't click this link, HE is haunting it...
Add an "else" to make sure that using a stack of buckets still gets milk, ie,
Got this code from the vanilla bucket onItemRightClick method
Got it in.
I just took the Minecraft Noob test! Check out what I scored. Think you can beat me?!
To take the test, check out
https://minecraftnoobtest.com/test.php
Don't click this link, HE is haunting it...
What do you mean by sheep milk? Is it real or available on the minecraft pc version or the minecraft PE?
It's specifically for a mod I'm working on with sargeant987 called RAPAD. Search for it, it's amazing.
I just took the Minecraft Noob test! Check out what I scored. Think you can beat me?!
To take the test, check out
https://minecraftnoobtest.com/test.php
Don't click this link, HE is haunting it...