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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Ah, it appears I spoke too soon, I still need a little help here. I now have the forceloading working reliably.  However, I've realized it's not always the first tick that loads the entity.  I've seen it take anywhere from 2-20ish to actually go through, in which time my debugging has revealed that the chunk is loaded, but during which time calling  serverLevelIn.getEntity(uuidIn) returns a null result.  I suspect this has to do with queuing and how entities are loaded into the game.  While not optimal, it's acceptable, and I don't think there's a whole ton I can do to avoid it. However, my concern is that occasionally teleporting an entity in this manner causes a lag spike.  It's not every time and gives the appearance of being correlated with when other chunks are loading in.  It's also not typically a long spike, but can last a second or two, which is less than ideal.  The gist of how I'm summoning is here (although I've omitted some parts that weren't relevant.  The lag occurs before the actual summon so I'm pretty confident it's the loading, and not the actual summon call). ChunkPos chunkPos = new ChunkPos(entityPosIn); if (serverLevelIn.areEntitiesLoaded(chunkPos.toLong())) { boolean isSummoned = // The method I'm using for actual summoning is called here. Apart from a few checks, the bulk of it is shown later on. if (isSummoned) { // Code that runs here just notifies the player of the summon, clears it from the queue, and removes the forceload } } else { // I continue forcing the chunk until the summon succeeds, to make sure it isn't inadvertently cleared ForgeChunkManager.forceChunk(serverLevelIn, MODID, summonPosIn, chunkPos.x, chunkPos.z, true, true); } The summon code itself uses serverLevelIn.getEntity(uuidIn) to retrieve the entity, and moves it as such.  It is then moved thusly: if (entity.isAlive()) { entity.moveTo(posIn.getX(), posIn.getY(), posIn.getZ()); serverLevelIn.playSound(null, entity, SoundEvents.ENDERMAN_TELEPORT, SoundSource.NEUTRAL, 1.0F, 1.0F); return true; } I originally was calling .getEntity() more frequently and didn't have the check for whether or not entities were loaded in place to prevent unnecessary code calls, but even with those safety measures in place, the lag still persists.  Could this just be an issue with 1.18's lack of optimization in certain areas?  Is there anything I can do to mitigate it?  Is there a performance boosting mod I could recommend alongside my own to reduce the chunk loading lag? At the end of the day, it does work, and I'm putting measures in place to prevent players from abusing the system to cause lag (i.e. each player can only have one queued summon at a time-- trying to summon another replaces the first call).  It's also not an unacceptable level of lag, IMO, given the infrequency of such calls, and the fact that I'm providing the option to toggle off the feature if server admins don't want it used.  However, no amount of lag is ideal, so if possible I'd love to find a more elegant solution-- or at least a mod recommendation to help improve it. Thanks!
    • When i start my forge server its on but when i try to join its come a error Internal Exception: java.lang.OutOfMemoryError: Requested array size exceeds VM limit Server infos: Linux Minecraft version 1.20.1 -Xmx11G -Xms8G
    • Also add the latest.log from your logs-folder
    • Add the mods in groups
  • Topics

×
×
  • Create New...

Important Information

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