#Help with Gui with a Keybind

3 messages · Page 1 of 1 (latest)

forest gust
#

I am trying make a inventory-like Gui appear when the player press a button but after i followed a tutorial, it seems i got stuck with the code being a container for a entity instead of a inventory

The "CharmScreenHandler::new" is the one with the red underline mark


import com.hk.HollowKnight;
import com.hk.screens.custom.CharmScreenHandler;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.util.math.BlockPos;
import org.intellij.lang.annotations.Identifier;

public class ModScreensHandlers {
    //public static final ScreenHandlerType<CharmScreenHandler> CHARM_SCREEN_HANDLER =
    //       Registry.register(Registries.SCREEN_HANDLER, Identifier.of(HollowKnight.MOD_ID, "charm_screen_handler"),
    //               new ExtendedScreenHandlerType<>(CharmScreenHandler::new, BlockPos.PACKET_CODEC));
    public static final ScreenHandlerType<CharmScreenHandler> CHARM_SCREEN_HANDLER =
            Registry.register(
                    Registries.SCREEN_HANDLER,
                    new ExtendedScreenHandlerType<>(CharmScreenHandler::new)    
            );

    public static void registerScreenHandlers() {
        HollowKnight.LOGGER.info("Registering Screen Handlers " + HollowKnight.MOD_ID);
    }
}```
#

CharmScreenHandler


import com.hk.screens.ModScreensHandlers;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.math.BlockPos;
import org.jetbrains.annotations.Nullable;

public class CharmScreenHandler extends ScreenHandler {

    private final Inventory inventory;

    public CharmScreenHandler(int syncId, PlayerInventory playerInventory, SimpleInventory inventory) {
        super(ModScreensHandlers.CHARM_SCREEN_HANDLER, syncId);
        this.inventory = inventory;

        this.addSlot(new Slot(inventory, 0, 11, 9));

        addPlayerInventory(playerInventory);
        addPlayerHotbar(playerInventory);
    }```
#
    public ItemStack quickMove(PlayerEntity player, int invSlot) {
        ItemStack newStack = ItemStack.EMPTY;
        Slot slot = this.slots.get(invSlot);
        if (slot != null && slot.hasStack()) {
            ItemStack originalStack = slot.getStack();
            newStack = originalStack.copy();
            if (invSlot < this.inventory.size()) {
                if (!this.insertItem(originalStack, this.inventory.size(), this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.insertItem(originalStack, 0, this.inventory.size(), false)) {
                return ItemStack.EMPTY;
            }

            if (originalStack.isEmpty()) {
                slot.setStack(ItemStack.EMPTY);
            } else {
                slot.markDirty();
            }
        }
        return newStack;
    }

    @Override
    public boolean canUse(PlayerEntity player) {
        return this.inventory.canPlayerUse(player);
    }

    private void addPlayerInventory(PlayerInventory 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, 84 + i * 18));
            }
        }
    }

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