Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Can anyone give me some pointers as to what is wrong with loadNBTData() in the following code?  I can tell that saveNBTData() is saving the proper data by opening the player.dat file with NBTExplorer, however, tagList.tagCount() is always 0 in loadNBTData(), and I can't figure out what is wrong with the code.

 

package net.woogie.extraDimensions;

import java.util.Set;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
import net.minecraftforge.common.util.Constants;

public class ExtraDimensionsPlayerCreativeInventoryProperties implements IExtendedEntityProperties {

private final EntityPlayer player;
private static final String identifier = "ExtraDimensionsCreativeInventory";
private InventoryPlayer creativeInventory;

public ExtraDimensionsPlayerCreativeInventoryProperties(EntityPlayer player) {
	this.player = player;
	this.creativeInventory = new InventoryPlayer(player);

}

public static ExtraDimensionsPlayerCreativeInventoryProperties get(EntityPlayer player) {
	return (ExtraDimensionsPlayerCreativeInventoryProperties) player.getExtendedProperties(identifier);
}

public static void register(EntityPlayer player) {
	player.registerExtendedProperties(identifier, new ExtraDimensionsPlayerCreativeInventoryProperties(player));
}

public void setCreativeInventory(InventoryPlayer inventory) {
	this.creativeInventory.clear();
	for (int i = 0; i < inventory.getSizeInventory(); ++i) {
		ItemStack stack = inventory.getStackInSlot(i);
		this.creativeInventory.setInventorySlotContents(i, (stack == null ? null : stack.copy()));
	}
}

public InventoryPlayer getCreativeInventory() {
	return this.creativeInventory;
}

@Override
public void saveNBTData(NBTTagCompound compound) {

	int i;
	NBTTagCompound nbttagcompound;
	NBTTagList creativeInventoryNBT = new NBTTagList();

	for (i = 0; i < this.creativeInventory.mainInventory.length; ++i) {
		if (this.creativeInventory.mainInventory[i] != null) {
			nbttagcompound = new NBTTagCompound();
			nbttagcompound.setByte("Slot", (byte) i);
			this.creativeInventory.mainInventory[i].writeToNBT(nbttagcompound);
			creativeInventoryNBT.appendTag(nbttagcompound);
		}
	}

	for (i = 0; i < this.creativeInventory.armorInventory.length; ++i) {
		if (this.creativeInventory.armorInventory[i] != null) {
			nbttagcompound = new NBTTagCompound();
			nbttagcompound.setByte("Slot", (byte) (i + 100));
			this.creativeInventory.armorInventory[i].writeToNBT(nbttagcompound);
			creativeInventoryNBT.appendTag(nbttagcompound);
		}
	}

	compound.setTag(identifier, creativeInventoryNBT);
}

@Override
public void loadNBTData(NBTTagCompound compound) {
	this.creativeInventory.mainInventory = new ItemStack[36];
	this.creativeInventory.armorInventory = new ItemStack[4];

	NBTTagList tagList = compound.getTagList(identifier, Constants.NBT.TAG_COMPOUND);

	for (int i = 0; i < tagList.tagCount(); i++) {
		NBTTagCompound itemStackNBT = tagList.getCompoundTagAt(i);
		int slot = itemStackNBT.getByte("Slot") & 255;
		ItemStack itemstack = ItemStack.loadItemStackFromNBT(itemStackNBT);

		if (itemstack != null) {
			if (slot >= 0 && slot < this.creativeInventory.mainInventory.length) {
				this.creativeInventory.mainInventory[slot] = itemstack;
			}

			if (slot >= 100 && slot < this.creativeInventory.armorInventory.length + 100) {
				this.creativeInventory.armorInventory[slot - 100] = itemstack;
			}
		}
	}
}

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

}

}

 

Thanks!

Have you printed out the list of tags that's handed to you by the load function? May be some abnormalities there

I think its my java of the variables.

  • Author

I've put back the debugging code, so it now looks like this:

 

package net.woogie.extraDimensions;

import java.util.Set;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
import net.minecraftforge.common.util.Constants;

public class ExtraDimensionsPlayerCreativeInventoryProperties implements IExtendedEntityProperties {

private final EntityPlayer player;
private static final String identifier = "ExtraDimensionsCreativeInventory";
private InventoryPlayer creativeInventory;

public ExtraDimensionsPlayerCreativeInventoryProperties(EntityPlayer player) {
	this.player = player;
	this.creativeInventory = new InventoryPlayer(player);

}

public static ExtraDimensionsPlayerCreativeInventoryProperties get(EntityPlayer player) {
	return (ExtraDimensionsPlayerCreativeInventoryProperties) player.getExtendedProperties(identifier);
}

public static void register(EntityPlayer player) {
	player.registerExtendedProperties(identifier, new ExtraDimensionsPlayerCreativeInventoryProperties(player));
}

public void setCreativeInventory(InventoryPlayer inventory) {
	this.creativeInventory.clear();
	for (int i = 0; i < inventory.getSizeInventory(); ++i) {
		ItemStack stack = inventory.getStackInSlot(i);
		this.creativeInventory.setInventorySlotContents(i, (stack == null ? null : stack.copy()));
	}
}

public InventoryPlayer getCreativeInventory() {
	return this.creativeInventory;
}

@Override
public void saveNBTData(NBTTagCompound compound) {

	int i;
	NBTTagCompound nbttagcompound;
	NBTTagList creativeInventoryNBT = new NBTTagList();

	for (i = 0; i < this.creativeInventory.mainInventory.length; ++i) {
		if (this.creativeInventory.mainInventory[i] != null) {
			nbttagcompound = new NBTTagCompound();
			nbttagcompound.setByte("Slot", (byte) i);
			this.creativeInventory.mainInventory[i].writeToNBT(nbttagcompound);
			creativeInventoryNBT.appendTag(nbttagcompound);
		}
	}

	for (i = 0; i < this.creativeInventory.armorInventory.length; ++i) {
		if (this.creativeInventory.armorInventory[i] != null) {
			nbttagcompound = new NBTTagCompound();
			nbttagcompound.setByte("Slot", (byte) (i + 100));
			this.creativeInventory.armorInventory[i].writeToNBT(nbttagcompound);
			creativeInventoryNBT.appendTag(nbttagcompound);
		}
	}

	compound.setTag(identifier, creativeInventoryNBT);
}

@Override
public void loadNBTData(NBTTagCompound compound) {
	this.creativeInventory.mainInventory = new ItemStack[36];
	this.creativeInventory.armorInventory = new ItemStack[4];

	for (String key : (Set<String>) compound.getKeySet()) {
		System.out.println("Found tags: " + key + " tagtype: " + compound.getTagType(key));
	}

	if (compound.hasKey(identifier)) {
		System.out.println("Found key " + identifier);
	}

	if (compound.hasKey(identifier, Constants.NBT.TAG_LIST)) {
		System.out.println("Found key " + identifier + " with type " + Constants.NBT.TAG_LIST);
	}

	NBTTagList tagList = compound.getTagList(identifier, Constants.NBT.TAG_LIST);

	if (tagList == null) {
		System.out.println("tagList is null!!!");
	}

	System.out.println("tagList is type " + tagList.getTagType());
	System.out.println("tagList has " + tagList.tagCount() + " entries");


	for (int i = 0; i < tagList.tagCount(); i++) {
		NBTTagCompound itemStackNBT = tagList.getCompoundTagAt(i);
		int slot = itemStackNBT.getByte("Slot") & 255;
		ItemStack itemstack = ItemStack.loadItemStackFromNBT(itemStackNBT);

		if (itemstack != null) {
			if (slot >= 0 && slot < this.creativeInventory.mainInventory.length) {
				this.creativeInventory.mainInventory[slot] = itemstack;
			}

			if (slot >= 100 && slot < this.creativeInventory.armorInventory.length + 100) {
				this.creativeInventory.armorInventory[slot - 100] = itemstack;
			}
		}
	}
}

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

}

}

 

Here is the output from the console:

 

[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: HurtByTimestamp tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Spawns tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: SleepTimer tagtype: 2
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Attributes tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Invulnerable tagtype: 1
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: PortalCooldown tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: AbsorptionAmount tagtype: 5
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: abilities tagtype: 10
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: FallDistance tagtype: 5
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: DeathTime tagtype: 2
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: XpSeed tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: ExtraDimensionsSurvivalInventory tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: HealF tagtype: 5
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: XpTotal tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: playerGameType tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: SelectedItem tagtype: 10
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Motion tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: UUIDLeast tagtype: 4
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Health tagtype: 2
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: foodSaturationLevel tagtype: 5
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Air tagtype: 2
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: OnGround tagtype: 1
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Dimension tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Rotation tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: XpLevel tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Score tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: UUIDMost tagtype: 4
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Sleeping tagtype: 1
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Pos tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Fire tagtype: 2
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: XpP tagtype: 5
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: EnderItems tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: foodLevel tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: foodExhaustionLevel tagtype: 5
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: HurtTime tagtype: 2
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: SelectedItemSlot tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: ExtraDimensionsCreativeInventory tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: Inventory tagtype: 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:81]: Found tags: foodTickTimer tagtype: 3
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:85]: Found key ExtraDimensionsCreativeInventory
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:89]: Found key ExtraDimensionsCreativeInventory with type 9
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:98]: tagList is type 0
[10:46:21] [server thread/INFO] [sTDOUT]: [net.woogie.extraDimensions.ExtraDimensionsPlayerCreativeInventoryProperties:loadNBTData:99]: tagList has 0 entries

 

Here is a screenshot of NBTExplorer taken just before starting Minecraft:

 

nbt.png

Oh god... I am ashamed that I didn't notice this when I looked earlier today.

 

compound.getTagList(identifier, Constants.NBT.TAG_LIST);

 

Get List of NBTBase objects from "identifier" and "cast" them to "Constants.NBT.TAG_LIST".

So yeah - you don't want LIST of LISTS, but LIST of TAGS, which is actually "10", not "9" (Constants.NBT.TAG_LIST).

 

Note the fact: That way you can make e.g: List of strings or List or anything really. (just saying, something that i missed long ago in past).

1.7.10 is no longer supported by forge, you are on your own.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.