Jump to content

[1.8] TagList in IExtendedEntityProperties


worldwidewoogie

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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