Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. package com.bukkit.subanomic.VoteNotify;
  2.  
  3. import java.io.File;
  4.  
  5. import java.awt.List;
  6. import java.util.*;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import java.sql.*;
  14. import com.mysql.jdbc.Driver;
  15.  
  16. import com.bukkit.subanomic.VoteNotify.VoteNotifyPlayerListener;
  17.  
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.ChatColor;
  20. import org.bukkit.Server;
  21. import org.bukkit.event.Event;
  22. import org.bukkit.event.Event.Priority;
  23. import org.bukkit.event.Event.Type;
  24. import org.bukkit.plugin.PluginDescriptionFile;
  25. import org.bukkit.plugin.PluginLoader;
  26. import org.bukkit.plugin.java.JavaPlugin;
  27. import org.bukkit.plugin.PluginManager;
  28. import org.bukkit.command.Command;
  29. import org.bukkit.command.CommandSender;
  30. import org.bukkit.entity.Player;
  31. import org.bukkit.scheduler.BukkitScheduler;
  32.  
  33. public class VoteNotifyPlugin extends JavaPlugin {
  34. private static String mainDirectory = "plugins/VoteNotifyPlugin";
  35. private static File VoteNotify = new File(mainDirectory + File.separator + "VoteNotify.dat");
  36. private static Properties prop = new Properties();
  37.  
  38. // Config directives
  39. private int tickInterval = 10;
  40. private String mysqlUser = "root";
  41. private String mysqlPassword = "";
  42. private String mysqlHost = "127.0.0.1";
  43. private String mysqlDatabase = "minecraft";
  44. private String mysqlPort = "3306";
  45.  
  46. // MySQL
  47. Connection conn = null;
  48.  
  49. private final VoteNotifyPlayerListener playerListener = new VoteNotifyPlayerListener(this);
  50.  
  51. public void onEnable() {
  52. new File(mainDirectory).mkdir();
  53. if (!VoteNotify.exists()) {
  54. try {
  55. VoteNotify.createNewFile();
  56. FileOutputStream out = new FileOutputStream(VoteNotify);
  57. prop.put("tick-interval", "10");
  58. prop.put("mysql-user", "root");
  59. prop.put("mysql-password", "");
  60. prop.put("mysql-host", "");
  61. prop.put("mysql-database", "");
  62. prop.put("mysql-port", "3306");
  63. out.flush();
  64. out.close();
  65. } catch (IOException ex) {
  66. ex.printStackTrace();
  67. }
  68. } else {
  69. loadProcedure();
  70. }
  71.  
  72. String url = "";
  73. try {
  74. url = "jdbc:mysql://" + mysqlHost + ":" + mysqlPort + "/" + mysqlDatabase;
  75. Class.forName("com.mysql.jdbc.Driver").newInstance();
  76. conn = DriverManager.getConnection(url, mysqlUser, mysqlPassword);
  77. } catch (Exception e) {
  78. System.err.println("Error trying to connect to the database! "+ url + ", " + mysqlUser + ", " + mysqlPassword);
  79. e.printStackTrace();
  80. }
  81.  
  82. PluginManager pm = getServer().getPluginManager();
  83.  
  84.  
  85.  
  86. this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
  87. public void run() {
  88. notifyPlayers();
  89. }
  90. }, tickInterval * 20, tickInterval * 20);
  91.  
  92. }
  93.  
  94. public void loadProcedure() {
  95. FileInputStream in;
  96.  
  97. try {
  98. in = new FileInputStream(VoteNotify);
  99. try {
  100. prop.load(in);
  101.  
  102. tickInterval = Integer.parseInt(prop.getProperty("tick-interval"));
  103. mysqlUser = prop.getProperty("mysql-user");
  104. mysqlPassword = prop.getProperty("mysql-password");
  105. mysqlHost = prop.getProperty("mysql-host");
  106. mysqlDatabase = prop.getProperty("mysql-database");
  107.  
  108. in.close();
  109. } catch (IOException e) {
  110. e.printStackTrace();
  111. }
  112. } catch (FileNotFoundException e) {
  113. e.printStackTrace();
  114. }
  115. }
  116.  
  117. public void notifyPlayers() {
  118. int timestamp = (int)System.currentTimeMillis()/1000;
  119. int rowCount = 0;
  120.  
  121. if (conn != null) {
  122. Statement s = null;
  123. ResultSet rs = null;
  124. for (Player p : getServer().getOnlinePlayers()) {
  125. try {
  126. s = conn.createStatement();
  127. s.executeQuery("SELECT COUNT(*) FROM votes WHERE nick = '" + p.getName() + "' AND time > " + (timestamp-86400));
  128. rs = s.getResultSet();
  129. rs.next();
  130. rowCount = rs.getInt(1);
  131. if (rowCount > 0) {
  132. p.sendMessage(ChatColor.BLUE + "Danke fürs Voten! Schicke e3-base.de/vote/" + p.getName() + " an deine Freunde, um weitere Jings zu kassieren!");
  133. } else {
  134. 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!");
  135. }
  136. } catch (Exception e) {
  137. System.err.println("Error trying to perform database operations");
  138. e.printStackTrace();
  139. }
  140.  
  141. }
  142. }
  143. }
  144.  
  145. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  146. if (command.getName().equalsIgnoreCase("vn")) {
  147. if (!sender.isOp()) {
  148. sender.sendMessage("You are not allowed to issue this command!");
  149. return true;
  150. }
  151.  
  152. notifyPlayers();
  153. return true;
  154.  
  155. }
  156. return false;
  157. }
  158.  
  159. public void onDisable() {
  160. if (conn != null) {
  161. try {
  162. conn.close();
  163. } catch (Exception e) {
  164. // Ignore
  165. }
  166. }
  167. System.out.println("VoteNotify disabled");
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement