#Firestarter item script

23 messages · Page 1 of 1 (latest)

split barn
#

Tried to make an item as a one-use flint and steel for campfires- Much saddeningly, it has been a very harsh struggle. Campfire will not light up.

    if (!event.player.isHolding('kubejs:firestarter')) return
    event.player.tell('Firestarter is held while clicking on a campfire')

    if (event.getBlock().getProperties().get('lit') == 'false') return
    event.player.tell('Firestarter has observed that the campfire is not lit') // Will not go past this point

    event.block.set( event.block, event.block.getProperties().set('lit', 'true') )
    event.player.tell('Firestarter has lit the campfire')

    event.block.playSound('minecraft:item.flintandsteel.use')
    event.player.tell('Firestarter has played a sound effect')

    if(event.player.isCreative()) {event.player.mainHandItem.count--}
    event.player.tell('Firestarter is gone from the player inventory')

    event.cancel()
})```
flat lindenBOT
#

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

split barn
#

It is very harsh as I could not find any documentation regarding that stuff-

#

No clue of what I'm doing so it might be horrific to look at

plush horizon
split barn
#

oh this is rather handy

plush horizon
#
if (event.getBlock().getProperties().get('lit') == 'false') return

I think loosely comparing a boolean with string representing a boolean value should work if this is done with JS rules, but I'm not quite sure what happens with a Java boxed Boolean

#

Should be the same

#

Oh, OK, KubeJS describes all properties as strings

#

🤷

#
BlockEvents.rightClicked('minecraft:campfire', event => {
  if (event.hand != "main_hand") return;
  event.player.tell(typeof event.block.properties['lit']) // string
})
split barn
#

oh

plush horizon
#
const $CampfireBlock = Java.loadClass('net.minecraft.world.level.block.CampfireBlock')

BlockEvents.rightClicked('minecraft:campfire', event => {
  const { player, block, level } = event
  if (!player.isHolding('kubejs:firestarter')) return
  player.tell('Firestarter is held while clicking on a campfire')

  if (block.blockState.getValue($CampfireBlock.LIT)) return
  player.tell('Firestarter has observed that the campfire is not lit')
  block.setBlockState(block.blockState.cycle($CampfireBlock.LIT))
  player.tell('Firestarter has lit the campfire')

  level.playSound(null, block.pos, 'minecraft:item.flintandsteel.use', "blocks")
  player.tell('Firestarter has played a sound effect')

  if (player.isCreative()) {
    player.mainHandItem.count--
  }
  player.tell('Firestarter is gone from the player inventory')

  event.cancel()
})
#

I wish KubeJS had an easier way to manipulate blockstates without Java.loadClassing properties

plush horizon
split barn
#

ahh so that was the blunder I did

plush horizon
#

While you want to exit early if the campfire is lit

#

Also, LevelBlock does not have playSound, so in my script above I used level.playSound

split barn
#

Thank you very much kind sir-
I shall now keep this for further reference

#

Seems absolutely perfect! Thanks a ton
Only had to put the ! in if (!player.isCreative())

plush horizon
#

Oh, that too

split barn
#

But that sure is quite nice-

#

I will then close this peacefully