Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package d3x.BlockLogLite;
- import java.io.File;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- //import java.sql.ResultSetMetaData;
- import java.sql.Statement;
- import java.util.HashMap;
- import java.util.logging.Logger;
- import org.bukkit.Server;
- import org.bukkit.block.Block;
- //import org.bukkit.entity.Player;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.PluginLoader;
- import org.bukkit.plugin.java.JavaPlugin;
- public class BlockLogLite extends JavaPlugin {
- static final Logger log = Logger.getLogger("Minecraft");
- Connection db;
- BlockLogLiteBlockListener blockListener = new BlockLogLiteBlockListener(this);
- HashMap<String, Integer> playerIdCache = new HashMap<String, Integer>();
- public void onEnable() {
- //log.info("[BlockLogLite] onEnable();");
- try
- {
- getDataFolder().mkdirs();
- File config = new File(getDataFolder(), "config.yml");
- if ( !config.exists() )
- config.createNewFile();
- Class.forName("com.mysql.jdbc.Driver");
- db = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
- Statement s = db.createStatement();
- s.execute("CREATE TABLE IF NOT EXISTS `bll_players` (`id` int(11) NOT NULL AUTO_INCREMENT, `player` varchar(32) NOT NULL, PRIMARY KEY (`id`), KEY `player` (`player`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- s.execute("CREATE TABLE IF NOT EXISTS `bll_data` (`id` int(11) NOT NULL AUTO_INCREMENT, `date` int(11) unsigned NOT NULL, `playerid` int(11) NOT NULL, `action` tinyint(4) NOT NULL, `x` int(11) NOT NULL, `y` int(11) NOT NULL, `z` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `playerid` (`playerid`), KEY `x` (`x`), KEY `y` (`y`), KEY `z` (`z`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
- s.close();
- }
- catch (Exception e) { e.printStackTrace(); }
- this.getServer().getPluginManager().registerEvent(org.bukkit.event.Event.Type.BLOCK_PLACED, blockListener, org.bukkit.event.Event.Priority.Monitor, this);
- this.getServer().getPluginManager().registerEvent(org.bukkit.event.Event.Type.BLOCK_BREAK, blockListener, org.bukkit.event.Event.Priority.Monitor, this);
- }
- public int getPlayerId(String player)
- {
- //log.info("[BlockLogLite] getPlayerId(" player ");");
- if ( playerIdCache.containsKey(player) )
- {
- //log.info("[BlockLogLite] - Found in cache");
- return (int) playerIdCache.get(player);
- }
- else
- {
- //log.info("[BlockLogLite] - Looking in database");
- try
- {
- Statement s = db.createStatement();
- ResultSet result = s.executeQuery("SELECT id FROM `bll_players` WHERE player = '" player "';");
- int id = 0;
- if ( result.next() )
- {
- //log.info("[BlockLogLite] -- Found in DB");
- id = result.getInt("id");
- playerIdCache.put(player, id);
- //log.info("[BlockLogLite] --- Cached player with id " id);
- s.close();
- return id;
- }
- else
- {
- //log.info("[BlockLogLite] -- Nothing there, inserting...");
- try
- {
- s.close();
- s = db.createStatement();
- s.executeUpdate("INSERT INTO `bll_players` (`player`) VALUES ('" player "');", s.RETURN_GENERATED_KEYS);
- result = s.getGeneratedKeys();
- if ( result.next() )
- {
- id = result.getInt("GENERATED_KEY");
- //log.info("[BlockLogLite] --- Inserted with id " id);
- playerIdCache.put(player, id);
- //log.info("[BlockLogLite] --- Cached player with id " id);
- }
- else
- log.warning("[BlockLogLite] getPlayerId(" player "): Somehow a freaking INSERT failed?");
- s.close();
- }
- catch (Exception ex) { ex.printStackTrace(); }
- return id;
- }
- }
- catch (Exception ex) { ex.printStackTrace(); }
- return 0;
- }
- }
- public void blockAction(String player, int blockAction, Block block)
- {
- //log.info("[BlockLogLite] blockAction(" player ", " blockAction ", " block.getX() ", " block.getY() ", " block.getZ() ", " block.getTypeId() ");");
- int playerId = getPlayerId(player);
- if ( playerId > 0 )
- {
- try
- {
- Statement s = db.createStatement();
- s.executeUpdate("INSERT INTO `bll_data` (`date`, `playerid`, `action`, `x`, `y`, `z`, `block_id`) VALUES (UNIX_TIMESTAMP(), " playerId ", " blockAction ", " block.getX() ", " block.getY() ", " block.getZ() ", " block.getTypeId() ");");
- s.close();
- }
- catch (Exception ex) { ex.printStackTrace(); }
- }
- else
- log.warning("[BlockLogLite] getPlayerId(" player "): failed (returned 0)");
- }
- public void onDisable() {
- //log.info("[BlockLogLite] onDisable();");
- try
- {
- db.close();
- }
- catch (Exception e) { e.printStackTrace(); }
- }
- }
- package d3x.BlockLogLite;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockListener;
- import org.bukkit.event.block.BlockPlaceEvent;
- public class BlockLogLiteBlockListener extends BlockListener
- {
- BlockLogLite plugin;
- public BlockLogLiteBlockListener(BlockLogLite i)
- {
- plugin = i;
- }
- @Override
- public void onBlockPlace(BlockPlaceEvent e)
- {
- plugin.blockAction(e.getPlayer().getName(), 1, e.getBlock());
- }
- @Override
- public void onBlockBreak(BlockBreakEvent e)
- {
- plugin.blockAction(e.getPlayer().getName(), 2, e.getBlock());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement