Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package net.stoneharry.VoteCache;
- /**
- *
- * @author Harry
- */
- import java.io.FileReader;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.HashMap;
- import java.util.Properties;
- import java.util.logging.Logger;
- import org.bukkit.Server;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitScheduler;
- public final class VoteCache extends JavaPlugin
- implements Runnable
- {
- private static Logger m_log = Logger.getLogger("Minecraft");
- private HashMap<String, Long> m_voteCache;
- private Connection m_connection;
- private String m_tableName;
- private String m_nameCol;
- private String m_timeCol;
- private PreparedStatement m_queryStatement;
- protected String m_message;
- private PreparedStatement m_insertStatement;
- protected void insertNewPlayer(String name)
- {
- try
- {
- long t = System.currentTimeMillis() + 84600000L;
- t /= 1000L;
- this.m_insertStatement.setString(1, name);
- this.m_insertStatement.setLong(2, t);
- this.m_insertStatement.execute();
- synchronized (this) {
- this.m_voteCache.put(name, Long.valueOf(t));
- }
- m_log.info("[VoteCache] I've never seen " + name + " before, so I'm adding him to the cache.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- protected synchronized boolean canPlace(String name)
- {
- Long ms = (Long)this.m_voteCache.get(name);
- if (ms == null) {
- insertNewPlayer(name);
- return true;
- }
- return ms.longValue() > System.currentTimeMillis() / 1000L;
- }
- public void onEnable()
- {
- m_log.info("[VoteCache] Enabled.");
- this.m_voteCache = new HashMap();
- new BlockListener(this);
- Properties conf = new Properties();
- try {
- conf.load(new FileReader("votecache.conf"));
- } catch (Exception e1) {
- e1.printStackTrace();
- return;
- }
- String host = (String)conf.get("host");
- String user = (String)conf.get("user");
- String pass = (String)conf.get("pass");
- int port = Integer.parseInt((String)conf.get("port"));
- String db = (String)conf.get("db");
- this.m_tableName = ((String)conf.get("table_name"));
- this.m_nameCol = ((String)conf.get("column_name"));
- this.m_timeCol = ((String)conf.get("column_time"));
- this.m_message = ((String)conf.get("message"));
- String url = "jdbc:mysql://" + host + ":" + port + "/" + db;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- this.m_connection = DriverManager.getConnection(url, user, pass);
- m_log.info("[VoteCache] Established connection to " + url + ".");
- this.m_queryStatement = this.m_connection.prepareStatement(
- "SELECT * FROM " + this.m_tableName + ";");
- this.m_insertStatement = this.m_connection.prepareStatement(
- "INSERT INTO " + this.m_tableName + " (" + this.m_nameCol + "," + this.m_timeCol + ") VALUES (?, ?)");
- } catch (Exception e) {
- e.printStackTrace();
- onDisable();
- return;
- }
- getServer().getScheduler().scheduleAsyncRepeatingTask(this, this, 0L, 1200L);
- }
- public void onDisable()
- {
- m_log.info("[VoteCache] Disabled.");
- this.m_voteCache = null;
- }
- public void run()
- {
- synchronized (this) {
- m_log.info("Refreshing cache..");
- try {
- ResultSet s = this.m_queryStatement.executeQuery();
- boolean b = s.first();
- if (b) {
- this.m_voteCache.clear();
- do {
- String name = s.getString(this.m_nameCol);
- long time = s.getLong(this.m_timeCol);
- this.m_voteCache.put(name, Long.valueOf(time));
- }
- while (
- s.next());
- s.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return;
- }
- m_log.info("Cache refreshed with " + this.m_voteCache.size() + " entries.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment