/*
Copyright (c) 2012, Mushroom Hostage
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package me.exphc.AtmosphericHeights;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.UUID;
import java.util.Iterator;
import java.util.logging.Logger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Formatter;
import java.lang.Byte;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.io.*;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.*;
import org.bukkit.event.*;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;
import org.bukkit.event.entity.*;
import org.bukkit.Material.*;
import org.bukkit.material.*;
import org.bukkit.block.*;
import org.bukkit.entity.*;
import org.bukkit.command.*;
import org.bukkit.inventory.*;
import org.bukkit.configuration.*;
import org.bukkit.configuration.file.*;
import org.bukkit.scheduler.*;
import org.bukkit.enchantments.*;
import org.bukkit.*;

import net.minecraft.server.CraftingManager;

import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.craftbukkit.inventory.CraftItemStack;

class AtmosphericHeightsListener implements Listener {
	AtmosphericHeights plugin;

    final int tropopause, mesopause, magnetopause;

	public AtmosphericHeightsListener(AtmosphericHeights plugin) {
		this.plugin = plugin;

        tropopause = plugin.getConfig().getInt("tropopause", 128);
        mesopause = plugin.getConfig().getInt("mesopause", 256);
        magnetopause = plugin.getConfig().getInt("magnetopause", 512);
        // TODO: kalman line, 1024? = legally in outerspace
        // for inspiration see http://en.wikipedia.org/wiki/Earth%27s_atmosphere#Principal_layers

		Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
	}

    @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true) 
    public void onFoodLevelChange(FoodLevelChangeEvent event) {
        HumanEntity entity = event.getEntity();

        if (!(entity instanceof Player)) {
            return;
        }

        int oldLevel = ((Player)entity).getFoodLevel();
        int newLevel = event.getFoodLevel();

        // hunger change in half hearts
        int delta = oldLevel - newLevel;

        plugin.log("Hunger change: " + oldLevel + " -> " + newLevel + " (delta = "+delta+")");

        if (delta < 0) {
            // increased = ate, do nothing
            return;
        }

        int height = entity.getLocation().getBlockY();


        // Thinner air, more hungry
        if (height > tropopause) {
            double above = height - tropopause;
            plugin.log("above "+above);
            double extra = Math.ceil(above / plugin.getConfig().getDouble("hungerPerMeter", 10.0));

            delta += extra;
            plugin.log("new delta = " + delta);

            newLevel = oldLevel - delta;
            plugin.log("set level: " + newLevel);
            event.setFoodLevel(newLevel);
        }
    }
}

public class AtmosphericHeights extends JavaPlugin implements Listener {
    Logger log = Logger.getLogger("Minecraft");

    public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveConfig();
        reloadConfig();

        new AtmosphericHeightsListener(this);
    }

    public void onDisable() {
    }

    public void log(String message) {
        if (getConfig().getBoolean("verbose", true)) {
            log.info(message);
        }
    }

}

@@ -3547,32 +3547,73 @@
 urn;%0A        %7D%0A%0A
+        Player player = (Player)entity;%0A%0A
         int oldL
@@ -3623,24 +3623,14 @@
 l = 
-((P
+p
 layer
-)entity)
 .get
@@ -3986,22 +3986,22 @@
 eight = 
-entity
+player
 .getLoca
@@ -4116,119 +4116,52 @@
 ble 
-above = height - tropopause;%0A            plugin.log(%22above %22+above);%0A            double extra = Math.ceil(above
+moreHunger = Math.ceil((height - tropopause)
  / p
@@ -4240,13 +4240,19 @@
  += 
-extra
+moreHunger 
 ;%0A  
@@ -4273,19 +4273,44 @@
 in.log(%22
-new
+Above tropopause, new hunger
  delta =
@@ -4448,32 +4448,33 @@
 evel(newLevel);%0A
+%0A
         %7D%0A    %7D%0A
@@ -4463,24 +4463,672 @@
 %0A%0A        %7D%0A
+%0A        // Meteors or no air, suffocating%0A        if (height %3E mesopause) %7B%0A            if (player.getHealth() %3E= plugin.getConfig().getInt(%22damageWhenHealthMin%22, 2)) %7B%0A                int damage = (int)Math.ceil((height - mesopause) / plugin.getConfig().getDouble(%22damagePerMeter%22, 10.0));%0A                damage = Math.max(damage, plugin.getConfig().getInt(%22damageMax%22, 10));%0A%0A                player.damage(damage);%0A                player.setLastDamageCause(new EntityDamageEvent(player, EntityDamageEvent.DamageCause.SUFFOCATION, damage));%0A                plugin.log(%22Above mesopause, suffocation damage = %22 + damage);%0A            %7D%0A        %7D%0A%0A
     %7D%0A%7D%0A%0Apub

Add suffocation damage when above mesopause