package cat.nyaa.autobloodmoon.events;

import cat.nyaa.autobloodmoon.AutoBloodmoon;
import cat.nyaa.autobloodmoon.arena.Arena;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class PlayerListener implements Listener {
    private AutoBloodmoon plugin;

    public PlayerListener(AutoBloodmoon pl) {
        plugin = pl;
        plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e) {
        if (plugin.currentArena != null && plugin.currentArena.state == Arena.ArenaState.PLAYING &&
                plugin.currentArena.players.contains(e.getPlayer().getUniqueId())) {
            plugin.currentArena.quit(e.getPlayer());
        }
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent e) {
        if (plugin.currentArena != null && plugin.currentArena.state == Arena.ArenaState.PLAYING &&
                plugin.currentArena.players.contains(e.getEntity().getUniqueId())) {
            plugin.currentArena.quit(e.getEntity());
        }
    }
}

@@ -121,16 +121,167 @@
 .Arena;%0A
+import org.bukkit.Location;%0Aimport org.bukkit.Material;%0Aimport org.bukkit.block.Block;%0Aimport org.bukkit.block.Chest;%0Aimport org.bukkit.entity.Player;%0A
 import o
@@ -440,16 +440,121 @@
 itEvent;
+%0Aimport org.bukkit.inventory.Inventory;%0Aimport org.bukkit.inventory.ItemStack;%0A%0Aimport java.util.HashMap;
 %0A%0Apublic
@@ -1376,58 +1376,1747 @@
     
-plugin.currentArena.quit(e.getEntity());%0A        %7D
+Player player = e.getEntity();%0A            if (plugin.cfg.save_inventory && e.getDrops() != null && !e.getDrops().isEmpty() &&%0A                    player.getLocation().getY() %3E 5) %7B%0A                for (int y = 0; y %3C 32; y++) %7B%0A                    Location loc = player.getLocation().clone();%0A                    loc.setY(loc.getY() + y);%0A                    if (loc.getBlock().getType() == Material.AIR) %7B%0A                        Block block = loc.getBlock();%0A                        block.setType(getChestType(loc.getBlock()));%0A                        Chest chest = (Chest) block.getState();%0A                        Inventory inventory = chest.getInventory();%0A                        HashMap items = inventory.addItem((ItemStack%5B%5D) e.getDrops().toArray(new ItemStack%5B0%5D));%0A                        e.getDrops().clear();%0A                        e.getDrops().addAll(items.values());%0A                        if (e.getDrops().isEmpty()) %7B%0A                            e.setKeepInventory(false);%0A                            break;%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A            plugin.currentArena.quit(e.getEntity());%0A        %7D%0A    %7D%0A    %0A        public static Material getChestType(Block block) %7B%0A        Material material;%0A        if (block.getX() %25 2 == 0) %7B%0A            material = Material.TRAPPED_CHEST;%0A        %7D else %7B%0A            material = Material.CHEST;%0A        %7D%0A        if (block.getY() %25 2 == 0) %7B%0A            material = material.equals(Material.CHEST) ? Material.TRAPPED_CHEST : Material.CHEST;%0A        %7D%0A        if (block.getZ() %25 2 == 0) %7B%0A            material = material.equals(Material.TRAPPED_CHEST) ? Material.CHEST : Material.TRAPPED_CHEST;%0A        %7D%0A        return material;
 %0A   

save inventory