Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package me.kkiomen.pl;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.Listener;
  10. import org.bukkit.event.entity.PlayerDeathEvent;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12.  
  13. public class Main extends JavaPlugin implements Listener{
  14.  
  15.     String zamordowany;
  16.     String morderca;
  17.    
  18.     private Connection conn;
  19.    
  20.     @Override
  21.     public void onEnable() {
  22.         System.out.println("Plugin ON");
  23.         checkTable();
  24.         try {
  25.             saveData();
  26.         } catch (SQLException e) {
  27.             e.printStackTrace();
  28.         }
  29.         getServer().getPluginManager().registerEvents(this, this);
  30.        
  31.     }
  32.    
  33.     @Override
  34.     public void onDisable() {
  35.         System.out.println("Plugin OFF");
  36.         try {
  37.             saveData();
  38.         } catch (SQLException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.    
  44.     private void checkTable(){
  45.         openConnection();
  46.         StringBuilder sb = new StringBuilder();
  47.         sb.append("create table if not exists kill(");
  48.         sb.append("killer varchar(50) not null,");
  49.         sb.append("killed varchar(50) not null,");
  50.         sb.append("primary key(killer));");
  51.         try {
  52.             conn.createStatement().executeUpdate(sb.toString());
  53.         } catch (SQLException e) {
  54.             e.printStackTrace();
  55.         }
  56.         closeConnection();
  57.     }
  58.  
  59.    
  60.    
  61.    
  62.     @EventHandler
  63.    public void onKill(PlayerDeathEvent e)
  64.    {
  65.    morderca = e.getEntity().getName();
  66.    zamordowany = e.getEntity().getKiller().getName();
  67.    e.setDeathMessage(ChatColor.RED + zamordowany + " zostal zabitt przez " + morderca);
  68.    }
  69.    
  70.    
  71.     private void saveData() throws SQLException{
  72.         openConnection();
  73.         StringBuilder sb = new StringBuilder();
  74.         sb.append("INSERT INTO kill (killer, killed) VALUES (");
  75.         sb.append("’"+ morderca +"’,");
  76.         sb.append("’"+ zamordowany +"’");
  77.         sb.append(");");
  78.         conn.createStatement().executeUpdate(sb.toString());
  79.         closeConnection();
  80.     }
  81.    
  82.    
  83.     private synchronized void openConnection(){
  84.         if(!isConnected()){
  85.             try{
  86.                 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rank?user=rank&password=RANK");
  87.             }catch(SQLException e){
  88.                 e.printStackTrace();
  89.             }
  90.         }
  91.     }
  92.    
  93.     private synchronized void closeConnection(){
  94.         if(isConnected()){
  95.             try{
  96.                 conn.close();
  97.             }catch(SQLException e){
  98.                 e.printStackTrace();
  99.             }
  100.         }
  101.     }
  102.     public boolean isConnected(){
  103.         try{
  104.             if(conn == null) return false;
  105.             if(conn.isClosed()) return false;
  106.         }catch(SQLException e){
  107.             e.printStackTrace();
  108.         }
  109.         return false;
  110.     }
  111.    
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement