Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.github.Holyirus.votifiermod;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.github.Holyirus.votifiermod.database.Database;
- import com.github.Holyirus.votifiermod.database.drivers.iDriver;
- import com.github.Holyirus.votifiermod.listeners.VoteListener;
- public class VoteTracker extends JavaPlugin{
- HashMap<Player, HashMap<String, Long>> votes = new HashMap<Player, HashMap<String, Long>>();
- HashMap<String, String> conf = new HashMap<String, String>();
- File configFile;
- FileConfiguration config;
- public static final Logger log = Logger.getLogger("Minecraft");
- int TaskID;
- long nowTime;
- public static VoteTracker plugin;
- public VoteTracker(){
- conf.put("database", value);
- conf.put("host", value);
- conf.put("port", value);
- conf.put("user", value);
- conf.put("password", value);
- conf.put("prefix", value);
- setPlugin();
- }
- /* private MySQL(HashMap<String, String> c) {
- this.plugin = VoteTracker.getPlugin();
- this.db = c.get("database");
- this.host = c.get("host");
- this.port = String.valueOf(c.get("port"));
- this.user = c.get("user");
- this.pass = c.get("password");
- this.prefix = c.get("prefix");
- this.connect();
- }*/
- public void setPlugin(){
- plugin = this;
- }
- public static VoteTracker getPlugin(){
- return plugin;
- }
- @Override
- public void onDisable(){
- log.log(Level.INFO, "[Vote Tracker] Plugin disabled!");
- }
- @Override
- public void onEnable(){
- getServer().getPluginManager().registerEvents(new VoteListener(), this);
- configFile = new File(getDataFolder(), "config.yml");
- try {
- firstRun();
- } catch (Exception e) {
- e.printStackTrace();
- }
- config = new YamlConfiguration();
- loadYamls();
- log.log(Level.INFO, "[Vote Tracker] Plugin enabled!");
- }
- private void firstRun() throws Exception {
- if(configFile.exists()){
- log.log(Level.INFO, "Config file found!");
- }else{
- log.log(Level.INFO, "Config file NOT found, creating now!");
- configFile.getParentFile().mkdirs();
- copy(getResource("config.yml"), configFile);
- }
- }
- private void copy(InputStream in, File file) {
- try {
- OutputStream out = new FileOutputStream(file);
- byte[] buf = new byte[1024];
- int len;
- while((len=in.read(buf))>0){
- out.write(buf,0,len);
- }
- out.close();
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void loadYamls() {
- try {
- config.load(configFile);
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void saveYamls() {
- try {
- config.save(configFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void doTask(){
- TaskID = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
- public void run() {
- checkVotes();
- }
- }, 0, 6000);
- }
- public void checkVotes(){
- Scanner s = null;
- try {
- s = new Scanner(configFile);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- while(s.hasNextLine()){
- String v = s.nextLine();
- HashMap<String, Long> a = new HashMap<String, Long>();
- String[] Track = v.split(":");
- String service = Track[1];
- long time = Long.parseLong(Track[2]);
- a.put(service, time);
- votes.put(getServer().getPlayer(Track[0]), a);
- if(nowTime > time + 86400){
- iDriver db = Database.Obtain().getEngine();
- }
- }
- }
- public void sendMessage(Player p){
- int i = 0;
- HashMap<String, Long> v = votes.get(p);
- StringBuilder s = new StringBuilder();
- s.append("Hey " + p.getName() + " you are missing out on rewards :( !!! You can still vote at: ");
- if(!v.containsKey("Minestatus")){
- i++;
- s.append("Minestatus");
- }
- if(!v.containsKey("Minestatus") && !v.containsKey("MCSL")){
- s.append(" or ");
- }
- if(!v.containsKey("MCSL")){
- i++;
- s.append("MCSL");
- }
- s.append(" for " + i * 150 + "dollars!!!");
- p.sendMessage(s.toString());
- }
- public void voteInsert(Player p){
- iDriver db = Database.Obtain().getEngine();
- ArrayList<String> fields = new ArrayList<String>();
- fields.add("voter");
- fields.add("votes");
- HashMap<Integer, HashMap<String, String>> values = new HashMap<Integer, HashMap<String, String>>();
- for(int i = 0; i < fields.size(); i++) {
- HashMap<String, String> value = new HashMap<String, String>();
- String field = fields.get(i);
- if(field.equalsIgnoreCase("voter")) {
- value.put("data", p.getName());
- }else if(field.equalsIgnoreCase("votes")) {
- value.put("kind", "INT");
- value.put("data", "1");
- }
- }
- db.insert("#__Votes", fields, values).updateQuery();
- }
- public void voteIncrease(Player p){
- int votes = this.getVotes(p);
- if(votes == 0) {
- this.voteInsert(p);
- return;
- }
- iDriver db = Database.Obtain().getEngine();
- HashMap<String, String> where = new HashMap<String, String>();
- where.put("voter", p.getName());
- HashMap<String, HashMap<String, String>> values = new HashMap<String, HashMap<String, String>>();
- HashMap<String, String> d = new HashMap<String, String>();
- d.put("kind", "INT");
- d.put("data", String.valueOf(votes + 1));
- values.put("votes", d);
- db.update("#__Votes").set(values, true).where(where).updateQuery();
- }
- public int getVotes(Player p){
- int playerVotes = 0;
- iDriver db = Database.Obtain().getEngine();
- HashMap<String, String> where = new HashMap<String, String>();
- where.put("voter", p.getName());
- db.select("votes").from("#__Votes").where(where).execQuery();
- if(db.countResult() > 0) {
- HashMap<String, String> voteCount = db.getSingleResult();
- playerVotes = Integer.parseInt(voteCount.get("votes"));
- }
- return playerVotes;
- }
- public String[] getTopVotes(int page){
- iDriver db = Database.Obtain().getEngine();
- HashMap<String, String> order = new HashMap<String, String>();
- order.put("votes", "DESC");
- String[] msgs = new String[5];
- ArrayList<HashMap<String, String>> voteCount = db.select("voter", "votes").from("#__Votes").orderBy(order).limit(5).execQuery();
- for(int i = 0; i < 5 ; i++){
- HashMap<String, String> playerVotes = voteCount.get(i);
- String player = playerVotes.keySet().iterator().next();
- msgs[i] = ChatColor.GOLD + "" + i + ". " + ChatColor.RED + player + ChatColor.GOLD + " with a total of " + playerVotes.get(player) + " votes!";
- }
- return msgs;
- }
- }
Add Comment
Please, Sign In to add comment