<commit_before>package protocolsupport.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.handler.codec.EncoderException;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import net.minecraft.server.v1_8_R3.GameProfileSerializer;
import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
import net.minecraft.server.v1_8_R3.Item;
import net.minecraft.server.v1_8_R3.ItemStack;
import net.minecraft.server.v1_8_R3.Items;
import net.minecraft.server.v1_8_R3.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R3.NBTReadLimiter;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
import net.minecraft.server.v1_8_R3.NBTTagList;
import net.minecraft.server.v1_8_R3.NBTTagString;

import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.spigotmc.LimitStream;
import org.spigotmc.SneakyThrow;

import protocolsupport.api.ProtocolVersion;
import protocolsupport.api.events.ItemStackWriteEvent;
import protocolsupport.protocol.transformer.utils.LegacyUtils;
import protocolsupport.protocol.typeremapper.id.IdRemapper;
import protocolsupport.protocol.typeskipper.id.IdSkipper;
import protocolsupport.protocol.typeskipper.id.SkippingTable;
import protocolsupport.utils.Allocator;
import protocolsupport.utils.Utils;

import com.mojang.authlib.GameProfile;

public class PacketDataSerializer extends net.minecraft.server.v1_8_R3.PacketDataSerializer {

	private ProtocolVersion version;

	public PacketDataSerializer(ByteBuf buf, ProtocolVersion version) {
		super(buf);
		this.version = version;
	}

	public ProtocolVersion getVersion() {
		return version;
	}

	public void setVersion(ProtocolVersion version) {
		this.version = version;
	}

	@Override
	public void a(ItemStack itemstack) {
		itemstack = transformItemStack(itemstack);
		if ((itemstack == null) || (itemstack.getItem() == null)) {
			writeShort(-1);
		} else {
			int itemId = Item.getId(itemstack.getItem());
			writeShort(IdRemapper.ITEM.getTable(getVersion()).getRemap(itemId));
			writeByte(itemstack.count);
			writeShort(itemstack.getData());
			a(itemstack.getTag());
		}
	}

	@Override
	public void a(NBTTagCompound nbttagcompound) {
		if (getVersion() != ProtocolVersion.MINECRAFT_1_8) {
			if (nbttagcompound == null) {
				writeShort(-1);
			} else {
				byte[] abyte = write(nbttagcompound);
				writeShort(abyte.length);
				writeBytes(abyte);
			}
		} else {
			if (nbttagcompound == null) {
				writeByte(0);
			} else {
				ByteBufOutputStream out = new ByteBufOutputStream(Allocator.allocateBuffer());
				try {
					NBTCompressedStreamTools.a(nbttagcompound, (DataOutput) new DataOutputStream(out));
					writeBytes(out.buffer());
				} catch (Throwable ioexception) {
					throw new EncoderException(ioexception);
				} finally {
					out.buffer().release();
				}
			}
		}
	}

	private ItemStack transformItemStack(ItemStack original) {
		if (original == null) {
			return null;
		}
		ItemStack itemstack = original.cloneItemStack();
		Item item = itemstack.getItem();
		NBTTagCompound nbttagcompound = itemstack.getTag();
		if (nbttagcompound != null) {
			if (getVersion() != ProtocolVersion.MINECRAFT_1_8 && item == Items.WRITTEN_BOOK) {
				if (nbttagcompound.hasKeyOfType("pages", 9)) {
					NBTTagList pages = nbttagcompound.getList("pages", 8);
					NBTTagList newpages = new NBTTagList();
					for (int i = 0; i < pages.size(); i++) {
						newpages.add(new NBTTagString(LegacyUtils.fromComponent(ChatSerializer.a(pages.getString(i)))));
					}
					nbttagcompound.set("pages", newpages);
				}
			}
			switch (getVersion()) {
				case MINECRAFT_1_7_5:
				case MINECRAFT_1_6_4:
				case MINECRAFT_1_6_2:
				case MINECRAFT_1_5_2: {
					transformSkull(nbttagcompound, "SkullOwner", "SkullOwner");
					transformSkull(nbttagcompound, "Owner", "ExtraType");
					break;
				}
				default: {
					break;
				}
			}
			if (nbttagcompound.hasKeyOfType("ench", 9)) {
				SkippingTable enchSkip = IdSkipper.ENCHANT.getTable(getVersion());
				NBTTagList enchList = nbttagcompound.getList("ench", 10);
				NBTTagList newList = new NBTTagList();
				for (int i = 0; i < enchList.size(); i++) {
					NBTTagCompound enchData = enchList.get(i);
					if (!enchSkip.shouldSkip(enchData.getInt("id") & 0xFFFF)) {
						newList.add(enchData);
					}
				}
				nbttagcompound.set("ench", newList);
			}
		}
		ItemStackWriteEvent event = new InternalItemStackWriteEvent(original, itemstack);
		Bukkit.getPluginManager().callEvent(event);
		return itemstack;
	}

	private void transformSkull(NBTTagCompound tag, String tagname, String newtagname) {
		if (tag.hasKeyOfType(tagname, 10)) {
			GameProfile gameprofile = GameProfileSerializer.deserialize(tag.getCompound(tagname));
			if (gameprofile.getName() != null) {
				tag.set(newtagname, new NBTTagString(gameprofile.getName()));
			} else {
				tag.remove(tagname);
			}
		}
	}

	@Override
	public NBTTagCompound h() throws IOException {
		if (getVersion() != ProtocolVersion.MINECRAFT_1_8) {
			final short length = readShort();<commit_msg>Filter item meta before writing itemstack<commit_after>package protocolsupport.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.handler.codec.EncoderException;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import net.minecraft.server.v1_8_R3.GameProfileSerializer;
import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
import net.minecraft.server.v1_8_R3.Item;
import net.minecraft.server.v1_8_R3.ItemStack;
import net.minecraft.server.v1_8_R3.Items;
import net.minecraft.server.v1_8_R3.NBTCompressedStreamTools;
import net.minecraft.server.v1_8_R3.NBTReadLimiter;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
import net.minecraft.server.v1_8_R3.NBTTagList;
import net.minecraft.server.v1_8_R3.NBTTagString;

import org.bukkit.Bukkit;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.spigotmc.LimitStream;
import org.spigotmc.SneakyThrow;

import protocolsupport.api.ProtocolVersion;
import protocolsupport.api.events.ItemStackWriteEvent;
import protocolsupport.protocol.transformer.utils.LegacyUtils;
import protocolsupport.protocol.typeremapper.id.IdRemapper;
import protocolsupport.protocol.typeskipper.id.IdSkipper;
import protocolsupport.protocol.typeskipper.id.SkippingTable;
import protocolsupport.utils.Allocator;
import protocolsupport.utils.Utils;

import com.mojang.authlib.GameProfile;

public class PacketDataSerializer extends net.minecraft.server.v1_8_R3.PacketDataSerializer {

	private ProtocolVersion version;

	public PacketDataSerializer(ByteBuf buf, ProtocolVersion version) {
		super(buf);
		this.version = version;
	}

	public ProtocolVersion getVersion() {
		return version;
	}

	public void setVersion(ProtocolVersion version) {
		this.version = version;
	}

	@Override
	public void a(ItemStack itemstack) {
		itemstack = transformItemStack(itemstack);
		if ((itemstack == null) || (itemstack.getItem() == null)) {
			writeShort(-1);
		} else {
			int itemId = Item.getId(itemstack.getItem());
			writeShort(IdRemapper.ITEM.getTable(getVersion()).getRemap(itemId));
			writeByte(itemstack.count);
			writeShort(itemstack.getData());
			a(itemstack.getTag());
		}
	}

	@Override
	public void a(NBTTagCompound nbttagcompound) {
		if (getVersion() != ProtocolVersion.MINECRAFT_1_8) {
			if (nbttagcompound == null) {
				writeShort(-1);
			} else {
				byte[] abyte = write(nbttagcompound);
				writeShort(abyte.length);
				writeBytes(abyte);
			}
		} else {
			if (nbttagcompound == null) {
				writeByte(0);
			} else {
				ByteBufOutputStream out = new ByteBufOutputStream(Allocator.allocateBuffer());
				try {
					NBTCompressedStreamTools.a(nbttagcompound, (DataOutput) new DataOutputStream(out));
					writeBytes(out.buffer());
				} catch (Throwable ioexception) {
					throw new EncoderException(ioexception);
				} finally {
					out.buffer().release();
				}
			}
		}
	}

	private ItemStack transformItemStack(ItemStack original) {
		if (original == null) {
			return null;
		}
		ItemStack itemstack = original.cloneItemStack();
		Item item = itemstack.getItem();
		NBTTagCompound nbttagcompound = itemstack.getTag();
		if (nbttagcompound != null) {
			if (getVersion() != ProtocolVersion.MINECRAFT_1_8 && item == Items.WRITTEN_BOOK) {
				if (nbttagcompound.hasKeyOfType("pages", 9)) {
					NBTTagList pages = nbttagcompound.getList("pages", 8);
					NBTTagList newpages = new NBTTagList();
					for (int i = 0; i < pages.size(); i++) {
						newpages.add(new NBTTagString(LegacyUtils.fromComponent(ChatSerializer.a(pages.getString(i)))));
					}
					nbttagcompound.set("pages", newpages);
				}
			}
			switch (getVersion()) {
				case MINECRAFT_1_7_5:
				case MINECRAFT_1_6_4:
				case MINECRAFT_1_6_2:
				case MINECRAFT_1_5_2: {
					transformSkull(nbttagcompound, "SkullOwner", "SkullOwner");
					transformSkull(nbttagcompound, "Owner", "ExtraType");
					break;
				}
				default: {
					break;
				}
			}
			if (nbttagcompound.hasKeyOfType("ench", 9)) {
				SkippingTable enchSkip = IdSkipper.ENCHANT.getTable(getVersion());
				NBTTagList enchList = nbttagcompound.getList("ench", 10);
				NBTTagList newList = new NBTTagList();
				for (int i = 0; i < enchList.size(); i++) {
					NBTTagCompound enchData = enchList.get(i);
					if (!enchSkip.shouldSkip(enchData.getInt("id") & 0xFFFF)) {
						newList.add(enchData);
					}
				}
				nbttagcompound.set("ench", newList);
			}
		}
		ItemStackWriteEvent event = new InternalItemStackWriteEvent(original, itemstack);
		Bukkit.getPluginManager().callEvent(event);
		CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack));
		return itemstack;
	}

	private void transformSkull(NBTTagCompound tag, String tagname, String newtagname) {
		if (tag.hasKeyOfType(tagname, 10)) {
			GameProfile gameprofile = GameProfileSerializer.deserialize(tag.getCompound(tagname));
			if (gameprofile.getName() != null) {
				tag.set(newtagname, new NBTTagString(gameprofile.getName()));
			} else {
				tag.remove(tagname);
			}
		}
	}

	@Override
	public NBTTagCompound h() throws IOException {
		if (getVersion() != ProtocolVersion.MINECRAFT_1_8) {
			final short length = readShort();
{'fraction_non_alphanumeric': 0.1163808828894564, 'fraction_numerical': 0.012221816855162349, 'mean_word_length': 4.813891834570519, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3431266', 'n_tokens_mistral': 4224, 'n_tokens_neox': 3751, 'n_words': 618}