Fix getShareTag replacing data on the server (#3776) Closes #3682

This commit is contained in:
mezz authored and GitHub committed 2017-06-04 18:38:16 -07:00
1 parent e67bde9e9e
commit 286777b824
8 files changed
+240 -4

No files matched your search

@@ -0,0 +1,58 @@
/*
* Minecraft Forge
* Copyright (c) 2016.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.util;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
public class PacketUtil
{
private PacketUtil() {}
/**
* Most ItemStack serialization is Server to Client, and must go through PacketBuffer.writeItemStack which uses Item.getNBTShareTag.
* One exception is items from the creative menu, which must be sent from Client to Server with their full NBT.
* <br/>
* This method matches PacketBuffer.writeItemStack but without the Item.getNBTShareTag patch.
* It is compatible with PacketBuffer.readItemStack.
*/
public static void writeItemStackFromClientToServer(PacketBuffer buffer, ItemStack stack)
{
if (stack.isEmpty())
{
buffer.writeShort(-1);
}
else
{
buffer.writeShort(Item.getIdFromItem(stack.getItem()));
buffer.writeByte(stack.getCount());
buffer.writeShort(stack.getMetadata());
NBTTagCompound nbttagcompound = null;
if (stack.getItem().isDamageable() || stack.getItem().getShareTag())
{
nbttagcompound = stack.getTagCompound();
}
buffer.writeCompoundTag(nbttagcompound);
}
}
}