Jump to content

[SOLVED][1.11]Custom Sound Played to a Single Player on the right click of an Item


Recommended Posts

Posted (edited)

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 by Ocraftyone
Posted

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.

Posted (edited)

https://www.tutorialspoint.com/java/java_inheritance.htm

Edited 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.

Posted

Thank you!!!! Solved the problem with this code:

worldIn.playSound((EntityPlayer)null, playerIn.posX, playerIn.posY, playerIn.posZ, ModSoundHandler.DOORBELL, SoundCategory.PLAYERS, 1, 1);

 

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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.