Jump to content

Stopping and Checking Sound


AlexInCube

Recommended Posts

I need to see if the sound I played is playing at the moment and I need to stop it somehow. I want when a player stops using an item, the sound is turned off. And when he's not holding the need item, it's off too.

 

Event:

Spoiler

 

@SubscribeEvent public static void checkPortalInMainHand(final LivingEvent.LivingUpdateEvent event) { if(event.getEntity() instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) event.getEntity(); ItemStack heldItem = player.getHeldItem(Hand.MAIN_HAND); ItemStack portal = new ItemStack(ModItems.PORTABLE_NETHER_PORTAL.get()); if (heldItem.getItem() == portal.getItem()){ System.out.print("Item in main hand"); player.playSound(SoundEvents.BLOCK_PORTAL_AMBIENT, 1, 1); } } }

 

Item:

Spoiler

public class portable_nether_portal extends Item{ public portable_nether_portal(){ super(new Properties() .maxStackSize(1) .group(ModItemGroups.MOD_ITEM_GROUP)); } @Override public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) { ItemStack itemstack = playerIn.getHeldItem(handIn); playerIn.setActiveHand(handIn);//trigger item use playerIn.playSound(SoundEvents.BLOCK_PORTAL_TRIGGER, 1, 1); return ActionResult.resultPass(itemstack); } @Override public int getUseDuration(ItemStack stack) { return 80; } @Override public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) { if (!worldIn.isRemote) { if (entityLiving.world.dimension.isNether()) { //playerIn.setPortal(playerIn.getPosition()); entityLiving.changeDimension(DimensionType.OVERWORLD); }else{ //playerIn.setPortal(playerIn.getPosition()); entityLiving.changeDimension(DimensionType.THE_NETHER); } } return stack; } @Override public void onPlayerStoppedUsing(ItemStack stack, World worldIn, LivingEntity entityLiving, int timeLeft) { } @Override public void onUsingTick(ItemStack stack, LivingEntity player, int count) { System.out.print("using?"); } @Override public UseAction getUseAction(ItemStack stack) { return UseAction.BOW; } }

 

Link to comment
Share on other sites

You will need to make your own Sound class object so that you can control when it is playing. Ambient sounds are controlled by vanilla code in other ways.

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.

Link to comment
Share on other sites

I never got around to doing it with 1.14+, but if you wanted to check out the sounds I did for 1.12:
https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/ores/client/SoundWindmill.java

It was a block positioned sound, but I would probably handle an item similarly.

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.

Link to comment
Share on other sites

I went through the playsound and stopsound commands, found SPlaySoundPacket and SStopSoundPacket there.
I tried to use them

new SPlaySoundPacket(new ResourceLocation("block.portal.trigger"), SoundCategory.BLOCKS,playerIn.getPositionVec(),1,1);
new SStopSoundPacket(new ResourceLocation("block.portal.ambient"),SoundCategory.BLOCKS);
but it didn't work.

Link to comment
Share on other sites

3 hours ago, AlexInCube said:

I went through the playsound and stopsound commands, found SPlaySoundPacket and SStopSoundPacket there.
I tried to use them

new SPlaySoundPacket(new ResourceLocation("block.portal.trigger"), SoundCategory.BLOCKS,playerIn.getPositionVec(),1,1);
new SStopSoundPacket(new ResourceLocation("block.portal.ambient"),SoundCategory.BLOCKS);
but it didn't work.

Probably because that is not how packets work. Packets are for sending information over a network. IE from a server to client. Draco has already given you what you need to do. Take a look at his example and try to implement your own version for your idea.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

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.