Jump to content

[1.10.2] Container GUI slot calculation problem


luckie12

Recommended Posts

I made a custom gui :

bigchest.png

Its a 99 slot container

I cant get the slots index to work...

i have this now:

 

	for (int y = 0; y < 11; ++y) {
		for (int x = 0; x < 9; ++x) {
			this.addSlotToContainer(new Slot(te, x+y*11, 8 + x * 18, 14 + y * 18));
		}
	}

But at the slots, the only top row works with saving items, when u put something in row 2 or anywhere else it dissapears...

i cant get the

x+y*11,

part right...

 

I hope someone can help me with this.

 

Thankyou :D

Link to comment
Share on other sites

Think you got it a bit off. Haven't touched containers since 1.7.10, but I do remember what hell I had with 'em. (They are the bane of my existence).

 

Any slots "outside" the texture? Hover the mouse just outside the outer slots, and see if anything highlights like the slots.

 

Quick look shows ContainerChest uses:

for (int j = 0; j < this.numRows; ++j)
        {
            for (int k = 0; k < 9; ++k)
            {
                this.addSlotToContainer(new Slot(chestInventory, k + j * 9, 8 + k * 18, 18 + j * 18));
            }
        }

Or rather, more dynamically:

for (int y = 0; y < maxRows; ++y)
        {
            for (int x = 0; x < maxSlotsPerLine; ++x)
            {                                         //Index                  xpos         ypos
                this.addSlotToContainer(new Slot(tile, x + y * maxSlotsPerLine, 8 + x * 18, 18 + y * 18));
            }
        }

You seem to be using x+y*maxRows rather than x+y*maxSlotsPerLine.

Do you have a github or similar repository with your current code? I'd like to see the whole container-class as well as the Gui-class.

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

There are no slots outside of it, not that we can see, maybe outside my screen somewhere :P

 

I have NO idea how to use github to upload haha

 

but i can use pastebin for better overview then using the code tags haha

 

This is my container class

 

http://pastebin.com/nqnXCH36

 

And this is the gui Class

 

http://pastebin.com/1CbM6Ehv

 

Sorry if its still not the best way to share codes haha

Link to comment
Share on other sites

I still had no luck getting this right, i looked at some examples but i guess my calculation is just baaaaad at the index,

 

the slots higlight all perfectly, but from row2- the rest of the rows, the items dissapear.

Row1 saves the items.

 

There are no slots OUTSIDE the gui visible on hover

Link to comment
Share on other sites

Can I see your TE class.

* Edit shouldn't the 11 be a 9 for when you are calculating the index?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

This is my TE

 

package Fatal1tyGC.BitofTuts.tileentities;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;

public class TileEntityBigChest extends TileEntity implements IInventory{

private ItemStack[] inventory;
private String customName;

public TileEntityBigChest(){
	this.inventory = new ItemStack[this.getSizeInventory()];
}

public String getCustomName() {
	return customName;
}

public void setCustomName(String customName) {
	this.customName = customName;
}

@Override
public String getName() {
	return this.hasCustomName() ? this.customName : "Big Chest";
}
@Override
public boolean hasCustomName() {
	return this.customName != null && !this.customName.equals("");
}

@Override
public ITextComponent getDisplayName() {
	return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
}

@Override
public int getSizeInventory() {
	return 9;
}

@Override
public ItemStack getStackInSlot(int index) {
    if (index < 0 || index >= this.getSizeInventory())
        return null;
    return this.inventory[index];
}

@Override
public ItemStack decrStackSize(int index, int count) {
    if (this.getStackInSlot(index) != null) {
        ItemStack itemstack;

        if (this.getStackInSlot(index).stackSize <= count) {
            itemstack = this.getStackInSlot(index);
            this.setInventorySlotContents(index, null);
            this.markDirty();
            return itemstack;
        } else {
            itemstack = this.getStackInSlot(index).splitStack(count);

            if (this.getStackInSlot(index).stackSize <= 0) {
                this.setInventorySlotContents(index, null);
            } else {
                //Just to show that changes happened
                this.setInventorySlotContents(index, this.getStackInSlot(index));
            }

            this.markDirty();
            return itemstack;
        }
    } else {
        return null;
    }
}

@Override
public void setInventorySlotContents(int index, ItemStack stack) {
    if (index < 0 || index >= this.getSizeInventory())
        return;

    if (stack != null && stack.stackSize > this.getInventoryStackLimit())
        stack.stackSize = this.getInventoryStackLimit();
        
    if (stack != null && stack.stackSize == 0)
        stack = null;

    this.inventory[index] = stack;
    this.markDirty();
}

@Override
public ItemStack removeStackFromSlot(int index) {
    ItemStack stack = this.getStackInSlot(index);
    this.setInventorySlotContents(index, null);
    return stack;
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

public boolean isUseableByPlayer(EntityPlayer player){
	return this.worldObj.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5,0.5,0.5)) <= 64;
}

@Override
public void openInventory(EntityPlayer player) {
}
@Override
public void closeInventory(EntityPlayer player) {	
}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
	return true;
}

@Override
public int getField(int id) {
	return 0;
}

@Override
public void setField(int id, int value) {
}

@Override
public int getFieldCount() {
	return 0;
}

@Override
public void clear() {
	for(int i = 0; i < this.getSizeInventory(); i++)
		this.setInventorySlotContents(i, null);
}


@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
    super.writeToNBT(nbt);

    NBTTagList list = new NBTTagList();
    for (int i = 0; i < this.getSizeInventory(); ++i) {
        if (this.getStackInSlot(i) != null) {
            NBTTagCompound stackTag = new NBTTagCompound();
            stackTag.setByte("Slot", (byte) i);
            this.getStackInSlot(i).writeToNBT(stackTag);
            list.appendTag(stackTag);
        }
    }
    nbt.setTag("Items", list);

    if (this.hasCustomName()) {
        nbt.setString("CustomName", this.getCustomName());
    }
	return nbt;
}


@Override
public void readFromNBT(NBTTagCompound nbt) {
    super.readFromNBT(nbt);

    NBTTagList list = nbt.getTagList("Items", 10);
    for (int i = 0; i < list.tagCount(); ++i) {
        NBTTagCompound stackTag = list.getCompoundTagAt(i);
        int slot = stackTag.getByte("Slot") & 255;
        this.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(stackTag));
    }

    if (nbt.hasKey("CustomName", ) {
        this.setCustomName(nbt.getString("CustomName"));
    }
}

}

 

I just tested with 9 instead of 11 and that didnt change anything, still the exact same thing

Link to comment
Share on other sites

getSizeInventory returns 9 kinda surprised you didn't get an Array out of Bounds Exception..

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.