Jump to content

Recommended Posts

Posted

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.

 

Posted

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!

Posted

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.

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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