Jump to content

[1.12.2] Implementing a Scrollbar to a gui based off vanilla GuiContainerCreative


quinn50

Recommended Posts

I have been working on getting a scroll bar to work for both items and a list of buttons which will be based off the items put into the container in which i'm using. Recently during testing I have been having a issue where markDirty(); is infinitely called causing a stackoverflow error whenever I click on a slot inside of the gui of the container I am using, and I would like to find out why and fix it so I can get the scrollbars working properly.

 

 

GUI CODE:

public class GuiChamber extends GuiContainer {
    private static final ResourceLocation BACKGROUND = new ResourceLocation(soulomancy.MODID, "textures/gui/chamber.png");

ArrayList list = new ArrayList();


    int ScrollRow = 0;
    private boolean isScrolling = false;
    private boolean wasClicking = false;
    private float scrollCurrent;
    public static TileEntityChamber chamberInv;
    private static SoulChamberContainer chamContainer;


    private InventoryPlayer playerInv;
    public GuiChamber (Container container, InventoryPlayer playerInv) {
        super(container);
        this.playerInv = playerInv;
    }




    @Override
    public void initGui() {
        super.initGui();
        this.buttonList.add(new GuiButton(1, 100, 200, 100, 20, "Button id: 1"));
    }


    @Override
    protected void actionPerformed(GuiButton button) throws IOException {
        if (button.id == 1){
            System.out.println("Button id 1: Test");
        }
    }

    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        this.drawDefaultBackground();
        super.drawScreen(mouseX, mouseY, partialTicks);
        this.renderHoveredToolTip(mouseX, mouseY);
        boolean flag = Mouse.isButtonDown(0);
        int k = this.guiLeft;
        int l = this.guiTop;
        int i1 = k + 175;
        int j1 = l + 18;
        int k1 = i1 + 14;
        int l1 = j1 + 108;

        if (!this.wasClicking && flag && mouseX >= i1 && mouseY >= j1 && mouseX < k1 && mouseY < l1) {
            this.isScrolling = this.needsScrollBars();
        }

        if (!flag) {
            this.isScrolling = false;
        }

        this.wasClicking = flag;

        if (this.isScrolling) {
            this.scrollCurrent = (mouseY - j1 - 7.5F) / (l1 - j1 - 15.0F);
            this.scrollCurrent = MathHelper.clamp(this.scrollCurrent, 0.0F, 1.0F);
            scrollTo(this.scrollCurrent);
        }



    }

    private boolean needsScrollBars()
    {
        if (chamContainer.moreThan1PageItems())
        {
            return true;
        }
        else
        {
            return false;
        }
    }






    public void setInventorySlotContents(int i, ItemStack itemStack)
    {
        chamberInv.invContents(i, itemStack);

        if (itemStack != null && itemStack.getMaxStackSize() > 64)
        {
            itemStack.setCount(64);
        }
        chamberInv.markDirty();
    }




    public void scrollTo(float pos)
    {
        int i = (this.list.size() + 9 - 1) / 9 - 5;
        int j = (int)((double)(pos * (float)i) + 0.5D);

        if (j < 0)
        {
            j = 0;
        }

        for (int k = 0; k < 5; ++k)
        {
            for (int l = 0; l < 9; ++l)
            {
                int i1 = l + (k + j) * 9;

                if (i1 >= 0 && i1 < this.list.size())
                {
                    setInventorySlotContents(l + k * 9, (ItemStack) this.list.get(i1));
                }
                else
                {
                    setInventorySlotContents(l + k * 9, ItemStack.EMPTY);
                }
            }
        }
    }


    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        GlStateManager.color(1, 1, 1, 1);
        mc.getTextureManager().bindTexture(BACKGROUND);
        int x = (width - xSize) / 2;
        int y = (height - ySize) / 2;
        drawTexturedModalRect(x, y, 0, 0, xSize, 222);
    }

    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
        String name = I18n.format(ModBlocks.soulChamber.getUnlocalizedName() + ".name");
        fontRenderer.drawString(name, xSize / 2 - fontRenderer.getStringWidth(name) / 2, 6, 0x404040);
        fontRenderer.drawString(playerInv.getDisplayName().getUnformattedText(), 8, 222 - 94, 0x404040);
    }

  /*  public int rowsVisible() {
        return 6;
    }
*/

 

 

CONTAINER CODE:


  TileEntityChamber chamber;
    public static TileEntityChamber chamberInv;
    public List list = new ArrayList();




    public SoulChamberContainer(InventoryPlayer playerInv, final TileEntityChamber chamber){
        this.chamber = chamber;

        IItemHandler inventory = chamber.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH);


        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 140 + i * 18));
            }
        }

        for (int j = 0; j < 6 ; ++j)
        {
            for (int k = 0; k < 4; ++k)
            {
                addSlotToContainer(new soulChamberSlot(inventory, k + j * 4, 80 + k * 18, 18 + j * 18) {
                    @Override
                    public void onSlotChanged() {
                        chamber.markDirty();
                    }
                });

            }
        }




        for (int k = 0; k < 9; k++) {
            addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 198));
        }

    }


    public boolean moreThan1PageItems()
    {
        return this.list.size() > 24;
    }



    @Override
    public boolean canInteractWith(EntityPlayer playerIn) {
        return true;
    }


    @Override
    public ItemStack transferStackInSlot(EntityPlayer player, int index) {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = inventorySlots.get(index);

        if (slot != null && slot.getHasStack()) {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

                int containerSlots = inventorySlots.size() - player.inventory.mainInventory.size();

                if (index < containerSlots) {
                    if (!this.mergeItemStack(itemstack1, containerSlots, inventorySlots.size(), true)) {
                        return ItemStack.EMPTY;
                    }
                } else if (!this.mergeItemStack(itemstack1, 0, containerSlots, false)) {
                    return ItemStack.EMPTY;
                }

                if (itemstack1.getCount() == 0) {
                    slot.putStack(ItemStack.EMPTY);
                } else {
                    slot.onSlotChanged();
                }

                if (itemstack1.getCount() == itemstack.getCount()) {
                    return ItemStack.EMPTY;
                }

                slot.onTake(player, itemstack1);
            }


        return itemstack;
    }

 

 

TILE ENTITY CODE:


private ItemStackHandler inv = new ItemStackHandler(24);
    private final NonNullList<ItemStack> inventoryContents;
public TileEntityChamber(){
    this.inventoryContents = NonNullList.<ItemStack>withSize(24, net.minecraft.item.ItemStack.EMPTY);
}


        public void invContents(int i, ItemStack stack){
                this.inventoryContents.set(i, stack);

        }



        public void markDirty(){
            this.markDirty();
        }


    public ItemStackHandler getInv() {
        return inv;
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        compound.setTag("inventory", inv.serializeNBT());
        return super.writeToNBT(compound);
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        inv.deserializeNBT(compound.getCompoundTag("inventory"));
        super.readFromNBT(compound);
    }

    @Override
    public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
        return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
    }

    @Nullable
    @Override
    public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
        return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (T)inv: super.getCapability(capability, facing);
    }

 

Edited by quinn50
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.