#Add objective when server loaded

13 messages · Page 1 of 1 (latest)

wary pier
#

Hi everyone.

I'm tring to create an objective to count player's number of death. I try to read kubejs offline doc but it's very hard.

For now I've this pice of code :

ServerEvents.loaded(event => {
    const { server } = event
    // if (server.persistentData.gameRules) return // later when I finish this script to to all this actions once only

    // Scoreboards
    const { scoreboard } = server
    console.log('=======================================================')
    console.log(scoreboard) // see net.minecraft.server.ServerScoreboard@7bf4afcb [net.minecraft.server.ServerScoreboard]c
    const { objectives } = scoreboard
    console.log(objectives) // see [net.minecraft.world.scores.Objective@58a07670, ..., net.minecraft.world.scores.Objective@1c32e1dd] [java.util.ArrayList] so ok, this is a JAVA array so cannot use addObjective on it but where in this case ?
    objectives.addObjective('nbDeath', 'DEATH_COUNT', {text: "Nombre de mort", color: "black"}, 'INTEGER')    // equivalent of scoreboard objectives add nbDeath deathCount if I understand
    scoreboard.setDisplayObjective('LIST', 'nbDeath')    // equivalent of scoreboard objectives setdisplay list nbDeath if I understand
    scoreboard.startTrackingObjective('nbDeath')    // equivalent of scoreboard players enable @a nbDeath ?

    server.persistentData.gameRules = true
})

But kubejs throw error "Error in 'ServerEvents.loaded': dev.latvian.mods.rhino.EcmaError: TypeError: Cannot find function addObjective in object {...}" but :

  • In the offline doc I can view this function
  • Why it's an instance of "object" and not "net.minecraft.server.ServerScoreboard" ?
  • In addition, what's the two last parameters of addObjective ? (a boolean and an integer, right, but what is used for ?)

I realy try to understand KubeJS by myself but it's very complexe without a true doc :/ Thank in advance 🙂

radiant mesaBOT
#

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

worn ledge
#

You were very close - scoreboard.objectives in this case is a shorthand for scoreboard.getObjectives(), which returns all existing scoreboards.
You need to call .addObjective() on scoreboard instead.
As for the last two parameters, setting the boolean to true will always update scoreboard displays whenever the score gets set/updated. Idk what the last parameter is supposed to do, but it's nullable so i'd suggest justleaving it out for now.
This is the function you're calling btw:

#

Oh and you can use Component.black("some text value") for components in kubejs, i don't know if recreating the nbt structure of text components works for those

wary pier
worn ledge
#

as far as i can tell you don't need to manually call startTrackingObjective, it should just be tracked for all players.
And the /coreboard players enable command exists only to make /trigger commands for the scoreboard work for players, it's not necessary otherwise

#

the last boolean makes it so any change to the value of the scoreboard is immediately displayed if you set up something like a sidebar display to track your count

worn ledge
wary pier
#

thanks for all this precisions 🙂

wary pier
#

sorry but I've the same error than previously :

Error in 'ServerEvents.loaded': dev.latvian.mods.rhino.EvaluatorException: Can't find method dev.latvian.mods.rhino.CachedClassInfo.addObjective(string,string,object,string,boolean).

Actual code :

ServerEvents.loaded(event => {
    const { server } = event
    // if (server.persistentData.gameRules) return

    // Scoreboards
    const { scoreboard } = server
    console.log('=======================================================')
    console.log(scoreboard) // net.minecraft.server.ServerScoreboard@2cb803bd [net.minecraft.server.ServerScoreboard]
    scoreboard.addObjective('nbDeath', 'DEATH_COUNT', {text: "Nombre de mort", color: "black"}, 'INTEGER', true) // <=> scoreboard objectives add nbDeath deathCount
    scoreboard.setDisplayObjective('LIST', 'nbDeath') // <=> scoreboard objectives setdisplay list nbDeath

    server.persistentData.gameRules = true
})
worn ledge
#

oh, you do actually have to pass null as the last parameter to addObjective or rhino won't recognize the function call
(on a side note, whenever you get these "Can't find method x(type,type,...)" log lines, you can console.log the function (console.log(object.x) without the parenthesis to call the function x() ) and you will get all known type signatures)

wary pier
#

Same error...

Error in 'ServerEvents.loaded': dev.latvian.mods.rhino.EvaluatorException: Can't find method dev.latvian.mods.rhino.CachedClassInfo.addObjective(string,string,net.minecraft.network.chat.MutableComponent,string,boolean,null).

ServerEvents.loaded(event => {
    const { server } = event
    // if (server.persistentData.gameRules) return

    // Scoreboards
    const { scoreboard } = server
    console.log('=======================================================')
    console.log(scoreboard.addObjective) // net.minecraft.world.scores.Objective addObjective(java.lang.String,net.minecraft.world.scores.criteria.ObjectiveCriteria,net.minecraft.network.chat.Component,net.minecraft.world.scores.criteria.ObjectiveCriteria$RenderType,boolean,net.minecraft.network.chat.numbers.NumberFormat) [dev.latvian.mods.rhino.NativeJavaMethod]
    scoreboard.addObjective('nbDeath', 'DEATH_COUNT', Component.black("Nombre de mort"), 'INTEGER', true, null) // <=> scoreboard objectives add nbDeath deathCount
    scoreboard.setDisplayObjective('LIST', 'nbDeath') // <=> scoreboard objectives setdisplay list nbDeath

    server.persistentData.gameRules = true
})