Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.93 KB | None | 0 0
  1. package com.battlecraft.zonedabone.battlehomes.controllers;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import org.bukkit.Location;
  12. import org.bukkit.World;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.util.config.Configuration;
  15.  
  16. import com.battlecraft.zonedabone.battlehomes.BattleHomes;
  17.  
  18. public class MYSQLController {
  19.  
  20.     public Connection con;
  21.    
  22.     public boolean DEBUG;
  23.     public String URL;
  24.     public String PORT;
  25.     public String USERNAME;
  26.     public String PASSWORD;
  27.     public BattleHomes plugin;
  28.    
  29.     public MYSQLController(String url, String port, String username, String password, boolean debug, BattleHomes instance){
  30.         DEBUG = debug;
  31.         URL = url;
  32.         PORT = port;
  33.         USERNAME = username;
  34.         PASSWORD = password;
  35.         plugin = instance;
  36.         load();
  37.     }
  38.    
  39.  
  40.     public boolean load(){
  41.         try {
  42.             Class.forName("com.mysql.jdbc.Driver");
  43.             if (DEBUG) System.out.println("Got Driver");
  44.         } catch (ClassNotFoundException e1) {
  45.             System.err.println("Failed getting driver");
  46.             e1.printStackTrace();
  47.             return false;
  48.         }
  49.         String strStmt = "CREATE DATABASE IF NOT EXISTS BattleHomes";
  50.         try {
  51.             con = DriverManager.getConnection("jdbc:mysql://"+URL+":" + PORT, USERNAME,PASSWORD);
  52.             Statement st = con.createStatement();
  53.             st.executeUpdate(strStmt);
  54.             if (DEBUG) System.out.println("Creating db");
  55.         } catch (SQLException e) {
  56.             System.err.println("Failed creating db: "  + strStmt);
  57.             e.printStackTrace();
  58.             return false;
  59.         }
  60.         try {
  61.             con = DriverManager.getConnection("jdbc:mysql://"+URL+":" + PORT +"/BattleHomes", USERNAME,PASSWORD);
  62.         } catch (SQLException e1) {
  63.             e1.printStackTrace();
  64.             return false;
  65.         }
  66.        
  67.         createTable("desc Homes"
  68.                 , "CREATE TABLE Homes (Player char(32) NOT NULL, Number INTEGER UNSIGNED, World char(32) NOT NULL, X INTEGER SIGNED, Y FLOAT, Z INTEGER SIGNED, Yaw INTEGER SIGNED, Pitch INTEGER SIGNED, PRIMARY KEY (Player,Number))"
  69.                 ,"CREATE INDEX IDX_PLAYER_HOME on Homes (Player,Number)");
  70.         try {
  71.             con.setAutoCommit(false);
  72.         } catch (SQLException e) {
  73.             e.printStackTrace();
  74.         }
  75.  
  76.         if(plugin.config.getBoolean("imported", false)){return true;}
  77.        
  78.         System.out.println("IMPORTING OLD DATA");
  79.        
  80.         Configuration config = new Configuration(new File("plugins/BattleHomes/old.yml"));
  81.         config.load();
  82.        
  83.         for(String key:config.getKeys()){
  84.             System.out.println(key);
  85.             for(int i = 0;i<10;i++){
  86.                 if(config.getBoolean(key+".home_"+i+".exists", false)){
  87.                     int x = (int) ((int) config.getDouble(key+".home_"+i+".x", 0)-.5);
  88.                     int y = (int) config.getDouble(key+".home_"+i+".y", 0);
  89.                     int z = (int) ((int) config.getDouble(key+".home_"+i+".z", 0)-.5);
  90.                     String world = plugin.config.getString(key+".home_"+i+".world", "world");
  91.                     int yaw = config.getInt(key+".home_"+i+".yaw", 0);
  92.                     int pitch = config.getInt(key+".home_"+i+".pitch", 0);
  93.                     //Start duplicate code ftw!
  94.                     PreparedStatement ps;
  95.                     try {
  96.                         ps = con.prepareStatement("INSERT INTO Homes VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE World = ?, X = ?, Y = ?, Z = ?, Yaw = ?, Pitch = ?");
  97.                         ps.setString(1, key);
  98.                         ps.setInt(2, i);
  99.                         ps.setString(3, world);
  100.                         ps.setInt(4, x);
  101.                         ps.setFloat(5, y);
  102.                         ps.setInt(6, z);
  103.                         ps.setInt(7, yaw);
  104.                         ps.setInt(8, pitch);
  105.                         ps.setString(9, world);
  106.                         ps.setInt(10, x);
  107.                         ps.setFloat(11, y);
  108.                         ps.setInt(12, z);
  109.                         ps.setInt(13, yaw);
  110.                         ps.setInt(14, pitch);
  111.                         ps.execute();
  112.                         con.commit();
  113.                     } catch (SQLException e) {
  114.                         // TODO Auto-generated catch block
  115.                         e.printStackTrace();
  116.                         return false;
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.         plugin.config.setProperty("imported", true);
  122.        
  123.         return true;
  124.     }
  125.    
  126.     private boolean createTable(String sql_table_exists,
  127.             String sql_create_table,String sql_create_index) {
  128.         String strStmt;
  129.         strStmt = sql_table_exists;
  130.         /// Check to see if our table exists;
  131.         boolean table_exists = false;
  132.         try {
  133.             Statement st = con.createStatement();
  134.             st.executeUpdate(strStmt);
  135.             if (DEBUG) System.out.println("table exists");
  136.             table_exists = true;
  137.         } catch (SQLException e) {
  138.             if (DEBUG) System.out.println("table does not exist");
  139.         }
  140.         /// If the table exists nothing left to do
  141.         if (table_exists)
  142.             return true;
  143.         /// Create our table and index
  144.         strStmt = sql_create_table;
  145.         Statement st = null;
  146.         int result =0;
  147.         try {
  148.             st = con.createStatement();
  149.             result = st.executeUpdate(strStmt);
  150.             if (DEBUG) System.out.println("Created Table with stmt=" + strStmt);
  151.  
  152.             if (sql_create_index != null){
  153.                 try{
  154.                     st = con.createStatement();
  155.                     st.executeUpdate(sql_create_index);
  156.                     if (DEBUG) System.out.println("Created Index");            
  157.                 } catch (Exception e){
  158.                     if (DEBUG) System.err.println("Failed in creating Index");
  159.                     return false;
  160.                 }                  
  161.             }
  162.         } catch (SQLException e) {
  163.             if (DEBUG) System.err.println("Failed in creating Table " +
  164.                     strStmt + "   result=" + result);
  165.             e.printStackTrace();
  166.             return false;
  167.         }
  168.    
  169.         return true;
  170.     }
  171.    
  172.     public boolean setHome(String player, int number, Location loc){
  173.         try{
  174.             PreparedStatement ps = con.prepareStatement("INSERT INTO Homes VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE World = ?, X = ?, Y = ?, Z = ?, Yaw = ?, Pitch = ?");
  175.             ps.setString(1, player);
  176.             ps.setInt(2, number);
  177.             ps.setString(3, loc.getWorld().getName());
  178.             ps.setInt(4, (int) Math.round(loc.getX()-.5));
  179.             ps.setFloat(5, (int) loc.getY());
  180.             ps.setInt(6, (int) Math.round(loc.getZ()-.5));
  181.             ps.setInt(7, (int) Math.round(loc.getYaw()/10)*10);
  182.             ps.setInt(8, (int) Math.round(loc.getPitch()/10)*10);
  183.             ps.setString(9, loc.getWorld().getName());
  184.             ps.setInt(10, (int) Math.round(loc.getX()-.5));
  185.             ps.setFloat(11, (int) loc.getY());
  186.             ps.setInt(12, (int) Math.round(loc.getZ()-.5));
  187.             ps.setInt(13, (int) Math.round(loc.getYaw()/10)*10);
  188.             ps.setInt(14, (int) Math.round(loc.getPitch()/10)*10);
  189.             ps.execute();
  190.             con.commit();
  191.         }catch (SQLException e){
  192.             e.printStackTrace();
  193.             return false;
  194.         }
  195.         return true;
  196.     }
  197.    
  198.     public boolean setHome(Player player, int number){
  199.         return setHome(player.getName(),number,player.getLocation());
  200.     }
  201.    
  202.     public Object[] getHome(String player, int number){
  203.         try{
  204.             Statement ps = con.createStatement();
  205.             ResultSet rs = ps.executeQuery("select Player,World,X,Y,Z,Yaw,Pitch from Homes WHERE Player LIKE '"+player+"%' AND Number = "+number);
  206.             if(!rs.next()){return null;}
  207.             Location loc = new Location(plugin.getServer().getWorld(rs.getString("World")), ((float)rs.getInt("X"))+.5, rs.getFloat("Y"), ((float)rs.getInt("Z"))+.5, rs.getInt("Yaw"), rs.getInt("Pitch"));
  208.             String p = rs.getString("Player");
  209.             Object[] obj = new Object[2];
  210.             obj[0] = loc;
  211.             obj[1] = p;
  212.             return obj;
  213.         }catch (SQLException e){
  214.             e.printStackTrace();
  215.             return null;
  216.         }
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement