Jump to content

More than 45 slots in the gui.


svk2140

Recommended Posts

How to make more than 45 slots in the gui?

crash :(

package com.svk.generateChests.Tile;

import java.util.ArrayList;

import com.svk.generateChests.base.CommonProxy;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileGenerateChest extends TileEntity implements IInventory
{
public ItemStack[] slots = new ItemStack[60];

public int[] procentSlot = null;
public boolean setGen = false;
public String customName = null;
    
public TileGenerateChest(){}

public void updateEntity()
{
	super.updateEntity();
}

    public void readFromNBT(NBTTagCompound nbt)
    {
        super.readFromNBT(nbt);
        NBTTagList nbttaglist = nbt.getTagList("Items", 10);
        this.slots = new ItemStack[this.getSizeInventory()];

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

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
            int j = nbttagcompound1.getByte("Slot") & 255;

            if (j >= 0 && j < this.slots.length)
            {
                this.slots[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
            }
        }
        
        this.procentSlot = nbt.getIntArray("procentSlots");
        this.setGen = nbt.getBoolean("setGen");
    }

    public void writeToNBT(NBTTagCompound nbt)
    {
        super.writeToNBT(nbt);
        NBTTagList nbttaglist = new NBTTagList();

        for (int i = 0; i < this.slots.length; ++i)
        {
            if (this.slots[i] != null)
            {
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                nbttagcompound1.setByte("Slot", (byte)i);
                this.slots[i].writeToNBT(nbttagcompound1);
                nbttaglist.appendTag(nbttagcompound1);
            }
        }

        nbt.setTag("Items", nbttaglist);

        if (this.hasCustomInventoryName())
        {
        	nbt.setString("CustomName", this.customName);
        }
        
       	nbt.setIntArray("procentSlots", this.procentSlot);
        nbt.setBoolean("setGen",this.setGen);
    }

public int getSizeInventory() 
{
	return 60;
}

@Override
public ItemStack getStackInSlot(int i) 
{
	return this.slots[i]; 
}

public ItemStack decrStackSize(int i, int j) 
{
	if(this.slots[i] != null)
	{	
		ItemStack itemstack;

		if(this.slots[i].stackSize < j)
		{
			itemstack = this.slots[i];
			this.slots[i] = null;

			return itemstack;
		}
		else
		{
			itemstack = this.slots[i].splitStack(j);

			if(this.slots[i].stackSize == 0)
			{
				this.slots[i] = null;
			}
			return itemstack;
		}
	}
	return null;
}

public ItemStack getStackInSlotOnClosing(int i) 
{
	if(this.slots[i] != null)
	{
		ItemStack itemstack = this.slots[i];
		this.slots[i] = null;
		return itemstack;
	}
	return null;
}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack) 
{
	this.slots[i] = itemstack;

	if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
	{
		itemstack.stackSize = this.getInventoryStackLimit();
	}
}

public void setInventoryName(String string)
{
}

    public String getInventoryName()
    {
        return "CustomName";
    }

@Override
public boolean hasCustomInventoryName() 
{
        return true;
}

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

@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
	return false;
}

@Override
public void openInventory() 
{
}

@Override
public void closeInventory() 
{
}

@Override
public boolean isItemValidForSlot(int slot, ItemStack itemStack) 
{
	if(slots[slot] != null)
	{
		return true;
	}
	else
	{
		return false;
	}
}
}

package com.svk.generateChests.GUI.container;

import java.util.ArrayList;
import java.util.Random;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

import com.svk.generateChests.Tile.TileGenerateChest;
import com.svk.generateChests.base.BaseClassMod;
import com.svk.generateChests.base.CommonProxy;
import com.svk.generateChests.packets.PacketsID;
import com.svk.generateChests.util.UtilPackerDataSet;

public class ContainerEditGenerateChest extends Container
{	
public ContainerEditGenerateChest(InventoryPlayer inventoryPlayer, TileGenerateChest tileChest)
{
	tileChest.openInventory();
	this.addCustomSlots(tileChest);
	this.addPlayerSlots(inventoryPlayer);
}

public void addCustomSlots(TileGenerateChest chest)
{
	for(int i = 0; i < 8; i++)
	{
		this.addSlotToContainer(new Slot(chest, i,  8, 12 + 18*i));
		this.addSlotToContainer(new Slot(chest, i + 8,  53, 12 + 18*i));
	}
}

public void addPlayerSlots(InventoryPlayer inventoryPlayer)
{
	for(int i = 0; i < 3; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			this.addSlotToContainer(new Slot(inventoryPlayer, 9+j+i*9, 8+18*j, 166+i*18));
		}
	}

	for(int i = 0; i < 9; i++)
	{
		this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 224));
	}
}

public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_)
{
	return null;
}

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

Link to comment
Share on other sites

If you have a crash, post it.

There is no arbitrary limitation on the number of slots you can have.

Sorry, I forgot

java.lang.IndexOutOfBoundsException: Index: 45, Size: 45

    at java.util.ArrayList.rangeCheck(Unknown Source)

    at java.util.ArrayList.get(Unknown Source)

    at net.minecraft.inventory.Container.getSlot(Container.java:130)

    at net.minecraft.inventory.Container.putStacksInSlots(Container.java:558)

    at net.minecraft.client.network.NetHandlerPlayClient.handleWindowItems(NetHandlerPlayClient.java:1197)

    at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:70)

    at net.minecraft.network.play.server.S30PacketWindowItems.processPacket(S30PacketWindowItems.java:78)

    at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241)

    at net.minecraft.client.multiplayer.PlayerControllerMP.updateController(PlayerControllerMP.java:317)

    at net.minecraft.client.Minecraft.runTick(Minecraft.java:1693)

    at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1039)

    at net.minecraft.client.Minecraft.run(Minecraft.java:962)

    at net.minecraft.client.main.Main.main(Main.java:164)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)

    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

    at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)

    at GradleStart.main(Unknown Source)

Link to comment
Share on other sites

Show where you open the GUI (most likely your block).

Does it matter?

        if(world.isRemote)
        {
        	return false;
        } 
        else 
        {        	
        	player.openGui(BaseClassMod.instance, GuiID.guiGenerateChest, world, x, y, z);
        	return true;
        }

Link to comment
Share on other sites

If you have an array of 45 slots, you cannot access slot 45 as there is no slot called that. The highest slot number is 44 as the first slot number is 0.

:P

Inside the chest is more than 45 slots. 4x9 player's inventory and 6x3 inventory chest ...

 

And two of those have Slot #0.  Slot #0 (player's inventory) and Slot #0 (chest inventory).

 

			for(int j = 0; j < 9; j++)
		{
			this.addSlotToContainer(new Slot(inventoryPlayer, 9+j+i*9, 8+18*j, 166+i*18)); // "9+j+i*9" why is there a +9 here?
		}
	}

	for(int i = 0; i < 9; i++)
	{
		this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 224)); // "8 + i * 18" why is there a +9 here?
	}

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

If you have an array of 45 slots, you cannot access slot 45 as there is no slot called that. The highest slot number is 44 as the first slot number is 0.

:P

Inside the chest is more than 45 slots. 4x9 player's inventory and 6x3 inventory chest ...

 

And two of those have Slot #0.  Slot #0 (player's inventory) and Slot #0 (chest inventory).

 

			for(int j = 0; j < 9; j++)
		{
			this.addSlotToContainer(new Slot(inventoryPlayer, 9+j+i*9, 8+18*j, 166+i*18)); // "9+j+i*9" why is there a +9 here?
		}
	}

	for(int i = 0; i < 9; i++)
	{
		this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 224)); // "8 + i * 18" why is there a +9 here?
	}

I corrected, but still crash

package com.svk.generateChests.GUI.container;

import java.util.ArrayList;
import java.util.Random;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

import com.svk.generateChests.Tile.TileGenerateChest;
import com.svk.generateChests.base.BaseClassMod;
import com.svk.generateChests.base.CommonProxy;
import com.svk.generateChests.packets.PacketsID;
import com.svk.generateChests.util.UtilPackerDataSet;

public class ContainerEditGenerateChest extends Container
{	
private TileGenerateChest table;
private EntityPlayer player;

public ContainerEditGenerateChest(InventoryPlayer inventoryPlayer, TileGenerateChest table)
{
	this.table = table;
	table.openInventory();
	player = inventoryPlayer.player;
	this.addPlayersSlots(inventoryPlayer);
	this.addChestSlots(table);
}

public void addPlayersSlots(InventoryPlayer inventoryPlayer)
{
	for(int i = 0; i < 9; i++)
	{
		addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 224));
	}

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

public void addChestSlots(TileGenerateChest table)
{
	for(int i = 0; i < 8; i++)
        {
            addSlotToContainer(new Slot(table, i,  8, 12 + 18*i));
            addSlotToContainer(new Slot(table, i + 8,  53, 12 + 18*i));
        }
}
    
@Override
    public void onContainerClosed(EntityPlayer p_75134_1_)
    {
	table.closeInventory();
    }

public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_)
{
	return null;
}

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

Link to comment
Share on other sites

If you have an array of 45 slots, you cannot access slot 45 as there is no slot called that. The highest slot number is 44 as the first slot number is 0.

:P

Inside the chest is more than 45 slots. 4x9 player's inventory and 6x3 inventory chest ...

Are you saying you are storing the players inventory in your chest?

I might be terribly wrong.. Like really, really wrong. But I'm just trying to help.

Link to comment
Share on other sites

If you have an array of 45 slots, you cannot access slot 45 as there is no slot called that. The highest slot number is 44 as the first slot number is 0.

:P

Inside the chest is more than 45 slots. 4x9 player's inventory and 6x3 inventory chest ...

Are you saying you are storing the players inventory in your chest?

What? I add slots players, and they are stored in the player's inventory?

this.addSlotToContainer(new Slot(inventoryPlayer, 9+j+i*9, 8+18*j, 166+i*18));

And I add slots chest in the chest

this.addSlotToContainer(new Slot(chest, i,  8, 12 + 18*i));

 

I wrote an example that keeps the chest more than 45 slots

Link to comment
Share on other sites

- You have to call super.initGui if you override it.

- If you override mouseClicked to do nothing the inventory part of your GUI will be non-functional. Same with keyTyped.

- What the hell is that packet sending stuff there?

- You showed your GuiContainer, not your IGuiHandler.

Oh, wrong: D

Where comments I hidden code

I have delivered the packages to save the settings on the server that are in gui.

 

package com.svk.generateChests.base;

 

import com.svk.generateChests.GUI.GuiEditGenerateChest;

import com.svk.generateChests.GUI.container.ContainerEditGenerateChest;

import com.svk.generateChests.Tile.TileGenerateChest;

 

import net.minecraft.block.Block;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.world.World;

import cpw.mods.fml.common.network.IGuiHandler;

 

public class GuiHandler implements IGuiHandler

{

@Override

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)

{

TileEntity tileentity = world.getTileEntity(x, y, z);

 

switch(ID)

{

case GuiID.guiGenerateChest: return new ContainerEditGenerateChest(player.inventory, (TileGenerateChest) tileentity);

}

return null;

}

 

@Override

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)

{

TileEntity tileentity = world.getTileEntity(x, y, z);

 

switch(ID)

{

case GuiID.guiGenerateChest: return new GuiEditGenerateChest(player.inventory, (TileGenerateChest) tileentity);

}

return null;

}

}

 

Link to comment
Share on other sites

- You have to call super.initGui if you override it.

- If you override mouseClicked to do nothing the inventory part of your GUI will be non-functional. Same with keyTyped.

- What the hell is that packet sending stuff there?

- You showed your GuiContainer, not your IGuiHandler.

Thank you so much!!!

I killed 2 days on this problem!

 

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.