#Is there an event for using an item

203 messages · Page 1 of 1 (latest)

terse phoenix
#

I am trying to find an event that doesn't require ticking but can detect when a player begins using a certain item.
2nd option is trying to edit the .use method in an item registry event in StartupEvents but idk the correct syntax for it. As of right now i just added a boolean to each item .use method like in the tutorials , because when i tried doing something else it sends an error saying it expects a boolean and just crashes the game.

event
.create('betterasfour:amethyst_crab_scroll')
.texture('betterasfour:item/amethyst_crab_scroll')
.unstackable()
.tooltip('Use to increase the spell level for "Conjure Amethyst Crab" by 1 (Max level is 1, Must have summoner school!)')
.displayName("Scroll of Conjure Amethyst Crab")
.rarity("epic")
.useAnimation('bow')
.useDuration(itemstack => 60)
.use((level, entity, hand) => true)  **How do I edit the use method**
.releaseUsing((itemstack, level, player) => {
player.playSound("entity.villager.no")
})
.finishUsing((itemstack, level, player) => {
                        player.runCommandSilent("puffish_skills skills unlock @s puffish_skills:schools1 isarg2egegtmq2sf")
                        player.playSound("entity.villager.trade")
                    }
                )
            }
)```
gilded fractalBOT
#

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

solid pagoda
#

ItemEvents.rightClick should be the one

worn lodge
#

for a custom item you are right for using the .use(), but you need to return a boolean

terse phoenix
#

Something simple

terse phoenix
worn lodge
#
.use((level, entity, hand) => {
  // do your stuff here
  return true
})
#

@terse phoenix

#

only return true tho when you want to use to take action

#

if you want to fail return false

terse phoenix
#

so i did this

#

and it works on the event.cancel

#

but when i try to activate it with me having the stage, it crashes the game

#

do i need to create a seperate condition and type return true there as well?

#

0r waot

#
    if (!player.stages.has("summoner")){
        player.tell("Only summoners can use this!")
        event.cancel
    } else return true
})``` When i did this it worked the other way around crashing when i dont have the stages and working when i do
#

Nevermind I GOT IT

worn lodge
#

don't do event cancel, startup dont need cancelation

#

it is return false

#

not event cancel

terse phoenix
#

ye i got it

#

Ty

#

Im trying now something even more complicated

#

not that that was complicated

#

it's just different

terse phoenix
worn lodge
#

show code

terse phoenix
# worn lodge show code
    if ((player.getAttributeValue("additional_attributes:innate_spell/cataclysm_spellbooks/conjure_amethyst_crab") == 0)){
    if (!player.stages.has("summoner")){
        player.tell("Only summoners can use this!")
        player.playSound("entity.villager.no")
        return false
    } else return true
    } else {
        player.tell("Max level has been reached!")
        player.playSound("entity.villager.no")
        return false
    }
})
.releaseUsing((itemstack, level, player) => {
player.playSound("entity.villager.no")
})
.finishUsing((itemstack, level, player) => {
    player.playSound("entity.villager.trade")
    player.runCommandSilent("puffish_skills skills unlock @s puffish_skills:schools1 isarg2egegtmq2sf")
    }
)}) ```
#

do i need to call server?

worn lodge
#

remember that player.runCommandSilent this will check player permissions, if player can do that it is fine, but I don't know if player is allowed to run that without OP

terse phoenix
#

Wait?

#

Does it have to?

worn lodge
#

i would rather get player.server so you run from the server

terse phoenix
#

Is it like that on any type of script?

worn lodge
#

and pass player name

#

yes

terse phoenix
#

Oh No ;-;

worn lodge
#

if you call player.runCommand

#

if you call server it is fine

terse phoenix
#

I have so much fixing to do that

#

then*

solid pagoda
#

if you are using vscode you can just use the find and replace feature and then you can fix everything in one click

worn lodge
#

just replace player with player.server
and fix the name instead of @s

terse phoenix
#

Lexxi you dont understand how many scripts i got ;-;

solid pagoda
#

irrelevant tho

worn lodge
#

@s -> player.username I guess?

terse phoenix
#

will @s work?

#

I mean

worn lodge
#

on server no

terse phoenix
#

will replacing it with player.username work?

worn lodge
#

you need player.username

terse phoenix
#

even if it is on string?

worn lodge
#

username is string

#

oh no

#

you need to evaluate

#

${player.username}

#

if using backticks

terse phoenix
#

;-; oh no im gonna die today

#

So much fixing

#

I guess i wil try to do it with the mark all

worn lodge
#

if not using backticks do " + player.username + "

terse phoenix
#

$[player.username} this is the safer way right?

solid pagoda
#

._.

terse phoenix
#

it'd work either ways

worn lodge
#

safer in which way?

#

there is no diff

terse phoenix
#

no i mean i dont remember if i have ones with backticks or no >_>

#

I didn't know there was a difference

#

Well i will give it a shot and see what happens

worn lodge
#

that is what happens when you test scripts with OP/Creative 😭

#

you would notice on first test

terse phoenix
#

ye but i dont have any other means ;-;

worn lodge
terse phoenix
#

and wait

#

if use "commands" through API methods

#

like playsound

#

It'd be fine right?

#

or should i call server there as well

worn lodge
#

playsound is tricky and bugged, don't know which one is bugged

#

also depends if you want it to play on the level or on client side of the player

terse phoenix
#

client side

worn lodge
#

player should be fine

terse phoenix
#

Well what does it mean about player.tell then >_<?

#

Am i screwed

#

Before using ${player.username} do i need to make any variables. Is it fine if according to VS it is part of a string?

worn lodge
#

player needs to exists lol

#

of a string, it will be wrong

terse phoenix
#

Ye i guessed so

worn lodge
#

needs to be backstick

terse phoenix
#

O I SEE

#

then im mega screwed

#

"puffish_skills skills unlock '${player.username}' puffish_skills:schools1 eeiol77mbim197ls" supposedly like so?

worn lodge
#
- player.runCommandSilent("puffish_skills skills unlock @s puffish_skills:schools1 isarg2egegtmq2sf")

+ player.server.runCommandSilent("puffish_skills skills unlock " + player.username + " puffish_skills:schools1 isarg2egegtmq2sf")
worn lodge
#

if using ` backstick it is liek this:

#
player.server.runCommandSilent(`puffish_skills skills unlock ${player.username} puffish_skills:schools1 isarg2egegtmq2sf`)
terse phoenix
#

O the whole thing needs to be in backstick >_>

#

O im HELA screwed

#

Is it fine if player is a parameter for $LivingEntity instead of a variable?

worn lodge
#

show it

terse phoenix
#
    player.playSound("entity.villager.trade")
    let attvalue = player.getAttributeValue("additional_attributes:innate_spell/cataclysm_spellbooks/conjure_ignited_reinforcement")
    if (attvalue == 0) {
                        player.runCommandSilent(`"puffish_skills skills unlock ${player.username} puffish_skills:schools1 amp7f2p9l03jwumz"`)
    }
    if (attvalue == 1) {
                        player.runCommandSilent(`"puffish_skills skills unlock ${player.username} puffish_skills:schools1 zh82qhte5z66xb6k"`)
    }
    if (attvalue == 2) {
                        player.runCommandSilent(`"puffish_skills skills unlock ${player.username} puffish_skills:schools1 avw5x2krujlsix1e"`)
    }
                    }
                )
            }```
#

See how player is a parameter on .finishUsing

worn lodge
#

you need to remove those " after the `

#

you don't need any " inside it

worn lodge
terse phoenix
#

i figured as much -.- that just make it even more fun...

#

removing "" ;-;

#

so for a recap

#

I need to change every player.runcommandSilent to player.server.runcommandSilent on every script possible

#

Change to `` for every command script

#

and change @s to the ${player.username}

worn lodge
#

and pray

#

lol

terse phoenix
#

🙏

#

at least thank god i forced myself to use the api most of the time because of my reluctance

worn lodge
#

i personally avoid every command stuff, unless that saves me some code

#

your puffskills looks like a decent use tho

terse phoenix
#

i did sadly use commands for quiet a bit of puffish but also quiet a bit of playsound because as you said it is bugged..

#

i gotta make sure though, for certain: player.tell doesn't require the player to be op or become player.server.tell?

#

Even on server scripts?

#

and what about player.potionEffects.add ?

#

@worn lodge sorry i just really need to know do i need to add .server to any player.<method>

#

or is it specifically for commands

terse phoenix
#

WAIT CAN I NOT TEST the runcommandSIlent on singleplayer because it isn't server >_>?

#

I keep getting disconnected due to network issue when i try to use it

#

seems like having player.server.runcommandSilent on a startupEvent doesn't work properly

#

no because i cant set player as a variable in startupEvents.registry i have to create a server script for it im dead

#

it detects null

terse phoenix
#

@solid pagoda ive encountered a problem with using ItemEvents.rightClicked for the functionality of an item that has a useDuration. It uses the functionality even without finishing the duration of the use obviously since it activates on first right click.... Is there a way around it? Other than making it an insta use

solid pagoda
#

pretty sure thats because player is the default ServerPlayer, not the kubejs player, unless that changed

terse phoenix
terse phoenix
#

yikes

terse phoenix
solid pagoda
#

i think you either need to check for the second event call, or if its a custom item, probably best to use the builder function like finishUsing (if i recall correctly)

terse phoenix
#

on a server event?

#

I tried using finishUsingItem method but it asks for a level for no reason and it doesn't even work

#

forget about the return event.cancel pay it no mind it's just to showcase the only method available for finishUse type of stuff

worn lodge
#

@terse phoenix oh I remember now, I think you need to filter the side of the use, it fires on client and server? can you do the first line to be console.log(player.level)

see if it prints client level and server level

coment everything else, and let only return true besides that

terse phoenix
#

filter the side of the use? like only client or only server

#

i will try

#

@worn lodge ```StartupEvents.registry('item', event => {
event
.create('betterasfour:ignited_reinforcement_scroll')
.texture('betterasfour:item/ignited_reinforcement_scroll')
.unstackable()
.tooltip('Use to increase the spell level for "Conjure Ignited Reinforcement" by 1 (Max level is 3, Must have summoner school!)')
.displayName("Scroll of Conjure Ignited Reinforcements")
.rarity("epic")
.useAnimation('bow')
.useDuration(itemstack => 60)
.use((level, player, hand) => {
let attvalue = player.getAttributeValue("additional_attributes:innate_spell/cataclysm_spellbooks/conjure_ignited_reinforcement")
if (attvalue == 3) {
player.tell("Max Level has been reached")
player.playSound("entity.villager.no")
return false
}
if (!player.stages.has("summoner")){
player.tell("Only summoners can use this!")
player.playSound("entity.villager.no")
return false
} else return true
})
.releaseUsing((itemstack, level, player) => {
player.playSound("entity.villager.no")
})
.finishUsing((itemstack, level, player) => {
let attvalue = player.getAttributeValue("additional_attributes:innate_spell/cataclysm_spellbooks/conjure_ignited_reinforcement")
if (attvalue == 0) {
player.server.runCommandSilent(puffish_skills skills unlock ${player.username} puffish_skills:schools1 amp7f2p9l03jwumz)
}
if (attvalue == 1) {
player.server.runCommandSilent(puffish_skills skills unlock ${player.username} puffish_skills:schools1 zh82qhte5z66xb6k)
}
if (attvalue == 2) {
player.server.runCommandSilent(puffish_skills skills unlock ${player.username} puffish_skills:schools1 avw5x2krujlsix1e)
}
player.playSound("entity.villager.trade")

                }
            )
        }

)``` this is what i currently have

#

you basically want me to console.log on the .use?

#

or the .finishUsing?

#

The protocol error i get is when it finishes using

worn lodge
#

one the use and on finishUsing too, but finishUsing you already have direct access to level, so do console.log(level)

terse phoenix
#

ye sorry im new to this and i never used the console for checking things so im trying to understand how to use it

#

couple mins

#

is this it?

worn lodge
#

what

#

what are you doing with node stuff

terse phoenix
#

Im not using any node stuff ;-;

#

i dont even know how i got this node thing in

worn lodge
#
.use((level, player, hand) => {
  console.log(level) 
  return true
})

oh you have access to level on use too just directly log it

terse phoenix
#

what am i supposed to expect as a result ;-;

#

i have the same message again..

solid pagoda
#

what message?

terse phoenix
solid pagoda
#

how did you even get there?!

#

thats for website servers

#

has NOTHING to do with minecraft

terse phoenix
#

Ok so you mean console.log logs onto the startup log?

#

or the latest logs?

solid pagoda
#

startup

terse phoenix
#

_> HMMM my bad i tried it in the past and it didn't work so i didnt know

#

well it logged nothing into the startup log

#

maybe i did it wrorng

#
    // let attvalue = player.getAttributeValue("additional_attributes:innate_spell/cataclysm_spellbooks/conjure_ignited_reinforcement")
    // if (attvalue == 3) {
    //     player.tell("Max Level has been reached")
    //     player.playSound("entity.villager.no")
    //     return false
    // }
    // if (!player.stages.has("summoner")){
    //     player.tell("Only summoners can use this!")
    //     player.playSound("entity.villager.no")
    //     return false
    // } else return true
    console.log(level)
})
.releaseUsing((itemstack, level, player) => {
player.playSound("entity.villager.no")
})
.finishUsing((itemstack, level, player) => {
    console.log(level) js```
#

is this not like so?

solid pagoda
#

it is
did you use the item?

terse phoenix
#

Madam i refuse to answer the question

#

i will answer the question in a sec

solid pagoda
terse phoenix
#

ClientLevel [net.minecraft.client.multiplayer.ClientLevel]

#

i got this

#

so it does go to client

#

Sorry i assumed there was a smarter way to log that type of stuff >_>

worn lodge
#

did you eat the return true? 😐

solid pagoda
terse phoenix
#

ye my bad

#

fixed it rn

#

i got so flustered i forgot about it

#

this is the result

#

seems like it does go for both

#

you were right

#

How do i filter this though?

worn lodge
#
if (level.client) {
  // do client stuff
  return true
} else {
  // do server stuff
  return true
}
terse phoenix
#

oooo

worn lodge
#

you need to add return on both too

#

don't forget to return true/false

#

.client is short for .isClient()

terse phoenix
#

hmm this make the code so much longer

worn lodge
#

it is what it is :/

terse phoenix
#

it's definitely worth it i mean i dont have to do some makeshift rightClicked event

distant matrixBOT
#

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.