The Meaning of Life, the Universe, and Everything.
Location:
California
Join Date:
6/21/2013
Posts:
45
Location:
California, USA
Minecraft:
Creepinson101
Xbox:
Creepinson101
Member Details
I am trying to detect if the player right-clicks my item, a syringe in this case, on a zombie. When this happens, it gives the player my zombie blood syringe and deletes to old syringe. If It is anywhere else when right-clicked, it will do the same, except give the player a player blood syringe. But it just seems to do the same thing no matter what I click on or where... Give the player a player blood syringe. I don't know what I'm doing wrong!
public class Syringe extends ModItems{
private Minecraft mc = Minecraft.getMinecraft();
public Syringe(String name, CreativeTabs tab) {
super(name, tab);
}
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack item, World world, EntityPlayer user, EnumHand hand)
It's probably not working because you're passing 30 as the partialTicks argument of Entity#rayTrace, but this is meant to be a value in the range [0.0 1.0].
You can't reference client-only classes like Minecraft or call client-only methods like Entity#rayTrace from common code (e.g. an Item class); otherwise you'll crash the dedicated server.
Delete the mc field and use ForgeHooks.rayTraceEyes or World#rayTraceBlocks instead of Entity#rayTrace.
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 forgot that World#rayTraceBlocks (used by Entity#rayTrace and ForgeHooks.rayTraceEyes) doesn't include entities, so RayTraceResult#entityHit will never be a non-null value in the RayTraceResult returned by it.
Instead, override Item#itemInteractionForEntity to do something when your item is right clicked on an entity and override Item#onItemRightClick to do something when your item is right clicked on air.
In future, please use Gist or Pastebin to post logs/crash reports (if applicable) and code with syntax highlighting. To get syntax highlighting on Gist, give each file the appropriate extension (.java for Java code). To get syntax highlighting on Pastebin, select the language from the dropdown at the bottom of the page.
It's much easier to read code with proper formatting and syntax highlighting.
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.
I am trying to detect if the player right-clicks my item, a syringe in this case, on a zombie. When this happens, it gives the player my zombie blood syringe and deletes to old syringe. If It is anywhere else when right-clicked, it will do the same, except give the player a player blood syringe. But it just seems to do the same thing no matter what I click on or where... Give the player a player blood syringe. I don't know what I'm doing wrong!
Here is my code for the syringe:
package me.creepinson.item;
import me.creepinson.handlers.ItemHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.EntityInteract;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class Syringe extends ModItems{
private Minecraft mc = Minecraft.getMinecraft();
public Syringe(String name, CreativeTabs tab) {
super(name, tab);
}
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack item, World world, EntityPlayer user, EnumHand hand)
{
RayTraceResult raytrace = user.rayTrace(10, 30);
if(raytrace.entityHit instanceof EntityZombie){
user.inventory.deleteStack(item);
user.inventory.addItemStackToInventory(new ItemStack(ItemHandler.Syringe_Full_Zombie, 1));
new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
}else{
user.inventory.deleteStack(item);
user.inventory.addItemStackToInventory(new ItemStack(ItemHandler.Syringe_Full_Player, 1));
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
}
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
}
}
It's probably not working because you're passing 30 as the partialTicks argument of Entity#rayTrace, but this is meant to be a value in the range [0.0 1.0].
You can't reference client-only classes like Minecraft or call client-only methods like Entity#rayTrace from common code (e.g. an Item class); otherwise you'll crash the dedicated server.
Delete the mc field and use ForgeHooks.rayTraceEyes or World#rayTraceBlocks instead of Entity#rayTrace.
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.
It still isn't doing anything. Wierd. I am using
package me.creepinson.item;
import me.creepinson.handlers.ItemHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.EntityInteract;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class Syringe extends ModItems{
public Syringe(String name, CreativeTabs tab) {
super(name, tab);
}
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack item, World world, EntityPlayer user, EnumHand hand)
{
RayTraceResult raytrace = ForgeHooks.rayTraceEyes(user, 5);
if(raytrace.entityHit instanceof EntityZombie){
user.inventory.deleteStack(item);
user.inventory.addItemStackToInventory(new ItemStack(ItemHandler.Syringe_Full_Zombie, 1));
new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
}else{
user.inventory.deleteStack(item);
user.inventory.addItemStackToInventory(new ItemStack(ItemHandler.Syringe_Full_Player, 1));
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
}
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
}
}
I forgot that World#rayTraceBlocks (used by Entity#rayTrace and ForgeHooks.rayTraceEyes) doesn't include entities, so RayTraceResult#entityHit will never be a non-null value in the RayTraceResult returned by it.
Instead, override Item#itemInteractionForEntity to do something when your item is right clicked on an entity and override Item#onItemRightClick to do something when your item is right clicked on air.
In future, please use Gist or Pastebin to post logs/crash reports (if applicable) and code with syntax highlighting. To get syntax highlighting on Gist, give each file the appropriate extension (.java for Java code). To get syntax highlighting on Pastebin, select the language from the dropdown at the bottom of the page.
It's much easier to read code with proper formatting and syntax highlighting.
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.