#Add a sound/music when a certain entity is nearby

16 messages · Page 1 of 1 (latest)

steel spindleBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

rustic coral
#

For when a entity spawns use the EntityEvent.spawned, for the range just use the ServerEvents.tick and for each player just use this function on Level:

public EntityArrayList kjs$getEntitiesWithin(AABB aabb)
#

or this:

public List<Entity> getEntities(EntityTypeTest<Entity, Entity> arg0, AABB arg1, Predicate<Object> arg2)
vale tree
#

might take me awhile sorry, im awful at using Kubejs, idk all the formatting or phrases

rustic coral
#

So basically you can register certain functions to happen when an event happens on the server.
In a .js file in server_scripts/ you can thus do the following:

EntityEvents.spawned(event => {
    if (event.entity.type === "minecraft:skeleton"){
        // Your code here
    }
})

Of course, you can here replace "minecraft:skeleton" with any other valid mod ID.

#

Now, you can also register things to happen every tick:

ServerEvents.tick(event => {
    event.server.getPlayers().forEach(player => {
        // Execute some code for each player
    })
    event.server.getEntities().forEach(entity => {
        // Execute some code for each entity
    })
})
#

We can utilise this to make the code you wanted:

const k = 5 // Radius for entity detection
ServerEvents.tick(event => {
    event.server.getPlayers().forEach(player => {
        // Create a bounding box around the player and make it bigger
        let oAABB = player.getBoundingBox().inflate(k)

        // Get all entities within the bounding box
        event.server.getLevel().getEntitiesWithin(oAABB).forEach(entity => {
            if (entity.type === "minecraft:skeleton"){
                // Example code for playing the sound
                player.playSound("some_namespace:some_sound", 1.0, 1.0) // With fixed volume and pitch
                player.playSound("some_namespace:some_sound") // With random volume and pitch
            }
        })
    })
})
#

I haven't tested this, but this should work. Again, here you replace the entity type by whatever you want and the sound by whatever you want

#

Now, it'd be best storing some tag in player.data to ensure it doesn't get played over and over again, but this is just the base idea

#

If you want to actually player some soundtrack and stop it once the entity leaves, you'd have to look up in the wiki how playing music is handled and look for the corresponding methods in the kubejs docs

#

But if you want them locally just install the ProbeJS mod and do /probejs dump, then it generates a kubejs/documentation/ folder with that html inside of it

vale tree
#

unfortunately it's not working, i tried to keep the skeleton as an easy testing mob but the sound isn't playing

rustic coral
vale tree
#

const k = 5 // Radius for entity detection
ServerEvents.tick(event => {
event.server.getPlayers().forEach(player => {
// Create a bounding box around the player and make it bigger
let oAABB = player.getBoundingBox().inflate(k)

    // Get all entities within the bounding box
    event.server.getLevel().getEntitiesWithin(oAABB).forEach(entity => {
        if (entity.type === "minecraft:skeleton"){
            // Example code for playing the sound
            player.playSound("kubejs:music.calm4", 1.0, 1.0) // With fixed volume and pitch
        }
    })
})

})