Jump to content

[SOLVED] Read/Write Compressed NBT from/to DataStream


Recommended Posts

Posted

SOLUTION: Don't try to send the entire inventory as NBT. Just iterate through the contents and use Packet.writeItemStack to write each itemstack to the stream.

 

I'm trying to synchronize an inventory stored in IExtendedEntityProperties on loading so as to display something in the HUD. It works fine as soon as I open the inventory at least once, but before that, all the client-side data is null.

 

I tried sending a compressed version of the NBT from server to client, much like the TileEntity packet, and printing parNBTTagCompound.toString() shows it has all the correct information before being sent. However, once I send and receive the data, there's nothing there.

 

Is this one of those cases where you can't use the assignment operator '=' ? Or is there something else going on here?

 

Code:

 

 

Writing:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(bos);


// parNBTTagCompound.toString() shows all correct information at this point	
try {
byte[] abyte = CompressedStreamTools.compress(parNBTTagCompound);
outputStream.writeShort((short) abyte.length);
outputStream.write(abyte);
} catch (Exception ex) {
ex.printStackTrace();
}

PacketDispatcher.sendPacketToPlayer(PacketDispatcher.getPacket(ModInfo.CHANNEL, bos.toByteArray()), (Player) player);

 

Reading:

NBTTagCompound tag;

try {
short short1 = inputStream.readShort();
byte[] abyte = new byte[short1];
        inputStream.readFully(abyte);
        tag = CompressedStreamTools.decompress(abyte);
} catch (IOException e) {
e.printStackTrace();
return;
}
// tag doesn't have anything in it?!

 

 

Posted

EDIT: Ok, sorry I'm a bit slow... I don't know what I was thinking trying to send the entire inventory in a single NBT compound <facepalm>. I used Packet.write/readItemStack, iterating through the inventory, and it works great. Wow... thank you guys for your patience. I'm sure it must be tough sometimes  :-[

 

Thanks for the ideas. I tried using read/writeCompressed instead, but it has the same result. The NBT tag is correct when preparing the packet, but either during the writing or reading process it gets translated into null. It shouldn't make a difference, but the NBT Tag is for an IInventory storing some ItemStacks.

 

Actually, I think it does. I just tried storing another variable directly into the tag compound and it was sent and received correctly, but the items stored in a tag list appended to the compound don't get written to the packet. Sending the tag list compound shows the tag list correctly, but it doesn't contain any tags. So it looks like I have to manually go through and write/read every individual tag compound? There has to be a better way than that, right?

 

The Packet methods were where I found the code with the byte array from earlier. I didn't use the actual methods because I didn't make a subclass of Packet, just a static method to build the packet as a customPayload250, and writeNBTTagCompound is protected.

 

Code with read/writeCompressed instead:

 

 

Writing:

// This shows correct data stored in NBT
System.out.println("Preparing NBT packet. Data: " + compound.toString());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(bos);

try {
CompressedStreamTools.writeCompressed(compound, outputStream);
} catch (Exception ex) {
ex.printStackTrace();
}

PacketDispatcher.sendPacketToPlayer(PacketDispatcher.getPacket(ModInfo.CHANNEL, bos.toByteArray()), (Player) player);

Reading:

NBTTagCompound tag;

try {
        tag = CompressedStreamTools.readCompressed(inputStream);
} catch (IOException e) {
e.printStackTrace();
return;
}
// But here is still null...
System.out.println("NBT Tag from packet: " + tag.toString());

ExtendedPlayer.get(player).customInventory.readFromNBT(tag);

 

 

Posted

Should read/writeCompressed work for any format of NBTTagCompound? One would think so, and I haven't done anything weird with mine, so far as I know. It's all pretty basic stuff in the Inventory and ExtendedPlayer classes.

 

Like I mentioned earlier, I got it working by writing each ItemStack to the outputStream, but I'm curious why the NBT method didn't work.

 

I make the call to 'sync' onEntityJoinWorld and the packet is handled like I showed earlier with the compressed stream tools. I've checked and everything is getting called properly, it's just the NBT read/write that was messed up.

 

Code:

 

 

I use the player's NBT Tag Compound so that the inventory can save/load an 'active' slot, i.e. a slot that can be used with a key press rather than by holding the item.

 

From IExtendedEntityProperties class 'ExtendedPlayer'

public final InventoryCustom inventory;

public ExtendedPlayer(EntityPlayer player)
{
this.player = player;
this.inventory= new InventoryCustom(player.getEntityData());
}

@Override
public final void saveNBTData(NBTTagCompound compound)
{
        // I save to a new tag to avoid conflicts with the vanilla tag identifiers
NBTTagCompound properties = new NBTTagCompound();
inventory.writeToNBT(properties);
compound.setTag(EXT_PROP_NAME, properties);
}

@Override
public final void loadNBTData(NBTTagCompound compound)
{
NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
inventory.readFromNBT(properties);
}

public final void sync()
{
if (FMLCommonHandler.instance().getEffectiveSide().isServer()) {
	PacketHandler.sendSyncInventoryPacket(player, inventory);
}
}

 

NBT stuff from Inventory:

private final NBTTagCompound compound;

public InventoryEye(NBTTagCompound compound) {
this.compound = compound;
}

public int getActiveSlot() {
return compound.getByte("ActiveSlot");
}

public void setActiveSlot(int index) {
if (index >= 0 && index < ACTIVE_SLOT)
	compound.setByte("ActiveSlot", (byte) index);
else
	compound.setByte("ActiveSlot", (byte) 0);
}

public void nextActiveSlot() {
setActiveSlot(getActiveSlot() + 1);
}

@Override
public ItemStack getStackInSlot(int slot)
{
if (slot == ACTIVE_SLOT) {
	return items[getActiveSlot()];
} else {
	return items[slot];
}
}

public void readFromNBT(NBTTagCompound compound)
{
NBTTagList items = compound.getTagList("CustomInventory");

for (int i = 0; i < items.tagCount(); ++i)
{
	NBTTagCompound item = (NBTTagCompound) items.tagAt(i);
	byte slot = item.getByte("Slot");

	if (slot >= 0 && slot < getSizeInventory()) {
		setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
	}
}

setActiveSlot(compound.getByte("ActiveSlot"));
}

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

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

compound.setTag("CustomInventory", items);
compound.setByte("ActiveSlot", (byte) getActiveSlot());
}

 

Posted

Ah, it's left over from the wiki Packet Handling tutorial ;) Didn't know it was any more costly to use than worldObj.isRemote.

 

Anyway, yes, my packet handler uses ids, I write a byte to the beginning of every custom payload packet that is the packet id, I just left that part out of the earlier code that used compressed stream tools because the problem wasn't sending/receiving the packet. That was working fine. The problem was that using the code from earlier took my inventory nbt tag with stuff in it and wrote or read it as null.

 

I mentioned several of the steps I took to debug, including writing other things to the nbt tag that were successful, so the packets and handling are fine. For example, I could use 'compound.setInteger("Tag1", 5)' in the inventory writeNBT method and it would be handled fine by the packet, but adding a TagList to 'compound' didn't work. I forget what I did, but at one point I got the name of the TagList to process correctly, but the list was empty on the receiving end (but not on the sending end).

 

The Inventory loads and saves fine, which suggests the NBT is all in proper order.

 

It seems like writeCompressed only writes data directly in the tag compound, but not any other tags stored within the tag. That seems dead wrong to me, but from what I've tried that's how it appears to be.

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

    • I need to know what mod is doing this crash, i mean the mod xenon is doing the crash but i want to know who mod is incompatible with xenon, but please i need to know a solution if i need to replace xenon, i cant use optifine anymore and all the other mods i tried(sodium, lithium, vulkan, etc) doesn't work, it crash the game.
    • I have been trying to solve a consistent crashing issue on my brother's computer where it will crash during the "Scanning Mod Candidates" phase of the loading process that starts when you click the play button on the Minecraft launcher. The issue seems to stem from a missing library that it mentions in the log file I provide below. I might I'm missing the bigger issue here for a smaller one but hopefully someone can find what I'm missing. Here's all of the stuff that I've been able to figure out so far: 1. It has nothing to do with mods, the crash happened with a real modpack, and even when I made a custom modpack and launched it without putting ANY mods into it (That is where the log file comes from by the way). 2. I have tried to find this class like a file in the Minecraft folders, but I've had no luck finding it (I don't think it works like that, but since I really don't understand how it works, I just figured I'd try). 3. I haven't seen anyone else have this issue before. 4. I know that my modpack (with mods) does work since I've run it on my computer, and it works fantastic. For some reason my brother's computer can't seem to run anything through curseforge. 5. This is for Minecraft version 1.20.1, Minecraft launcher version 3.4.50-2.1.3, forge 47.3.0, and curseforge app version 1.256.0.21056 6. My brother is using a Dell laptop from 6 years ago running Windows 10 (If you think more info on this would help, please ask as I do have it. I'm just choosing not to put it here for now). 7. I have reinstalled the curseforge app and installed Minecraft version 1.20.1. I have not reinstalled Minecraft or forge 47.3.0 but I didn't know if that would help. 8. I had an error code of 1 Please let me know if there is anything else that I am missing that you would like me to add to this post/add in a comment! Lastly, many thanks in advance to whoever can help! ------------- LOG FILE (latest.log) ------------- (from /Users/<NAME OF USER>/cursforge/minecraft/Instances/<THE NAME OF MY EMPTY MODPACK>/logs/latest.log) (This was made after running an empty modpack with same versions for all apps) ("[REDACTED]" is not the actual text from the log, it is me replacing text I figured wouldn't be necessary for fixing and would hurt my privacy) https://pastebin.com/hxXvGGEK ------------- DEBUG.LOG (I realized that I should have put this here first after I had done all of the work on putting latest.log in) -------------------- (again, "[REDACTED]" is not the actual text from the log, it is me replacing text I figured wouldn't be necessary for fixing and would hurt my privacy) https://pastebin.com/Fmh8GHYs
    • Pastebin... https://pastebin.com/Y3iZ85L5   Brand new profile, does not point to a mod as far as I can tell, my fatal message just has something about mixins. Don't know much about reading logs like this, but am genuinely stuck, please help. Java updated, pc restarted.
    • I was playing minecraft, forge 47.3.0 and 1.20.1, but when i tried to play minecraft now only crashes, i need help please. here is the crash report: https://securelogger.net/files/e6640a4f-9ed0-4acc-8d06-2e500c77aaaf.txt
  • Topics

×
×
  • Create New...

Important Information

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