Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bukkit.subanomic.VoteNotify;
- import java.io.File;
- import java.awt.List;
- import java.util.*;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.sql.*;
- import com.mysql.jdbc.Driver;
- import com.bukkit.subanomic.VoteNotify.VoteNotifyPlayerListener;
- import org.bukkit.entity.Player;
- import org.bukkit.ChatColor;
- import org.bukkit.Server;
- import org.bukkit.event.Event;
- import org.bukkit.event.Event.Priority;
- import org.bukkit.event.Event.Type;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.PluginLoader;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.scheduler.BukkitScheduler;
- public class VoteNotifyPlugin extends JavaPlugin {
- private static String mainDirectory = "plugins/VoteNotifyPlugin";
- private static File VoteNotify = new File(mainDirectory + File.separator + "VoteNotify.dat");
- private static Properties prop = new Properties();
- // Config directives
- private int tickInterval = 10;
- private String mysqlUser = "root";
- private String mysqlPassword = "";
- private String mysqlHost = "127.0.0.1";
- private String mysqlDatabase = "minecraft";
- private String mysqlPort = "3306";
- // MySQL
- Connection conn = null;
- private final VoteNotifyPlayerListener playerListener = new VoteNotifyPlayerListener(this);
- public void onEnable() {
- new File(mainDirectory).mkdir();
- if (!VoteNotify.exists()) {
- try {
- VoteNotify.createNewFile();
- FileOutputStream out = new FileOutputStream(VoteNotify);
- prop.put("tick-interval", "10");
- prop.put("mysql-user", "root");
- prop.put("mysql-password", "");
- prop.put("mysql-host", "");
- prop.put("mysql-database", "");
- prop.put("mysql-port", "3306");
- out.flush();
- out.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- } else {
- loadProcedure();
- }
- String url = "";
- try {
- url = "jdbc:mysql://" + mysqlHost + ":" + mysqlPort + "/" + mysqlDatabase;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(url, mysqlUser, mysqlPassword);
- } catch (Exception e) {
- System.err.println("Error trying to connect to the database! "+ url + ", " + mysqlUser + ", " + mysqlPassword);
- e.printStackTrace();
- }
- PluginManager pm = getServer().getPluginManager();
- this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
- public void run() {
- notifyPlayers();
- }
- }, tickInterval * 20, tickInterval * 20);
- }
- public void loadProcedure() {
- FileInputStream in;
- try {
- in = new FileInputStream(VoteNotify);
- try {
- prop.load(in);
- tickInterval = Integer.parseInt(prop.getProperty("tick-interval"));
- mysqlUser = prop.getProperty("mysql-user");
- mysqlPassword = prop.getProperty("mysql-password");
- mysqlHost = prop.getProperty("mysql-host");
- mysqlDatabase = prop.getProperty("mysql-database");
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public void notifyPlayers() {
- int timestamp = (int)System.currentTimeMillis()/1000;
- int rowCount = 0;
- if (conn != null) {
- Statement s = null;
- ResultSet rs = null;
- for (Player p : getServer().getOnlinePlayers()) {
- try {
- s = conn.createStatement();
- s.executeQuery("SELECT COUNT(*) FROM votes WHERE nick = '" + p.getName() + "' AND time > " + (timestamp-86400));
- rs = s.getResultSet();
- rs.next();
- rowCount = rs.getInt(1);
- if (rowCount > 0) {
- p.sendMessage(ChatColor.BLUE + "Danke fürs Voten! Schicke e3-base.de/vote/" + p.getName() + " an deine Freunde, um weitere Jings zu kassieren!");
- } else {
- p.sendMessage(ChatColor.BLUE + "Du hast heute noch nicht für uns gevotet! Jetzt auf e3-base.de/vote/" + p.getName() + " voten und 1 Jing kassieren!");
- }
- } catch (Exception e) {
- System.err.println("Error trying to perform database operations");
- e.printStackTrace();
- }
- }
- }
- }
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (command.getName().equalsIgnoreCase("vn")) {
- if (!sender.isOp()) {
- sender.sendMessage("You are not allowed to issue this command!");
- return true;
- }
- notifyPlayers();
- return true;
- }
- return false;
- }
- public void onDisable() {
- if (conn != null) {
- try {
- conn.close();
- } catch (Exception e) {
- // Ignore
- }
- }
- System.out.println("VoteNotify disabled");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement