Jump to content

[1.19.3]Can't open screen via NetworkHooks.openScreen()


Xie Peng

Recommended Posts

public class OpenFiltListScreenC2SPacket {
    private FriendlyByteBuf buf;
    public OpenFiltListScreenC2SPacket(){}
    public OpenFiltListScreenC2SPacket(FriendlyByteBuf buf){
        this.buf = buf;
    }
    public void encode(FriendlyByteBuf buf){}
    public void handle(Supplier<NetworkEvent.Context> supplier){
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(()->{
            context.getSender().getCapability(FiltListProvider.FILT_LIST).ifPresent(serverFiltList -> {
                //Server Logic
                context.getSender().sendSystemMessage(Component.literal("Open Screen"));
                NetworkHooks.openScreen(context.getSender(), new SimpleMenuProvider(
                        (containerId, playerInventory, player) -> new Menu(containerId, playerInventory, buf),
                        Component.translatable("menu.title.filtpick")));

            });
        });
        context.setPacketHandled(true);
    }
}
public class Menu extends AbstractContainerMenu {
    private Inventory inventory;
    private final Level level;
    private static final DeferredRegister<MenuType<?>> REGISTER = DeferredRegister.create(ForgeRegistries.MENU_TYPES, FiltPick.MOD_ID);
    public static final RegistryObject<MenuType<Menu>> MENU_TYPE = REGISTER.register("filtlist", ()-> IForgeMenuType.create(Menu::new));


    // Client menu constructor
    public Menu(int containerId, Inventory playerInventory, FriendlyByteBuf buf) {
        this(containerId, playerInventory, new ItemStackHandler(27), new SimpleContainerData(2));
    }

    // Server menu constructor
    public Menu(int containerId, Inventory playerInventory, IItemHandler dataInventory, SimpleContainerData  dataMultiple) {
        super(MENU_TYPE.get(),containerId);
        checkContainerSize(playerInventory,2);
        this.level = playerInventory.player.level;
        // Check if the data inventory size is some fixed value
        // Then, add slots for data inventory
        this.addSlot(new SlotItemHandler(dataInventory, 0, 0, 0));

        // Add slots for player inventory
        this.addSlot(new Slot(playerInventory, 0, 0 ,0));

        // Add data slots for handled integers
        this.addDataSlots(dataMultiple);

        // ...
    }


    @Override
    public ItemStack quickMoveStack(Player player, int quickMovedSlotIndex) {
        return ItemStack.EMPTY;
    }

    @Override
    public boolean stillValid(Player player) {
        return true;
    }
}
public class FiltContainerScreen extends AbstractContainerScreen<Menu> {


    public FiltContainerScreen(Menu menu, Inventory playerInventory, Component title) {
        super(menu, playerInventory, title);

        this.titleLabelX = 10;
        this.inventoryLabelX = 10;
    }

    @Override
    protected void init() {
        super.init();

    }


    @Override
    public void render(PoseStack pose, int mouseX, int mouseY, float partialTick) {
        this.renderBackground(pose);
        super.render(pose, mouseX, mouseY, partialTick);

        /*
         * This method is added by the container screen to render
         * a tooltip for whatever slot is hovered over.
         */
        this.renderTooltip(pose, mouseX, mouseY);
    }

    @Override
    protected void renderBg(PoseStack pose, float p_97788_, int p_97789_, int p_97790_) {
        /*
         * Sets the texture location for the shader to use. While up to
         * 12 textures can be set, the shader used within 'blit' only
         * looks at the first texture index.
         */
        RenderSystem.setShaderTexture(0, BACKGROUND_LOCATION);

        /*
         * Renders the background texture to the screen. 'leftPos' and
         * 'topPos' should already represent the top left corner of where
         * the texture should be rendered as it was precomputed from the
         * 'imageWidth' and 'imageHeight'. The two zeros represent the
         * integer u/v coordinates inside the 256 x 256 PNG file.
         */
        this.blit(pose, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
    }
}
    @Mod.EventBusSubscriber(modid = FiltPick.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
    public class ClientModEvents{
        @SubscribeEvent
        public static void onKeyRegister(RegisterKeyMappingsEvent event){
            event.register(KeyBinding.SET_ITEM_MAP);
            event.register(KeyBinding.OPEN_FILTLIST_MAP);
        }

        @SubscribeEvent
        public static void gatherData(GatherDataEvent event) {

        }
        @SubscribeEvent
        public static void clientSetup(FMLClientSetupEvent event) {
            event.enqueueWork(
                    // Assume RegistryObject<MenuType<MyMenu>> MY_MENU
                    // Assume MyContainerScreen<MyMenu> which takes in three parameters
                    () -> MenuScreens.register(Menu.MENU_TYPE.get(), FiltContainerScreen::new)
            );
        }
    }

This is what I want to implement:
When I press a key, client will sent the above packet to server, then the server will make my client to open the screen.
But:
My client only gets the systemMessage, "Open Screen", but no screen is opened... Why?

Link to comment
Share on other sites

22 hours ago, ChampionAsh5357 said:

Is this your entire codebase? Have you added the `DeferredRegister` to the mod event bus via `#register`? Where are you sending the packet to the server? Why do you need a `FriendlyByteBuf` when you aren't sending any data?

This is not my entire codebase. I haven't added the `DeferredRegister` to the mod event bus via `#register` and I think this is where the problem is. I send the packet via keybinding, when I press a key, the packet will be sent. I haven't used `FriendlyByteBuf` because I am still studying how to use it.

UPDATE:
I made it after I added "Menu.REGISTER.register(FMLJavaModLoadingContext.get().getModEventBus());" in the main entry. Thanks.

Edited by Xie Peng
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.



×
×
  • Create New...

Important Information

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