Advertisement
DibDibsTH13TEEN

Main.java 2

Jul 10th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package com.dibdibcraft.dBans;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.command.Command;
  5. import org.bukkit.command.CommandSender;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.event.EventException;
  8. import org.bukkit.event.EventHandler;
  9. import org.bukkit.event.Listener;
  10. import org.bukkit.event.player.PlayerLoginEvent;
  11. import org.bukkit.plugin.PluginManager;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13. import com.dibdibcraft.dBans.SQL;
  14.  
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17.  
  18. public class Main extends JavaPlugin implements Listener {
  19.     public SQL sql = new SQL();
  20.    
  21.     JavaPlugin plugin;
  22.    
  23.     //PERMISSION DEFINITIONS
  24.     String permPrefix = "dBans."; //This will be used later....
  25.    
  26.     public String colorize(String msg) {
  27.         String coloredMsg = "";
  28.         for (int i = 0; i < msg.length(); i++) {
  29.             if (msg.charAt(i) == '&') {
  30.                 coloredMsg += '§';
  31.             }
  32.            
  33.             else{
  34.                 coloredMsg += msg.charAt(i);
  35.             }
  36.         }
  37.         return coloredMsg;
  38.     }
  39.    
  40.     @Override
  41.     public void onEnable(){
  42.         System.out.println("[dBans] dBans Enabled");
  43.         PluginManager pManager = getServer().getPluginManager();
  44.         pManager.registerEvents(this, this);
  45.        
  46.         try {
  47.             sql.connect(); //Opens the SQL connection.
  48.         }
  49.        
  50.         catch (SQLException e) {
  51.             e.printStackTrace();
  52.         }
  53.     }
  54.    
  55.     @Override
  56.     public void onDisable(){
  57.         System.out.println("[dBans] dBans Disabled");
  58.        
  59.         try {
  60.             sql.close(null);
  61.         }
  62.         catch (SQLException e) {
  63.             // TODO Auto-generated catch block
  64.             e.printStackTrace();
  65.         }
  66.     }
  67.    
  68.     public boolean onCommand(CommandSender s, Command cmd, String label, String[] args){
  69.         if (cmd.getName().equalsIgnoreCase("dBans")){
  70.             if (args.length == 0){ //If the command is just /dbans or /dbans help, return the help menu.
  71.                 s.sendMessage(colorize("&f&ldBans Help Menu"
  72.                         + "\n&&f/ban - "
  73.                         + "\n  &f&oDescription: Permanently Bans a Player from the Selected Platform(s)"
  74.                         + "\n &f&oUsage: /ban <playerName> <platform> <reason>"));
  75.                
  76.             }
  77.         }
  78.         return false;
  79.     }
  80.    
  81.     @EventHandler
  82.     public void playerLogin(PlayerLoginEvent event) throws SQLException, EventException{
  83.         Player player = event.getPlayer();
  84.         Bukkit.broadcastMessage("UUID from Client: " + player.getUniqueId());
  85.        
  86.         ResultSet pData = sql.checkPlayer(plugin, player.getUniqueId().toString());
  87.         while (pData.next()) { Bukkit.broadcastMessage("UUID: " + pData.getString("mcUUID")); }
  88.        
  89.     }
  90. }
  91.  
  92. //Again, the SQL class would normally be in a different file but it has been included here for simplicity.
  93. package com.dibdibcraft.dBans;
  94. import java.sql.Connection;
  95. import java.sql.DriverManager;
  96. import java.sql.PreparedStatement;
  97. import java.sql.SQLException;
  98.  
  99. import org.bukkit.plugin.java.JavaPlugin;
  100. import org.bukkit.scheduler.BukkitRunnable;
  101.  
  102. import java.sql.ResultSet;
  103.  
  104. public class SQL {
  105.    
  106.     private String host = "dibdibguy.com";
  107.     private String username = "dibdibgu_mc";
  108.     private String db = "dibdibgu_ddc";
  109.     private int port = 3306;
  110.     private String password = "password";
  111.    
  112.     private Connection con;
  113.     private JavaPlugin plugin;
  114.    
  115.     public void connect() throws SQLException{     
  116.         this.con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db,
  117.                 username, password);
  118.     }
  119.    
  120.     @SuppressWarnings("unused")
  121.     public ResultSet checkPlayer(JavaPlugin plugin, String uuid) throws SQLException{
  122.         ResultSet data = (ResultSet) new BukkitRunnable(){
  123.             public ResultSet run(JavaPlugin plugin, String uuid) throws SQLException{
  124.                 if(!con.isValid(2)) { connect(); }
  125.                 String query = "SELECT * FROM `u_u_bans` WHERE `mcUUID`=? LIMIT 1";
  126.                 PreparedStatement stat = con.prepareStatement(query);
  127.                
  128.                 stat.setString(1, uuid);
  129.                
  130.                 return stat.executeQuery();
  131.             }
  132.  
  133.             @Override
  134.             public void run() {
  135.                 // TODO Auto-generated method stub
  136.                
  137.             }
  138.         }.runTaskAsynchronously(this.plugin);
  139.         return data;
  140.     }
  141.    
  142.     public void close(JavaPlugin plugin) throws SQLException{
  143.         this.con.close();
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement