My sound isnt triggering:
if (!attacker.getWorld().isClient) {
attacker.getWorld().playSound(
null, // null = all nearby players hear it
attacker.getBlockPos(), // position of sound
ScytheSound.SCYTHE_SLASH, // your custom sound
SoundCategory.PLAYERS, // category
1.0F, // volume
1.0F // pitch
);
#Sound help?
90 messages · Page 1 of 1 (latest)
!!code
You can use codeblocks in discord as shown below:
```java
code
```
You can also specify the syntax highlighting to use by specifying the language after the last backtick (`) on the top.
attacker.getWorld().playSound(
null, // null = all nearby players hear it
attacker.getBlockPos(), // position of sound
ScytheSound.SCYTHE_SLASH, // your custom sound
SoundCategory.PLAYERS, // category
1.0F, // volume
1.0F // pitch
);
sorry about that didnt know how to do that lol
!!llm
Whilst LLMs (Large Language Models) like ChatGPT and Gemini are impressive tools, **they are not recommended for first-time Fabric mod developers due to their inconsistency and potential for generating inaccurate code. **
LLMs may generate incorrect code that:
- Targets the wrong Minecraft version, leading to outdated or incompatible features.
- Uses incorrect mappings, causing errors or unexpected behavior.
- Is designed for the wrong loader (NeoForge vs. Fabric), resulting in incompatibility.
- Relies on non-existent Fabric API modules, creating code that references features that don't exist (called LLM hallucinations)
It's crucial to remember that LLMs should be seen as problem-solving aids, not code-generating machines. The output they provide often requires significant modification and understanding of Java before it can be implemented as a functional mod.
Therefore, learning Java is an absolute necessity before attempting to use any LLM-generated code in your mod. Knowing how the generated code works is key to using it effectively and fixing any problems that may arise.
probably the passing null as the first argument in playSound() is causing no one to hear anything
where are you running this?
originally my code was attacker.playSound(SCYTHE_SLASH,1.0F,1.0F);
Im overriding the postHit and running it there with some other code
the other code works but not the sound
^
i removed it?

they're asking you to pass in an actual entity instead of null but that might not be the issue
postHit should only be running on the server in modern versions iirc so that client check is redundant anyway
sure
im going to leave out the imports
public scytheItem(Settings settings) {
super(settings);
}
@Override
public void postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
Pvp_mod.LOGGER.info("TEST SUCCESS POST DAMAGE SCYTHE");
Vec3d direction = attacker.getRotationVec(1.0F);
Vec3d knockback = direction.multiply(-1);
knockback = new Vec3d(knockback.x, 0, knockback.z);
Vec3d movement = target.getVelocity();
movement = movement.add(knockback);
target.setVelocity(movement);
if (!attacker.getWorld().isClient) {
attacker.playSound(SCYTHE_SLASH,1.0F,1.0F);
if (target instanceof ServerPlayerEntity serverPlayerEntity) {
serverPlayerEntity.networkHandler.sendPacket(new EntityVelocityUpdateS2CPacket(serverPlayerEntity));
}
}
}
}```
your sound file isn't missing or anything, correct?
I can play the sound in game with /playsound
try using a vanilla soundevent instead just to see if it works and get rid of that client check it's not needed
I replaced it with attacker.playSound(SoundEvents.BLOCK_IRON_BREAK,1.0F,1.0F);
still didnt work
is there another way to implement this
ditch using the entity implementation of playSound and cast LivingEntity#getWorld to ServerWorld and use that to play a sound
that worked thanks!
at least for vanilla sound
So close yet so far....
formatted like this right? if (!attacker.getWorld().isClient && attacker.getWorld() instanceof ServerWorld serverWorld) { serverWorld.playSound( null, // Null = send to all nearby players attacker.getBlockPos(), // Position to play sound at ScytheSound.SCYTHE_SLASH, // Sound to play (can be your custom sound) SoundCategory.PLAYERS, // Affects volume slider 1.0f, // Volume 1.0f // Pitch ); }
get rid of the client check that is redundant also yes that's fine and it should also be fine to pass in null for the source depending on whether you want to exclude the source or not
there's plenty of overloads so use whatever works for you
how do I format the custom sound SCYTHE_SLASH
because i tested both SCYTHE_SLASH and scytheslash.SCYTHE_SLASH
you can also just cast world to ServerWorld without using an instanceof check since postHit always runs on the server
can you show me your sound registration class?
public static final SoundEvent SCYTHE_SLASH = registerSoundEvent("scythe_slash");
private static SoundEvent registerSoundEvent(String name) {
Identifier id = Identifier.of(Pvp_mod.MOD_ID, name);
return Registry.register(Registries.SOUND_EVENT, id, SoundEvent.of(id));
}
public static void registerSounds() {
Pvp_mod.LOGGER.info("Registering Mod Sounds " + Pvp_mod.MOD_ID);
}
}```
idk why It doesnt work the sound shows up in the game
so vanilla sounds play but yours does not, correct?
because i tested your exact structure and it works fine on my end with custom sounds
An issue with the registering of your sound perhaps or is your sound not in the correct format?
Is your sound in the OGG format in mono?
in mono?
Change channel to mono and it might work. Sound should be singular channel. This site can do it or you can even do it in audacity
Best way to convert your MP3 to OGG file in seconds. 100% free, secure and easy to use! Convertio — advanced online tool that solving any problems with any files.
ok thanks i’m not home rn but ill try when I get there
Misinformation
The entity parameter is actually the entity that should be excluded from the playSound, so passing in null is correct
that's why I said "probably", if you don't know the function it makes more sense for that to be a list of entities that need to hear the sound
do you have a sounds.json file set up as well?
take a look at https://docs.fabricmc.net/develop/sounds/custom just to make sure
I do and it works
I can do /playsound and it works fine
but the actuall code that plays the custom sound on hit doesnt work for CUSTOM sounds
but does for vanilla
im double checking the mono file
it doesnt work 😭
I just ran into this exact same issue, so if you're still having issues what I finally found worked was
ENTITY.getWorld().playSound(null, ENTITY.getBlockPos(), SOUND, SOUNDCATEGORY, 1f, 1f);
tysm
i’ll give that a try
is there some sort of import or override I need to do to define ENTITY
Entity is just whatever entity you want playing the sound
I just left the standins in caps
Like the exact code I have is
attacker.getWorld().playSound(null, target.getBlockPos(), Sounds.VINE_BOOM, SoundCategory.MASTER, 1f, 1f);
thanks that’s helpful I thought entity was a predefined thing 🤦
also dw I am also very new
I've been working on this on and off for like 3 weeks and it's my first time really even using java
Entity#getWorld can be replaced with ServerWorld because serverWorld is a world too
Then you can avoid redundant getWorld calls because they should return the same world
same lol
also i can’t tell if my registry file isn’t working or if it’s my json
oh i got vanilla events down i just cant add custom sounds
Do i need to reformat the Identifier code because im doing this on 1.21.8?
"vine_boom": {
"subtitle": "sound.socwars.vine_boom",
"sounds": [
"socwars:vine_boom"
]
}
}```
```public interface Sounds {
static void initialise() {}
private static SoundEvent register(String id) {
return Registry.register(Registries.SOUND_EVENT, Identifier.of(SocWars.MOD_ID, id), SoundEvent.of(Identifier.of(SocWars.MOD_ID, id)));
}
SoundEvent VINE_BOOM = register("vine_boom");
}```
Not sure since this is how I'm doing it on 1.21.8 and it looks like exactly the same thing you have
oh do i have to convert it to an ogg?