#Context to Action click()

66 messages · Page 1 of 1 (latest)

charred whale
#

I'm trying to make a custom Action to add to the Group menu, and I'm trying to implement the click() function on it. For example, say the action would change the group's name. I would hope for something like js click(group) { group.name = "Hello"; } , but the argument to click doesn't contain the group. How can I get the group that this action was run on?

#

actually maybe it does contain the group, i havent tested it

#

but the typescript bindings just say the type is Event, which just says whether the shift key is held or not when clicking

uneven wind
#

Actions are generally global, so using them doesn't provide menu context. You can just use Group.multi_selected to get the list of selected groups, which would contain the one that was clicked, since opening a context menu also always selects a group or element. That way it would also work when assigned to a keyboard shortcut

#

If you do want to directly get the group that was selected, you can also add an object that resolves to a menu entry directly into the context menu. That way the context (here group) is the first argument of the click function.

charred whale
#

ah using the selected group(s) seems like a good idea

#

for some reason though my click function doesn't seem to even be running at all which is kinda weird

#

the action appears in the menu, but clicking it doesn't log anything or run the text prompt

#

oh, very interesting

#

the condition is evaluated with the group as the context when determining whether it's visible

#

but is evaluated with the action as the context when you actually click it

#

at least, i think? not sure

#

okay, so my condition function also can't rely on its argument either

#

need to get the currently selected group(s) for that one as well

#

also... what should i do if my action only really makes sense to apply to a single group?

#

applying it on multiple groups at once feels incorrect, but you can have multiple groups selected

#

i guess i could make it part of the condition that only 1 group is selected?

#

and multi_selected also isnt in the typescript bindings 😔

#

huh, it is present if i update the bindings to latest, but its type is Group?? not Group[]

charred whale
#

alright, the clicking is working now, after changing a bunch

#

however sadly the icon lambda doesn't seem to work anymore, even though it used to in a past thing

#

it was really neat how the icon changed between checked and unchecked

#

yay~ through the power of an extremely cursed condition with side effects, it's working now

#

(also it's very painful to access group properties with typescript 😔 but not much can be done about that really)

charred whale
#

got it all working, made a helper function to get around the annoying checks and casts :)

ripe yoke
#
declare global {
  interface Group {
    vanilla_root: string
    replace_vanilla_root: boolean
  }
}
#

Animated Java uses this for it's custom properties and modifications

#

TypeScript merges duplicate interface definitions for this specific reason

charred whale
#

oh, neat

#

does that work even though group is a class?

#

I googled it and from my limited understanding it said you can’t merge with a class, but idk much about typescript so I’ll give your thing a try

ripe yoke
ripe yoke
#

Including Group

charred whale
#

i tried adding this to my tsconfig.json but it's still complaining when i use those fields in Group

#

oh, i needed to just not have the declare global block at all

#

only declare interface Group

#

alright, cool :>

#

i now have 2 different conditions with side effects because i wanted actions' name/icon to change depending on the selected group :catplush:

#

this type extending strategy also means i can get rid of the any cast in order to call setName (since that function isn't in the bindings)

ripe yoke
#

You can avoid using a .d.ts file if you want, and put those declarations inside of each .ts file where their systems are actually implemented if you like that better. Works either way

ripe yoke
charred whale
#

Action

ripe yoke
#

Action.setName is in my fork of the blockbench-types repo evilsmile

#

You can install it directly using a package manager.
npm i --save-dev https://github.com/SnaveSutit/blockbench-types.git
yarn add -D https://github.com/SnaveSutit/blockbench-types.git

Or you can clone it using git, then reference it in your tsconfig

charred whale
#

ah right, i never checked that

#

did the any cast for setName before switching to your types

ripe yoke
#

I literally just added setName 2 seconds ago lmao

charred whale
#

oh nvm

ripe yoke
#

You'd need to update it locally if you haven't updated since 2 seconds ago

#

(I'd be surprised if you had just updated it lmao)

charred whale
#

yeah just had to go change the commit hash

ripe yoke
#

Feel free to open issues on my types fork, I'm much more active than Jannis is in fixing them because I'm constantly using them for my plugin projects.

ripe yoke
charred whale
ripe yoke
#

Huh, I wonder why they're disabled