Jump to content

[SOLVED][1.18.2] Playing sound with client-only mod


Recommended Posts

Hey, guys.

I'm trying to play a sound in a mod, which is client-only.

It works fine with single-player saves, but when trying to use in on a real multiplayer server (without installing the mod on server of course), I'm getting a crash:

Quote

java.lang.NullPointerException: Registry Object not present: trovogration:cooldown_end
    at java.util.Objects.requireNonNull(Objects.java:334) ~[?:?] {re:mixin}
    at net.minecraftforge.registries.RegistryObject.get(RegistryObject.java:320) ~[forge-1.18.2-40.1.92-universal.jar%23301!/:?] {re:mixin,xf:fml:forge:registry_object_binary_compat,re:classloading,xf:fml:forge:registry_object_binary_compat}

Here's the code:

Registration:

public class Sounds {
    public static final DeferredRegister<SoundEvent> SOUNDS = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, TrovoGration.MOD_ID);
    public static final RegistryObject<SoundEvent> SOUND_ACTION_NEGATIVE = SOUNDS.register("negative_action", () -> new SoundEvent(new ResourceLocation(TrovoGration.MOD_ID,"negative_action")));
    public static final RegistryObject<SoundEvent> SOUND_ACTION_POSITIVE = SOUNDS.register("positive_action", () -> new SoundEvent(new ResourceLocation(TrovoGration.MOD_ID,"positive_action")));
    public static final RegistryObject<SoundEvent> SOUND_COOLDOWN_START = SOUNDS.register("cooldown_start", () -> new SoundEvent(new ResourceLocation(TrovoGration.MOD_ID,"cooldown_start")));
    public static final RegistryObject<SoundEvent> SOUND_COOLDOWN_END = SOUNDS.register("cooldown_end", () -> new SoundEvent(new ResourceLocation(TrovoGration.MOD_ID,"cooldown_end")));
}

Main class constructor:

    public TrovoGration(){
        Sounds.SOUNDS.register(FMLJavaModLoadingContext.get().getModEventBus());
    }

Play code:

LocalPlayer player ... ;
player.playSound(Sounds.SOUND_ACTION_NEGATIVE.get(), 0.7f, 1.0f);

sounds.json:

{
  "positive_action": {
	"category": "record",
    "subtitle": "trovogration.subtitle.positive_action",
    "sounds": [ "trovogration:positive_action" ]
  },
  "negative_action": {
	"category": "record",
    "subtitle": "trovogration.subtitle.negative_action",
    "sounds": [ "trovogration:negative_action" ]
  },
  "cooldown_start": {
	"category": "record",
    "subtitle": "trovogration.subtitle.cooldown_start",
    "sounds": [ "trovogration:cooldown_start" ]
  },
  "cooldown_end": {
	"category": "record",
    "subtitle": "trovogration.subtitle.cooldown_end",
    "sounds": [ "trovogration:cooldown_end" ]
  }  
}

Help!!

Edited by ZigTheHedge
Solved
Link to comment
Share on other sites

From what I understand (which might be incorrect, I am not an expert on this),

it doesn't use the local client registry for those registries that have data sent from a multiplayer server.

SoundEvents is one of those registries.

So in multiplayer your SoundEvent effectively won't exist unless it is also registered on the server?

 

The logic can be found here (and its related methods):

https://github.com/MinecraftForge/MinecraftForge/blob/bb49b9e6dc7525ae0fa935abf6eec18b36091bd6/src/main/java/net/minecraftforge/registries/GameData.java#L656

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

You might be able to make it work by using the sound directly?

i.e. something like:

        SoundInstance sound = new SimpleSoundInstance(...);
        Minecraft.getInstance().getSoundManager().play(sound);

But I guess you would need to use an AccessTransformer to make the SimpleSoundInstance constructor that takes a ResourceLocation instead of a SoundEvent publicly accessible.

https://forge.gemwire.uk/wiki/Access_Transformers

Edited by warjort
  • Like 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

There's a public constructor in SimpleSoundInstance )

Well, I've changed a code to use direct SoundInstance, big-fat Thank You for the suggestion ) Now it's time to test it in a multiplayer. I'll reply back.

Link to comment
Share on other sites

  • ZigTheHedge changed the title to [SOLVED][1.18.2] Playing sound with client-only mod

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

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