Jump to content

[SOLVED] Inventory not saved after relog


xTimPugz

Recommended Posts

Hello

 

Like the title says, the inventory contents are not being saved after a relog.

I use an unsupported version of Forge, though.

If this system has changed during the update after 1.7.10 feel and no one knows the answer, free to delete this thread. (If the answer has been given please do not delete this, as it might help others out)

 

Basically I have made a Container and GuiScreen which are opened by a keypress. It's all opening and such, and everything kind of works except for the fact that my inventory is not saving itself after a reload.

I probably messed up somewhere.

 

Here is the code...

 

 

IExtendedEntityProperty:

 

package com.example.examplemod;

import com.example.examplemod.inventory.RingInventory;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

/**
* Created by jove on 18/08/2016.
*/
public class RingInventoryProperty implements IExtendedEntityProperties {
    private ItemStack[] stacks;
    private RingInventory inventory = new RingInventory();
    @Override
    public void saveNBTData(NBTTagCompound compound) {
        System.out.println("save nbt");
        inventory.writeToNBT(compound);
    }

    @Override
    public void loadNBTData(NBTTagCompound compound) {
        System.out.println("load nbt");
        inventory.readFromNBT(compound);
    }

    @Override
    public void init(Entity entity, World world) {

    }

    public void setToSave(ItemStack[] stacks){
        this.stacks = stacks;
    }

    public ItemStack[] getStacks() {
        return stacks;
    }
    public static RingInventoryProperty get(EntityPlayer p){
        return (RingInventoryProperty) p.getExtendedProperties("ringInventory");
    }

    public RingInventory getInventory() {
        return inventory;
    }


}

 

 

 

IInventory

 

package com.example.examplemod.inventory;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

/**
* Created by jove on 18/08/2016.
*/
public class RingInventory implements IInventory {
    ItemStack[] contents;

    public RingInventory() {
        contents = new ItemStack[2];

    }

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

    @Override
    public ItemStack getStackInSlot(int p_70301_1_) {
        return contents[p_70301_1_];
    }

    @Override
    public ItemStack decrStackSize(int p_70298_1_, int p_70298_2_) {
        contents[p_70298_1_].stackSize--;
        return contents[p_70298_1_];
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int p_70304_1_) {
        return contents[p_70304_1_];
    }

    @Override
    public void setInventorySlotContents(int p_70299_1_, ItemStack p_70299_2_) {
        contents[p_70299_1_] = p_70299_2_;
    }

    @Override
    public String getInventoryName() {
        return "Ring Inventory";
    }

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

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

    @Override
    public void markDirty() {

    }

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

    @Override
    public void openInventory() {

    }

    @Override
    public void closeInventory() {
    }

    @Override
    public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
        return true;
    }


    public void writeToNBT(NBTTagCompound compound)
    {
        NBTTagList items = new NBTTagList();

        for (int i = 0; i < getSizeInventory(); ++i)
        {
            if (getStackInSlot(i) != null)
            {
                System.out.println("ggg");
                NBTTagCompound item = new NBTTagCompound();
                item.setByte("Slot", (byte) i);
                getStackInSlot(i).writeToNBT(item);
                items.appendTag(item);
            }
        }

        // We're storing our items in a custom tag list using our 'tagName' from above
        // to prevent potential conflicts
        compound.setTag("ringInventory", items);
    }

    public void readFromNBT(NBTTagCompound compound)
    {
        NBTTagList items = compound.getTagList("ringInventory",;
        System.out.println("TAG COUNT" + items.tagCount());
        for (int i = 0; i < items.tagCount(); ++i)
        {
            NBTTagCompound item = items.getCompoundTagAt(i);
            byte slot = item.getByte("Slot");
            contents[slot] = ItemStack.loadItemStackFromNBT(item);
        }
    }
}

 

 

I also use a PacketHandler, yet I do not really know why I have to use it...

 

 

 public EMPacketHandler(String channelName) {
        super(channelName);
    }

    @Override
    public void sendToServer(IMessage message) {
        super.sendToServer(message);
    }

    @Override
    public void sendTo(IMessage message, EntityPlayerMP player) {
        super.sendTo(message, player);
    }

IMessage and its handler

package com.example.examplemod.network;

 

import com.example.examplemod.ExampleMod;

import com.example.examplemod.RingInventoryProperty;

import com.example.examplemod.network.message_abs.AbstractClientMessageHandler;

import com.example.examplemod.network.message_abs.AbstractMessageHandler;

import com.example.examplemod.network.message_abs.AbstractServerMessageHandler;

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

import cpw.mods.fml.common.network.simpleimpl.IMessage;

import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;

import cpw.mods.fml.common.network.simpleimpl.MessageContext;

import io.netty.buffer.ByteBuf;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.EntityPlayerMP;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.network.PacketBuffer;

 

import java.io.IOException;

 

/**

* Created by jove on 19/08/2016.

*/

public class SyncPlayerPropsMessage implements IMessage {

    private EntityPlayer entity;

    private NBTTagCompound data;

    public SyncPlayerPropsMessage() {

    }

    public SyncPlayerPropsMessage(EntityPlayer entityPlayer) {

        this.entity = entityPlayer;

        data = new NBTTagCompound();

        RingInventoryProperty.get(entityPlayer).saveNBTData(data);

    }

 

    @Override

    public void fromBytes(ByteBuf buf) {

        data = ByteBufUtils.readTag(buf);

    }

 

    @Override

    public void toBytes(ByteBuf buf) {

        ByteBufUtils.writeTag(buf, data);

 

 

    }

 

 

    public static class Handler implements IMessageHandler<SyncPlayerPropsMessage, IMessage> {

 

 

        @Override

        public IMessage onMessage(SyncPlayerPropsMessage message, MessageContext ctx) {

            System.out.println("GOT PACKET AND HANDLING IT AT : " + ctx.side);

            ctx.getServerHandler().playerEntity.getExtendedProperties("ringInventory").saveNBTData(message.data);

 

            return null;

        }

    }

 

}

 

How I call the Packet

package com.example.examplemod;

 

import com.example.examplemod.inventory.RingInventory;

import com.example.examplemod.network.SyncPlayerPropsMessage;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.EntityPlayerMP;

import net.minecraftforge.event.entity.EntityEvent;

import net.minecraftforge.event.entity.EntityJoinWorldEvent;

 

/**

* Created by jove on 18/08/2016.

*/

public class EventHandling {

    @SubscribeEvent

    public void onEntityConstruct(EntityEvent.EntityConstructing e){

        if(e.entity instanceof EntityPlayer){

            if(RingInventoryProperty.get((EntityPlayer)e.entity) == null){

                System.out.println("registered now ringInventory");

                e.entity.registerExtendedProperties("ringInventory", new RingInventoryProperty());

            }

        }

    }

    @SubscribeEvent

    public void onWorldJoin(EntityJoinWorldEvent e){

        if(e.entity instanceof EntityPlayerMP){

            System.out.println("packet sent");

            ExampleMod.wrapper.sendToServer(new SyncPlayerPropsMessage((EntityPlayer) e.entity));

        }

    }

}

 

 

 

In case anyone is wondering; this will be custom EntityPlayer inventory, used to store rings :)

And yes, I do know I'm still using the examplemod package and such. This is my playground project.

I did my best formatting this post, please be gentle! :P

 

Thank you and sorry for the long thread.

 

EDIT:

I fixed it, basically when getting an instance of an NBTTagList you can use a cast from NBTBase to NBTTagList. First I tried it with compound.getTagList("ringInventory",8) but that did NOT work for whatever reason. If you know why it did not work, please comment and I will add it to the answer.

 

Link to comment
Share on other sites

Are you in 1.7.10 not quite sure if so update, if not use the capability system as after 1.8.9 IEEP was removed.

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

Yes, 1.7.10.

It's really unsuported, I know. But no other option exists as the mod I cooperate with only exists for 1.7.10 and I'd really like to add some sweet content to the game!

You have two options update the mod you are dependent on or looking around on previous posts/tutorials.

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

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • So, lately my modloader (curseforge) has been loading 162 mods instead of the 97 that my modpack only has, appart of that, when I try to join onto my public server the curseforge logs just starts spamming "OpenGL debug message: id=1280, source=API, type=ERROR, severity=HIGH, message='GL error GL_INVALID_ENUM'" and just freezes, my drivers are up to date, I've reinstalled curseforge, same with minecraft launcher, the java is updated, I've got enough ram and I got no viruses, Long after all this happened, my game could enter my online server and it didn't register as many mods, even my game was running at 40 fps, now I can't even play multiplayer, and when I try singleplayer, it goes about 20 fps to 10
    • It does but I'm struggling to see how to make it work in my recipe? (Its structure is the same as OP's, with a serializer subclass)
    • That looks pretty cool, nice!   Sure, so looking at that JSON file I posted, I pretty much made a record class for each "custom" data type in that JSON. The Input is a good example of why ``` "inputs": [ { "ingredient": { "item": "relativedimensions:aberrant_shard" }, "count": 8 } ], ``` So here's the inputs, it's an array, which we can use the Codec builder's builder.listOf to define an array. Each Item is of some arbitrary object with keys "ingredient" (which we know is an Ingredient) and a "count" which is an int. You don't have to have an intermediate class to map this to necessarily but I found that it's just easier to see the data that way, hence the 'ParticleReboundIngredient' represents one of these inputs.   Let me know if that makes sense or not. 
    • Pretty much, although all the recipes im planning to make on it are shapeless. The idea is that the chamber uses energy to "fuse" the items in each of the center slots together, in this case an ingot. The two slots at the sides are fuel. (A special kind of wood in this case). Here is an image of the interface just for reference (The center slot is the output)   As for the code- Can you elaborate a little bit on it? Seeing three different record classes has confused me a lot. (Elaborate as in why make them in three different records. I understand the code itself more or less)
    • Hello im trying to make a mod and the past few days GSON has almost killed me, when i export the mod and then launch it through minecraft launcher i get java.lang.NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement; i have literally tried everything here is my build config   dependencies { minecraft("com.mojang:minecraft:1.8.9") mappings("de.oceanlabs.mcp:mcp_stable:22-1.8.9") forge("net.minecraftforge:forge:1.8.9-11.15.1.2318-1.8.9") compileOnly("cc.polyfrost:oneconfig-1.8.9-forge:0.2.2-alpha+") shadowImpl("cc.polyfrost:oneconfig-wrapper-launchwrapper:1.0.0-beta+") { isTransitive = false exclude(module = "gson") } shadowImpl("org.spongepowered:mixin:0.7.11-SNAPSHOT") { isTransitive = false exclude(module = "gson") } annotationProcessor("org.spongepowered:mixin:0.8.5-SNAPSHOT") shadowImpl("org.javassist:javassist:3.15.0-GA") { isTransitive = false exclude(module = "gson") } shadowImpl("com.neovisionaries:nv-i18n:1.28") { isTransitive = false exclude(module = "gson") } shadowImpl("org.apache.commons:commons-lang3:3.4") { isTransitive = false exclude(module = "gson") } shadowImpl("org.apache.httpcomponents:httpcore:4.4.5") { isTransitive = false exclude(module = "gson") } compileOnly("com.google.code.gson:gson:2.8.6") { isTransitive = false } configurations.all { resolutionStrategy { force("com.google.code.gson:gson:2.8.6") } } shadowImpl(fileTree( mapOf( "dir" to "libs", "include" to listOf("*.jar"), "exclude" to listOf( "asm", "asm-commons", "asm-tree", "gson", "unspecified", "nv-i18n" ) ) )) }  
  • Topics

×
×
  • Create New...

Important Information

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