package com.haniokasai.nukkit.KillBearBoys;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Base64;
import java.util.Date;

import cn.nukkit.Player;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.Listener;
import cn.nukkit.event.block.BlockBreakEvent;
import cn.nukkit.event.block.BlockPlaceEvent;
import cn.nukkit.event.player.PlayerInteractEvent;
import cn.nukkit.plugin.PluginBase;


/*
Logs
	13.8.18 1.0 Begin
	13.8.20 1.0.1
	13.8.24 1.0.2
	13.11.16 2.0
		-rewrite all things
	13.11.23 2.0.1
		-Fix bug
	14.3.1 2.0.2
		-Remove default timezone
    14.10.18 2.0.3 and 2.0.4
             -1.4Beta Support(ljy Thank you!)
	15.09.16 3.0.0
		-Database changed to sqlite3.(haniokasai)
	16.04.28 0.0.1n
		-for nukkit.
 -thanks hmy2001! http://hmy2001.dip.jp/Blog/?m=201510
*/


public class Main extends PluginBase implements Listener{

	Connection connection = null;
	Statement statement = null;
	public void onEnable() {

		 this.getServer().getPluginManager().registerEvents(this, this);
		getDataFolder().mkdir();
		 // load the sqlite-JDBC driver using the current class loader

		try{
			Class.forName("org.sqlite.JDBC");
			}catch(Exception e){
				 System.err.println(e.getMessage());
			}
		   Connection connection = null;
		   try
		   {
		     connection = DriverManager.getConnection("jdbc:sqlite:"+getDataFolder()+"/killb.db");
		     Statement statement = connection.createStatement();
		     statement.setQueryTimeout(30);
		     statement.executeUpdate("CREATE TABLE IF NOT EXISTS  block (xyz TEXT PRIMARY KEY, level TEXT , who TEXT ,ip TEXT, cid TEXT, action TEXT, time INT, blockname TEXT, blockid INT, meta INT ,skinid TEXT)");

		   }
		   catch(SQLException e)
		   {

		     System.err.println(e.getMessage());
		   }
		   finally
		   {

		   }

        this.getServer().getLogger().info("[AdminNotice] Loaded");

}

	public void onDisable() {
		try
	     {
	       if(connection != null)
	         connection.close();
	     }
	     catch(SQLException e)
	     {
	       // connection close failed.
	       System.err.println(e);
	     }
	}


@EventHandler
	public void t(PlayerInteractEvent event){
		Player player = event.getPlayer();
		if(player.hasPermission("KillBearBoys.touch")){
			double x = event.getBlock().x;
			double y = event.getBlock().y;
			double z = event.getBlock().z;
			String xyz ="x"+x+"y"+y+"z"+z;
			try {
			ResultSet rs = statement.executeQuery("SELECT who , ip, cid, action, blockname, blockid, meta, time ,skinid,FROM block WHERE xyz = "+xyz);
			if(rs.getString("xyz") != null){

				player.sendMessage("[KillBearBoys]////////////////////");
				player.sendMessage("[XYZ] "+xyz);
				player.sendMessage("[Name] "+rs.getString("who"));
				player.sendMessage("[IP] "+rs.getString("ip")+" [CID] "+rs.getString("cid"));
				player.sendMessage("[Action]"+rs.getString("action"));
				player.sendMessage("[Block] "+rs.getString("blockname")+" [ID] "+rs.getInt("blockid")+ ":"+rs.getInt("meta"));
				player.sendMessage("[Date] "+rs.getInt("time")+"[SkinID]"+rs.getString("skinid"));
				player.sendMessage("////////////////////////////////");
			}}
		     catch(SQLException e)
		     {
		       // connection close failed.
		       System.err.println(e);
		     }
			}else{
				player.sendMessage("[KillBearBoys]There are no log.");
			}

	}
	@EventHandler
	public void b(BlockBreakEvent event){
		String level = event.getPlayer().getLevel().getName();
		double x = event.getBlock().x;
		double y = event.getBlock().y;
		double z = event.getBlock().z;
		String xyz ="x"+x+"y"+y+"z"+z;
		Player player = event.getPlayer();
		String who = player.getName();
		String ip =  player.getAddress();
		String cid = player.getClientSecret();
		String action = "Place";
		int time = Integer.parseInt(String.valueOf(new Date()));
		String blockname = event.getBlock().getName();
		int blockid = event.getBlock().getId();
		int meta = event.getBlock().getDamage();
		String skinid=Base64.getEncoder()
        .encodeToString(player.getSkin().toString().getBytes()).substring(0,15);
		try {
			statement.executeUpdate("INSERT OR REPLACE INTO block VALUES("+xyz+","+level+","+who+","+ip+","+cid+","+action+","+time+","+blockname+","+blockid+","+meta+","+skinid+","+")");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@EventHandler
	public void p(BlockPlaceEvent event){
		String level = event.getPlayer().getLevel().getName();
		double x = event.getBlock().x;
		double y = event.getBlock().y;
		double z = event.getBlock().z;
		String xyz ="x"+x+"y"+y+"z"+z;
		Player player = event.getPlayer();
		String who = player.getName();
		String ip =  player.getAddress();
		String cid = player.getClientSecret();
		String action = "Place";
		int time = Integer.parseInt(String.valueOf(new Date()));
		String blockname = event.getBlock().getName();
		int blockid = event.getBlock().getId();
		int meta = event.getBlock().getDamage();
		String skinid=Base64.getEncoder()
        .encodeToString(player.getSkin().toString().getBytes()).substring(0,15);
		try {
			statement.executeUpdate("INSERT OR REPLACE INTO block VALUES("+xyz+","+level+","+who+","+ip+","+cid+","+action+","+time+","+blockname+","+blockid+","+meta+","+skinid+","+")");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}



}
@@ -217,32 +217,8 @@
 64;%0D
-%0Aimport java.util.Date;%0D
 %0A%0D%0Ai
@@ -3929,32 +3929,34 @@
 ion = %22Place%22;%0D%0A
+%0D%0A
 %09%09int time = Int
@@ -3956,51 +3956,62 @@
 e = 
-Integer.parseInt(String.valueOf(new Date())
+ (int) (System.currentTimeMillis() %25 Integer.MAX_VALUE
 );%0D%0A
@@ -4967,51 +4967,62 @@
 e = 
-Integer.parseInt(String.valueOf(new Date())
+ (int) (System.currentTimeMillis() %25 Integer.MAX_VALUE
 );%0D%0A

fix unixtime