#Assistance with changing a block on right click with an item

96 messages · Page 1 of 1 (latest)

acoustic idol
#

Hello. I am using a few mods on this world, but the ones that will be effected by the code would be TerraFirmaCraft, ArbourFirmaCraft, and Ex Nihilo: Sequentia.

My goal is to enable a function of ENS to be used with TFC. The mod currently works with vanilla leaves, and I cannot figure out how to get it to work with TFC. With the normal pack in mostly vanilla minecraft, you hold a silkworm item in your hand, then right click on a leaf block. This turns the leaf block into an infesting leaf block, which then turns into an infested leaf block after a cool down.

I have tested this in a superflat world with the mods I am trying to compat with, and it still works just fine with vanilla leaves, just not the modded ones.

This is the code I am attempting to use in the server_scripts folder.

    if (event.block.id === 'minecraft:leaves' && !(event.block.id === 'tfc:fruit_tree_leaves')) {
        if (event.player.getHeldItem('main_hand').id === 'exnihilosequentia:silkworm') {
            event.block.set('exnihilosequentia:infesting_leaves')
            ItemEvents.rightClicked('exnihilosequentia:silkworm', (event) => {
              const { item } = event

              item.count--
            })
        }
    }
})

I primarily work with normal Java, so this is a bit of a learning curve/adjustment for me. Therefore I may have some incorrect syntax. Any advice would be appreciated.

dull agateBOT
#

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

dire otterBOT
#

You can write your code in a codeblock by typing it between the codeblock delimiters:
Note that these are backticks, not apostrophes

```js :arrow_left:

ServerEvents.recipes(event => {
event.smelting('minecraft:glass', '#forge:sand').xp(.1)
})

``` :arrow_left:

This example will look like this:

ServerEvents.recipes(event => {
  event.smelting('minecraft:glass', '#forge:sand').xp(.1)
})
cursive kayak
#

Don't use ===, just use ==

#

I believe the method .getMainHandItem() exists instead of what you have currently

acoustic idol
cursive kayak
#

I mean I use == but === might work, not sure

#

I'm just suggesting it as a test

steel grotto
#

you cant have events nested inside other events

cursive kayak
#

Oh I missed that on mobile

steel grotto
#

mobile codeblocks are very bad

#

illegible basically

cursive kayak
#

Mhmm

acoustic idol
acoustic idol
#

i mean, not the same time but ykwim

cursive kayak
#

You can just do the exact same logic at the end of the block event

#

The block event still contains the item

#

Also you can probably simplify event.player.getHeldItem... to event.item

acoustic idol
cursive kayak
#

Nope

dire otterBOT
#

KubeJS has a feature known as beans which allows you to make scripts a tad more readable, and the wiki has a page on it!

cursive kayak
#

Because of beans it's a parameter

acoustic idol
#

awesome!

acoustic idol
#

this is the new code that is still not functioning:

    if (event.block.id == 'minecraft:leaves' && !(event.block.id == 'tfc:fruit_tree_leaves')) {
        if (event.item.id == 'exnihilosequentia:silkworm') {
            event.block.set('exnihilosequentia:infesting_leaves')
                         const { item } = event

              item.count--
        }
    }
})```

removed the extra parentheticals and simplified the block id
steel grotto
#

is minecraft:leaves a valid block ID

#

sounds like a tag to me, not an ID

acoustic idol
#

oh dang

#

you might be right shoot

#

back to the drawing board then

steel grotto
#

event.block.hasTag('minecraft:leaves')

#
BlockEvents.rightClicked(event => {
  if (!event.block.hasTag('minecraft:leaves')) return
  if (event.block.id == 'tfc:fruit_tree_leaves')) return
  if (event.item.id != 'exnihilosequentia:silkworm') return

  event.block.set('exnihilosequentia:infesting_leaves')
  event.item.count--
})
acoustic idol
#

testing that now

#

first im going to test the correction with has tag, then i'm going to try the codeblock you sent

#

ok so my correction did not work, testing your code now

#

hmm. still not working with either way. just to double check, this is going in the server_scripts folder, correct?

steel grotto
#

yes

#

try to debug the IDs to see where it goes wrong

acoustic idol
steel grotto
#
BlockEvents.rightClicked(event => {
  console.log('start')
  console.log(event.block.hasTag('minecraft:leaves'))
  if (!event.block.hasTag('minecraft:leaves')) return
  console.log(event.block.id)
  if (event.block.id == 'tfc:fruit_tree_leaves')) return
  console.log(event.item.id)
  if (event.item.id != 'exnihilosequentia:silkworm') return

  event.block.set('exnihilosequentia:infesting_leaves')
  event.item.count--
})

run that and send me the log

acoustic idol
#

this is the server.log:

[12:30:02] [INIT] Loaded plugins:
[12:30:02] [INIT] - dev.latvian.mods.kubejs.forge.BuiltinKubeJSForgePlugin
[12:30:02] [INIT] - dev.latvian.mods.kubejs.forge.BuiltinKubeJSForgeClientPlugin
[12:30:02] [INIT] - com.notenoughmail.kubejs_tfc.KubeJSTFCPlugin
[12:30:02] [INIT] - com.notenoughmail.kubejs_tfc.addons.firmalife.FirmaLifePlugin
[12:30:02] [INIT] - com.notenoughmail.kubejs_tfc.addons.afc.AFCPlugin
[12:30:02] [INIT] - moe.wolfgirl.probejs.plugin.BuiltinProbeJSPlugin
[12:30:02] [INIT] - novamachina.exnihilosequentia.common.compat.kubejs.KubeJSExNihiloPlugin
[12:30:02] [INIT] KubeJS TFC configuration:
[12:30:02] [INIT] - Debug mode enabled: false
[12:30:02] [INIT] - Self tests console insertion enabled: true
[12:30:02] [INFO] example.js#5: Hello, World! (Loaded server scripts)
[12:30:02] [INFO] Loaded script server_scripts:example.js in 0.002 s
[12:30:02] [INFO] Loaded 1/1 KubeJS server scripts in 0.019 s with 0 errors and 0 warnings
[12:30:02] [INFO] Scripts loaded
[12:30:11] [INFO] Server resource reload complete!
steel grotto
#

did you rightclick a block before sending the log?

acoustic idol
#

yeah, but that's the wrong one. my files decided to reverse order

steel grotto
#

you can just upload the log directly, you dont need to copy the contents

acoustic idol
#

oh ok

dire otterBOT
#

Paste version of latest.log from @acoustic idol

acoustic idol
#

that's the log. lmk if it's not what you're looking for. I'll be unable to access my computer for the next hour, so it'll be delayed response

steel grotto
#

thats the latest log, not server log

acoustic idol
#

ok

steel grotto
#

i need the server log, nothing else :P

acoustic idol
#

then i'll have to take a look in an hour

steel grotto
#

you just need to copy it over thinking_lex

acoustic idol
#

how do I do that? because I'm only seeing the server log I sent above and the latest.log

#

also, apologies for how detailed you are having to be with this.

dire otterBOT
#

Please send your KubeJS server log. It can be found at /minecraft/logs/kubejs/server.log.
If you are on 1.18 or 1.16 it will be called server.txt.
Please send the file directly, without links or snippets.

acoustic idol
#

this is the only version I can find. I think this is my fault for running this in a modloader instead of vanilla modded launcher. all other server.log files are for the vanilla launch.

dire otterBOT
#

Paste version of server.log from @acoustic idol

steel grotto
#

right so that log says theres only one script file, and it is empty

#

the script i wrote is not in your game

acoustic idol
#

i've got it in \kubejs\server_scripts\src. I'm going to try moving it around to different folders and seeing if something will change it.

steel grotto
#

whats the file name

acoustic idol
#

logtest.json

steel grotto
#

uh

#

you need a javascript file, not a json file lol

#

logtest.js

acoustic idol
#

ah. that has probably been the entire freaking issue. didn't even notice.

steel grotto
#

your code editor shouldve been screaming at you

acoustic idol
#

i'm so used to ignoring errors when writing java because my ide is usually wrong lol. i can't believe i made such a simple mistake. fixing and running now

steel grotto
#

o.o

acoustic idol
#

ok looking like a syntax error, pulling up log file now

dire otterBOT
#

Paste version of server.log from @acoustic idol

acoustic idol
#

can you tell i am unfamiliar with writing mods lol

steel grotto
#

this isnt writing mods :P

#

so yeah that tells me a lot xD

#
BlockEvents.rightClicked(event => {
  console.log('start')
  console.log(event.block.hasTag('minecraft:leaves'))
  if (!event.block.hasTag('minecraft:leaves')) return
  console.log(event.block.id)
  if (event.block.id == 'tfc:fruit_tree_leaves') return
  console.log(event.item.id)
  if (event.item.id != 'exnihilosequentia:silkworm') return

  event.block.set('exnihilosequentia:infesting_leaves')
  event.item.count--
})
acoustic idol
#

yep, just caught the same parenthetical error. crosschecking between this and what i've got

#

alright running take 2

#

@steel grotto IT WORKS FUCK YES THANK YOU SO MUCH

#

I have learned a lot about javascript in the last 24 hrs lol

steel grotto
#

xD

#

and all because of the file extension

acoustic idol
#

kk so i tweaked it just slightly more to change the event.block.id == tfc fruit leaves to be event.block.hasTag(tfc fruit etc) and that fixed it completely omg im so excited thank you so much for all you've done!!!

steel grotto
cursive kayak
#

I'm glad you got it sorted in the end

#

Please close the ticket

dire otterBOT
#

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.

cursive kayak