Jump to content

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


ZigTheHedge

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

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

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.