Vampire's Delight

Vampire's Delight

161k Downloads

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

Commoble opened this issue ยท 3 comments

commented

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.

commented

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

commented

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

commented

@TheGridExpert Hey, thanks for the update! I can verify VampiresDelight-1.21.1-0.1.10c-beta.1.jar resolves the issue