Guest User

Untitled

a guest
Nov 18th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. package com.github.Holyirus.votifiermod;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Scanner;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. import org.bukkit.Bukkit;
  17. import org.bukkit.ChatColor;
  18. import org.bukkit.configuration.file.FileConfiguration;
  19. import org.bukkit.configuration.file.YamlConfiguration;
  20. import org.bukkit.entity.Player;
  21. import org.bukkit.plugin.java.JavaPlugin;
  22.  
  23. import com.github.Holyirus.votifiermod.database.Database;
  24. import com.github.Holyirus.votifiermod.database.drivers.iDriver;
  25. import com.github.Holyirus.votifiermod.listeners.VoteListener;
  26.  
  27. public class VoteTracker extends JavaPlugin{
  28.  
  29. HashMap<Player, HashMap<String, Long>> votes = new HashMap<Player, HashMap<String, Long>>();
  30. HashMap<String, String> conf = new HashMap<String, String>();
  31.  
  32. File configFile;
  33. FileConfiguration config;
  34. public static final Logger log = Logger.getLogger("Minecraft");
  35. int TaskID;
  36. long nowTime;
  37. public static VoteTracker plugin;
  38.  
  39. public VoteTracker(){
  40. conf.put("database", value);
  41. conf.put("host", value);
  42. conf.put("port", value);
  43. conf.put("user", value);
  44. conf.put("password", value);
  45. conf.put("prefix", value);
  46. setPlugin();
  47. }
  48. /* private MySQL(HashMap<String, String> c) {
  49. this.plugin = VoteTracker.getPlugin();
  50. this.db = c.get("database");
  51. this.host = c.get("host");
  52. this.port = String.valueOf(c.get("port"));
  53. this.user = c.get("user");
  54. this.pass = c.get("password");
  55. this.prefix = c.get("prefix");
  56. this.connect();
  57. }*/
  58. public void setPlugin(){
  59. plugin = this;
  60. }
  61. public static VoteTracker getPlugin(){
  62. return plugin;
  63. }
  64. @Override
  65. public void onDisable(){
  66. log.log(Level.INFO, "[Vote Tracker] Plugin disabled!");
  67. }
  68.  
  69. @Override
  70. public void onEnable(){
  71. getServer().getPluginManager().registerEvents(new VoteListener(), this);
  72. configFile = new File(getDataFolder(), "config.yml");
  73. try {
  74. firstRun();
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78. config = new YamlConfiguration();
  79. loadYamls();
  80. log.log(Level.INFO, "[Vote Tracker] Plugin enabled!");
  81. }
  82.  
  83. private void firstRun() throws Exception {
  84. if(configFile.exists()){
  85. log.log(Level.INFO, "Config file found!");
  86. }else{
  87. log.log(Level.INFO, "Config file NOT found, creating now!");
  88. configFile.getParentFile().mkdirs();
  89. copy(getResource("config.yml"), configFile);
  90. }
  91. }
  92.  
  93. private void copy(InputStream in, File file) {
  94. try {
  95. OutputStream out = new FileOutputStream(file);
  96. byte[] buf = new byte[1024];
  97. int len;
  98. while((len=in.read(buf))>0){
  99. out.write(buf,0,len);
  100. }
  101. out.close();
  102. in.close();
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. }
  107.  
  108. public void loadYamls() {
  109. try {
  110. config.load(configFile);
  111. }catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. }
  115.  
  116. public void saveYamls() {
  117. try {
  118. config.save(configFile);
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. }
  122. }
  123.  
  124. public void doTask(){
  125. TaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  126. public void run() {
  127. checkVotes();
  128. }
  129. }, 0, 6000);
  130. }
  131.  
  132. public void checkVotes(){
  133. Scanner s = null;
  134. try {
  135. s = new Scanner(configFile);
  136. } catch (FileNotFoundException e) {
  137. e.printStackTrace();
  138. }
  139. while(s.hasNextLine()){
  140. String v = s.nextLine();
  141. HashMap<String, Long> a = new HashMap<String, Long>();
  142. String[] Track = v.split(":");
  143. String service = Track[1];
  144. long time = Long.parseLong(Track[2]);
  145. a.put(service, time);
  146. votes.put(getServer().getPlayer(Track[0]), a);
  147. if(nowTime > time + 86400){
  148. iDriver db = Database.Obtain().getEngine();
  149.  
  150. }
  151. }
  152. }
  153.  
  154. public void sendMessage(Player p){
  155. int i = 0;
  156. HashMap<String, Long> v = votes.get(p);
  157. StringBuilder s = new StringBuilder();
  158. s.append("Hey " + p.getName() + " you are missing out on rewards :( !!! You can still vote at: ");
  159. if(!v.containsKey("Minestatus")){
  160. i++;
  161. s.append("Minestatus");
  162. }
  163. if(!v.containsKey("Minestatus") && !v.containsKey("MCSL")){
  164. s.append(" or ");
  165. }
  166. if(!v.containsKey("MCSL")){
  167. i++;
  168. s.append("MCSL");
  169. }
  170. s.append(" for " + i * 150 + "dollars!!!");
  171. p.sendMessage(s.toString());
  172. }
  173.  
  174. public void voteInsert(Player p){
  175. iDriver db = Database.Obtain().getEngine();
  176. ArrayList<String> fields = new ArrayList<String>();
  177. fields.add("voter");
  178. fields.add("votes");
  179.  
  180. HashMap<Integer, HashMap<String, String>> values = new HashMap<Integer, HashMap<String, String>>();
  181.  
  182. for(int i = 0; i < fields.size(); i++) {
  183. HashMap<String, String> value = new HashMap<String, String>();
  184. String field = fields.get(i);
  185. if(field.equalsIgnoreCase("voter")) {
  186. value.put("data", p.getName());
  187. }else if(field.equalsIgnoreCase("votes")) {
  188. value.put("kind", "INT");
  189. value.put("data", "1");
  190. }
  191. }
  192. db.insert("#__Votes", fields, values).updateQuery();
  193. }
  194.  
  195. public void voteIncrease(Player p){
  196. int votes = this.getVotes(p);
  197. if(votes == 0) {
  198. this.voteInsert(p);
  199. return;
  200. }
  201.  
  202. iDriver db = Database.Obtain().getEngine();
  203. HashMap<String, String> where = new HashMap<String, String>();
  204. where.put("voter", p.getName());
  205.  
  206. HashMap<String, HashMap<String, String>> values = new HashMap<String, HashMap<String, String>>();
  207. HashMap<String, String> d = new HashMap<String, String>();
  208. d.put("kind", "INT");
  209. d.put("data", String.valueOf(votes + 1));
  210. values.put("votes", d);
  211. db.update("#__Votes").set(values, true).where(where).updateQuery();
  212. }
  213.  
  214. public int getVotes(Player p){
  215. int playerVotes = 0;
  216. iDriver db = Database.Obtain().getEngine();
  217. HashMap<String, String> where = new HashMap<String, String>();
  218. where.put("voter", p.getName());
  219. db.select("votes").from("#__Votes").where(where).execQuery();
  220. if(db.countResult() > 0) {
  221. HashMap<String, String> voteCount = db.getSingleResult();
  222. playerVotes = Integer.parseInt(voteCount.get("votes"));
  223. }
  224.  
  225. return playerVotes;
  226. }
  227.  
  228. public String[] getTopVotes(int page){
  229. iDriver db = Database.Obtain().getEngine();
  230. HashMap<String, String> order = new HashMap<String, String>();
  231. order.put("votes", "DESC");
  232. String[] msgs = new String[5];
  233. ArrayList<HashMap<String, String>> voteCount = db.select("voter", "votes").from("#__Votes").orderBy(order).limit(5).execQuery();
  234. for(int i = 0; i < 5 ; i++){
  235. HashMap<String, String> playerVotes = voteCount.get(i);
  236. String player = playerVotes.keySet().iterator().next();
  237. msgs[i] = ChatColor.GOLD + "" + i + ". " + ChatColor.RED + player + ChatColor.GOLD + " with a total of " + playerVotes.get(player) + " votes!";
  238. }
  239. return msgs;
  240. }
  241. }
Add Comment
Please, Sign In to add comment