Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creative tab crashes servers in 1.21.1-0.1.10b when mods try to observe its contents serverside #24

Open
Commoble opened this issue Jan 26, 2025 · 2 comments

Comments

@Commoble
Copy link

Log:
https://api.mclo.gs/1/raw/FqZSPtI

This issue occurs when another mod (computercraft in this case) evaluates creative tab contents serverside. This is legal for other mods to do, so mods need to be careful about using client code while populating their creative tabs.

Cause of the issue is here https://github.com/TheGridExpert/VampiresDelight/blob/1.21/src/main/java/net/grid/vampiresdelight/common/item/WeatheredLetterItem.java#L49-L58

    public static void generateCreativeTab(CreativeModeTab.Output output) {
        ClientLevel level = Minecraft.getInstance().level;

        if (level != null) {
            Registry<WeatheredLetter> registry = level.registryAccess().registryOrThrow(VDRegistries.WEATHERED_LETTER);

            registry.stream().findAny().ifPresent(letter -> output.accept(makeLetter(letter), CreativeModeTab.TabVisibility.PARENT_TAB_ONLY));
            registry.stream().forEach(letter -> output.accept(makeLetter(letter), CreativeModeTab.TabVisibility.SEARCH_TAB_ONLY));
        }
    }

Referring to net.minecraft.client classes is unsafe from within Item classes in any case, even setting aside the creative tab issue.

However, the client classes shouldn't be needed here. Observe the creative tab builder:

https://github.com/TheGridExpert/VampiresDelight/blob/1.21/src/main/java/net/grid/vampiresdelight/common/registry/VDCreativeTabs.java

    public static final Supplier<CreativeModeTab> TAB_VAMPIRES_DELIGHT = CREATIVE_TABS.register(VampiresDelight.MODID,
            () -> CreativeModeTab.builder()
                    .title(Component.translatable("itemGroup.vampiresdelight"))
                    .icon(() -> new ItemStack(VDBlocks.DARK_STONE_STOVE.get()))
                    .displayItems((displayParams, output) -> {
                        // . . .
                        WeatheredLetterItem.generateCreativeTab(output);

You can use displayParams.holders() to get a HolderLookup.Provider, which is a view over the registries, averting the need to get registries from the client level, and allowing your tab to be functional when mods try to check its contents serverside.

@VaporeonScripts
Copy link

VaporeonScripts commented Jan 26, 2025

no way, im in the logs!!! :D

@TheGridExpert
Copy link
Owner

TheGridExpert commented Jan 26, 2025

Hello. Thank you for reporting, I wasn't aware of this issue. Here's a changed part of the code.

public static void generateCreativeTab(CreativeModeTab.ItemDisplayParameters displayParameters, CreativeModeTab.Output output) {
        Optional<HolderLookup.RegistryLookup<WeatheredLetter>> registryLookup = displayParameters.holders().lookup(VDRegistries.WEATHERED_LETTER);

        registryLookup.ifPresent(lookup -> {
            lookup.listElements().findAny().ifPresent(letter -> output.accept(makeLetter(letter.value()), CreativeModeTab.TabVisibility.PARENT_TAB_ONLY));
            lookup.listElements().forEach(letter -> output.accept(makeLetter(letter.value()), CreativeModeTab.TabVisibility.SEARCH_TAB_ONLY));
        });
    }

Please checks if it works for you now, here's an updated version of the mod.

VampiresDelight-1.21.1-0.1.10c-beta.1.jar.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants