#damage item depending on block hardness

13 messages · Page 1 of 1 (latest)

jovial orchid
#
BlockEvents.broken('.*', e => {
    const { player, block } = e 
    if ('kubejs:end_stone_carver' == player.getMainHandItem()) {
        player.mainHandItem.durability -= block.hardness- 1
        e.cancel()
    }
})

.durability and .hardness dont work apparently and probe still doesnt work so ill need your help

granite tinselBOT
#

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

jovial orchid
#

figured out how to damage the tool but block hardness or destroyspeed is still a mystery to me

jovial orchid
#

bump

serene mist
#
ForgeEvents.onEvent('net.minecraftforge.event.level.BlockEvent$BreakEvent', event => {
    let player = event.getPlayer()
    if ('minecraft:stone_pickaxe' == player.getMainHandItem()) {
        let mainHandItemStack = player.getMainHandItem();
        let newDamageValue = mainHandItemStack.getDamageValue() + 100;
        mainHandItemStack.setDamageValue(newDamageValue);
    }
})
#

Heres the solution using native script (change the damage value to whatever u want)

PUT IN STARTUP SCRIPT FOR THIS TO WORK

#

this is done using block break event in Forge

#

no idea how kubejs events work cuz i cant find the damage value in the kubejs block break one

prisma forgeBOT
#

Please close your ticket (with </ticket close:1054771505520717835> or the button atop this thread) once you resolved your issue!
This also helps others that would like to help out, as they don't have to look into this thread to check if it has been resolved by now.

Do you have any other questions regarding your issue? Feel free to ask!
Note: You should create a new post for unrelated issues.

jovial orchid
# serene mist ```java ForgeEvents.onEvent('net.minecraftforge.event.level.BlockEvent$BreakEven...

this sadly isnt depending on block hardness. You can get the hardness with block.blockState.getDestroySpeed(level, pos)
Here's my solution:

BlockEvents.broken(e => {
    const { server, player, block, level, block:{blockState, pos} } = e 
    if (global.carverItems.includes(player.getMainHandItem().id)) {
        let hardness = blockState.getDestroySpeed(level, pos)
        let durabilityDamage = hardness
        let enchants = e.player.mainHandItem.enchantments
        if(enchants) {
            if ("minecraft:unbreaking" in enchants) {
                let enchLevel = enchants["minecraft:unbreaking"] 
                
                let rolls = countSuccesses(hardness, enchLevel + 1)
                durabilityDamage = rolls
            }
        }
        player.mainHandItem.damageValue += Math.max(0, Math.ceil(durabilityDamage) - 1)
        if (durabilityDamage >= 10) { // big durability damage warning
            player.playNotifySound("minecraft:entity.item.break", "players", 1, 2)
        }
    }
    console.log(block.id)
    if (block.id == 'minecraft:obsidian' && global.carverItems.includes(player.getMainHandItem().id)) {
        e.server.scheduleInTicks(0, () => e.block.set('kubejs:shattered_obsidian'))
    }
})

function countSuccesses(count, sides) { // count = amount of throws, sides = sides of the dice
  let successes = 0
  for (let i = 0; i < count; i++) {
    let roll = Math.floor(Math.random() * sides) + 1
    if (roll == 1) successes++
  }
  return successes
}

(my custom pickaxes are called carvers)
i built in support for unbreaking

canceling a block breaking event (needed to set a new block) will result in the pickaxe not breaking, so i had to find a workaround for that
e.g.

block.set('kubejs:shattered_obsidian')
e.cancel()

results in the pickaxe not breaking, the workaround for this case is:

e.server.scheduleInTicks(0, () => e.block.set('kubejs:shattered_obsidian'))
prisma forgeBOT
#

Please close your ticket (with </ticket close:1054771505520717835> or the button atop this thread) once you resolved your issue!
This also helps others that would like to help out, as they don't have to look into this thread to check if it has been resolved by now.

Do you have any other questions regarding your issue? Feel free to ask!
Note: You should create a new post for unrelated issues.