Jump to content

[1.12.2] Additional inventory for player doesn't save


Cyb3R

Recommended Posts

I created an GUI to open with a key bind. This works with out any errors.

I also created a Capability that extends ItemStackHandler, an InventoryProvider, and an InventroyStorage.

The Capability seems to work, but if place an item into my GUI and close it, the item will moved back to the hotbars first slot and the items within the gui will not display. It seems like it never call

deserializeNBT

. I printed out everytime

serializeNBT

or

deserializeNBT

is called and

deserializeNBT

is never printed.

What am i missing?

 

My Class that extends ItemStackHandler

 

package de.cyb3r.testmod.capabilities;
;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.items.ItemStackHandler;

public class CustomInventoryDef extends ItemStackHandler{

    private String item;

    public CustomInventoryDef(int size) {
        this.setSize(size);
    }

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

    @Override
    public ItemStack getStackInSlot(int slot ) {
        return super.getStackInSlot(slot);
    }

    @Override
    public ItemStack insertItem( int slot , ItemStack stack , boolean simulate ) {
        item = stack.toString();
        return super.insertItem(slot, stack, simulate);
    }

    @Override
    public ItemStack extractItem( int slot , int amount , boolean simulate ) {
        return super.extractItem(slot ,amount,simulate);
    }

    @Override
    public int getSlotLimit(int slot) {
        return 1;
    }

    public String getItem() {
        return item;
    }

    @Override
    public NBTTagCompound serializeNBT()
    {
        NBTTagCompound nbt2 = new NBTTagCompound();
        NBTTagCompound nbt = super.serializeNBT();
        nbt2.setTag("customInventoryDef",nbt);
        System.out.println("CustomInventoryDef.serializeNBT:"+nbt2);
        return nbt2;
    }

    @Override
    public void deserializeNBT(NBTTagCompound nbt2)
    {
        System.out.println(nbt2);
        NBTTagCompound nbt = nbt2.getCompoundTag("customInventoryDef");
        super.deserializeNBT(nbt);
    }

}

 

 

My CapabilityProvider

 

package de.cyb3r.testmod.capabilities;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class InventoryProvider implements ICapabilitySerializable<NBTTagCompound> {

    private final CustomInventoryDef inv;

    public InventoryProvider( CustomInventoryDef inv ) {

        this.inv = inv ;

    }

    @Override
    public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
        return capability == CustomInventoryCap.CUSTOM_INVENTORY;
    }

    @Nullable
    @Override
    public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
        return (T)inv;
    }

    @Override
    public NBTTagCompound serializeNBT() {
        System.out.println("InventoryProvider.serializeNBT");
        return inv.serializeNBT();
    }

    @Override
    public void deserializeNBT(NBTTagCompound nbt) {
        System.out.println("InventoryProvider.deserializeNBT");
        inv.deserializeNBT(nbt);
    }
}

 

 

My IStorage class

 

package de.cyb3r.testmod.capabilities;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.ItemStackHandler;

public class InventoryStorage implements Capability.IStorage<CustomInventoryDef> {

    @Override
    public NBTBase writeNBT(Capability< CustomInventoryDef > capability ,
                            CustomInventoryDef instance , EnumFacing side ) {
        System.out.println("InventoryStorage.writeNBT");
        return instance.serializeNBT();
    }

    @Override
    public void readNBT(Capability< CustomInventoryDef > capability ,
                        CustomInventoryDef instance , EnumFacing side , NBTBase nbt ) {
        System.out.println("InventoryStorage.readNBT");
        instance.deserializeNBT((NBTTagCompound) nbt);

    }

}

 

 

Here it register my Capability (gets called from the @Mod class's preInit)(yes i am aware im using a deprecated method here)

 

package de.cyb3r.testmod.capabilities;

import net.minecraftforge.common.capabilities.CapabilityManager;

public class TestModCapabilities {


    public static void registerAllCapabilities() {
       CapabilityManager.INSTANCE.register(CustomInventoryDef.class , new InventoryStorage() , CustomInventoryDef.class );
    }
}

 

 

My GuiContainer

 

package de.cyb3r.testmod.client.gui;

import de.cyb3r.testmod.TestMod;
import de.cyb3r.testmod.capabilities.CustomInventoryDef;
import de.cyb3r.testmod.container.ContainerSamusMenu;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextComponentTranslation;

import java.awt.*;

public class GuiSamusMenuInventory extends GuiContainer {

    private static final ResourceLocation texture = new ResourceLocation(TestMod.MODID, "textures/gui/container.png");

    public GuiSamusMenuInventory(EntityPlayer player) {
        super(new ContainerSamusMenu(player,false));
        xSize = 176;
        ySize = 166;
    }


    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
        fontRenderer.drawString(new TextComponentTranslation("tile.tutorial_container.name").getFormattedText(), 5, 5, Color.darkGray.getRGB());
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
        drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
    }
}

 

 

My GuiHandler

 

package de.cyb3r.testmod.client.gui;

import de.cyb3r.testmod.capabilities.CustomInventoryDef;
import de.cyb3r.testmod.container.ContainerSamusMenu;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

import javax.annotation.Nullable;

public class GuiHandler implements IGuiHandler {

    public static final int GUI_SAMUS_MENU_CONTAINER_ID = 0;
    public static final int GUI_SAMUS_MENU_ID = 1;

    @Nullable
    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

        switch (ID) {
            case GUI_SAMUS_MENU_ID:
                return new ContainerSamusMenu(player,false );
            default: return null;
        }
    }

    @Nullable
    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

        switch (ID) {
            case GUI_SAMUS_MENU_ID:
                return new GuiSamusMenuInventory(player);
            default: return null;
        }
    }
}

My SlotItemHandler

 

package de.cyb3r.testmod.container;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

import javax.annotation.Nonnull;

public class SlotSamusMenu extends SlotItemHandler {

    private Item acceptedItem;

    public SlotSamusMenu(IItemHandler itemHandler, int index, int xPosition, int yPosition, Item acceptedItem) {
        super(itemHandler, index, xPosition, yPosition);
        this.acceptedItem = acceptedItem;

    }

    @Override
    public int getSlotStackLimit() {
        return 1;
    }

    @Override
    public boolean isItemValid(@Nonnull ItemStack stack) {
        if (stack.getItem() == acceptedItem) {
            return true;
        } else {
            return false;
        }
    }


}

 

 

My Container

 

package de.cyb3r.testmod.container;

import de.cyb3r.testmod.capabilities.CustomInventoryCap;
import de.cyb3r.testmod.capabilities.CustomInventoryDef;
import de.cyb3r.testmod.items.ModItems;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class ContainerSamusMenu extends Container {

    public ContainerSamusMenu(final EntityPlayer player, boolean localWorld) {

        if (player.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
        IItemHandler inventory = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        IItemHandler customInventoryDef = player.getCapability(CustomInventoryCap.CUSTOM_INVENTORY,null);

            // CONTAINER INVENTORY
            for (int y = 0; y < 3; y++) {
                for (int x = 0; x < 3; x++) {
                    addSlotToContainer(new SlotSamusMenu(customInventoryDef, x + (y * 3), 62 + x * 18, 17 + y * 18, ModItems.hiJumpBoots));
                }
            }

            // MAIN PLAYER INVENTORY
            for (int y = 0; y < 3; y++) {
                for (int x = 0; x < 9; x++) {
                    addSlotToContainer(new SlotItemHandler(inventory, x + (y * 9) + 9, 8 + x * 18, 84 + y * 18));
                }
            }

            // PLAYER HOTBAR INVENTORY
            for (int i = 0; i < 9; i++) {
                addSlotToContainer(new SlotItemHandler(inventory, i, 8 + (i * 18), 142));
            }
        }
    }

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

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

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

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

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

            slot.onTake(player, stackInSlot);

        }
        return stack;
    }

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

 

 

CustomInventoryDef.java

InventoryProvider.java

InventoryStorage.java

TestModCapabilities.java

GuiSamusMenuInventory.java

GuiHandler.java

SlotSamusMenu.java

ContainerSamusMenu.java

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • ChemsXpress (https://chemsxpress.com): ChemsXpress is a platform specializing in the distribution of research chemicals and related products. It offers a wide range of substances for scientific and laboratory use, catering to researchers and professionals. BubatzLand (https://bubatzland.com): BubatzLand focuses on providing high-quality cannabis products. The site offers a variety of strains, edibles, and related accessories, aiming to serve the needs of cannabis enthusiasts and medicinal users. HalluShroom (https://hallushroom.com): HalluShroom specializes in the sale of psychedelic mushrooms and related products. It caters to individuals interested in exploring the benefits and experiences associated with psilocybin and other psychedelic substances. HighAsch (https://highasch.com): HighAsch offers a diverse range of cannabis products, including flowers, concentrates, and edibles. The site aims to provide premium-quality products to both recreational and medicinal users. KetaminHaus (https://ketaminhaus.com): KetaminHaus provides ketamine and related products for research and therapeutic use. The platform focuses on ensuring the availability of high-quality, safe, and effective ketamine for various applications. KokainLabor (https://kokainlabor.com): KokainLabor offers cocaine and other related substances. The site targets individuals seeking high-purity products for personal or research purposes, emphasizing quality and safety. MDPHPWelt (https://mdphpwelt.com): MDPHPWelt specializes in the distribution of MDPHP and other similar research chemicals. It aims to supply researchers with high-quality substances for scientific and experimental use. NembutaLabor (https://nembutalabor.com): NembutaLabor provides Nembutal (pentobarbital) and related products. The site focuses on supplying these substances for research and veterinary purposes, ensuring high standards of quality and safety. NembutalQuelle (https://nembutalquelle.com): NembutalQuelle is dedicated to offering Nembutal and similar barbiturates. It caters to researchers and professionals who require these substances for scientific, medical, or veterinary applications. JBSaves Ltda Brazil (https://jbsavesltdabrazil.com): JBSaves Ltda Brazil is involved in the food and agricultural industry, providing a variety of products and services related to food processing, distribution, and agricultural practices. Bello Alimentos Ltda (https://belloalimentosltda.com): Bello Alimentos Ltda offers a wide range of food products. The site focuses on high-quality, nutritious, and safe food items, catering to consumers and businesses in the food industry. São Salvador Alimentos (https://saosalvadoralimentoss.com): São Salvador Alimentos specializes in the production and distribution of food products. The site provides a variety of options aimed at meeting the dietary needs and preferences of diverse consumers. WietWereld (https://wietwereld.com): WietWereld is a platform dedicated to the cannabis community. It offers information, products, and resources related to cannabis cultivation, consumption, and culture. 14. BeuhParadise (https://beuhparadis.com): BeuhParadise offers a range of cannabis products, including flowers, edibles, and concentrates. The site aims to provide high-quality cannabis products to recreational and medicinal users, emphasizing purity and potency. 15. WietWereld (https://wietwereld.com): WietWereld is a comprehensive resource for cannabis enthusiasts. It provides products, cultivation guides, and community forums for those interested in cannabis culture and cultivation. GrasOase (https://grasoase.com): GrasOase specializes in cannabis products and accessories. The site offers a variety of strains, seeds, and related items for cannabis users, focusing on quality and customer satisfaction. https://chemsxpress.com/ https://bubatzland.com/ https://hallushroom.com/ https://highasch.com/ https://ketaminhaus.com/ https://kokainlabor.com/ https://mdphpwelt.com/ https://nembutalabor.com/ https://nembutalquelle.com/ https://jbsavesltdabrazil.com/ https://belloalimentosltda.com/ https://saosalvadoralimentoss.com/ https://wietwereld.com https://beuhparadis.com https://wietwereld.com https://grasoase.com/
    • For Example I have an Main Mod that adds new Content to Minecraft. But I'm using custom Classes etc. and I want it so that is an api integrated in the Main Mod but can be download seperately like an Module. Is this possible? If you need more Information just comment what you need to know ^^
    • Delete the config of this mod (config folder) and test it again   If there is no change, try other builds  
    • Add the full crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here
  • Topics

×
×
  • Create New...

Important Information

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