Jump to content

Issue with creating a GUI (Forge 1.19.2 43.2.0)


The Typholorian

Recommended Posts

I am trying to make a GUI. It is supposed to replace the normal inventory. I made a keybind and such and here's the procedure when E is released:

package net.the_typholorian.skilled.procedures;

import net.the_typholorian.skilled.world.inventory.InventoryGUIMenu;

import net.minecraftforge.server.ServerLifecycleHooks;
import net.minecraftforge.network.NetworkHooks;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.MenuProvider;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.core.BlockPos;

import io.netty.buffer.Unpooled;

public class OpenInventoryProcedure {
public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
if (entity == null)
return;
if (entity instanceof Player _plr ? _plr.containerMenu instanceof InventoryGUIMenu : false) {
if (entity instanceof Player _player) {
_player.closeContainer();
}
if (!world.isClientSide()) {
MinecraftServer _mcserv = ServerLifecycleHooks.getCurrentServer();
if (_mcserv != null)
_mcserv.getPlayerList().broadcastSystemMessage(Component.literal("close"), false);
}
} else if (!(entity instanceof Player _plr ? _plr.containerMenu instanceof InventoryGUIMenu : false)) {
{
if (entity instanceof ServerPlayer _ent) {
BlockPos _bpos = new BlockPos(x, y, z);
NetworkHooks.openScreen((ServerPlayer) _ent, new MenuProvider() {
@Override
public Component getDisplayName() {
return Component.literal("InventoryGUI");
}

@Override
public AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) {
return new InventoryGUIMenu(id, inventory, new FriendlyByteBuf(Unpooled.buffer()).writeBlockPos(_bpos));
}
}, _bpos);
}
}
if (!world.isClientSide()) {
MinecraftServer _mcserv = ServerLifecycleHooks.getCurrentServer();
if (_mcserv != null)
_mcserv.getPlayerList().broadcastSystemMessage(Component.literal("open"), false);
}
}
}
}

I added text messages for debugging. It opens the inventory just fine, but it does not close or send the message in chat. It can only close by pressing 'Escape', which closes all GUIs. Can somebody help me?

Link to comment
Share on other sites

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

New issue. I got the keybind to work, but I have an issue with the procedure. It opens the GUI, but closes it nearly instantly. I set it to run the procedure when the key gets released. Here's my procedure code:

package net.the_typholorian.skilled.procedures;

import net.the_typholorian.skilled.world.inventory.InventoryGUIMenu;

import net.minecraftforge.server.ServerLifecycleHooks;
import net.minecraftforge.network.NetworkHooks;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.MenuProvider;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.network.chat.Component;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.core.BlockPos;

import io.netty.buffer.Unpooled;

public class InventoryProcedureProcedure {
public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
if (entity == null)
return;
boolean Disabler = false;
Disabler = false;
if ((entity instanceof Player _plr ? _plr.containerMenu instanceof InventoryGUIMenu : false) && Disabler == false) {
if (!world.isClientSide()) {
MinecraftServer _mcserv = ServerLifecycleHooks.getCurrentServer();
if (_mcserv != null)
_mcserv.getPlayerList().broadcastSystemMessage(Component.literal("close"), false);
}
if (entity instanceof Player _player)
_player.closeContainer();
Disabler = true;
}
if (!(entity instanceof Player _plr ? _plr.containerMenu instanceof InventoryGUIMenu : false) && Disabler == false) {
if (!world.isClientSide()) {
MinecraftServer _mcserv = ServerLifecycleHooks.getCurrentServer();
if (_mcserv != null)
_mcserv.getPlayerList().broadcastSystemMessage(Component.literal("open"), false);
}
{
if (entity instanceof ServerPlayer _ent) {
BlockPos _bpos = new BlockPos(x, y, z);
NetworkHooks.openScreen((ServerPlayer) _ent, new MenuProvider() {
@Override
public Component getDisplayName() {
return Component.literal("InventoryGUI");
}

@Override
public AbstractContainerMenu createMenu(int id, Inventory inventory, Player player) {
return new InventoryGUIMenu(id, inventory, new FriendlyByteBuf(Unpooled.buffer()).writeBlockPos(_bpos));
}
}, _bpos);
}
}
Disabler = true;
}
}
}

Link to comment
Share on other sites

This is a bit messy and could be implemented so much easier and cleaner. Just create your own AbstractContainerMenu at this point and have your own AbstractContainerScreen, make sure you override the stillValid() method in your Menu class and do whatever logic you deem necessary to keep that container open for that player.

Link to comment
Share on other sites

On 5/6/2023 at 3:37 PM, The Typholorian said:

Update: I did some investigating and it is running twice. I thought it was running on server and client, and added some code to fix that but it wasn't the problem. 

It should run twice, it does logic on both sides. 

Link to comment
Share on other sites

3 hours ago, The Typholorian said:

But it runs the command twice, which opens it then closes it immediately. Can you give me some example code for what you mentioned?

Create a new abstractcontainermenu, attach a screen to it, override isValid to return true with given logic, then call the menu wherever you seem it is fit.

Link to comment
Share on other sites

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.