#Retrieve the groupItem of an item - fabric 1.20.6
105 messages Β· Page 1 of 1 (latest)
Not sure if there's a proper way to do it, but the best I can see is fabrics ItemGroupHelper has all the groups as a public static sortedGroups variable, maybe iterate through that and check if the ItemStack is in any of them?
But afaik ItemStacks can be in multiple ItemGroups concurrently
Hello, thank you for your anwser. I don't find ItemGroupHelper neither sortedGroups variable in the doc (using maven fabric 1.20.6)
you have fabric api right?
Yes
if your in idea just click CTRL + N and it shows up for me
what version are you on?
(mc version)
Oh, i'm in 1.20.6
my fabric api is 0.98.0+1.20.6
i noticed i'm not up to date, i'll try to do that
ah yeah ItemGroupHelper doesn't exist in 1.20.6, they musta removed it
lemme see for 1.20.6
I guess i'm in the right way to do it, however ItemGroups is empty, so it does nothing. Is it due to the face i'm not calling the instance ?
currentBlock is itemStack
ah ItemGroups are in the registry now
oh yeah you can do what you're doing then yeah
That's strange it does nothing, completely empty π¦
a utility function to return the ItemGroup for a stack:
public static ItemGroup getItemGroup(ItemStack findStack) {
for (ItemGroup group : ItemGroups.getGroups()) {
for (ItemStack stack : group.getDisplayStacks()) {
if (stack.isOf(findStack.getItem())) {
return group;
}
}
}
return null;
}
but like I said I think ItemStacks can be found in multiple ItemGroups
so this would just return the first one
also this works but there could be a better way of doing this if anyone wants to chime in
Alright thank you ! I'll try after I ate. I'll keep you up to date π
no worries
okay so, i copy pasted your method and tried it, i still don't get the group item. I added a LOGGER.info after the if (stack.isOf(findStack.getItem())) and it never prints
at what point in your code are you running it, cause maybe the item groups are not initialized when your running them I guess?
hmm, i call it on a custom button click, where it sends a custom packet to the main mod file
hmm not sure actually
I assume the ItemGroups is initialized at the very beginning of the mod start
wait I'm messing around with it
this is weird it feels like it should be working
alright I my function uses the wrong list of items I guess
for (ItemStack stack : group.getSearchTabStacks()) { is what you want
not sure what the difference of searchTabStacks and displayStacks are but searchTabStacks seems to work
you know what maybe I'm entirely wrong ngl
could be working by miracle
lmao
Ok i'll try ! Thank you anyway for the help it's nice π
doesn't seems to work, i wonder if it's a real bug π
nah probably just a mistake I'm making, cause I'm just giving you the best I got for how to do this
I looked into it more, I think the actual ItemStacks only initalize after the client has opened the creative invnetory and then you can look for the items ._.
maybe there's something else??
very weird
also incase there's another way to do what you want, why do you want the ItemGroup of some item?
I would like to be able to sort items according to itemgroup categories
right yeah that does* sound needed
well that's it, it could have been cool but it's not a big issue for the mod
well there's a cursed solution, when the creative screen opens it basically runs ItemGroups#updateDisplayContext.
Maybe you can run it once on mod initialization to update all the itemstacks and then just use the helper function I gave?
it's up to you on how cursed you want it to get lmao
but yeah anyways I don't see anything else with the way vanilla seems to do it
ooh it's a tricky idea, i will give it a shot
it needs
- a
FeatureSetobject which you can get fromclient.player.networkHandler.getEnabledFeatures() - a boolean for whether to update the stacks with the operator tab included
- and a
RegistryWrapper.WrapperLookupwhich you can get fromworld.getRegistryManager
I think it's maybe feasible
but it's a weird hack
maybe all this time fabric has a nice utility function to just do getItemGroup(item) lmao
i'm currently trying it
i hope no, with all the combination of keywords of groupItem i tried on intellij, i hope not
about the featureSet param, i don't get the getter method from networkHandler
wdym it doesn't exist or?
yep don't existe, i tried with features word directly, still nothing
ah okay so
you said you want the item group for when a button was clicked right?
and I assume that's on the client?
yes button on client, using mixin
can't items be in multiple groups?
yes they can already said that
so if you want the items to be initialize on the client for when the button is clicked you want to initialize them on the client
so ig when he joins a server
hmm yep, i see what u mean, so onInitializeClient, which is more logical
and when he's connected to a server so when he has access to client.player.networkHandler.getEnabledFeatures() which I assume is initialized at some point before ClientPlayConnectionEvents.JOIN
yes alright, now i got it π
lmao lemme just test it aswell
wait now that I think of it if the player is in survival it might not work cause I think FeatureSet is like what he's allowed to do and what he isn't
i'm trying it rn
Cannot read field "networkHandler" because "net.minecraft.client.MinecraftClient.getInstance().player" is null
damn
you shouldn't be doing it in onInitializeClient if that's what you're doing
you should register an event for ClientPlayConnectionEvents.JOIN which is after the player connected to the server
Fuck i used wrong event
no worries lol
this feature make me crazy
ikr
vanilla having weird code + fabric not having a simple api to find the item group(s)
yes true
i've tried forge at the beginning, i instantly quit
no documentation that's a bye bye
yeah that's also what lead me away from forge, not that fabric has the best documentation but it feels easier to get into as a newbie
okay great news : it works
π₯³
Huge thank you ! I would have left this feature honestly
niceee
you might want to actually think of some edge cases cause I'm not entirely aware of all of them
with this way of doing it
but if you see it works and there's no bugs (hopefully) then mission complete
yes, i'll play a bit with it ! May i contact you on private ?