Jump to content

Funky Container Problem


Noah_Beech

Recommended Posts

Hello, as of recent I have been working with basic tileentities and containers. Since I am new to them, it has been very difficult, however I have been starting to get the hang of containers. Everything with them is almost done expect for the annoying fact that my larger container has slot/stack  transfer issues, and I can't seem to pinpoint why. All my code for my larger container came off of the smaller one, but I changed all the values to fit the size. But Whenever I try to transfer items via shiftclicking OR just plain drag-n-drop one item is ejected into the world and the stack size I click goes down by 1. My code is below

 

Below in this spoiler is the smaller container that works, it has 36 slots per inventory

 

 

BlockSmallChest.java

package mod_Rareores;

import java.util.Random;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class BlockSmallChest extends BlockContainer {

        public BlockSmallChest (int id) {
                super(id, Material.wood);
                setCreativeTab(CreativeTabs.tabDecorations);
        }

        @Override
        public boolean onBlockActivated(World world, int x, int y, int z,
                        EntityPlayer player, int idk, float what, float these, float are) {
                TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
                if (tileEntity == null || player.isSneaking()) {
                        return false;
                }
        player.openGui(mod_rareores.instance, 0, world, x, y, z);
                return true;
        }

        @Override
        public void breakBlock(World world, int x, int y, int z, int par5, int par6) {
                dropItems(world, x, y, z);
                super.breakBlock(world, x, y, z, par5, par6);
        }

        private void dropItems(World world, int x, int y, int z){
                Random rand = new Random();

                TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
                if (!(tileEntity instanceof IInventory)) {
                        return;
                }
                IInventory inventory = (IInventory) tileEntity;

                for (int i = 0; i < inventory.getSizeInventory(); i++) {
                        ItemStack item = inventory.getStackInSlot(i);

                        if (item != null && item.stackSize > 0) {
                                float rx = rand.nextFloat() * 0.8F + 0.1F;
                                float ry = rand.nextFloat() * 0.8F + 0.1F;
                                float rz = rand.nextFloat() * 0.8F + 0.1F;

                                EntityItem entityItem = new EntityItem(world,
                                                x + rx, y + ry, z + rz,
                                                new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));

                                if (item.hasTagCompound()) {
                                        entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
                                }

                                float factor = 0.05F;
                                entityItem.motionX = rand.nextGaussian() * factor;
                                entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
                                entityItem.motionZ = rand.nextGaussian() * factor;
                                world.spawnEntityInWorld(entityItem);
                                item.stackSize = 0;
                        }
                }
        }

        @Override
        public TileEntity createNewTileEntity(World world) {
                return new TileEntitySmallChest();
        }

}

 

TileEntitySmallChest.java

package mod_Rareores;

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;

public class TileEntitySmallChest extends TileEntity implements IInventory {

        private ItemStack[] inventorysize;

        public TileEntitySmallChest(){
                inventorysize = new ItemStack[36];
        }
        
        @Override
        public int getSizeInventory() {
                return inventorysize.length;
        }

        @Override
        public ItemStack getStackInSlot(int slot) {
                return inventorysize[slot];
        }
        
        @Override
        public void setInventorySlotContents(int slot, ItemStack stack) {
                inventorysize[slot] = stack;
                if (stack != null && stack.stackSize > getInventoryStackLimit()) {
                        stack.stackSize = getInventoryStackLimit();
                }               
        }

        @Override
        public ItemStack decrStackSize(int slot, int amt) {
                ItemStack stack = getStackInSlot(slot);
                if (stack != null) {
                        if (stack.stackSize <= amt) {
                                setInventorySlotContents(slot, null);
                        } else {
                                stack = stack.splitStack(amt);
                                if (stack.stackSize == 0) {
                                        setInventorySlotContents(slot, null);
                                }
                        }
                }
                return stack;
        }

        @Override
        public ItemStack getStackInSlotOnClosing(int slot) {
                ItemStack stack = getStackInSlot(slot);
                if (stack != null) {
                        setInventorySlotContents(slot, null);
                }
                return stack;
        }
        
        @Override
        public int getInventoryStackLimit() {
                return 64;
        }

        @Override
        public boolean isUseableByPlayer(EntityPlayer player) {
                return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
                player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
        }

        @Override
        public void openChest() {}

        @Override
        public void closeChest() {}
        
        @Override
        public void readFromNBT(NBTTagCompound tagCompound) {
                super.readFromNBT(tagCompound);
                
                NBTTagList tagList = tagCompound.getTagList("Inventory");
                for (int i = 0; i < tagList.tagCount(); i++) {
                        NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
                        byte slot = tag.getByte("Slot");
                        if (slot >= 0 && slot < inventorysize.length) {
                                inventorysize[slot] = ItemStack.loadItemStackFromNBT(tag);
                        }
                }
        }

        @Override
        public void writeToNBT(NBTTagCompound tagCompound) {
                super.writeToNBT(tagCompound);
                                
                NBTTagList itemList = new NBTTagList();
                for (int i = 0; i < inventorysize.length; i++) {
                        ItemStack stack = inventorysize[i];
                        if (stack != null) {
                                NBTTagCompound tag = new NBTTagCompound();
                                tag.setByte("Slot", (byte) i);
                                stack.writeToNBT(tag);
                                itemList.appendTag(tag);
                        }
                }
                tagCompound.setTag("Inventory", itemList);
        }

                @Override
                public String getInvName() {
                        return "mod_rareores.TileEntitySmallChest";
                }

			@Override
			public boolean isInvNameLocalized() {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean isStackValidForSlot(int i, ItemStack itemstack) {
				// TODO Auto-generated method stub
				return false;
			}
}

 

SmallChestContainer.java

package mod_Rareores;

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;

public class SmallChestContainer extends Container {

        protected TileEntitySmallChest tileEntity;

        public SmallChestContainer (InventoryPlayer inventoryPlayer, TileEntitySmallChest TileEntity)
        {
                tileEntity = TileEntity;
                for (int i = 0; i < 4; i++)
                {
                        for (int j = 0; j < 9; j++) {
                                addSlotToContainer(new Slot(tileEntity, j + i * 9, 8 + j * 18, 10 + i * 18));
                        }
                }
                bindPlayerInventory(inventoryPlayer);
        }

        @Override
        public boolean canInteractWith(EntityPlayer player)
        {
                return tileEntity.isUseableByPlayer(player);
        }


        protected void bindPlayerInventory(InventoryPlayer inventoryPlayer)
        {
                for (int i = 0; i < 3; i++)
                {
                        for (int j = 0; j < 9; j++)
                        {
                                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
                                                8 + j * 18, 96 + i * 18));
                        }
                }

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

        @Override
        public ItemStack transferStackInSlot(EntityPlayer player, int slotnumber)
        {
                ItemStack stack = null;
                Slot slot = (Slot) inventorySlots.get(slotnumber);

                if (slot != null && slot.getHasStack()) {
                        ItemStack stackInSlot = slot.getStack();
                        stack = stackInSlot.copy();

                        if (slotnumber < 36)
                        {
                                if (!this.mergeItemStack(stackInSlot, 36, 45, true)) {
                                        return null;
                                }
                        }
                        else if (!this.mergeItemStack(stackInSlot, 0, 36, false))
                        {
                                return null;
                        }

                        if (stackInSlot.stackSize == 0) {
                                slot.putStack(null);
                        } 
                        else
                        {
                                slot.onSlotChanged();
                        }

                        if (stackInSlot.stackSize == stack.stackSize)
                        {
                                return null;
                        }
                        slot.onPickupFromSlot(player, stackInSlot);
                }
                return stack;
        }
}

 

 

 

 

Below in this spoiler is the container that is larger and does not work

 

 

 

BlockLArgeChest.java

package mod_Rareores;

import java.util.Random;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class BlockLargeChest extends BlockContainer {

        public BlockLargeChest (int id) {
                super(id, Material.iron);
                setCreativeTab(CreativeTabs.tabDecorations);
        }

        @Override
        public boolean onBlockActivated(World world, int x, int y, int z,
                        EntityPlayer player, int a, float b, float c, float d) {
                TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
                if (tileEntity == null || player.isSneaking()) {
                        return false;
                }
        player.openGui(mod_rareores.instance, 1, world, x, y, z);
                return true;
        }

        @Override
        public void breakBlock(World world, int x, int y, int z, int par5, int par6) {
                dropItems(world, x, y, z);
                super.breakBlock(world, x, y, z, par5, par6);
        }

        private void dropItems(World world, int x, int y, int z){
                Random rand = new Random();

                TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
                if (!(tileEntity instanceof IInventory)) {
                        return;
                }
                IInventory inventory = (IInventory) tileEntity;

                for (int i = 0; i < inventory.getSizeInventory(); i++) {
                        ItemStack item = inventory.getStackInSlot(i);

                        if (item != null && item.stackSize > 0) {
                                float rx = rand.nextFloat() * 0.8F + 0.1F;
                                float ry = rand.nextFloat() * 0.8F + 0.1F;
                                float rz = rand.nextFloat() * 0.8F + 0.1F;

                                EntityItem entityItem = new EntityItem(world,
                                                x + rx, y + ry, z + rz,
                                                new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));

                                if (item.hasTagCompound()) {
                                        entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
                                }

                                float factor = 0.05F;
                                entityItem.motionX = rand.nextGaussian() * factor;
                                entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
                                entityItem.motionZ = rand.nextGaussian() * factor;
                                world.spawnEntityInWorld(entityItem);
                                item.stackSize = 0;
                        }
                }
        }

        @Override
        public TileEntity createNewTileEntity(World world) {
                return new TileEntityLargeChest();
        }

}

 

TileEntityLargeChest.java

package mod_Rareores;

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;

public class TileEntityLargeChest extends TileEntity implements IInventory {

        private ItemStack[] inventorysize;

        public TileEntityLargeChest(){
                inventorysize = new ItemStack[140];
        }
        
        @Override
        public int getSizeInventory() {
                return inventorysize.length;
        }

        @Override
        public ItemStack getStackInSlot(int slot) {
                return inventorysize[slot];
        }
        
        @Override
        public void setInventorySlotContents(int slot, ItemStack stack) {
                inventorysize[slot] = stack;
                if (stack != null && stack.stackSize > getInventoryStackLimit()) {
                        stack.stackSize = getInventoryStackLimit();
                }               
        }

        @Override
        public ItemStack decrStackSize(int slot, int amt) {
                ItemStack stack = getStackInSlot(slot);
                if (stack != null) {
                        if (stack.stackSize <= amt) {
                                setInventorySlotContents(slot, null);
                        } else {
                                stack = stack.splitStack(amt);
                                if (stack.stackSize == 0) {
                                        setInventorySlotContents(slot, null);
                                }
                        }
                }
                return stack;
        }

        @Override
        public ItemStack getStackInSlotOnClosing(int slot) {
                ItemStack stack = getStackInSlot(slot);
                if (stack != null) {
                        setInventorySlotContents(slot, null);
                }
                return stack;
        }
        
        @Override
        public int getInventoryStackLimit() {
                return 64;
        }

        @Override
        public boolean isUseableByPlayer(EntityPlayer player) {
                return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
                player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
        }

        @Override
        public void openChest() {}

        @Override
        public void closeChest() {}
        
        @Override
        public void readFromNBT(NBTTagCompound tagCompound) {
                super.readFromNBT(tagCompound);
                
                NBTTagList tagList = tagCompound.getTagList("Inventory");
                for (int i = 0; i < tagList.tagCount(); i++) {
                        NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
                        byte slot = tag.getByte("Slot");
                        if (slot >= 0 && slot < inventorysize.length) {
                                inventorysize[slot] = ItemStack.loadItemStackFromNBT(tag);
                        }
                }
        }

        @Override
        public void writeToNBT(NBTTagCompound tagCompound) {
                super.writeToNBT(tagCompound);
                                
                NBTTagList itemList = new NBTTagList();
                for (int i = 0; i < inventorysize.length; i++) {
                        ItemStack stack = inventorysize[i];
                        if (stack != null) {
                                NBTTagCompound tag = new NBTTagCompound();
                                tag.setByte("Slot", (byte) i);
                                stack.writeToNBT(tag);
                                itemList.appendTag(tag);
                        }
                }
                tagCompound.setTag("Inventory", itemList);
        }

                @Override
                public String getInvName() {
                        return "mod_rareores.TileEntityLargeChest";
                }

			@Override
			public boolean isInvNameLocalized() {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public boolean isStackValidForSlot(int i, ItemStack itemstack) {
				// TODO Auto-generated method stub
				return false;
			}
}

 

LargeChestContainer.java

package mod_Rareores;

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;

public class LargeChestContainer extends Container {

        protected TileEntityLargeChest tileEntity;

        public LargeChestContainer (InventoryPlayer PlayerInventory, TileEntityLargeChest te){
                tileEntity = te;

               
                for (int i = 0; i < 10; i++) {
                        for (int j = 0; j < 14; j++) {
                                addSlotToContainer(new Slot(tileEntity, j + i * 14, -54 + 10 + j * 18, -54 + 12 + i * 18));
                        }
                }
                bindPlayerInventory(PlayerInventory);
        }

        @Override
        public boolean canInteractWith(EntityPlayer player) {
                return tileEntity.isUseableByPlayer(player);
        }


        protected void bindPlayerInventory(InventoryPlayer inventoryPlayer)
        {
                for (int i = 0; i < 3; i++) {
                        for (int j = 0; j < 9; j++)
                        {
                                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
                                                1 + j * 18, 139 + i * 18));
                        }
                }
                for (int i = 0; i < 9; i++)
                {
                        addSlotToContainer(new Slot(inventoryPlayer, i, 1 + i * 18, 193));
                }
        }

        @Override
        public ItemStack transferStackInSlot(EntityPlayer player, int slotnumber) {
                ItemStack slotstack = null;
                Slot slot = (Slot) inventorySlots.get(slotnumber);

                if (slot != null && slot.getHasStack()) {
                        ItemStack stackInSlot = slot.getStack();
                        slotstack = stackInSlot.copy();
                        if (slotnumber < 140)
                        {
                                if (!this.mergeItemStack(stackInSlot, 140, this.inventorySlots.size() , true)) 
                                {
                                    return null;
                                }
                        }
                        //places it into the tileEntity is possible since its in the player inventory
                        else if (!this.mergeItemStack(stackInSlot, 0, 140, false)) 
                        {
                            return null;
                        }

                        if (stackInSlot.stackSize == 0)
                        {
                            slot.putStack(null);
                        }
                        else
                        {
                            slot.onSlotChanged();
                        }

                        if (stackInSlot.stackSize == slotstack.stackSize)
                        {
                            return null;
                        }
                        slot.onPickupFromSlot(player, stackInSlot);
                }
                return slotstack;
        }
}

 

 

 

 

I really hope someone can figure this out, I've just about had it...

This is the creator of the Rareores mod! Be sure to check it out at

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.