This tutorial is set by the author's setting without permission.
Project goal
Implement the effect as shown in the figure.
Photo information explanation (from left to right): The number of items contained in the drop objects, the items contained, the time to disappear naturally (unit: second, take it down)
serverEvents.tick (event => {// Write the code of the project here
})
Code analysis
1. Obtain and traverse the server of the server, and determine whether its type is Minecraft: Item.
Event.server.entities.Foreach (Entity => {{If (entity.type == "minecraft: item") {{
// Write the code of step 2 here
}
}
)
2. Get the NBT data of the drop.
// Get the time to get the remaining time disappearing (unit: second, take it up)var restlife = math.trunc ((6000-entity.age)/(20))
// Obtain the items contained in the dropped object
let ID = (Entity.nbt ["item"] ["ID"]))
// The number of items contained in the dropped object
let amont = Entity.nbt ["item"] ["count"]
3. String string string, and set its Customname display.
// Set the custom name as visibleEntity.customNamevisible = TRUE
// Set the custom name
Entity.customName = Component.green
So far, the main code of the project is completed, but at this time we still cannot get the name of the localization of the item.(As shown in the red line part)
Get the localization name of items
We only need to create an Itemstack object and get its display name.I tried it for 3 hours before I found this solution
let localName = item.of (id) .getdisplayName (). GetString ()complete codeserverEvents.tick (event => {Event.server.entities.Foreach (Entity => {{
If (entity.type == "minecraft: item") {{
// Get the time to get the remaining time to disappear from the fall (unit: second, take it up)
Var restlife = math.trunc ((6000-entity.age)/(20))
// Obtain the items contained in the dropped object
Let ID = (Entity.nbt ["item"] ["ID"]))
// The number of items contained in the dropped object
Let amont = Entity.nbt ["item"] ["count"]
// Get the localized name of the items contained in the dropped object
Let localName = item.of (id) .getdisplayName (). GetString ()
// show
Entity.customNamevisible = True Entity.CustomName = Component.green (`$ {amont} x $ {id} $ {restlife}`)
}
})
})
Code applicable version
Kubejs 6