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?
#Context to Action click()
66 messages · Page 1 of 1 (latest)
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
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.
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[]
https://github.com/SnaveSutit/blockbench-types actually this one seems up to date, now i just have to figure out how to import it
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)
got it all working, made a helper function to get around the annoying checks and casts :)
You can add custom properties using interfaces in a declare global block and interfaces
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
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
Group is just a type in Blockbench. It's not actual implementation. So adding fields in an interface is just modifying the type, not the functionality
That code I linked in Animated Java modifies multiple class types
Including Group
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)
Right, if you don't have any imports in a .d.ts file, it is considered a global file. IF you add imports or exports later though it turns into a module, and you need to add the declare global block.
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
What is setName a method on? I can't find it's declaration in the code for Group or OutlinerNode
Action
Action.setName is in my fork of the blockbench-types repo 
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
ah right, i never checked that
did the any cast for setName before switching to your types
I literally just added setName 2 seconds ago lmao
oh nvm
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)
yeah just had to go change the commit hash
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.
I finally found the documentation I was looking for to answer this question. I knew I'd read it before, but apparently I'd completely forgotten the name of the page...
https://www.typescriptlang.org/docs/handbook/declaration-merging.html
How merging namespaces and interfaces works
Found something where I actually wanted to create an issue and tell you about it - but turns out issues are actually disabled in your fork 😅 just thought I'd let you know
(#1338248431763722352 message this is the missing info if you're curious)
Huh, I wonder why they're disabled