#Retrieve the groupItem of an item - fabric 1.20.6

105 messages Β· Page 1 of 1 (latest)

jolly zephyr
#

Hello i would like to know if it's possible to retrieve the category of an item (like the category each item have on creative gamemode)
Thank you for your answers πŸ™‚

acoustic venture
#

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

jolly zephyr
#

Hello, thank you for your anwser. I don't find ItemGroupHelper neither sortedGroups variable in the doc (using maven fabric 1.20.6)

acoustic venture
#

you have fabric api right?

jolly zephyr
#

Yes

acoustic venture
#

if your in idea just click CTRL + N and it shows up for me

#

what version are you on?

#

(mc version)

jolly zephyr
#

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

acoustic venture
#

ah yeah ItemGroupHelper doesn't exist in 1.20.6, they musta removed it

#

lemme see for 1.20.6

jolly zephyr
#

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

acoustic venture
#

ah ItemGroups are in the registry now

#

oh yeah you can do what you're doing then yeah

jolly zephyr
#

That's strange it does nothing, completely empty 😦

acoustic venture
#

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

jolly zephyr
#

Alright thank you ! I'll try after I ate. I'll keep you up to date πŸ™‚

acoustic venture
#

no worries

jolly zephyr
#

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

acoustic venture
#

at what point in your code are you running it, cause maybe the item groups are not initialized when your running them I guess?

jolly zephyr
#

hmm, i call it on a custom button click, where it sends a custom packet to the main mod file

acoustic venture
#

hmm not sure actually

jolly zephyr
#

I assume the ItemGroups is initialized at the very beginning of the mod start

acoustic venture
#

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

jolly zephyr
#

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 πŸ’€

acoustic venture
#

nah probably just a mistake I'm making, cause I'm just giving you the best I got for how to do this

acoustic venture
jolly zephyr
#

nooo i was afraid of that

#

that's so silly

acoustic venture
#

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?

jolly zephyr
#

I would like to be able to sort items according to itemgroup categories

acoustic venture
#

right yeah that does* sound needed

jolly zephyr
#

well that's it, it could have been cool but it's not a big issue for the mod

acoustic venture
#

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

jolly zephyr
#

ooh it's a tricky idea, i will give it a shot

acoustic venture
#

it needs

  • a FeatureSet object which you can get from client.player.networkHandler.getEnabledFeatures()
  • a boolean for whether to update the stacks with the operator tab included
  • and a RegistryWrapper.WrapperLookup which you can get from world.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

jolly zephyr
#

i'm currently trying it

jolly zephyr
acoustic venture
#

hahaha

#

same

jolly zephyr
#

about the featureSet param, i don't get the getter method from networkHandler

acoustic venture
#

wdym it doesn't exist or?

jolly zephyr
#

yep don't existe, i tried with features word directly, still nothing

acoustic venture
#

seems to exist for me

#

how are you trying to access it?

#

like show me the code

jolly zephyr
#

whoah it's small, sorry

acoustic venture
#

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?

jolly zephyr
#

yes button on client, using mixin

merry sierra
#

can't items be in multiple groups?

acoustic venture
#

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

jolly zephyr
#

hmm yep, i see what u mean, so onInitializeClient, which is more logical

acoustic venture
#

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

jolly zephyr
#

yes alright, now i got it πŸ™‚

acoustic venture
#

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

jolly zephyr
#

i'm trying it rn

acoustic venture
#

ah nvm features are just this type of stuff

#

should work fine

jolly zephyr
#

Cannot read field "networkHandler" because "net.minecraft.client.MinecraftClient.getInstance().player" is null

#

damn

acoustic venture
#

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

jolly zephyr
#

Fuck i used wrong event

acoustic venture
#

no worries lol

jolly zephyr
#

this feature make me crazy

acoustic venture
#

ikr

#

vanilla having weird code + fabric not having a simple api to find the item group(s)

jolly zephyr
#

yes true

#

i've tried forge at the beginning, i instantly quit

#

no documentation that's a bye bye

acoustic venture
#

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

jolly zephyr
#

okay great news : it works

#

πŸ₯³

#

Huge thank you ! I would have left this feature honestly

acoustic venture
#

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

jolly zephyr
#

yes, i'll play a bit with it ! May i contact you on private ?