Jump to content

Problem Refreshing Screen GUI [1.16.5]


Yurim64

Recommended Posts

Hi!
I was creating a GUI with a scroll pane, but when i scroll up or down with the mouse (the button is not implemented yet) the GUI will not update until i click on a slot with an item.
How can i resolve this problem?

 

This is the Screen Code:

public class ColossalChestScreen extends BaseChestScreen {

    private ColossalChestContainer colossalContainer;

    public ColossalChestScreen(BaseChestContainer container, PlayerInventory inventory, ITextComponent title) {
        super(container, inventory, title);
        if (container instanceof ColossalChestContainer)
            this.colossalContainer = (ColossalChestContainer) container;
        else
            throw new IllegalArgumentException("Invalid Container for Colossal Chest");
        this.imageHeight = 204;
        this.imageWidth = 197;
        this.inventoryLabelY = 110;
        this.bgLocation = new ResourceLocation(TenChest.MODID, "textures/gui/colossal_chest_gui.png");
    }

    @Override
    public boolean mouseScrolled(double x, double y, double movement) {
        System.out.println("Trying to scroll off " + (-movement));
        this.colossalContainer.scrollOff((int) -movement);
        return super.mouseScrolled(x, y, movement);
    }
}

This is the Container Screen:

public class ColossalChestContainer extends BaseChestContainer {

    public static ColossalChestContainer defaultContainer(int id, IInventory inventory, PlayerInventory playerInventory) {
        return new ColossalChestContainer(id, inventory, playerInventory);
    }

    public static ColossalChestContainer defaultContainer(int id, PlayerInventory inventory) {
        return new ColossalChestContainer(id, new Inventory(9 * MAX_ROWS), inventory);
    }

    public static final int MAX_ROWS = 12;
    private static final int VISIBLE_ROWS = 5;

    private int startingRow = 0;

    public ColossalChestContainer(int id, IInventory inventory, PlayerInventory playerInventory) {
        super(BaseChestContainerType.COLOSSAL_CHEST.get(), id, inventory, playerInventory);
    }

    @Override
    protected void generateSlot(PlayerInventory playerInventory) {
        int index = 0;
        for (int i = 0; i < VISIBLE_ROWS; i++) {
            for (int j = 0; j < 9; j++) {
                this.addSlot(new Slot(this.inventory, index++, 12 + (j * 18), 18 + (i * 18)));
            }
        }

        index = 0;
        for (int i = 0; i < 9; i++) {
            this.addSlot(new Slot(playerInventory, index++, 12 + (i * 18), 181));
        }

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                this.addSlot(new Slot(playerInventory, index++, 12 + (j * 18), 123 + (18 * i)));
            }
        }
    }

    public void scrollOff(int val) {
        this.scrollTo(this.startingRow + val);
    }

    public void scrollTo(int row) {
        if (canScrollTo(row)) {
            this.startingRow = row;
            System.out.println("Changed First Row Index to: " + this.startingRow);
            int index = 0;
            for (int i = 0; i < VISIBLE_ROWS; i++) {
                for (int j = 0; j < 9; j++) {
                    int inventoryIndex = (this.startingRow * 9) + index;
                    System.out.println("Inventory Slot Visible at: " + index + " -> " + inventoryIndex);
                    this.setItem(index, this.inventory.getItem(inventoryIndex));
                    index++;
                }
            }
        } else {
            System.out.println("Can't Scroll To " + row);
        }
    }

    public boolean canScrollTo(int rows) {
        return rows >= 0 && rows + VISIBLE_ROWS <= MAX_ROWS;
    }
}

 

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.