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?