Jump to content

Solved: [1.16]Open custom inventory instead of vanilla player inventory


Zemelua

Recommended Posts

I'm currently working on a backpack item. I can equip it to my chest and when the equipped player opens the inventory I want to open a custom inventory instead, but that doesn't work. What should I do?

 

public class BackpackItem extends DyeableArmorItem implements INamedContainerProvider {
	public BackpackItem(IArmorMaterial material, EquipmentSlotType slot, Properties properties) {
		super(material, slot, properties);
	}

	@Nullable
	@Override
	public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
		return new BackpackInventory();
	}

	@Nullable
	@Override
	public Container createMenu(int p_createMenu_1_, PlayerInventory p_createMenu_2_, PlayerEntity p_createMenu_3_) {
		return new BackpackContainer(p_createMenu_1_, p_createMenu_2_.player);
	}

	@Override
	public ITextComponent getDisplayName() {
		return null;
	}

	public void onOpenGui(GuiOpenEvent event) {
		PlayerEntity player = Minecraft.getInstance().player;
		if (player == null || event.getGui().getClass() != InventoryScreen.class) return;

		ItemStack chest = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
		if (!(chest.getItem() instanceof BackpackItem)) return;

		NetworkHooks.openGui((ServerPlayerEntity) player, this);
	}

 

Link to comment
Share on other sites

I organized it a little. I found in GuiOpenEvent#getGui that I need to check that it is an InventoryScreen and cancel it. However, there are some questions here.

1: Is it correct to recognize that the server player PlayerContainer and the client player are opening the InventoryScreen when the player opens the inventory with the E key?

2: At 1, Is the InventoryScreen returned by GuiOpenEvent#getGui?

3: How can I open another screen after canceling with event.setCanceled(true)? event.setGui(new CustomScreen(...))?

4. Is the method of processing the client side (screen) with GuiOpenEvent correct in 1-3? Also, how can I perform server side (container) processing?

Link to comment
Share on other sites

Thanks, GuiOpenEvent#setGui was able to open another screen. However, since the container is probably still PlayerContainer on that screen, moving ItemStack etc. will be different from AnvilScreen. How can I change the container? (Because it's a test, don't worry about the clutter of the code right now)

 

@SubscribeEvent
	public void onOpenGui(GuiOpenEvent event) {
		if (Minecraft.getInstance().world == null) return;
		if (!Minecraft.getInstance().world.isRemote) return;
		PlayerEntity player = Minecraft.getInstance().player;
		if (player == null || !(event.getGui() instanceof InventoryScreen)) return;

		ItemStack chest = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
		if (!(chest.getItem() instanceof BackpackItem)) return;
		event.setGui(new AnvilScreen(ContainerType.ANVIL.create(0, player.inventory), player.inventory, new StringTextComponent("test")));
	}

 

Link to comment
Share on other sites

Thank you. I haven't touched on packets very much, so is there a good tutorial (or sample code) for it? What should I send as a packet?

 

@SubscribeEvent
	public void onOpenGui(GuiOpenEvent event) {
		World world = Minecraft.getInstance().world;
		if (Minecraft.getInstance().world == null) return;
		
		PlayerEntity player = Minecraft.getInstance().player;
		if (player == null) return;

		ItemStack chest = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
		if (!(chest.getItem() instanceof BackpackItem)) return;
		
		if (!(event.getGui() instanceof InventoryScreen)) return;
		
		if (world.isRemote()) {
			event.setGui(new AnvilScreen(ContainerType.ANVIL.create(0, player.inventory), player.inventory, new StringTextComponent("text")));
			// send a packet to server?
		} else {
			NetworkHooks.openGui((ServerPlayerEntity) player, new SimpleNamedContainerProvider((id, playerInventory, unused) -> {
				return new RepairContainer(id, playerInventory, IWorldPosCallable.of(world, player.getPosition()));
			}, new StringTextComponent("test")));
		}
	}

 

Link to comment
Share on other sites

2 hours ago, Zemelua said:

Thank you. I haven't touched on packets very much, so is there a good tutorial (or sample code) for it? What should I send as a packet?

your message/packet needs three methods encode, decode and handle.
the most important method for you is the handle method in it you handle to open your gui, the other two are only necessary if you want to send data (but are required for registration)

after reading the doc, here is a practical example

Edited by Luis_ST
Link to comment
Share on other sites

Very helpful. I think I was able to send the packet, but when I press the E key this time, the GUI is displayed for a moment and it closes without permission. What is the cause?

Network Handler:

Spoiler


public class UMUNetwork {
	private static final String PROTOCOL_VERSION = "1";
	public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
			new ResourceLocation(UMUMod.MODID, "main"),
			() -> PROTOCOL_VERSION,
			PROTOCOL_VERSION::equals,
			PROTOCOL_VERSION::equals
	);

	public static void packetRegister() {
		INSTANCE.registerMessage(
				0,
				BackpackOpenMessage.class,
				BackpackOpenMessage::encode,
				BackpackOpenMessage::decode,
				BackpackOpenMessage::handle
		);
	}

	public static void sendToServer(Object message) {
		INSTANCE.sendToServer(message);
	}
}

 

Message:

Spoiler


public class BackpackOpenMessage {
	public static void encode(BackpackOpenMessage message, PacketBuffer buffer) {
	}

	public static BackpackOpenMessage decode(PacketBuffer buffer) {
		return new BackpackOpenMessage();
	}

	public static void handle(BackpackOpenMessage message, Supplier<Context> networkContext) {
		ServerPlayerEntity player = networkContext.get().getSender();
		networkContext.get().enqueueWork(() -> {
			NetworkHooks.openGui(player, new SimpleNamedContainerProvider((id, playerInventory, unused) -> {
				return new RepairContainer(id, playerInventory, IWorldPosCallable.of(player.world, player.getPosition()));
			}, new StringTextComponent("GUI test")));
		});
		networkContext.get().setPacketHandled(true);
	}
}

 

Register message:

Spoiler


public class UMUMod {
	private void setup(final FMLCommonSetupEvent event) {
		UMUNetwork.packetRegister();
	}

	~~~~
}

 

 

	@SubscribeEvent
	public void onOpenGui(GuiOpenEvent event) {
		World world = Minecraft.getInstance().world;
		if (Minecraft.getInstance().world == null) return;

		PlayerEntity player = Minecraft.getInstance().player;
		if (player == null) return;

		ItemStack chest = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
		if (!(chest.getItem() instanceof BackpackItem)) return;

		if (!(event.getGui() instanceof InventoryScreen)) return;

		event.setGui(new AnvilScreen(ContainerType.ANVIL.create(0, player.inventory), player.inventory, new StringTextComponent("text")));
		UMUNetwork.sendToServer(new BackpackOpenMessage());
	}
Edited by Zemelua
Link to comment
Share on other sites

  • Zemelua changed the title to Solved: [1.16]Open custom inventory instead of vanilla player inventory

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.