Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

items disappearing from slot when container is closed


bigbaddevil6
 Share

Recommended Posts

??? I provided code in my last thread

http://www.minecraftforge.net/forum/index.php/topic,25281.msg128794.html#msg128794

 

before that code I had none to show. Just asking concept designs.

 

Although I do admit I failed providing the code this time around.

 

Here is the Container

 

 

package com.bigbaddevil7.rustic.client.interfaces;


import com.bigbaddevil7.rustic.init.ModBlocks;
import com.bigbaddevil7.rustic.tileentity.TileEntityTable;
import com.bigbaddevil7.rustic.utility.LogHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ContainerTable extends Container {

    public InventoryCrafting craftMatrix;
    //public static InventoryCrafting toolSlot;
    public IInventory craftResult;
    private static TileEntityTable toolSlot;
    private World worldObj;
    private int posX, posY, posZ;

    public ContainerTable(InventoryPlayer invPlayer, World world, int x, int y, int z){
        craftMatrix = new InventoryCrafting(this,5 ,5);
        toolSlot = new TileEntityTable(this, 2, 2);
        craftResult = new InventoryCraftResult();
        worldObj = world;
        posX = x;
        posY = y;
        posZ = z;

        this.addSlotToContainer(new SlotCrafting(invPlayer.player, craftMatrix, craftResult, 0, 141, 11));


        for(int i = 0; i < 4; i++){
            for(int k = 0; k < 4; k++){
                this.addSlotToContainer(new SlotTool(toolSlot, k + i * 4, 115  + k * 18, 43 + i * 18));
            }
        }



        for(int i = 0; i < 5; i++){
            for(int k = 0; k < 5; k++){
                this.addSlotToContainer(new Slot(craftMatrix, k + i * 5, 8 + k * 18, 7 + i * 18));
            }
        }

        for(int i = 0; i < 3; i++){
            for(int k  = 0; k < 9; k++){
                this.addSlotToContainer(new Slot(invPlayer, k + i * 9 + 9, 8 + k * 18, 106 + i * 18));
            }
        }

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

        onCraftMatrixChanged(craftMatrix);
    }

    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(par2);

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

            if (par2 == 0)
            {
                if (!this.mergeItemStack(itemstack1, 10, 46, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 >= 10 && par2 < 37)
            {
                if (!this.mergeItemStack(itemstack1, 37, 46, false))
                {
                    return null;
                }
            }
            else if (par2 >= 37 && par2 < 46)
            {
                if (!this.mergeItemStack(itemstack1, 10, 37, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 10, 46, false))
            {
                return null;
            }

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

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
        }

        return itemstack;
    }

    public void onContainerClosed(EntityPlayer par1EntityPlayer)
    {
        //super.onContainerClosed(par1EntityPlayer);

        if (!this.worldObj.isRemote)
        {
            for (int i = 0; i < 25; ++i)
            {
                ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);

                if (itemstack != null)
                {
                    par1EntityPlayer.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
            /*for(int i = 0; i < 4; i++) {
                ItemStack itemStack2 = this.toolSlot.getStackInSlotOnClosing(i);
                if (itemStack2 != null) {
                    par1EntityPlayer.dropPlayerItemWithRandomChoice(itemStack2, false);
                }
            }*/
        }
    }

    public void onCraftMatrixChanged(IInventory inventory){
        craftResult.setInventorySlotContents(0, CraftingManagerTable.getInstance().findMatchingRecipe(craftMatrix, worldObj));
    }


    @Override
    public boolean canInteractWith(EntityPlayer player) {
        if(worldObj.getBlock(posX, posY, posZ) != ModBlocks.blockTable){
            return false;
        }else{
            return player.getDistanceSq((double)posX + 0.5D, (double)posY + 0.5D, (double)posZ + 0.5D) <= 64.0D;
        }
    }

    public static boolean toolInSlot(){
        boolean hasItem = true;
        for(int i = 0; i < 4; i++) {
            ItemStack slot = toolSlot.getStackInSlot(i);
            if (slot == null) {
                hasItem = false;
            }
        }
        return hasItem;
    }
}

 

 

 

Here is the TileEntity that acts for the slots

 

 

package com.bigbaddevil7.rustic.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
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;

/**
* Created by bigbaddevil7 on 11/15/2014.
*/
public class TileEntityTable extends TileEntity implements IInventory {

    private ItemStack[] stackList;
    private int inventoryWidth;
    private static Container eventHandler;

    public TileEntityTable(){};

    public TileEntityTable(Container container, int x, int y){
        int k = x * y;
        this.stackList = new ItemStack[k];
        this.eventHandler = container;
        this.inventoryWidth = x;
    }

    @Override
    public int getSizeInventory() {
        return this.stackList.length;
    }

    @Override
    public ItemStack getStackInSlot(int slot) {
        return slot >= this.getSizeInventory() ? null : this.stackList[slot];
    }


    /**
     * Returns the itemstack in the slot specified (Top left is 0, 0). Args: row, column
     */
    public ItemStack getStackInRowAndColumn(int par1, int par2)
    {
        if (par1 >= 0 && par1 < this.inventoryWidth)
        {
            int k = par1 + par2 * this.inventoryWidth;
            return this.getStackInSlot(k);
        }
        else
        {
            return null;
        }
    }


    @Override
    public ItemStack decrStackSize(int par1, int par2)
    {
        if (this.stackList[par1] != null)
        {
            ItemStack itemstack;

            if (this.stackList[par1].stackSize <= par2)
            {
                itemstack = this.stackList[par1];
                this.stackList[par1] = null;
                this.eventHandler.onCraftMatrixChanged(this);
                return itemstack;
            }
            else
            {
                itemstack = this.stackList[par1].splitStack(par2);

                if (this.stackList[par1].stackSize == 0)
                {
                    this.stackList[par1] = null;
                }

                this.eventHandler.onCraftMatrixChanged(this);
                return itemstack;
            }
        }
        else
        {
            return null;
        }
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int par1) {
        if (this.stackList[par1] != null)
        {
            ItemStack itemstack = this.stackList[par1];
            this.stackList[par1] = null;
            return itemstack;
        }
        else
        {
            return null;
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);

        NBTTagList items = new NBTTagList();

        for(int i = 0; i < getSizeInventory(); i++){
            ItemStack stack = getStackInSlot(i);

            if(stack != null){
                NBTTagCompound item = new NBTTagCompound();
                item.setByte("Slot", (byte)i);
                stack.writeToNBT(item);
                items.appendTag(item);
            }
        }

        compound.setTag("Items", items);
    }


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

        NBTTagList items = compound.getTagList("Items", 10);

        for(int i = 0; i < items.tagCount(); i++){
            NBTTagCompound item = (NBTTagCompound)items.getCompoundTagAt(i);
            int slot = item.getByte("Slot");

            if(slot >= 0 && slot <getSizeInventory()){
                setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
            }
        }
    }

    @Override
    public void setInventorySlotContents(int par1, ItemStack itemStack) {
        this.stackList[par1] = itemStack;
        this.eventHandler.onCraftMatrixChanged(this);

    }

    @Override
    public String getInventoryName() {
        return "container.prototyping table";
    }

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

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

    @Override
    public boolean isUseableByPlayer(EntityPlayer var1) {
        return true;
    }

    @Override
    public void openInventory() {}

    @Override
    public void closeInventory() {}

    @Override
    public boolean isItemValidForSlot(int var1, ItemStack var2) {
        return false;
    }
}
[/spoiler]

 

Link to comment
Share on other sites

if you know what you are doing.

 

Yea... sounds tricky, although I think I may have an understanding of what you mean by it, would it be something along the lines of making two methods in the Container called buildContainer() and buildTileEntity() to handle the different types and making of different slots then call them accordingly?

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
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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • omg thanks so so much!! you're the best!!!
    • 1.12 is no longer supported on this forum. Please update to a modern version of Minecraft to receive support.
    • I recommend getting Java from https://adoptium.net/.
    • ---- Minecraft Crash Report ---- WARNING: coremods are present:   Quark Plugin (Quark-r1.6-179.jar)   weaponlib (mw_2.0-0.4.4.6_mc1.12.2.jar)   SecurityCraftLoadingPlugin ([1.12.2]+SecurityCraft+v1.9.0.2.jar) Contact their authors BEFORE contacting forge // You're mean. Time: 1/23/22 4:24 PM Description: Unexpected error java.lang.NoSuchMethodError: net.minecraft.client.multiplayer.WorldClient.isPlayerUpdate()Z     at net.minecraft.client.renderer.chunk.RenderChunk.isWorldPlayerUpdate(RenderChunk.java:626)     at net.minecraft.client.renderer.chunk.RenderChunk.func_178575_a(RenderChunk.java:588)     at net.minecraft.client.renderer.ViewFrustum.func_187474_a(ViewFrustum.java:171)     at net.minecraft.client.renderer.RenderGlobal.func_184385_a(RenderGlobal.java:2940)     at net.minecraft.client.renderer.RenderGlobal.func_72734_e(RenderGlobal.java:1790)     at net.minecraft.client.Minecraft.func_71407_l(Minecraft.java:1835)     at net.minecraft.client.Minecraft.func_71411_J(Minecraft.java:1098)     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:398)     at net.minecraft.client.main.Main.main(SourceFile:123)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)     at net.minecraft.launchwrapper.Launch.main(Launch.java:28) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Client thread Stacktrace:     at net.minecraft.client.renderer.chunk.RenderChunk.isWorldPlayerUpdate(RenderChunk.java:626)     at net.minecraft.client.renderer.chunk.RenderChunk.func_178575_a(RenderChunk.java:588)     at net.minecraft.client.renderer.ViewFrustum.func_187474_a(ViewFrustum.java:171)     at net.minecraft.client.renderer.RenderGlobal.func_184385_a(RenderGlobal.java:2940)     at net.minecraft.client.renderer.RenderGlobal.func_72734_e(RenderGlobal.java:1790) -- Affected level -- Details:     Level name: MpServer     All players: 1 total; [EntityPlayerSP['MilesMoise'/279, l='MpServer', x=8.50, y=65.00, z=8.50]]     Chunk stats: MultiplayerChunkCache: 121, 121     Level seed: 0     Level generator: ID 00 - default, ver 1. Features enabled: false     Level generator options:      Level spawn location: World: (192,64,248), Chunk: (at 0,4,8 in 12,15; contains blocks 192,0,240 to 207,255,255), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)     Level time: 9996 game time, 9996 day time     Level dimension: 0     Level storage version: 0x00000 - Unknown?     Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)     Level game mode: Game mode: survival (ID 0). Hardcore: false. Cheats: false     Forced entities: 25 total; [EntityChicken['Chicken'/70, l='MpServer', x=212.86, y=75.00, z=220.47], EntityChicken['Chicken'/71, l='MpServer', x=218.19, y=75.00, z=219.92], EntityZombie['Zombie'/1607, l='MpServer', x=245.50, y=60.00, z=308.50], EntityItem['item.tile.torch'/73, l='MpServer', x=218.94, y=27.00, z=287.13], EntityItem['item.item.feather'/842, l='MpServer', x=170.20, y=73.00, z=193.66], EntityPlayerSP['MilesMoise'/279, l='MpServer', x=8.50, y=65.00, z=8.50], EntityItem['item.tile.torch'/77, l='MpServer', x=210.13, y=23.00, z=301.13], EntityItem['item.tile.torch'/78, l='MpServer', x=213.47, y=27.00, z=298.29], EntityItem['item.tile.torch'/79, l='MpServer', x=214.70, y=27.00, z=302.48], EntityItem['item.item.feather'/594, l='MpServer', x=169.78, y=75.00, z=191.61], EntityItem['item.item.quark:root_blue_flower'/1187, l='MpServer', x=197.30, y=24.00, z=243.25], EntityChicken['Chicken'/36, l='MpServer', x=137.50, y=64.00, z=300.50], EntityChicken['Chicken'/37, l='MpServer', x=142.50, y=71.00, z=294.79], EntityItem['item.item.feather'/293, l='MpServer', x=202.03, y=74.00, z=235.01], EntityItem['item.tile.doublePlant.paeonia'/295, l='MpServer', x=173.97, y=70.00, z=269.49], EntityItem['item.tile.doublePlant.rose'/44, l='MpServer', x=156.31, y=68.00, z=258.96], EntityChicken['Chicken'/45, l='MpServer', x=151.07, y=71.00, z=292.92], EntityChicken['Chicken'/46, l='MpServer', x=145.41, y=67.00, z=304.50], EntityChicken['Chicken'/48, l='MpServer', x=172.81, y=75.00, z=188.90], EntityChicken['Chicken'/49, l='MpServer', x=175.32, y=73.00, z=191.80], EntityChicken['Chicken'/51, l='MpServer', x=169.86, y=75.00, z=192.39], EntityChicken['Chicken'/53, l='MpServer', x=181.50, y=75.00, z=217.56], EntityChicken['Chicken'/59, l='MpServer', x=201.60, y=74.00, z=235.83], EntityMinecartChest['Minecart with Chest'/60, l='MpServer', x=202.50, y=23.06, z=290.50], EntityItem['item.item.feather'/1404, l='MpServer', x=145.88, y=67.00, z=305.21]]     Retry entities: 0 total; []     Server brand: fml,forge     Server type: Non-integrated multiplayer server Stacktrace:     at net.minecraft.client.multiplayer.WorldClient.func_72914_a(WorldClient.java:420)     at net.minecraft.client.Minecraft.func_71396_d(Minecraft.java:2741)     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:427)     at net.minecraft.client.main.Main.main(SourceFile:123)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)     at net.minecraft.launchwrapper.Launch.main(Launch.java:28) -- System Details -- Details:     Minecraft Version: 1.12.2     Operating System: Windows 10 (amd64) version 10.0     Java Version: 1.8.0_51, Oracle Corporation     Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation     Memory: 1014992848 bytes (967 MB) / 2147483648 bytes (2048 MB) up to 2147483648 bytes (2048 MB)     JVM Flags: 8 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -Xmx2G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M     IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0     FML: MCP 9.42 Powered by Forge 14.23.5.2860 Optifine OptiFine_1.12.1_HD_U_G5 13 mods loaded, 13 mods active     States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored     | State  | ID              | Version      | Source                              | Signature                                |     |:------ |:--------------- |:------------ |:----------------------------------- |:---------------------------------------- |     | LCHIJA | minecraft       | 1.12.2       | minecraft.jar                       | None                                     |     | LCHIJA | mcp             | 9.42         | minecraft.jar                       | None                                     |     | LCHIJA | FML             | 8.0.99.99    | forge-1.12.2-14.23.5.2860.jar       | e3c3d50c7c986df74c645c0ac54639741c90a557 |     | LCHIJA | forge           | 14.23.5.2860 | forge-1.12.2-14.23.5.2860.jar       | e3c3d50c7c986df74c645c0ac54639741c90a557 |     | LCHIJA | securitycraft   | v1.9.0.2     | [1.12.2]+SecurityCraft+v1.9.0.2.jar | None                                     |     | LCHIJA | jei             | 4.16.1.302   | jei_1.12.2-4.16.1.302.jar           | None                                     |     | LCHIJA | quark           | r1.6-179     | Quark-r1.6-179.jar                  | None                                     |     | LCHIJA | autoreglib      | 1.3-32       | AutoRegLib-1.3-32.jar               | None                                     |     | LCHIJA | cfm             | 6.3.1        | furniture-6.3.1-1.12.2.jar          | None                                     |     | LCHIJA | mw              | 0.4.4.6      | mw_2.0-0.4.4.6_mc1.12.2.jar         | None                                     |     | LCHIJA | thutcore        | 5.21.0       | thutcore-1.12.2-5.21.0.jar          | None                                     |     | LCHIJA | thutcore_compat | 1.0          | thutcore-1.12.2-5.21.0.jar          | None                                     |     | LCHIJA | thuttech        | 6.0.16       | thuttech-1.12.2-6.0.16.jar          | None                                     |     Loaded coremods (and transformers):  Quark Plugin (Quark-r1.6-179.jar)   vazkii.quark.base.asm.ClassTransformer weaponlib (mw_2.0-0.4.4.6_mc1.12.2.jar)   com.vicmatskiv.weaponlib.core.WeaponlibClassTransformer SecurityCraftLoadingPlugin ([1.12.2]+SecurityCraft+v1.9.0.2.jar)        GL info: ' Vendor: 'ATI Technologies Inc.' Version: '4.6.13596 Compatibility Profile Context 20.10.28 27.20.1028.1' Renderer: 'AMD Radeon(TM) Vega 3 Graphics'     Launched Version: 1.12.2-forge-14.23.5.2860     LWJGL: 2.9.4     OpenGL: AMD Radeon(TM) Vega 3 Graphics GL version 4.6.13596 Compatibility Profile Context 20.10.28 27.20.1028.1, ATI Technologies Inc.     GL Caps: Using GL 1.3 multitexturing. Using GL 1.3 texture combiners. Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported. Shaders are available because OpenGL 2.1 is supported. VBOs are available because OpenGL 1.5 is supported.     Using VBOs: Yes     Is Modded: Definitely; Client brand changed to 'fml,forge'     Type: Client (map_client.txt)     Resource Packs: vanilla, file/§4§l§o§kaaa§6§l§oCustomTotem§a1.18§4§l§o§kaaa.zip     Current Language: English (US)     Profiler Position: N/A (disabled)     CPU: 4x AMD Ryzen 3 3200U with Radeon Vega Mobile Gfx      OptiFine Version: OptiFine_1.12.1_HD_U_G5     OptiFine Build: 20210124-161721     Render Distance Chunks: 11     Mipmaps: 3     Anisotropic Filtering: 1     Antialiasing: 0     Multitexture: false     Shaders: null     OpenGlVersion: 4.6.13596 Compatibility Profile Context 20.10.28 27.20.1028.1     OpenGlRenderer: AMD Radeon(TM) Vega 3 Graphics     OpenGlVendor: ATI Technologies Inc.     CpuCount: 4
    • what should i download? can i get the link?
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.