Posted February 9, 20178 yr I would like my custom sound played to the player who right clicks this item. I have the sound set up and registered correctly because if I use /playsound, it works perfectly. Here is the code for the item that I want the sound to be played on when right-clicked: package ocraftyone.MoreEverything.items; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.SoundEvents; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumHand; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentString; import net.minecraft.world.World; import ocraftyone.MoreEverything.handlers.ModSoundHandler; import ocraftyone.MoreEverything.init.ModItemBasic; public class ItemHome extends ModItemBasic { public ItemHome(String name, CreativeTabs tab) { super(name, tab); this.setMaxStackSize(1); } @Override public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { if(stack.getTagCompound() == null) { stack.setTagCompound(new NBTTagCompound()); stack.getTagCompound().setBoolean("homeSet", false); } } @Override public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) { ItemStack itemStack = playerIn.getHeldItem(handIn); if(!worldIn.isRemote) { NBTTagCompound nbt = itemStack.getTagCompound(); if(playerIn.isSneaking()) { nbt.setInteger("xpos", playerIn.getPosition().getX()); nbt.setInteger("ypos", playerIn.getPosition().getY()); nbt.setInteger("zpos", playerIn.getPosition().getZ()); nbt.setInteger("dim", playerIn.dimension); nbt.setString("playerName", playerIn.getName()); nbt.setBoolean("homeSet", true); playerIn.sendMessage(new TextComponentString("Succesfully set home! Bound to " + playerIn.getName() + ".")); }else { if(nbt.getBoolean("homeSet") == true) { if(playerIn.getName() == nbt.getString("playerName")) { if(nbt.getInteger("dim") != playerIn.dimension) { playerIn.changeDimension(nbt.getInteger("dim")); playerIn.attemptTeleport(nbt.getInteger("xpos"), nbt.getInteger("ypos"), nbt.getInteger("zpos")); playerIn.playSound(ModSoundHandler.DOORBELL, 1.01F, 1.0F); } else { playerIn.attemptTeleport(nbt.getInteger("xpos"), nbt.getInteger("ypos"), nbt.getInteger("zpos")); playerIn.playSound(ModSoundHandler.DOORBELL, 1.0F, 1.0F); //worldIn.playSound(playerIn, playerIn.getPosition(), ModSoundHandler.DOORBELL, SoundCategory.PLAYERS, 1.0F, 1.0F); } }else { playerIn.sendMessage(new TextComponentString("You can not use another person's home item! (Owner: " + nbt.getString("playerName") + ")")); } }else { playerIn.sendMessage(new TextComponentString("Invalid home!")); } } } return super.onItemRightClick(worldIn, playerIn, handIn); } } I have also heard in another post to cast EntityPlayerSP, but I could not get that to work: What they wanted me to do: //Cast EntityPlayer to EntityPlayerSP EntityPlayerSP playerSP = (EntityPlayerSP) playerIn; //Play sound to EntityPlayerSP playerSP.playSound(ModSoundHandler.DOORBELL, 1, 1); //Log said that EntityPlayerMP could not be cast to EntityPlayerSP Edited February 11, 20178 yr by Ocraftyone
February 9, 20178 yr onItemRightClick is called on both sides, so no, you can't cast like that. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
February 9, 20178 yr Author then how should I solve this problem. i am just having a problem with playing the sound from the code
February 9, 20178 yr https://www.tutorialspoint.com/java/java_inheritance.htm Edited February 9, 20178 yr by Draco18s Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
February 10, 20178 yr Author i still dont get how to play a sound to just the person who clicked the item
February 11, 20178 yr Author Thank you!!!! Solved the problem with this code: worldIn.playSound((EntityPlayer)null, playerIn.posX, playerIn.posY, playerIn.posZ, ModSoundHandler.DOORBELL, SoundCategory.PLAYERS, 1, 1);
February 11, 20178 yr Author On 2/9/2017 at 4:01 PM, diesieben07 said: First of all: You cannot compare Strings like that. As for how to play sounds, there is an excellent documentation page on that, which you should read. I am able to compare these strings. It works 100% in all testing
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.