#Preventing blocks from being broken without silk touch

209 messages · Page 1 of 1 (latest)

lime inlet
#

is something like this possible? i need to know how to test if the player is holding a silk tool. here's my current code to try and get the item im holding when breaking a block:

BlockEvents.broken(e => {
    let player = e.player
    if (!player) return;
    if (e.block.getId() == 'randomium:randomium'){
        console.log(player.getUseItem())
    }
})

but it just isnt doing anything

turbid ironBOT
#

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

lime inlet
#

if possible, id like it to also tell the player that it can only be broken w a silk tool

#

so they arent left there itchin their heads on wut they need to do

rocky rivet
#

do you have probejs?

#

that might help you with things like that

lime inlet
#

i do, but im not sure where to go from there-

rocky rivet
#

i'll start my kjs testing instance one sec

lime inlet
#

oki

rocky rivet
#

nice, i don't have probe on the testing instance pepepixelstare

lime inlet
#

oop-

rocky rivet
#

maybe i took the "prevent block from being broken" too literally

#

bu that's basically the whole script

#
BlockEvents.broken(event =>{
    const itemEnchantments = event.player.mainHandItem.enchantmentTags.asString
    if (!itemEnchantments.contains('silk_touch')) event.cancel()
})
#

of course you'd filter it only to be applied on randomium ore

lime inlet
#

Ill hav to try it when i get home!

#

Could i filter out #forge:ores/randomium or can i not filter block tags?

#

If i can, how so?

#

This'll b huge

rocky rivet
#
BlockEvents.broken('#forge:ores/randomium', event =>{
    const itemEnchantments = event.player.mainHandItem.enchantmentTags.asString
    if (!itemEnchantments.contains('silk_touch')) event.cancel()
})
#

idk if that works on tags

#

should work

uncut oasis
#

that wont work i dont believe, what you want is ```js
if (!event.block.hasTag('forge:ores/randomium')) return

lime inlet
#

So:

BlockEvents.broken(event => {
     let player = event.player
     let block = event.block
     const itemEnchantments = player.mainHandItem.enchantmentTags.asString
     if (!itemEnchantments.contains('silk_touch')) {
          player.tell('Perhaps you need silk touch for this...')
          event.cancel()
     }
     if (itemEnchantments.contains('silk_touch') && block.hasTag('forge:ores/randomium') return;
})
#

if i made any mistakes, blame mobile keyboard

rocky rivet
#
BlockEvents.broken(event => {
     let player = event.player
     const itemEnchantments = player.mainHandItem.enchantmentTags.asString
     if (!itemEnchantments.contains('silk_touch') &&
         !block.hasTag('forge:ores/randomium')){
         return
     }
     player.tell('Perhaps you need silk touch for this...')
     event.cancel()
})
uncut oasis
#

i believe its includes here instead of contains js !itemEnchantments.includes('silk_touch')

rocky rivet
#

yeah

#

i didn't even see that

#

contains also works tho...

uncut oasis
#

oh fair enough

#

btw here we want to use the or statement instead of checking if both are false js BlockEvents.broken(event => { let player = event.player const itemEnchantments = player.mainHandItem.enchantmentTags.asString if (itemEnchantments.includes('silk_touch') || !block.hasTag('forge:ores/randomium')) return player.tell('Perhaps you need silk touch for this...') event.cancel() })

rocky rivet
#

programming in 3 languages at the same time is very confusing

uncut oasis
#

lol

#

i feel that

bronze sail
#

shouldnt
if (!itemEnchantments.includes('silk_touch') || !block.hasTag('forge:ores/randomium')) return
be
if (itemEnchantments.includes('silk_touch') || !block.hasTag('forge:ores/randomium')) return

(removed the inversion on the silk touch check)

because if they have silk touch you dont need to cancel the event since you whant them to have silk touch

lime inlet
#

Ill try all these

uncut oasis
bronze sail
uncut oasis
#

if the player doesnt have silk touch enchantment we return

#

you're inverting that

#

by returning if the player has silk touch BigEyes

bronze sail
#

we dont whant to cancel when the item has silk touch
so if it has silk touch we whant to return so it dosnt cancel the event

uncut oasis
#

yeah hes right mb

#

we want to do nothing if they have silk touch

#

lemme edit my script

#

good catch

#
BlockEvents.broken(event => {
    let player = event.player
    const itemEnchantments = player.mainHandItem.enchantmentTags.asString
    if (!event.block.hasTag('forge:ores/randomium')) return
    if (itemEnchantments.includes('silk_touch')) return
    player.tell('Perhaps you need silk touch for this...')
    event.cancel()
})``` revised script
lime inlet
#

i forgor the let block = event.block bit-

uncut oasis
#

there

lime inlet
#

could i use the actionbar as opposed to the textbox?

uncut oasis
#

should be displayClientMessage

lime inlet
#

and could i pull a translation id?

lime inlet
#
BlockEvents.broken(event => {
    let player = event.player
    let block = event.block
    const itemEnchantments = player.mainHandItem.enchantmentTags.asString
    if (!block.hasTag('forge:ores/randomium')) return
    if (itemEnchantments.includes('silk_touch')) return
    player.displayClientMessage('Perhaps you need silk touch for this...')
    event.cancel()
})
uncut oasis
#

yea

#

should be Text.translate('your.lang.key')

#

it takes a component

uncut oasis
lime inlet
#

[19:36:35] [ERROR] ! misc.js#106: Error occurred while handling event 'BlockEvents.broken': Can't find method net.minecraft.server.level.ServerPlayer.m_5661_(net.minecraft.network.chat.MutableComponent)

#

it's pointing at the text-

uncut oasis
#

sec

#

do Component.of(Text.translate('your.lang.key'))

lime inlet
#

same thing

#

how did one simple change make the game go all whack

uncut oasis
#

i mean you could just do Component.of('some string message') if you wanted but im trying to find the method to display a client message from a lang key

lime inlet
#

even still

uncut oasis
#

??code

spice charmBOT
# uncut oasis ??code

🗒️**Send the code!**🗒️
You may have an issue with a KubeJS script and you explain it to the best of your ability yet without the actual code in question we have very little to go off of in trying to assist you.

lime inlet
#
BlockEvents.broken(event => {
    let player = event.player
    let block = event.block
    const itemEnchantments = player.mainHandItem.enchantmentTags.asString
    if (!block.hasTag('forge:ores/randomium')) return
    if (itemEnchantments.includes('silk_touch')) return
    player.displayClientMessage(Component.of('You may want Silk Touch for this...'))
    event.cancel()
})
uncut oasis
#

and is there a server error?

#

same as before?

lime inlet
#

[19:42:24] [ERROR] ! misc.js#106: Error occurred while handling event 'BlockEvents.broken': Can't find method net.minecraft.server.level.ServerPlayer.m_5661_(net.minecraft.network.chat.MutableComponent).

uncut oasis
#

oh

lime inlet
#

is there something obvious im missing?

uncut oasis
#

its the right code, tho its reading from server player instead of client player

#

try this js if (player.level.isClientSide()) { player.displayClientMessage(Component.of('You may want Silk Touch for this...')) }

#

though it should work without it hmmm

lime inlet
#

ok, it's fixed....apart from actually saying anything....

uncut oasis
#

omg

#

its cause we arent using backticks!?!?

#

BREH

lime inlet
#

i even yoinked the Component.of() and it just skips-

uncut oasis
#

no no it works

#

instead of ' use `

#

thats so weird, why does it do that

lime inlet
#

nope

uncut oasis
#

and get rid of that clientside check

#
player.displayClientMessage(Component.of(`You may want Silk Touch for this...`), true)```
#

this

#

oh wait

lime inlet
#
BlockEvents.broken(event => {
    let player = event.player
    let block = event.block
    const itemEnchantments = player.mainHandItem.enchantmentTags.asString
    if (!block.hasTag('forge:ores/randomium')) return
    if (itemEnchantments.includes('silk_touch')) return
    player.displayClientMessage(Component.of(Text.translate(`dar.failed.needs_silk`)))
    event.cancel()
})
uncut oasis
#

try that

lime inlet
#

😄

#

yippie!!

uncut oasis
#

lmao

#

it would be something that dumb

lime inlet
#

i love it when a problem arises cuz we missed somethin simple

uncut oasis
#

yea despair

#

is that the end of the ticket? feel free to keep it open if needed

lime inlet
#

i just realized-

#

my pack has Create and ppl could create a drill contraption-

#

how to i acount for that?

uncut oasis
#

i mean create kinda is known to shatter these kinds of dreams a lot

#

though i guess you could prevent them from placing it nearby the ores, though then theres a matter of can a mechanical piston push it next to one

#

or can a normal piston push the ore next to the drill

#

lotta different angles we can go here on that matter, depends on how dedicated they are to getting it without silk touch

#

you'd think create would have some config for that by now, might be worth checking if theres an ore blacklist to it

bronze sail
#

cause if its just to control whats droped could potentialy just overight the blocks loot table

lime inlet
#

there's a bug rn that's making the regular mining drop system drop things ive exclusively blacklisted

#

so im doing a workaround so that you're forced to use an item rightclick event

uncut oasis
#

actually zarch is right in that lootjs would be able to remove those drops from the drop table completely

#

??kjsaddons

spice charmBOT
# uncut oasis ??kjsaddons

LootJS is an addon for KubeJS that allows modifying loot from all sources dynamically (much nicer than loot tables)!
It also supports removing loot added by mods, unlike KubeJS' current loot table events.

uncut oasis
#

??xy

spice charmBOT
# uncut oasis ??xy

The XY problem is when you really want to do X, and you think that Y can achieve X. However, you can't get Y to work, and so ask for help exclusively about Y.

This can lead to a lot of confusion from the people trying to help you, as Y can seem like a very random thing to want to do, and a lot of the time isn't the best way to achieve X anyway.

Its fine to ask about Y, just always include some context about X so you can be put on the right track if Y won't do X well, or there is an easier way to do X.

lime inlet
#

thats the thing

#

randomium doesnt handle wut it drops by a loot table, it uses hardcoded methods

uncut oasis
#

like spawning the item entity on the block or something?

lime inlet
#

i believe so, ye

uncut oasis
#

hmm then the entity spawned event should be what you need here if you just want to prevent it from "dropping" certain things

lime inlet
#

then some blocks will be broken and drop nothing

#

and some of the mods i hav blacklisted will make it so u hav a HUGE gap of shizz that it will fail to drop

#

i also want it to be immune to explosions, but it thinks that explosionResistance is deprecated

uncut oasis
#

do the items go off an item tag or nah

lime inlet
#

yes

uncut oasis
#

whats the item tag

lime inlet
#

randomium:blacklist

uncut oasis
#

ty

lime inlet
#

mhm!

uncut oasis
#

test this and try dropping or breaking the item/block into the world js let ItemEntity = Java.loadClass('net.minecraft.world.entity.item.ItemEntity') EntityEvents.spawned(event => { const {entity,entity:{item}} = event if (entity instanceof ItemEntity){ if (item.hasTag('randomium:blacklist')) event.cancel() } })

lime inlet
#

will this fire if the item is thrown out of a player's inventory?

uncut oasis
#

should

lime inlet
#

then i cant use this...

#

wut im doing is working just fine

lime inlet
uncut oasis
#

btw

#

it says its highly configurable through datapacks

#

is that what youre doing rn?

lime inlet
#

using kube to ad the tags, yes

#

but something broke...

#

and things that i added to said tags are dropping...

#

i alr shared this w the mod dev

uncut oasis
#

they lack any documentation, disappointing as it's already at 2m downloads despair

lime inlet
#

yeah...

uncut oasis
#

try something like this js BlockEvents.modification(e => { e.modify('minecraft:stone', block => { block.setExplosionResistance(1) }) })

lime inlet
#

will this require a game reload on every change?

uncut oasis
#

this is a startup script

#

it edits the explosion resistance on startup

#

so no

uncut oasis
spice charmBOT
lime inlet
#

im trying, still going kaboom-

lime inlet
#

how can i check the block's explosion resistance?

#

cuz the script doesnt seem to be firing at all-

rocky rivet
#

Is it inside a startup file?

#

Also i dont think that's how you set the properties of things

uncut oasis
#

try this even though its deprecated js BlockEvents.modification(e => { e.modify('minecraft:stone', block => { block.explosionResistance = 15 }) })

#

and make sure you change out 'minecraft:stone' to your block

lime inlet
#

still didnt work...

#

there's gotta b somethin im missing...

uncut oasis
#

??code

spice charmBOT
# uncut oasis ??code

🗒️**Send the code!**🗒️
You may have an issue with a KubeJS script and you explain it to the best of your ability yet without the actual code in question we have very little to go off of in trying to assist you.

lime inlet
#
BlockEvents.modification(event => {
    event.modify('randomium:randomium_ore', block => {
        block.setExplosionResistance = 100000
    })
    event.modify('randomium:randomium_ore_deepslate', block => {
        block.setExplosionResistance = 100000
    })
    event.modify('randomium:randomium_ore_end', block => {
        block.setExplosionResistance = 100000
    })
})
#

istg if they need to be in separate blocks, someone's going to the shadow realm-

uncut oasis
#

no no i meant this js BlockEvents.modification(event => { event.modify('randomium:randomium_ore', block => { block.explosionResistance = 100000 }) event.modify('randomium:randomium_ore_deepslate', block => { block.explosionResistance = 100000 }) event.modify('randomium:randomium_ore_end', block => { block.explosionResistance = 100000 }) }) heh

lime inlet
#

i....did that

#

it did nothing-

uncut oasis
lime inlet
#

i even put a console.log in there and it also did nothing

uncut oasis
#

whered you put the console.log?

#

also test it with a normal vanilla block and see if that works

#

if it does then randomium must be implimenting their own explosion resistance method in which case this wont work

#
BlockEvents.modification(e => {
    e.modify('minecraft:stone', block => {
        block.explosionResistance = 100
        block.destroySpeed = 0.1
        block.hasCollision = false
    })
})``` thats with this script
#

wait.. are they just copying block properties from existing blocks? BigEyes

#

though that shouldnt matter hmmm

lime inlet
lime inlet
uncut oasis
#

do you just want the explosion to be canceled?

uncut oasis
#

havent tested it yet, this goes in server scripts

lime inlet
#

ill giv it a shot

#

nope-

#

wait-

#

nvm, still goes kaboom

indigo tendon
#

That is the wrong event you would need

LevelEvents.afterExplosion(event => {
  event.affectedBlocks.forEach(block => {
    if (block.id == 'randomium:randomium_ore') {
      event.removeAffectedBlock(block)
    }
  })
})
lime inlet
#

that did the trick!

uncut oasis
#

reverter comin in clutch as always

lime inlet
#

this did wonders!