Jump to content

Recommended Posts

Posted

I've been looking all over in vanilla code for how records are handled, and I'm stumped on 2 things:

 

1. Where do the records actually START playing in vanilla code?

2. How can I stop the record track once it starts without terminating all sounds?

 

So far, I've looked through BlockJukebox and its nested TileEntity, ItemRecord, the World playRecord and playAuxSFX methods, and the Client and Server NetPlayHandlers for S24PacketBlockAction and S28PacketEffect, thinking that there might have been some 'special cases' tucked away in the packet handling, but nope.

 

When ItemRecord is inserted, it plays auxiliary sound effect #1005 with the item id and sets the itemstack in the tile entity. When the record is ejected from the BlockJukebox, it plays aux #1005, with 0 as the 'id' and calls playRecord(null, x, y, z). All of that happens only on the server side, btw.

 

I suspect that the auxiliary sound effect is what's playing the record track, since nothing in BlockJukebox appears to play the record, but I can't find where aux SFX actually get handled. Not in World, WorldClient, WorldServer, packets...

 

Now, I'm playing the record on the client side for now from a Gui (I know how to broadcast via packets if I need), and I can play the record using mc.theWorld.playRecord, BUT I can't get it to stop!

 

For the x/y/z coordinates, I tried using MathHelper.floor_double of mc.thePlayer's position, as well as passing in the x/y/z from the GuiHandler when the gui is opened (which are also from the player's position). I use the same coordinates for playing the song as I do for attempting to stop it.

@Override
public void onGuiClosed() {
if (song != null) { // && ticksSinceLastNote < song.getDuration()
	LogHelper.info("Gui closed, stopping song"); // prints
	mc.theWorld.playAuxSFX(1005, x, y, z, 0); // no effect, but this is what BlockJukebox does when the disc is ejected
	mc.theWorld.playRecord((String) null, x, y, z); // no effect, but this is what BlockJukebox does when the disc is ejected
}
}

I have also sent a packet to the server making the exact same method calls (obviously NOT with mc.theWorld), but to no avail.

 

It looked so obvious when I first wrote the code, but it doesn't work at all. The record just keeps on playing. Does anyone know how the vanilla code actually works in this case?

Posted

This was very easy to find out. Learn to use your IDE ;)

When I searched for references, RenderGlobal#playAuxSFX never showed up, neither from World nor IWorldAccess :\

 

Nevertheless, you are right - I am not very good with my IDE. Something to work on xD

 

EDIT: Derp... opened up the type hierarchy and voilá. Lol.

 

However, I'm still confused: calling WorldClient#playRecord iterates through all the IWorldAccesses and calls playRecord, so when I call mc.theWorld.playRecord(null) it should also call RenderGlobal#playRecord(null) automatically, should it not?

Posted

Exactly - WorldManager sends packets, RenderGlobal actually plays the record.

 

In my Gui#keyTyped, I call 'mc.theWorld.playRecord("records.cat", x, y, z);', then in #onGuiClosed(), I call 'mc.theWorld.playAuxSFX(1005, x, y, z, 0);' and 'mc.theWorld.playRecord((String) null, x, y, z);', but the record keeps playing.

 

Pastebin of Gui code; don't mind the ZeldaSongPacket - it doesn't play the record again.

 

Thanks for the help so far.

Posted

Glad it's not just me xD

 

So I went ahead and created my own mapped sound positions in my ClientProxy to see more closely what's going on. Now it gets even more interesting - the sound is found playing at the position, but calling 'stopSound(isound)' does not stop it! SoundHandler#stopSounds() works, with the downside that it will stop all other sounds from playing at the same time.

 

Code:

 

// In GuiOcarina#onGuiClosed: replaced vanilla mc.theWorld method calls with this:
ZSSMain.proxy.playRecord(null, x, y, z);

// And starting the music with:
ZSSMain.proxy.playRecord("records.cat", x, y, z);

// in ClientProxy
/** Map of all custom streaming sounds for this client. See RenderGlobal#mapSoundPositions */
private final Map<ChunkCoordinates, ISound> mapSoundPositions = new HashMap<ChunkCoordinates, ISound>();

@Override
public void playRecord(String sound, int x, int y, int z) {
ChunkCoordinates chunkcoordinates = new ChunkCoordinates(x, y, z);
ISound isound = mapSoundPositions.get(chunkcoordinates);
if (isound != null) {
	LogHelper.info("Sound already playing at " + x + "/" + y + "/" + z + "; stopping sound");
	Minecraft.getMinecraft().getSoundHandler().stopSounds();//.stopSound(isound);
	mapSoundPositions.remove(chunkcoordinates);
}
if (sound != null) {
	LogHelper.info("Playing sound at " + x + "/" + y + "/" + z + ": " + sound);
	ResourceLocation resource = new ResourceLocation(sound);
	PositionedSoundRecord psr = PositionedSoundRecord.func_147675_a(resource, x, y, z);
	mapSoundPositions.put(chunkcoordinates, psr);
	Minecraft.getMinecraft().getSoundHandler().playSound(psr);
}
}

 

 

Console Output is as expected:

[11:15:33] [ZeldaSwordSkills] [iNFO] Playing record on client
[11:15:33] [ZeldaSwordSkills] [iNFO] Playing sound at -417/74/838: records.cat
[11:15:35] [ZeldaSwordSkills] [iNFO] Song finished - closing screen
[11:15:35] [ZeldaSwordSkills] [iNFO] Gui closed, stopping song
[11:15:35] [ZeldaSwordSkills] [iNFO] Sound already playing at -417/74/838; stopping sound

 

I'm very confused why the vanilla code works, but me using what seems to be the same code fails. As far as I can tell, it should be doing exactly the same thing.

Posted

It sure does - I even checked if Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(isound) is returning true in my client proxy method just now, and it does; judging from the SoundManager isSoundPlaying and stopSound methods, this should be enough to guarantee that the sound system is able to stop it.

 

EDIT: Okay, after listening carefully, the record IS stopping, but immediately starts playing again, whether using my custom method or the vanilla one... curiouser and curiouser :\  I'm 100% sure that I'm not playing the record anywhere else, but I'm going to look again anyway. So bizarre.

Posted

I tried replacing the playRecord calls in my gui like so:

private ISound songTrack;

// trigger song:
songTrack = PositionedSoundRecord.func_147675_a(new ResourceLocation("records.mall"), x, y, z);
mc.getSoundHandler().playSound(songTrack);

// turn it off:
mc.getSoundHandler().stopSound(songTrack);

This has the exact same behavior as everything else - the music stops but immediately restarts from the beginning, even though I'm not playing the sound from anywhere else and the gui has closed. Any ideas why it might be doing this?

Posted

Turns out that it IS playing twice, but not in the way one might think.

 

What's happening is that the sound system sends the stop notification, but the sound entry is still in the playingSounds Map - after the Gui closes in Minecraft#displayGuiScreen, it calls SoundHandler#resumeSounds -> SoundMananger#resumeAllSounds and finds the entry still there, for which it then calls SoundSystem#play.

 

I'm not exactly sure how "CommandQueue( new CommandObject( CommandObject.STOP, sourcename) );" works, but for whatever reason it is either not executing properly or something else is going awry.

 

The only thing in my code different from vanilla that I can see is I'm trying to stop the sound from a Gui and the game resumes sounds after it closes, so I'm going to try sending a packet back and forth to stop the sound instead of doing it from inside the Gui. StopAllSounds clears all of the sound maps, which is why it works no matter what.

 

I shall post back with results of my experiment shortly. Thanks again for helping out ;)

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.