#Item does not get removed when reached 0 durability

82 messages · Page 1 of 1 (latest)

hallow atlas
#

Am I missing something here? Essentially the gas mask is reducing its durability every tick and whenever it reaches or goes below 0, it doesn't get removed or cleraed from the player head slot.

onEvent('player.tick', event => {
    if (!event.player.isAlive()) return
    if (event.player.headArmorItem.id == "apocalypsenow:gasmask_helmet")
    {
        if (event.entity.block.biomeId == "biomesoplenty:wasteland" || 
            event.entity.block.biomeId == "biomesoplenty:wooded_wasteland")
        {
            let item = event.player.headArmorItem
            item.nbt.Damage++;
            if (item.nbt.Damage >= item.maxDamage)
            {
                player.inventory.clear(item);
            }
        }
    }
})
candid dustBOT
#

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

low tangle
#

Have you tested without having it clear the item on zero durability? It might just delete itself upon reaching zero/one more damage once it is on zero. Depends how kubejs is applying the damage.

hallow atlas
#

nope it doesnt' at all

low tangle
#

I've never touched anything in kubejs regarding players so I don't know if this is intentional or not, but you have if (event.player.headArmorItem.id == and let item = event.player.headArmorItem

#

first one ends in .id when you are referencing head slot item

#

is event.player.headArmorItem going to return something that is usable by player.inventory.clear(item);?

glad vessel
hallow atlas
#

increasing damage = reducing durability isnt it?

glad vessel
#

oh yeah maybe minecraft does Component.of(maxDamage - currentDamage)

#

didn thought about it

#

can u console log inside the clear statement to see if thats hit at all

#

maybe inventory clear ignores armor slots

hallow atlas
#

surprisingly the item.maxDamage is undefined

#

        if (event.entity.block.biomeId == "biomesoplenty:wasteland" || 
            event.entity.block.biomeId == "biomesoplenty:wooded_wasteland")
        {
            let item = event.player.headArmorItem
            item.nbt.Damage++;
            event.player.tell([
                Component.of(item.maxDamage).bold().red()
            ])
            if (item.nbt.Damage >= item.maxDamage)
            {
                player.inventory.clear(item);
            }
        }
glad vessel
#

let me check the correct name

#

getMaxDamage() exists, so it should be beaned

hallow atlas
#

wtf it doesn't print anything now

            event.player.tell([
                Component.of(item.getMaxDamage()).bold().red()
            ])```
glad vessel
#

Oh wait u are on 1.18. On 1.18 kubejs still had all their wrappers MonkaS Could be that maxDamage does not exist on the wrapper

hallow atlas
#

oh it works now, but it doesn't clear or remove the item

#

the condition check passes

glad vessel
#

wait xD

hallow atlas
#

k this works

#

if (item.nbt.Damage >= item.itemStack.getMaxDamage())
{
item.count--;
}

#

apparently im just missing the itemstack, thanks a lot

glad vessel
#

but you maybe want hurtAndBreak, which also triggers client sync, break sounds etc.


function onBreakItem(player) {
  player.broadcastBreakEvent("head")
}

onEvent('player.tick', event => {
    if (!event.player.isAlive()) return
    if (event.player.headArmorItem.id == "apocalypsenow:gasmask_helmet")
    {
        if (event.entity.block.biomeId == "biomesoplenty:wasteland" || 
            event.entity.block.biomeId == "biomesoplenty:wooded_wasteland")
        {
            event.player.headArmorItem.itemStack.hurtAndBreak(1, event.player.minecraftEntity, onBreakItem);
        }
    }
})
#

I hope I hit the syntax lol

#

hurtAndBreak also counts in unbreaking enchantment etc

#

if thats something you want to consider

hallow atlas
#

interesting

#

that's a very good one

#

let me try switch

#

duh the tick is too fast

#

need to find another way to slow it down

#

and I don't know how much performance cost this gonna impact with a lot of players

glad vessel
#

it will

glad vessel
#

you can do something like that

if(event.player.minecraftEntity.age % 60 != 0) { 
  return;
}
glad vessel
hallow atlas
#

no error

#

it doesn't do anything

#

I don't think there's a hurt and break function

glad vessel
#

add a console log into onBreakItem

hallow atlas
#

odd, there's no error on my end

#

hm might know why

#

lemme restart the game

#

no it actually have no error

#

wtf

#

I thought it was logbegone filtered the kubejs server's message but it wasnt

glad vessel
#

no error in the log files ?

hallow atlas
#

ah found it

#

[20:06:14] [ERR ] Error occurred while handling event 'player.tick': TypeError: Cannot find function hurtAndBreak in object Item.of('apocalypsenow:gasmask_helmet', '{Damage:0,SimpleInsulation:[]}'). (server_scripts:gasmask_check.js#15)

glad vessel
#

mmh

hallow atlas
#

yea it doesn't exist

#

probably might be another syntax or name

glad vessel
#

kubejs seems to have a shortcut in 1.18:

event.player.damageEquipment("head", 1);

// or maybe u need to set EquipmentSlot directly
event.player.damageEquipment(EquipmentSlot.HEAD, 1);
hallow atlas
#

hmm odd no damage

#

no error either

#
onEvent('player.tick', event => {
    if (!event.player.isAlive()) return
    if (event.player.headArmorItem.id == "apocalypsenow:gasmask_helmet")
    {
        if (event.entity.block.biomeId == "biomesoplenty:wasteland" || 
            event.entity.block.biomeId == "biomesoplenty:wooded_wasteland")
        {
            event.player.damageEquipment("head", 1);
        }
    }
})
glad vessel
#

let me setup 1.18 instance

hallow atlas
#

o wait

#

huh strange

#

it works in single player

#

but not in server

#

this one

onEvent('player.tick', event => {
    if (!event.player.isAlive()) return
    if (event.player.headArmorItem.id == "apocalypsenow:gasmask_helmet")
    {
        if (event.entity.block.biomeId == "biomesoplenty:wasteland" || 
            event.entity.block.biomeId == "biomesoplenty:wooded_wasteland")
        {
            event.player.damageEquipment(EquipmentSlot.HEAD, 1);
        }
    }
})
glad vessel
#

then the damageEquipment does not sync ... mmh

hallow atlas
#

o wait

#

it does now

#

wtf

#

its buggy just now

#

rejoin the server now it works

#

I moved to this, hopefully more optimize and easier to aadjust with the ticks


onEvent('player.logged_in', event => {
    event.server.scheduleInTicks(60, event, (callback) => {
        if (event.player.headArmorItem.id == "apocalypsenow:gasmask_helmet")
        {
            if (event.entity.block.biomeId == "biomesoplenty:wasteland" || 
                event.entity.block.biomeId == "biomesoplenty:wooded_wasteland")
            {
                event.player.damageEquipment(EquipmentSlot.HEAD, 1);
            }
        }
        callback.reschedule();
    })
})
glad vessel
#

that should work yeah

hallow atlas
#

thanks a lot man, appreciate it, closing the ticket