package mezz.jei.network.packets;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Map;

import mezz.jei.network.IPacketId;
import mezz.jei.network.PacketIdServer;
import net.minecraft.command.CommandGive;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandManager;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.server.FMLServerHandler;

public class PacketGiveItemStack extends PacketJEI {
	private ItemStack itemStack;

	public PacketGiveItemStack() {

	}

	public PacketGiveItemStack(@Nonnull ItemStack itemStack) {
		this.itemStack = itemStack;
	}

	@Override
	public IPacketId getPacketId() {
		return PacketIdServer.GIVE_BIG;
	}

	@Override
	public void writePacketData(PacketBuffer buf) throws IOException {
		buf.writeNBTTagCompoundToBuffer(itemStack.serializeNBT());
	}

	@Override
	public void readPacketData(PacketBuffer buf, EntityPlayer player) throws IOException {
		if (player instanceof EntityPlayerMP) {
			EntityPlayerMP sender = (EntityPlayerMP) player;

			MinecraftServer minecraftServer = sender.mcServer;
			ICommandManager commandManager = minecraftServer.getCommandManager();
			Map<String, ICommand> commands = commandManager.getCommands();
			ICommand giveCommand = commands.get("give");
			if (giveCommand != null && giveCommand.checkPermission(minecraftServer, sender)) {
				NBTTagCompound itemStackSerialized = buf.readNBTTagCompoundFromBuffer();
				if (itemStackSerialized != null) {
					ItemStack itemStack = ItemStack.loadItemStackFromNBT(itemStackSerialized);
					if (itemStack != null) {
						executeGive(sender, itemStack);
					}
				}
			} else {
				TextComponentTranslation textcomponenttranslation1 = new TextComponentTranslation("commands.generic.permission");
				textcomponenttranslation1.getStyle().setColor(TextFormatting.RED);
				sender.addChatMessage(textcomponenttranslation1);
			}
		}
	}

	public void executeGive(EntityPlayer entityplayer, ItemStack itemStack) {
		boolean addedToInventory = entityplayer.inventory.addItemStackToInventory(itemStack);

		if (addedToInventory) {
			entityplayer.worldObj.playSound(null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ((entityplayer.getRNG().nextFloat() - entityplayer.getRNG().nextFloat()) * 0.7F + 1.0F) * 2.0F);
			entityplayer.inventoryContainer.detectAndSendChanges();
		}

		if (!addedToInventory || itemStack.stackSize > 0) {
			EntityItem entityitem = entityplayer.dropItem(itemStack, false);
			if (entityitem != null) {
				entityitem.setNoPickupDelay();
				entityitem.setOwner(entityplayer.getName());
			}
		}
	}
}

@@ -191,50 +191,8 @@
 er;%0A
-import net.minecraft.command.CommandGive;%0A
 impo
@@ -222,24 +222,24 @@
 d.ICommand;%0A
+
 import net.m
@@ -614,32 +614,141 @@
 inecraftServer;%0A
+import net.minecraft.server.management.UserListOps;%0Aimport net.minecraft.server.management.UserListOpsEntry;%0A
 import net.minec
@@ -879,63 +879,8 @@
 ing;
-%0Aimport net.minecraftforge.fml.server.FMLServerHandler;
 %0A%0Apu
@@ -1520,321 +1520,26 @@
 %0A%09%09%09
-MinecraftServer minecraftServer = sender.mcServer;%0A%09%09%09ICommandManager commandManager = minecraftServer.getCommandManager();%0A%09%09%09Map%3CString, ICommand%3E commands = commandManager.getCommands();%0A%09%09%09ICommand giveCommand = commands.get(%22give%22);%0A%09%09%09if (giveCommand != null && giveCommand.checkPermission(minecraftServer, 
+if (hasPermission(
 send
@@ -2092,19 +2092,784 @@
 %7D%0A%09%7D%0A%0A%09p
-ubl
+rivate static boolean hasPermission(EntityPlayerMP sender) %7B%0A%09%09if (sender.isCreative()) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%0A%09%09MinecraftServer minecraftServer = sender.mcServer;%0A%09%09ICommandManager commandManager = minecraftServer.getCommandManager();%0A%09%09Map%3CString, ICommand%3E commands = commandManager.getCommands();%0A%09%09ICommand giveCommand = commands.get(%22give%22);%0A%09%09if (giveCommand != null && giveCommand.checkPermission(minecraftServer, sender)) %7B%0A%09%09%09return true;%0A%09%09%7D else %7B%0A%09%09%09UserListOps oppedPlayers = minecraftServer.getPlayerList().getOppedPlayers();%0A%09%09%09UserListOpsEntry userlistopsentry = oppedPlayers.getEntry(sender.getGameProfile());%0A%09%09%09return userlistopsentry != null && userlistopsentry.getPermissionLevel() %3E= minecraftServer.getOpPermissionLevel();%0A%09%09%7D%0A%09%7D%0A%0A%09private stat
 ic void 

Fix #340 Allow creative-mode players to cheat in items