Jump to content

GUI Closes after Opening


Hipposgrumm

Recommended Posts

I know that there are other posts like this but none answer my question. If you know of osme ticket that I missed can you please send that?

 

I have a custom GUI that I am trying to create a recipe type for, but the gui closes on open.

I have frankensteined some content from vanilla minecraft, some other crafting-related mods, and a tutorial on custom crafting recipes, so some stuff may be mixed up or redundant.

 

Block:

Spoiler
// Imports Cut

public class EggingTableBlock extends Block {
    private static final Component CONTAINER_TITLE = Component.translatable("container.egging");
    public EggingTableBlock() {super(BlockBehaviour.Properties.of(Material.STONE, MaterialColor.SAND).sound(SoundType.WOOD).strength(1.6f, 3.5f));}
  
    public InteractionResult use(BlockState block, Level level, BlockPos pos, Player player, InteractionHand interactHand, BlockHitResult hitresult) {
        if (level.isClientSide) {
            return InteractionResult.SUCCESS;
        } else {
            var b_entity = level.getBlockEntity(pos);
            // If dragonIsDefeated or entity instanceof Player has instabuild (creative)
            if (true  || ((Entity)player instanceof Player _plr ? _plr.getAbilities().instabuild : false)) {
                player.openMenu(block.getMenuProvider(level, pos));
                Main.LOGGER.error("Opened GUI via Right Click"); // Debug
            } else {
                // Not Important
            }
            player.awardStat(Stats.INTERACT_WITH_CRAFTING_TABLE);
            return InteractionResult.CONSUME;
        }
    }

    public MenuProvider getMenuProvider(BlockState state, Level level, BlockPos pos) {
        return new SimpleMenuProvider((id, inv, player) -> new CraftingMenu(id, inv, ContainerLevelAccess.create(level, pos)), CONTAINER_TITLE);
    }
}

 

Menu:

Spoiler
// Cut Imports

public class EggingTableGUI extends AbstractContainerMenu {
    public static final int RESULT_SLOT = 10;
    public static final int MAIN_INGREDIENT_SLOT = 0;
    private static final int CRAFT_SLOT_START = 1;
    private static final int CRAFT_SLOT_END = 9;
    private static final int INV_SLOT_START = 11;
    private static final int INV_SLOT_END = 38;
    private static final int USE_ROW_SLOT_START = 38;
    private static final int USE_ROW_SLOT_END = 47;
    private final CraftingContainer craftSlots = new CraftingContainer(this, 3, 3);
    private final CraftingContainer inputSlot = new CraftingContainer(this,1,1);
    private final ResultContainer resultSlot = new ResultContainer();
    private final ContainerLevelAccess access;
    private final Player player;

    public EggingTableGUI(int id, Inventory inv, net.minecraft.network.FriendlyByteBuf extraData) {this(id, inv, ContainerLevelAccess.NULL);}

    public EggingTableGUI(int id, Inventory inv, ContainerLevelAccess access) {
        super(GUIRegisterer.EGGING_GUI.get(), id);
        this.access = access;
        this.player = inv.player;
        addPlayerInventory(inv);
        addPlayerHotbar(inv);
        this.addSlot(new ResultSlot(inv.player, this.craftSlots, this.resultSlot, 0, 140, 40));
        this.addSlot(new Slot(this.inputSlot,0,96,40));
        for (int i = 1; i < 10; i++) {
            for (int j = 0; j < 37; j += 18) {
                for (int l = 0; l < 37; l += 18) {
                    this.addSlot(new Slot(this.craftSlots, i, 16 + l, 22 + j));
                }
            }
        }
    }

    protected static void slotChangedCraftingGrid(/*Cut*/) {
      // I cut this because it is long and has nothing to do with the problem
    }

    public void slotsChanged(Container crafting) {
        this.access.execute((level, pos) -> {
            slotChangedCraftingGrid(this, level, this.player, this.craftSlots, this.resultSlot);
        });
    }

    public void fillCraftSlotsStackedContents(StackedContents itemstack) {
        this.craftSlots.fillStackedContents(itemstack);
    }

    public void clearCraftingContent() {
        this.craftSlots.clearContent();
        this.inputSlot.clearContent();
        this.resultSlot.clearContent();
    }

    public void removed(Player player) {
        super.removed(player);
        this.access.execute((level, pos) -> {
            this.clearContainer(player, this.craftSlots);
        });
    }

    public boolean stillValid(Player player) {
        return stillValid(this.access, player, Main.EGGING_TABLE.get());
    }

    public ItemStack quickMoveStack(Player player, int slotselected) {
      // I cut this from my example because it is too long nad has nothing to do with the problem.
    }

    public boolean canTakeItemForPickAll(ItemStack stack, Slot slot) {
        return slot.container != this.resultSlot && super.canTakeItemForPickAll(stack, slot);
    }

    public int getResultSlotIndex() {return RESULT_SLOT;}

    public int getGridWidth() {return this.craftSlots.getWidth();}

    public int getGridHeight() {return this.craftSlots.getHeight();}

    public int getSize() {return 11;}

    //public RecipeBookType getRecipeBookType() {return RecipeBookType.CRAFTING;}

    public boolean shouldMoveToInventory(int slot) {return slot != this.getResultSlotIndex();}

    private void addPlayerInventory(Inventory playerInventory) {
        for (int i = 0; i < 3; ++i) {
            for (int l = 0; l < 9; ++l) {
                this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 86 + i * 18));
            }
        }
    }

    private void addPlayerHotbar(Inventory playerInventory) {
        for (int i = 0; i < 9; ++i) {
            this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 144));
        }
    }
}

 

Screen:

Spoiler

Probably not important but I'll include it anyway in case it is.

// Imports Cut

public class EggingTableGUIHelper extends AbstractContainerScreen<EggingTableGUI> {
    private static final ResourceLocation texture = new ResourceLocation("craftable_spawn_eggs:textures/screens/egging_gui.png");

    public EggingTableGUIHelper(EggingTableGUI container, Inventory inventory, Component text) {
        super(container, inventory, text);
        this.imageWidth = 176;
        this.imageHeight = 166;
    }

    @Override
    public void init() {
        super.init();
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
    }

    @Override
    public void render(PoseStack ms, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(ms);
        super.render(ms, mouseX, mouseY, partialTicks);
        this.renderTooltip(ms, mouseX, mouseY);
    }

    @Override
    protected void renderBg(PoseStack ms, float partialTicks, int gx, int gy) {
        RenderSystem.setShaderColor(1, 1, 1, 1);
        RenderSystem.enableBlend();
        RenderSystem.defaultBlendFunc();
        RenderSystem.setShaderTexture(0, texture);
        this.blit(ms, this.leftPos, this.topPos, (width-imageWidth)/2, (height-imageHeight)/2, this.imageWidth, this.imageHeight);
        RenderSystem.disableBlend();
    }

    @Override
    public boolean keyPressed(int key, int b, int c) {
        if (key == 256) {
            this.minecraft.player.closeContainer();
            return true;
        }
        return super.keyPressed(key, b, c);
    }

    @Override
    public void containerTick() {
        super.containerTick();
    }

    @Override
    protected void renderLabels(PoseStack poseStack, int mouseX, int mouseY) {
        this.font.draw(poseStack, "Egging Table", 60, 7, -12829636);
    }

    @Override
    public void onClose() {
        super.onClose();
        Minecraft.getInstance().keyboardHandler.setSendRepeatsToGui(false);
    }
}

 

GUI Registerer:

Spoiler
// Imports Cut

public class GUIRegisterer {
    public static final DeferredRegister<MenuType<?>> REGISTRY;
    public static final RegistryObject<MenuType<EggingTableGUI>> EGGING_GUI;


    private static <T extends AbstractContainerMenu> RegistryObject<MenuType<T>> registerMenuType(String name,IContainerFactory<T> factory) {
        return REGISTRY.register(name, () -> IForgeMenuType.create(factory));
    }

    public static void register(IEventBus eventBus) {REGISTRY.register(eventBus);}

    static {
        REGISTRY = DeferredRegister.create(ForgeRegistries.MENU_TYPES, Main.MODID);
        EGGING_GUI = registerMenuType("egging_gui",EggingTableGUI::new);
    }
}

 

Main:

Spoiler
// Imports Cut



@Mod(Main.MODID)
public class Main {
    public static final String MODID = "craftable_spawn_eggs";
  	
  	/*
    I cut all of the unneeded stuff.
    */

    public Main() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
        modEventBus.addListener(this::commonSetup); // No idea waht this does but probably isn't important.

        GUIRegisterer.register(modEventBus);

        MinecraftForge.EVENT_BUS.register(this);
    }

    @Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
    public static class ClientModEvents {
        @SubscribeEvent
        public static void onClientSetup(FMLClientSetupEvent event) {
            MenuScreens.register(GUIRegisterer.EGGING_GUI.get(), EggingTableGUIHelper::new);
        }
    }
}

 

 

I'm not good at modding, but at least I can read a crash report (well enough). That's something, right?

Link to comment
Share on other sites

Quote

public MenuProvider getMenuProvider(BlockState state, Level level, BlockPos pos) {

    return new SimpleMenuProvider((id, inv, player) -> new CraftingMenu(id, inv, ContainerLevelAccess.create(level, pos)), CONTAINER_TITLE);

}

You are telling it to open the crafting table menu instead of your EggingTableGui?

Since your block is not a crafting table block, it will close the gui in its stillValid() check.

  • Thanks 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.