#Trying to transform an item when right-clicking a block

79 messages · Page 1 of 1 (latest)

hybrid umbra
#

As title, I'm trying to set up a set of machines that when right-clicked with Slime Crystals (from Tinkers), the Slime Crystal is consumed and another item is created (I don't really care whether it's a "destroy & create" or a "transform", both accomplish the same goal). At the moment I'm following this thread, but it's not working for my use case: #1054683926989377586 message

Could I get some help please?

let gels = ['kubejs:slime_gel_machine', 'kubejs:sky_gel_machine', 'kubejs:ender_gel_machine', 'kubejs:ichor_gel_machine']
let crystals = ['tinkersconstruct:earth_slime_crystal', 'tinkersconstruct:sky_slime_crystal', 'tinkersconstruct:ender_slime_crystal', 'tinkersconstruct:ichor_slime_crystal']

onEvent('block.right_click', event => {
    gels.forEach(e => {
        if (!block.id(e) && !item.id.endsWith('_slime_crystal'))
            event.item.set('minecraft:bone_meal')
    })
})
gentle marlinBOT
#

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

sharp sequoiaBOT
#

[Quote ➤](#1054683926989377586 message) Very important for the theme of my modpack, is that posibble to write a script which would allow you to rightclick a specific block with certain item to turn it into another block? if so, how would i do that

snow smelt
#

if i recall correctly you can do e.item.id = 'new item ID'

hybrid umbra
#

Cool I'll try that. My next concern would be stack size. I don't want to nerf the stack size of Slime Crystals, but I also don't want the player to be able to transform an entire stack in a single click (that would defeat the point of automating). As far as I'm aware the only real way to do that would be to destroy the item & create a new one no?

snow smelt
#

in that case do e.item.count-- and e.player.give('new item ID')

hybrid umbra
#

How do I drop the new item into the world?

snow smelt
#

in what context

hybrid umbra
#

Instead of giving it to the player I mean. So that it works better with automation

snow smelt
#

one sec

#

something like

#

e.block.popItemFromFace(item, e.block.face)

#

i cant remember if its face or direction

hybrid umbra
#

I believe it's face. That's used elsewhere for the Alchemical Laser code.

#

I'm also not getting any interaction with the machine itself. As far as I can tell the script is loading fine, but it does not appear to be affecting the behaviour of the Gel Machine.

snow smelt
#

um

#

should it do anything to the machine?

#

or im not sure i understand

hybrid umbra
#

No, I just mean I'm not getting any interaction.

snow smelt
#

whats your code like

hybrid umbra
#
let gels = ['kubejs:slime_gel_machine', 'kubejs:sky_gel_machine', 'kubejs:ender_gel_machine', 'kubejs:ichor_gel_machine']
let crystals = ['tinkersconstruct:earth_slime_crystal', 'tinkersconstruct:sky_slime_crystal', 'tinkersconstruct:ender_slime_crystal', 'tinkersconstruct:ichor_slime_crystal']

onEvent('block.right_click', event => {
    gels.forEach(e => {
        if (!block.id(e) && !item.id.endsWith('_slime_crystal'))
            event.item.id = ('minecraft:bone_meal')
    })
})
#

Just that, same as top post but switched the "event.item.id" as you first suggested

#

Lemme try with the item count version

snow smelt
#

remove the brackets around the ID

#

random brackets are not good practice :P

hybrid umbra
#

Still nothing. Could it be the if statement?

snow smelt
#

try debugging it with a console log

cloud sphinx
#

what if they have a stack in their hand? direct conversion seems unwise

snow smelt
#

we already went throug hta

#

that*

#

thats the alternative script

#

with the count

hybrid umbra
#

Yeah just trying to get the interaction to work right now

cloud sphinx
#

I see now where you wrote that, but I was going directly off the latest code block

#

item and block are not defined in your if statement

hybrid umbra
#

^That's why it's useful to get an extra pair of eyes on things lol

#
onEvent('block.right_click', event => {
    let block = event.getBlock()
    let item = event.getItem()

    gels.forEach(e => {
        if (!block.id(e) && !item.id.endsWith('_slime_crystal'))
//            event.item.id = 'minecraft:bone_meal'
            event.item.count--
            event.player.give('minecraft:bone_meal')
    })
})
#

Alright, well now it's working

#

It's giving me 8 bone meal for each Slime Crystal, but hey

cloud sphinx
#

I can never remember if it's contains or includes but you could do something like

onEvent('block.right_click', event => {
    let block = event.getBlock()
    let item = event.getItem()

    if (gels.includes(block.id) && crystals.includes(item.id)) {
      item.count--
      event.player.give('minecraft:bone_meal')
    }
})
hybrid umbra
#

Look at how much Bone Meal I have. It's not consuming the Slime Crystal and giving 8 Bone Meal lol.

hybrid umbra
#

That second set of code doesn't work at all

hybrid umbra
#

Alright, I've got functional code but the ".popItem" function just isn't working

let gels = ['kubejs:slime_gel_machine', 'kubejs:sky_gel_machine', 'kubejs:ender_gel_machine', 'kubejs:ichor_gel_machine']
let crystals = ['tinkersconstruct:earth_slime_crystal', 'tinkersconstruct:sky_slime_crystal', 'tinkersconstruct:ender_slime_crystal', 'tinkersconstruct:ichor_slime_crystal']

onEvent('block.right_click', event => {
    let block = event.getBlock()
    let tags = block.getTags()    
    let item = event.getItem()
    let clickedFace = event.getFacing()
    let player = event.getPlayer()    

    gels.forEach(e => {
        if (event.block.id == e && event.item.id.endsWith('_slime_crystal')) {
            event.item.count--
            event.block.popItem('minecraft:bone_meal')    
        }
    })
})
#

This example from the wiki also isn't working,

onEvent('block.right_click', event => {
  if (event.block.id == 'minecraft:gravel' && event.item.id == 'minecraft:flint') {
    event.block.set('sand')
    event.item.count--
    event.block.popItem('clay')
  }
})

In both Survival & Creative, I can get the block interaction to work but the item never Pops.

#

Does .popItem not exist in 1.16?

snow smelt
#

thats not what i wrote tho uwu

#

block.popItemFromFace(item, direction)

cloud sphinx
#

popItem/popItemFromFace were both added in 1.18

hybrid umbra
#

Yeah that's what I was suspecting. Weird that the wiki doesn't mention that, it's usually good about which functions are available in which versions

#

Would ".spawn()" work? And then just create the item as an entity?

cloud sphinx
#

try

let gels = ['kubejs:slime_gel_machine', 'kubejs:sky_gel_machine', 'kubejs:ender_gel_machine', 'kubejs:ichor_gel_machine']
let crystals = ['tinkersconstruct:earth_slime_crystal', 'tinkersconstruct:sky_slime_crystal', 'tinkersconstruct:ender_slime_crystal', 'tinkersconstruct:ichor_slime_crystal']

let $Block = java('net.minecraft.world.level.block.Block')

onEvent('block.right_click', event => {
    let block = event.getBlock()
    let tags = block.getTags()    
    let item = event.getItem()
    let clickedFace = event.getFacing()
    let player = event.getPlayer()    

    gels.forEach(e => {
        if (event.block.id == e && event.item.id.endsWith('_slime_crystal')) {
            event.item.count--
            $Block.popResource(event.level.minecraftLevel, block.pos, 'minecraft:bone_meal')
        }
    })
})
#

other option that you were alluding to would be

let itemDrop = block.createEntity('minecraft:item')
itemDrop.item = 'minecraft:bone_meal'
itemDrop.spawn()
hybrid umbra
#

Getting an error. Let me try that second method

cloud sphinx
#

you need to turn off the class filter to use the java() method

hybrid umbra
#

I'm not a programmer, what does that mean lol

cloud sphinx
#

??disableClassFilter

sharp sequoiaBOT
# cloud sphinx ??disableClassFilter

Some things you do require access to classes that are blocked by default, but never fear! You can disable that!
kubejs/config/common.properties –> disableClassFilter –> true

This will not unblock classes that are explicitly excluded, such as java's networking and file management classes.

cloud sphinx
#

for 1.16 it's invertClassLoader=true instead of disableClassFilter

hybrid umbra
#

That's already on.

#KubeJS Common Properties
#Mon Oct 04 13:29:27 CEST 2021
hideServerScriptErrors=false
packmode=default
invertClassLoader=true
debugInfo=false
announceReload=true
serverOnly=false
cloud sphinx
#

ah in 1.16 it's java('net.minecraft.block.Block')

#

this is why I avoid 1.16 questions ;-D

hybrid umbra
#

At some point I will bring CAB to higher versions. Want to get my 1.4 version out first.

hybrid umbra
#

Fully functional. Thank you very much @cloud sphinx & @snow smelt !!

onEvent('block.right_click', event => {
    let block = event.getBlock()
    let tags = block.getTags()    
    let item = event.getItem()
    let clickedFace = event.getFacing()
    let player = event.getPlayer()    

    function gel_jump(output, gel, input) {
        if (event.block.id == gel && event.item.id == input) {
            event.item.count--    
            let itemDrop = block.createEntity('minecraft:item')
            itemDrop.item = output
            itemDrop.spawn()
        }
    }
    
    gel_jump(MC('clay_ball'), KJ('slime_gel_machine'), TC('earth_slime_crystal'))
    gel_jump(MC('rotten_flesh'), KJ('slime_gel_machine'), TC('sky_slime_crystal'))
    gel_jump(MC('bone_meal'), KJ('slime_gel_machine'), TC('ender_slime_crystal'))
    gel_jump(KJ('blasting_powder'), KJ('slime_gel_machine'), TC('ichor_slime_crystal'))

    gel_jump(MC('clay_ball'), KJ('sky_gel_machine'), TC('earth_slime_crystal'))
    gel_jump(MC('feather'), KJ('sky_gel_machine'), TC('sky_slime_crystal'))
    gel_jump(MC('egg'), KJ('sky_gel_machine'), TC('ender_slime_crystal'))
    gel_jump(KJ('blasting_powder'), KJ('sky_gel_machine'), TC('ichor_slime_crystal'))

    gel_jump(AE2('ender_dust'), KJ('ender_gel_machine'), TC('earth_slime_crystal'))
    gel_jump(KJ('prismatic_dust'), KJ('ender_gel_machine'), TC('sky_slime_crystal'))
    gel_jump(MC('bone_meal'), KJ('ender_gel_machine'), TC('ender_slime_crystal'))
    gel_jump(TE('sulfur_dust'), KJ('ender_gel_machine'), TC('ichor_slime_crystal'))

    gel_jump(MC('magma_cream'), KJ('ichor_gel_machine'), TC('earth_slime_crystal'))
    gel_jump(MC('magma_cream'), KJ('ichor_gel_machine'), TC('sky_slime_crystal'))
    gel_jump(MC('bone_meal'), KJ('ichor_gel_machine'), TC('ender_slime_crystal'))
    gel_jump(MC('blaze_powder'), KJ('ichor_gel_machine'), TC('ichor_slime_crystal'))
})
#

And it's fully functional with Deployers!

snow smelt
hybrid umbra
#

Closing Ticket for now, I'll repost the completed code as an example-script in the future

snow smelt
#

but you just reopened it