Advertisement
MsGamerHD

Untitled

Oct 9th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package de.msgamerhd.kingoftheladder.stats;
  2.  
  3. import java.sql.ResultSet;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.UUID;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.entity.Player;
  10.  
  11. import de.msgamerhd.kingoftheladder.Settings;
  12. import de.msgamerhd.kingoftheladder.utils.MySQL;
  13.  
  14. /**
  15.  * Class created by MsGamerHD on 16.06.2016
  16.  */
  17. public class Stats_RankingPoints {
  18.    
  19.     public static HashMap<UUID, Integer> points = new HashMap<>();
  20.  
  21.     public static String column = "points";
  22.    
  23.     public static void syncWithMySQL(){
  24.         for(UUID uuid : points.keySet()){
  25.             if(existPlayer(uuid)){
  26.                 MySQL.update("UPDATE "+Settings.stats_tabelname+" SET "+column+" = '"+points.get(uuid)+"' WHERE UUID = '"+uuid+"'");
  27.             } else {
  28.                 MySQL.update("INSERT INTO "+Settings.stats_tabelname+" (UUID,"+column+") VALUES ('"+uuid.toString()+"','"+points.get(uuid)+"')");
  29.             }
  30.         }
  31.     }
  32.  
  33.    
  34.     public static boolean existPlayer(UUID uuid){
  35.         try {
  36.             ResultSet rs = MySQL.query("SELECT "+column+" FROM "+Settings.stats_tabelname+" WHERE UUID = '"+uuid+"'");
  37.             return rs.next();
  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         }
  41.        
  42.         return false;
  43.     }
  44.    
  45.     public static void set(UUID uuid, int toSet){
  46.         points.put(uuid, toSet);
  47.     }
  48.    
  49.     public static int get(UUID uuid){
  50.         if(!points.containsKey(uuid)){
  51.             try{
  52.                 ResultSet rs = MySQL.query("SELECT "+column+" FROM "+Settings.stats_tabelname+" WHERE UUID = '"+uuid+"'");
  53.                 if(rs.next()){
  54.                     points.put(uuid, rs.getInt(column));
  55.                 }
  56.             } catch (Exception d){}
  57.             if(!points.containsKey(uuid)){
  58.                 points.put(uuid, 0);
  59.             }
  60.            
  61.         }
  62.         return points.get(uuid);
  63.     }
  64.  
  65.     public static void add(UUID uuid, int toAdd, boolean message){
  66.         int count = get(uuid);
  67.         count = count + toAdd;
  68.        
  69.         if(message){
  70.             Player p = Bukkit.getPlayer(uuid);
  71.             if(p != null) p.sendMessage(Settings.pr+Settings.acpt+"+ "+toAdd+(toAdd == 1 ? " Raking-Punkt" : " Raking-Punkte"));
  72.         }
  73.        
  74.         set(uuid, count);
  75.     }
  76.  
  77.     public static void remove(UUID uuid, int toRemove){
  78.         int count = get(uuid);
  79.         count = count - toRemove;
  80.        
  81.         set(uuid, count);
  82.     }
  83.    
  84.     public static ArrayList<UUID> getTopPlayers(){
  85.         ArrayList<UUID> top = new ArrayList<UUID>();
  86.         try{
  87.             ResultSet rs = MySQL.query("SELECT UUID FROM "+Settings.stats_tabelname+" ORDER BY "+column+" DESC");
  88.             rs.beforeFirst();
  89.             for(int i = 0; i < 3; i++){
  90.                 rs.next();
  91.                 top.add(UUID.fromString(rs.getString("uuid")));
  92.             }
  93.         } catch(Exception exc){}
  94.         return top;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement