package net.simpvp.Misc;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack;

/**
 * Logs death messages to console
 */
public class ConsoleDeathLog implements Listener {

	public ConsoleDeathLog(Misc plugin) {
		plugin.getServer().getPluginManager().registerEvents(this, plugin);
	}

	@EventHandler
	public void onPlayerDeath(PlayerDeathEvent event){
		Location loc = event.getEntity().getLocation();

		String death_msg = String.format("%s at '%d %d %d %s' (%s died)",
				event.getDeathMessage(),
				loc.getBlockX(),
				loc.getBlockY(),
				loc.getBlockZ(),
				loc.getWorld().getName(),
				event.getEntity().getName());
		/* Log player level and inventory contents just in case.
		 * The output log format should be consistent with the
		 * /eventrestore command format */
		String sInventoryContents = "";

		for (ItemStack itemStack : event.getEntity().getInventory().getContents()) {
			if (itemStack == null)
				continue;

			String tmp = itemStack.getType().toString()
				+ "." + itemStack.getAmount()
				+ "." + itemStack.getDurability()
				+ "." + itemStack.getEnchantments().toString();
			tmp = tmp.replaceAll(" ", "");
			sInventoryContents += " " + tmp;
		}

		/* Intentional use of bukkit logger */
		Bukkit.getLogger().info(death_msg + ": " + event.getEntity().getLevel() + sInventoryContents);
	}
}


@@ -19,16 +19,109 @@
 .Misc;%0A%0A
+import java.io.ByteArrayOutputStream;%0Aimport java.io.OutputStream;%0Aimport java.util.Base64;%0A%0A
 import o
@@ -325,16 +325,68 @@
 emStack;
+%0Aimport org.bukkit.util.io.BukkitObjectOutputStream;
 %0A%0A/**%0A *
@@ -1087,379 +1087,682 @@
 /%0A%09%09
-String sInventoryContents = %22%22;%0A%0A%09%09for (ItemStack itemStack : event.getEntity().getInventory().getContents()) %7B%0A%09%09%09if (itemStack
+ItemStack%5B%5D inventory = event.getEntity().getInventory().getContents();%0A%09%09String inventoryString;%0A%09%09try %7B%0A%09%09%09ByteArrayOutputStream bytearray
  =
-=
  n
-ull)%0A%09%09%09%09continue;%0A%0A%09%09%09String tmp = itemStack.getType().toString()%0A%09%09%09%09+ %22.%22 + itemStack.getAmount()%0A%09%09%09%09+ %22.%22 + itemStack.getDurability()%0A%09%09%09%09+ %22.%22 + itemStack.getEnchantments().toString();%0A%09%09%09tmp = tmp.replaceAll(%22 %22, %22%22
+ew ByteArrayOutputStream();%0A%09%09%09OutputStream base64 = Base64.getEncoder().wrap(bytearray);%0A%09%09%09BukkitObjectOutputStream bukkitstream = new BukkitObjectOutputStream(base64);%0A%0A%09%09%09int len = 0;%0A%09%09%09for (ItemStack itemStack : inventory) %7B%0A%09%09%09%09if (itemStack == null) %7B%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D%0A%09%09%09%09len += 1;%0A%09%09%09%7D%0A%09%09%09bukkitstream.writeInt(len);%0A%0A%09%09%09for (ItemStack itemStack : inventory) %7B%0A%09%09%09%09if (itemStack == null) %7B%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D%0A%0A%09%09%09%09bukkitstream.writeObject(itemStack);%0A%09%09%09%7D%0A%0A%09%09%09bukkitstream.close();%0A%09%09%09base64.close(
 );%0A
+%0A
 %09%09%09
-sI
+i
 nventory
 Cont
@@ -1761,29 +1761,112 @@
 tory
-Contents += %22 %22 + tmp
+String = new String(bytearray.toByteArray());%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09throw new RuntimeException(e)
 ;%0A%09%09
@@ -1939,26 +1939,45 @@
 nfo(
-death_msg + %22: %22 +
+String.format(%22%25s: %25d %25s%22, death_msg,
  eve
@@ -2005,21 +2005,19 @@
 el()
- + sI
+, i
 nventory
 Cont
@@ -2012,24 +2012,23 @@
 nventory
-Contents
+String)
 );%0A%09%7D%0A%7D%0A

Switch deathlog to new eventrestore format