stoneharry

Untitled

Mar 3rd, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package net.stoneharry.VoteCache;
  7.  
  8. /**
  9.  *
  10.  * @author Harry
  11.  */
  12.  
  13. import java.io.FileReader;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.util.HashMap;
  20. import java.util.Properties;
  21. import java.util.logging.Logger;
  22. import org.bukkit.Server;
  23. import org.bukkit.plugin.java.JavaPlugin;
  24. import org.bukkit.scheduler.BukkitScheduler;
  25.  
  26. public final class VoteCache extends JavaPlugin
  27.   implements Runnable
  28. {
  29.   private static Logger m_log = Logger.getLogger("Minecraft");
  30.   private HashMap<String, Long> m_voteCache;
  31.   private Connection m_connection;
  32.   private String m_tableName;
  33.   private String m_nameCol;
  34.   private String m_timeCol;
  35.   private PreparedStatement m_queryStatement;
  36.   protected String m_message;
  37.   private PreparedStatement m_insertStatement;
  38.  
  39.   protected void insertNewPlayer(String name)
  40.   {
  41.     try
  42.     {
  43.       long t = System.currentTimeMillis() + 84600000L;
  44.       t /= 1000L;
  45.       this.m_insertStatement.setString(1, name);
  46.       this.m_insertStatement.setLong(2, t);
  47.       this.m_insertStatement.execute();
  48.       synchronized (this) {
  49.         this.m_voteCache.put(name, Long.valueOf(t));
  50.       }
  51.       m_log.info("[VoteCache] I've never seen " + name + " before, so I'm adding him to the cache.");
  52.     } catch (SQLException e) {
  53.       e.printStackTrace();
  54.     }
  55.   }
  56.  
  57.   protected synchronized boolean canPlace(String name)
  58.   {
  59.     Long ms = (Long)this.m_voteCache.get(name);
  60.     if (ms == null) {
  61.       insertNewPlayer(name);
  62.       return true;
  63.     }
  64.  
  65.     return ms.longValue() > System.currentTimeMillis() / 1000L;
  66.   }
  67.  
  68.   public void onEnable()
  69.   {
  70.     m_log.info("[VoteCache] Enabled.");
  71.     this.m_voteCache = new HashMap();
  72.     new BlockListener(this);
  73.  
  74.     Properties conf = new Properties();
  75.     try {
  76.       conf.load(new FileReader("votecache.conf"));
  77.     } catch (Exception e1) {
  78.       e1.printStackTrace();
  79.       return;
  80.     }
  81.     String host = (String)conf.get("host");
  82.     String user = (String)conf.get("user");
  83.     String pass = (String)conf.get("pass");
  84.     int port = Integer.parseInt((String)conf.get("port"));
  85.     String db = (String)conf.get("db");
  86.     this.m_tableName = ((String)conf.get("table_name"));
  87.     this.m_nameCol = ((String)conf.get("column_name"));
  88.     this.m_timeCol = ((String)conf.get("column_time"));
  89.     this.m_message = ((String)conf.get("message"));
  90.     String url = "jdbc:mysql://" + host + ":" + port + "/" + db;
  91.     try {
  92.       Class.forName("com.mysql.jdbc.Driver").newInstance();
  93.       this.m_connection = DriverManager.getConnection(url, user, pass);
  94.       m_log.info("[VoteCache] Established connection to " + url + ".");
  95.       this.m_queryStatement = this.m_connection.prepareStatement(
  96.         "SELECT * FROM " + this.m_tableName + ";");
  97.       this.m_insertStatement = this.m_connection.prepareStatement(
  98.         "INSERT INTO " + this.m_tableName + " (" + this.m_nameCol + "," + this.m_timeCol + ") VALUES (?, ?)");
  99.     } catch (Exception e) {
  100.       e.printStackTrace();
  101.       onDisable();
  102.       return;
  103.     }
  104.     getServer().getScheduler().scheduleAsyncRepeatingTask(this, this, 0L, 1200L);
  105.   }
  106.  
  107.   public void onDisable()
  108.   {
  109.     m_log.info("[VoteCache] Disabled.");
  110.     this.m_voteCache = null;
  111.   }
  112.  
  113.   public void run()
  114.   {
  115.     synchronized (this) {
  116.       m_log.info("Refreshing cache..");
  117.       try {
  118.         ResultSet s = this.m_queryStatement.executeQuery();
  119.         boolean b = s.first();
  120.         if (b) {
  121.           this.m_voteCache.clear();
  122.           do {
  123.             String name = s.getString(this.m_nameCol);
  124.             long time = s.getLong(this.m_timeCol);
  125.             this.m_voteCache.put(name, Long.valueOf(time));
  126.           }
  127.           while (
  128.             s.next());
  129.           s.close();
  130.         }
  131.       } catch (SQLException e) {
  132.         e.printStackTrace();
  133.         return;
  134.       }
  135.       m_log.info("Cache refreshed with " + this.m_voteCache.size() + " entries.");
  136.     }
  137.   }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment