Jump to content

NBT tags and savind data.


SenpaiSubaraki

Recommended Posts

I'm making a new inventory, and all works fine.

Apart from the fact the slots do not get saved.

 

 

I've contacted multiple people, and one adviced me to write data to a .dat file, and one adviced me to write it to an ItemStack.

I'm still kind of lost and dont know what to do.

 

Here's the code i have right now.

Inventory

 

package RpgInventory;

 

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;

 

public class RpgInventory implements IInventory {

 

ItemStack[] armorSlots = new ItemStack[6];

 

private EntityPlayer player;

public RpgInventory instance;

public RpgInventory(EntityPlayer par1EntityPlayer)

    {

        this.player = par1EntityPlayer;

        instance = this;

    }

 

public boolean inventoryChanged = false;

 

 

public int getSizeInventory() {

return 6;

}

 

public ItemStack getJewelInSlot(int par1)

    {

        return this.armorSlots[par1];

    }

/**

    * Returns a slot index in main inventory containing a specific itemID

    */

    private int findJewel(int par1)

    {

        for (int var2 = 0; var2 < this.armorSlots.length; ++var2)

        {

            if (this.armorSlots[var2] != null && this.armorSlots[var2].itemID == par1)

            {

                return var2;

            }

        }

 

        return -1;

    }

   

    public boolean getJewel(int par1)

    {

        int var2 = this.findJewel(par1);

        return var2 >= 0;

    }

   

    public ItemStack getJewelFromStack(int par1)

    {

        ItemStack[] var2 = this.armorSlots;

 

        if (par1 >= var2.length)

        {

            par1 -= var2.length;

            var2 = this.armorSlots;

        }

 

        return var2[par1];

    }

 

   

   

/**

    * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a

    * new stack.

    */

  public ItemStack decrStackSize(int par1, int par2)

    {

        ItemStack[] var3 = this.armorSlots;

 

        if (var3[par1] != null)

        {

            ItemStack var4;

 

            if (var3[par1].stackSize <= par2)

            {

                var4 = var3[par1];

                var3[par1] = null;

                return var4;

            }

            else

            {

                var4 = var3[par1].splitStack(par2);

 

                if (var3[par1].stackSize == 0)

                {

                    var3[par1] = null;

                }

 

                return var4;

            }

        }

        else

        {

            return null;

        }

    }

 

    /**

    * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -

    * like when you close a workbench GUI.

    */

    public ItemStack getStackInSlotOnClosing(int par1)

    {

        ItemStack[] var2 = this.armorSlots;

 

        if (var2[par1] != null)

        {

            ItemStack var3 = var2[par1];

            var2[par1] = null;

            return var3;

        }

        else

        {

            return null;

        }

    }

 

    public void setInventorySlotContents(int par1, ItemStack par2ItemStack)

    {

        ItemStack[] var3 = this.armorSlots;

        var3[par1] = par2ItemStack;

    }

   

 

    public ItemStack getStackInSlot(int par1)

    {

        ItemStack[] var2 = this.armorSlots;

        return var2[par1];

    }

 

    public String getInvName()

    {

        return "RpgInventory";

    }

   

    public int getInventoryStackLimit()

    {

        return 64;

    }

   

    public void onInventoryChanged()

    {

        this.inventoryChanged = true;

    }

   

    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)

    {

        return this.player.isDead ? false : par1EntityPlayer.getDistanceSqToEntity(this.player) <= 64.0D;

    }

   

 

    /**

    * Writes the inventory out as a list of compound tags. This is where the slot indices are used (+100 for armor, +80

    * for crafting).

    */

    public NBTTagCompound writeToNBT(NBTTagCompound par1NBTTagCompound)

    {

   

    System.out.println("written to NBT");

        int var2;

        NBTTagList var3;

 

        for (var2 = 0; var2 < this.armorSlots.length; ++var2)

        {

            if (this.armorSlots[var2] != null)

            {

                var3 = new NBTTagList();

                par1NBTTagCompound.setByte("Slot", (byte)(var2));

                this.armorSlots[var2].writeToNBT(par1NBTTagCompound);

                var3.appendTag(var3);

            }

        }

        return par1NBTTagCompound;

    }

 

    /**

    * Reads from the given tag list and fills the slots in the inventory with the correct items.

    */

    public void readFromNBT(NBTTagCompound par1NBTTagCompound)

    {

    System.out.println("read from NBT");

    NBTTagList par1NBTTagList = new NBTTagList();

        this.armorSlots = new ItemStack[6];

 

        for (int var2 = 0; var2 < par1NBTTagList.tagCount(); ++var2)

        {

        NBTTagCompound var3 = (NBTTagCompound)par1NBTTagList.tagAt(var2);

            int var4 = var3.getByte("Slot");

            ItemStack var5 = ItemStack.loadItemStackFromNBT(var3);

 

            if (var5 != null)

            {

                if (var4 >= 100 && var4 < this.armorSlots.length)

                {

                    this.armorSlots[var4] = var5;

                }

            }

        }

    }

 

    public void openChest() {}

 

    public void closeChest() {}

 

}

 

 

 

Container

 

package RpgInventory;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

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

import net.minecraft.item.ItemArmor;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.CompressedStreamTools;

import net.minecraft.nbt.NBTTagCompound;

public class RpgContainer extends Container {

 

public RpgInventory rpgInv;

File var4 = new File("rpg.dat");

public RpgContainer (EntityPlayer p1)

    {

super();

    rpgInv = new RpgInventory(p1);

this.onGuiOpened();

this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  0,  6,  16,  0));// necklace

this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  1,  6,  37,  1));//shield

this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  2,  82, 16,  2));//cloak

this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  3,  82, 38,  3));//gloves

this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  4,  82, 59,  4));//ring

this.addSlotToContainer(new SlotRpgArmor( this, rpgInv,  5,  6,  58,  4));//ring

 

 

for (int var4 = 0; var4 < 3; ++var4)

        {

            for (int var5 = 0; var5 < 9; ++var5)

            {

                this.addSlotToContainer(new Slot(p1.inventory, (var5 + (var4 + 1) * 9), 8 + var5 * 18, 84 + var4 * 18));

             

            }

        }

 

        for (int var4 = 0; var4 < 9; ++var4)

        {

            this.addSlotToContainer(new Slot(p1.inventory, var4, 8 + var4 * 18, 142));

        }

    }

 

public void onGuiOpened()

{

try {

       

           

            NBTTagCompound var5 = CompressedStreamTools.readCompressed(new FileInputStream(var4));

            rpgInv.readFromNBT(var5);

  System.out.println("read data");

 

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

@Override

public void onCraftGuiClosed(EntityPlayer player)

    {

 

try {

NBTTagCompound var2 = new NBTTagCompound();

 

var2 = rpgInv.writeToNBT(var2);

CompressedStreamTools.writeCompressed(var2, new FileOutputStream(var4));

 

  System.out.println("write data");

 

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

     

    }

  public boolean doesGuiPauseGame()

    {

        return false;

    }

 

  @Override

  public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)

    {

        return null;

    }

 

public boolean canInteractWith(EntityPlayer var1) {

return true;

}

}

 

 

 

any help is welcome =)

Link to comment
Share on other sites

Well have you tried to use the debugger and some breakpoints, to see what happens

 

-WHEN the container's should be saved & loaded?

  - Are the read and write methods even called?

  - Do the containers they are writing to save, contain null? or do they contain items?

 

- When the container is used, is it working as it should on the server side as well as for the client?

  - Maybe the client sees the items as added to the slot(s), but the server never gets the package?

  - Or more likely, are you reading from the NBT first, then inn the constuctor of the TE(or somewhere else) setting the IInventory to 

      null by doing something like = new IInventory(); ?

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

tried all of those.

 

either it is not reading properly, or not writing properly.

 

http://pastebin.com/McNg1HE1

 

please take a look.

 

My guess is my call on the Inventory that makes it bug, but I would not know how to call on it differently

 

thanks you for taking a look and leaving an answer !

Link to comment
Share on other sites

So, I solved that problem.

it was a question of placing a variable in FRONT of another one, instead of behind.

 

anyway, new problem pops up.

 

i can place the item in the slot: but it makes a copy. and i can not take it out, it just disapears.

 

so: Server and Client are out of Sync.; anyone got an idea what i can do about this ?

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Thank you! I have been looking for a solution to the problem for 3 months now, without finding an answer. The problem with displaying the skin was precisely on version 1.18.2, even in a single player game and only with Forge. Moreover, everything worked correctly on another computer. I'm already tired of looking for the problem. I deleted everything I could from the programs on my PC, nothing helped. I saw your post and unchecked IPv6 in the properties of my network connection and the problem disappeared! Thank you, you saved my nerve cells! Sorry for my English.
    • I once had a friend who shared a fascinating story about his spending habits. He was an avid World of Warcraft player, and his obsession with the game reached a new level when he decided to invest a substantial amount of money in amirdrassil the dream's hope boost services for his characters. Despite my initial shock at the expense, he explained that it was his way of enhancing his gaming experience. This memory serves as a reminder that people are willing to go to great lengths to pursue their passions, even if it means spending a significant sum.
    • I've been playing the ATFC modpack recently, however I've been having some performance issues. I tried to fix this by adding various performance mods - but it's only created more issues. This is my log file: https://pastebin.com/9a6r2ah4 I use PrismMC, an external launcher - separate from the Official Minecraft Launcher. I run ZorinOS 16.3 (A Linux Distro). I have 16Gb of RAM, I have an Intel Core i5-4570 CPU, and I have a NVIDIA GeForce GTX 1050 Ti GPU. This is the beginning of the error: [main/ERROR] [ne.mi.fm.lo.mo.ModDiscoverer/SCAN]: Failed to build unique mod list after mod discovery. net.minecraftforge.fml.loading.EarlyLoadingException: Duplicate mods found Help would be greatly appreciated! Sorry if my formatting is wrong - I'm very new here.
    • I believe I posted this topic in the wrong forum so I shall post it again. Hello, I am getting an error when trying to load into a personal modpack of mine. It's been giving this error ever since I started working on adding mods again, and I don't know what's causing it. I don't have a crash report since it is merely me getting kicked from my singleplayer world but I do have the error from the logs. I can provide the full log and debug.log on mclogs or pastebin if required. [26Sep2023 19:45:40.003] [Server thread/ERROR] [net.minecraft.server.network.ServerLoginPacketListenerImpl/]: Couldn't place player in world java.util.NoSuchElementException: No value present at java.util.Optional.orElseThrow(Optional.java:377) ~[?:?] at potionstudios.byg.common.world.LevelBiomeTracker.lambda$fromServer$6(LevelBiomeTracker.java:55) ~[Oh_The_Biomes_You'll_Go-forge-1.19.2-2.0.1.4.jar%23514!/:**.**.**.**] at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178) ~[?:?] at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992) ~[?:?] at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[?:?] at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[?:?] at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[?:?] at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[?:?] at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:?] at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596) ~[?:?] at potionstudios.byg.common.world.LevelBiomeTracker.fromServer(LevelBiomeTracker.java:55) ~[Oh_The_Biomes_You'll_Go-forge-1.19.2-2.0.1.4.jar%23514!/:**.**.**.**] at net.minecraft.server.level.ServerLevel.handler$bkb000$warnExperimentalBYG(ServerLevel.java:3269) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.level.ServerLevel.m_8853_(ServerLevel.java) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.level.ServerLevel.m_8834_(ServerLevel.java:780) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.players.PlayerList.m_11261_(PlayerList.java:198) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.network.ServerLoginPacketListenerImpl.m_143699_(ServerLoginPacketListenerImpl.java:156) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.network.ServerLoginPacketListenerImpl.m_10055_(ServerLoginPacketListenerImpl.java:143) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.network.ServerLoginPacketListenerImpl.m_9933_(ServerLoginPacketListenerImpl.java:75) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.network.Connection.m_129483_(Connection.java:248) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.network.ServerConnectionListener.m_9721_(ServerConnectionListener.java:143) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.MinecraftServer.m_5703_(MinecraftServer.java:880) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.MinecraftServer.m_5705_(MinecraftServer.java:806) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.client.server.IntegratedServer.m_5705_(IntegratedServer.java:84) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:654) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?] at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:244) ~[client-1.19.2-20220805.130853-srg.jar%23585!/:?]  
    • When trying to open Minecraft with the modpack installed it simply does not open and does not generate any crash-report, I only get the following log Forge 1.20.1 Server Log [#nhetr9s] - mclo.gs [27sep.2023 20:20:52.088] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--username, Raven, --version, 1.20.1-forge-47.2.0, --gameDir, C:\Users\ADMIN\AppData\Roaming\.minecraft, --assetsDir, C:\Users\ADMIN\AppData\Roaming\.minecraft\assets, --assetIndex, 5, --uuid, c133294fda623a7fb0c85f27d7f0f3fc, --accessToken, ????????, --clientId, 0, --xuid, 0, --userType, mojang, --versionType, release, --width, 854, --height, 480, --launchTarget, forgeclient, --fml.forgeVersion, 47.2.0, --fml.mcVersion, 1.20.1, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20230612.114412] [27sep.2023 20:20:52.095] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 10.0.9+10.0.9+main.dcd20f30 starting: java version 17.0.8 by Microsoft; OS Windows 10 arch amd64 version 10.0 [27sep.2023 20:20:54.925] [main/INFO] [net.minecraftforge.fml.loading.ImmediateWindowHandler/]: Loading ImmediateWindowProvider fmlearlywindow [27sep.2023 20:20:55.087] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.6 [27sep.2023 20:20:55.109] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.5 [27sep.2023 20:20:55.111] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.4 [27sep.2023 20:20:55.113] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.3 [27sep.2023 20:20:55.115] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.2 [27sep.2023 20:20:55.124] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.1 [27sep.2023 20:20:55.132] [main/INFO] [EARLYDISPLAY/]: Trying GL version 4.0 [27sep.2023 20:20:55.156] [main/INFO] [EARLYDISPLAY/]: Requested GL version 4.0 got version 4.0 [27sep.2023 20:20:55.345] [main/INFO] [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.5 Source=union:/C:/Users/ADMIN/AppData/Roaming/.minecraft/libraries/org/spongepowered/mixin/0.8.5/mixin-0.8.5.jar%23100!/ Service=ModLauncher Env=CLIENT [27sep.2023 20:20:55.421] [pool-4-thread-1/INFO] [EARLYDISPLAY/]: GL info: Intel(R) HD Graphics 2500 GL version 4.0.0 - Build 10.18.10.5161, Intel [27sep.2023 20:20:56.983] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file C:\Users\ADMIN\AppData\Roaming\.minecraft\libraries\net\minecraftforge\fmlcore\1.20.1-47.2.0\fmlcore-1.20.1-47.2.0.jar is missing mods.toml file [27sep.2023 20:20:56.987] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file C:\Users\ADMIN\AppData\Roaming\.minecraft\libraries\net\minecraftforge\javafmllanguage\1.20.1-47.2.0\javafmllanguage-1.20.1-47.2.0.jar is missing mods.toml file [27sep.2023 20:20:56.991] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file C:\Users\ADMIN\AppData\Roaming\.minecraft\libraries\net\minecraftforge\lowcodelanguage\1.20.1-47.2.0\lowcodelanguage-1.20.1-47.2.0.jar is missing mods.toml file [27sep.2023 20:20:56.994] [main/WARN] [net.minecraftforge.fml.loading.moddiscovery.ModFileParser/LOADING]: Mod file C:\Users\ADMIN\AppData\Roaming\.minecraft\libraries\net\minecraftforge\mclanguage\1.20.1-47.2.0\mclanguage-1.20.1-47.2.0.jar is missing mods.toml file [27sep.2023 20:20:58.045] [main/WARN] [net.minecraftforge.jarjar.selection.JarSelector/]: Attempted to select two dependency jars from JarJar which have the same identification: Mod File:  and Mod File: . Using Mod File:  [27sep.2023 20:20:58.047] [main/WARN] [net.minecraftforge.jarjar.selection.JarSelector/]: Attempted to select a dependency jar for JarJar which was passed in as source: geckolib. Using Mod File: C:\Users\ADMIN\AppData\Roaming\.minecraft\mods\geckolib-forge-1.20.1-4.2.3.jar [27sep.2023 20:20:58.047] [main/INFO] [net.minecraftforge.fml.loading.moddiscovery.JarInJarDependencyLocator/]: Found 40 dependencies adding them to mods collection [27sep.2023 20:20:58.639] [main/INFO] [org.groovymc.gml.mappings.MappingsProvider/]: Starting runtime mappings setup... [27sep.2023 20:20:58.669] [main/INFO] [org.groovymc.gml.internal.locator.ModLocatorInjector/]: Injecting ScriptModLocator candidates... [27sep.2023 20:20:58.686] [main/INFO] [org.groovymc.gml.scriptmods.ScriptModLocator/]: Injected Jimfs file system [27sep.2023 20:20:58.695] [main/INFO] [org.groovymc.gml.scriptmods.ScriptModLocator/]: Skipped loading script mods from directory C:\Users\ADMIN\AppData\Roaming\.minecraft\mods\scripts as it did not exist. [27sep.2023 20:20:58.704] [main/INFO] [org.groovymc.gml.internal.locator.ModLocatorInjector/]: Injected ScriptModLocator mod candidates. Found 0 valid mod candidates and 0 broken mod files. [27sep.2023 20:21:03.252] [GML Mappings Thread/INFO] [org.groovymc.gml.mappings.MappingsProvider/]: Loaded runtime mappings in 4271ms [27sep.2023 20:21:03.253] [GML Mappings Thread/INFO] [org.groovymc.gml.mappings.MappingsProvider/]: Finished runtime mappings setup.  
  • Topics

×
×
  • Create New...

Important Information

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