package squeek.veganoption.blocks;

import java.lang.reflect.Field;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.DamageSource;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerWakeUpEvent;
import squeek.veganoption.ModInfo;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.ReflectionHelper;

public class BlockBedStraw extends BlockBedGeneric
{
	public static final DamageSource itchyDamageSource = new DamageSource(ModInfo.MODID + ".itchyBed");
	public static final int ITCH_DAMAGE = 1;
	public static final Field playerSleepingField = ReflectionHelper.findField(EntityPlayer.class, "sleeping", "field_71083_bS", "bA");

	public BlockBedStraw()
	{
		super();
		MinecraftForge.EVENT_BUS.register(this);
	}

	@SubscribeEvent
	public void onWakeUp(PlayerWakeUpEvent event)
	{
		ChunkCoordinates chunkcoordinates = event.entityPlayer.playerLocation;

		if (chunkcoordinates == null)
			return;

		Block block = event.entityPlayer.worldObj.getBlock(chunkcoordinates.posX, chunkcoordinates.posY, chunkcoordinates.posZ);

		if (block != this)
			return;

		// The player's sleeping bool must be set to false before calling 
		// attackEntityFrom; otherwise, an infinite loop would be created due to 
		// the wakeUpPlayer call in attackEntityFrom
		try
		{
			playerSleepingField.setBoolean(event.entityPlayer, false);
			event.entityPlayer.attackEntityFrom(itchyDamageSource, ITCH_DAMAGE);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

@@ -965,16 +965,108 @@
 ent)%0A%09%7B%0A
+%09%09// this flag is only set when the sleep was successful%0A%09%09if (!event.setSpawn)%0A%09%09%09return;%0A%0A
 %09%09ChunkC

Fix straw bed doing damage when sleeping wasn't successful * Only meant to do damage after actually sleeping