agentsix1

nothing to see here 3

May 13th, 2017
39,502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.70 KB | None | 0 0
  1. package me.jeremy.ifp;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12.  
  13. import me.jeremy.ifp.listener;
  14.  
  15. import org.bukkit.Material;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.event.Listener;
  18. import org.bukkit.inventory.ItemStack;
  19.  
  20. public class database implements Listener {
  21.     static main plugin;
  22.     static Connection con = null;
  23.     static Statement st = null;
  24.     static ResultSet rs = null;
  25.     static String username = null;
  26.     static String password = null;
  27.     static String url = null;
  28.     static PreparedStatement prep = null;
  29.     public database(main main) {
  30.     plugin = main;
  31.     }
  32.    
  33.     public static void quietConnect() {
  34.         try {
  35.             try {
  36.                 con.close();
  37.                 con = DriverManager.getConnection(url, username, password);
  38.             } catch (SQLException e) {}
  39.             con = DriverManager.getConnection(url, username, password);
  40.         } catch (SQLException e) {
  41.             System.out.println(e.getMessage() + " (Quiet Connect)");
  42.         }
  43.     }
  44.    
  45.     public static void connect() {
  46.         username = plugin.getConfig().getString("Database.Username");
  47.         password = plugin.getConfig().getString("Database.Password");;
  48.         url = "jdbc:mysql://"+ plugin.getConfig().getString("Database.Host") + ":" + plugin.getConfig().getString("Database.Port") + "/" + plugin.getConfig().getString("Database.Database");
  49.         try {
  50.             con = DriverManager.getConnection(url, username, password);
  51.             quietConnect();
  52.         } catch (SQLException e) {
  53.             // TODO Auto-generated catch block
  54.             e.printStackTrace();
  55.         }  
  56.         try {
  57.             //con = DriverManager.getConnection(url, user, pass);
  58.              st = con.createStatement();
  59.             try {
  60.                   String table =
  61.                           "CREATE TABLE players " +
  62.                                    "(id INTEGER NOT NULL AUTO_INCREMENT, " +
  63.                                    " state TINYINT(1), " +
  64.                                    " username VARCHAR(16), " +
  65.                                    " uuid VARCHAR(50), " +
  66.                                    " filter VARCHAR(10000), " +
  67.                                    " PRIMARY KEY ( id ))";
  68.                   prep = con.prepareStatement(table);
  69.                   prep.executeUpdate();
  70.                   //st.executeUpdate(table);
  71.                   System.out.println("[ItemFilterPickup] Database Table Created! - player");
  72.              }
  73.              catch(SQLException s){
  74.                  System.out.println("[ItemFilterPickup] Database Table Exist! - players");
  75.              }
  76.          
  77.             System.out.println("[ItemFilterPickup] Success! Connected To Database!");
  78.         } catch (SQLException e) {
  79.             e.printStackTrace();
  80.         }finally {
  81.             try {
  82.               if (rs != null) {
  83.                  
  84.                   rs.close();
  85.                 }
  86.    
  87.               if (prep != null) {
  88.                   prep.close();
  89.                 }
  90.               if (con != null) {
  91.                   con.close();
  92.                 }
  93.             } catch (SQLException ex) {
  94.                 // TODO Auto-generated catch block
  95.                 ex.printStackTrace();
  96.             }
  97.         }
  98.     }
  99.    
  100.    
  101.    
  102.    
  103.    
  104.     public static boolean checkDatabase(Player p, ItemStack itemA) {
  105.        
  106.             try {
  107.                 quietConnect();
  108.                 if (p.hasPermission("itemfilterpickup.user") || p.hasPermission("itemfilterpickup.user.canfilter")) {
  109.                     prep = con.prepareStatement("SELECT state, filter FROM players WHERE uuid = ?");
  110.                     prep.setString(1, p.getUniqueId().toString());
  111.                     rs = prep.executeQuery();
  112.                     if (rs.next()) {
  113.                         if (rs.getInt("state") == 1) {
  114.                                 int b = 1;
  115.                                 int max = listener.getMaxFilter(p);
  116.                                 for (String filter : rs.getString("filter").split(";")) {
  117.                                     if (max == -1) {
  118.                                         ItemStack a = new ItemStack(Material.matchMaterial(filter.split(":")[0]), itemA.getAmount(), (byte) Integer.parseInt(filter.split(":")[1]));
  119.                                         if (itemA.equals(a)) {
  120.                                             return true;
  121.                                         }
  122.                                         b++;
  123.                                     } else if (b <= max && max != -1) {
  124.                                         ItemStack a = new ItemStack(Material.matchMaterial(filter.split(":")[0]), itemA.getAmount(), (byte) Integer.parseInt(filter.split(":")[1]));
  125.                                         if (itemA.equals(a)) {
  126.                                             return true;
  127.                                         }
  128.                                         b++;
  129.                                     } else {
  130.                                         break;
  131.                                     }
  132.                                 }
  133.                                 return false;
  134.                             } else {
  135.                                 return false;
  136.                             }
  137.                         } else {
  138.                             return false;
  139.                         }
  140.                 } else {
  141.                     return false;
  142.                 }
  143.            
  144.         } catch (SQLException ex) {
  145.             System.out.println(ex.getMessage());
  146.  
  147.         }finally {
  148.             try {
  149.                   if (rs != null) {
  150.                      
  151.                       rs.close();
  152.                     }
  153.  
  154.                   if (prep != null) {
  155.                       prep.close();
  156.                     }
  157.                   if (con != null) {
  158.                       con.close();
  159.                     }
  160.             } catch (SQLException e) {
  161.                 // TODO Auto-generated catch block
  162.                 e.printStackTrace();
  163.             }
  164.            
  165.         }
  166.             return false;
  167.     }
  168.  
  169.     public static void setState(Player p, int state) {
  170.         try {
  171.             quietConnect();
  172.             prep = con.prepareStatement("UPDATE players SET state = ?, username = ? WHERE uuid = ?");
  173.             prep.setInt(1, state);
  174.             prep.setString(2, p.getName());
  175.             prep.setString(3, p.getUniqueId().toString());
  176.             prep.executeUpdate();
  177.         } catch (SQLException ex) {
  178.             System.out.println(ex.getMessage());
  179.    
  180.         }finally {
  181.             try {
  182.                   if (rs != null) {
  183.                      
  184.                       rs.close();
  185.                     }
  186.    
  187.                   if (prep != null) {
  188.                       prep.close();
  189.                     }
  190.                   if (con != null) {
  191.                       con.close();
  192.                     }
  193.             } catch (SQLException e) {
  194.                 // TODO Auto-generated catch block
  195.                 e.printStackTrace();
  196.             }
  197.            
  198.         }
  199.     }
  200.  
  201.     public static void attemptAddorUpdate(Player p) {
  202.         try {
  203.             quietConnect();
  204.             prep = con.prepareStatement("SELECT state, filter FROM players WHERE uuid = ?");
  205.             prep.setString(1, p.getUniqueId().toString());
  206.             rs = prep.executeQuery();
  207.             if (!rs.next()) {
  208.                 prep = con.prepareStatement("INSERT INTO players (id, state, username, uuid, filter) VALUES (NULL, 0, ?, ?, ?)");
  209.                 prep.setString(1, p.getName());
  210.                 prep.setString(2, p.getUniqueId().toString());
  211.                 prep.setString(3, "");
  212.                 prep.executeUpdate();
  213.             }
  214.         } catch (SQLException e) {
  215.             e.printStackTrace();
  216.         }finally {
  217.             try {
  218.               if (rs != null) {
  219.                  
  220.                   rs.close();
  221.                 }
  222.    
  223.               if (prep != null) {
  224.                   prep.close();
  225.                 }
  226.               if (con != null) {
  227.                   con.close();
  228.                 }
  229.             } catch (SQLException ex) {
  230.                 // TODO Auto-generated catch block
  231.                 ex.printStackTrace();
  232.             }
  233.         }
  234.     }
  235.  
  236.     public static boolean getState(Player p) {
  237.     try {
  238.             quietConnect();
  239.             prep = con.prepareStatement("SELECT state FROM players WHERE uuid = ?");
  240.             prep.setString(1, p.getUniqueId().toString());
  241.             rs = prep.executeQuery();
  242.             if (rs.next()) {
  243.                 if (rs.getInt("state") == 0) {
  244.                     return false;
  245.                 } else {
  246.                     return true;
  247.                 }
  248.             }
  249.     } catch (SQLException e) {
  250.         e.printStackTrace();
  251.     }finally {
  252.         try {
  253.           if (rs != null) {
  254.              
  255.               rs.close();
  256.             }
  257.  
  258.           if (prep != null) {
  259.               prep.close();
  260.             }
  261.           if (con != null) {
  262.               con.close();
  263.             }
  264.         } catch (SQLException ex) {
  265.             // TODO Auto-generated catch block
  266.             ex.printStackTrace();
  267.         }
  268.     }
  269.         return false;
  270.     }
  271.  
  272.     public static boolean addToFilter(Player p, String string) {
  273.         try {
  274.             String filterOut = "";
  275.             quietConnect();
  276.             prep = con.prepareStatement("SELECT state, filter FROM players WHERE uuid = ?");
  277.             prep.setString(1, p.getUniqueId().toString());
  278.             rs = prep.executeQuery();
  279.             if (rs.next()) {
  280.                 prep = con.prepareStatement("UPDATE players SET filter = ?, username = ? WHERE uuid = ?");
  281.                 if (rs.getString("filter").equalsIgnoreCase("")) {
  282.                     filterOut = string;
  283.                 } else {
  284.                     if (rs.getString("filter").contains(string)) {
  285.                         return false;
  286.                     } else {
  287.                         filterOut = rs.getString("filter") + ";" + string;
  288.                        
  289.                     }
  290.                 }
  291.                 prep.setString(1, filterOut);
  292.                 prep.setString(2, p.getName());
  293.                 prep.setString(3, p.getUniqueId().toString());
  294.                 prep.executeUpdate();
  295.                 return true;
  296.             }
  297.         } catch (SQLException e) {
  298.             e.printStackTrace();
  299.         }finally {
  300.             try {
  301.               if (rs != null) {
  302.                  
  303.                   rs.close();
  304.                 }
  305.    
  306.               if (prep != null) {
  307.                   prep.close();
  308.                 }
  309.               if (con != null) {
  310.                   con.close();
  311.                 }
  312.             } catch (SQLException ex) {
  313.                 // TODO Auto-generated catch block
  314.                 ex.printStackTrace();
  315.             }
  316.         }
  317.         return false;
  318.     }
  319.  
  320.     public static boolean removeItem(Player p, String string) {
  321.         try {
  322.             quietConnect();
  323.             prep = con.prepareStatement("SELECT filter FROM players WHERE uuid = ?");
  324.             prep.setString(1, p.getUniqueId().toString());
  325.             rs = prep.executeQuery();
  326.             if (rs.next()) {
  327.                 prep = con.prepareStatement("UPDATE players SET filter = ?, username = ? WHERE uuid = ?");
  328.                 boolean found = false;
  329.                 if (rs.getString("filter").equalsIgnoreCase("")) {
  330.                     return false;
  331.                 } else {
  332.                     if (rs.getString("filter").contains(";")) {
  333.                         String output = "";
  334.                         for (String item : rs.getString("filter").split(";")) {
  335.                             if (!item.equals(string)) {
  336.                                 if (output.equals("")) {
  337.                                     output = item;
  338.                                 } else {
  339.                                     output = output + ";" + item;
  340.                                 }
  341.                             } else { found = true; }
  342.                         }
  343.                         prep.setString(1, output);
  344.                         prep.setString(2, p.getName());
  345.                         prep.setString(3, p.getUniqueId().toString());
  346.                         prep.executeUpdate();
  347.                         return found;
  348.                     }  else {
  349.                         if (rs.getString("filter").equals(string)) {
  350.                             prep.setString(1, "");
  351.                             prep.setString(2, p.getName());
  352.                             prep.setString(3, p.getUniqueId().toString());
  353.                             prep.executeUpdate();
  354.                             return true;
  355.                         }
  356.                     }
  357.                 }
  358.                
  359.             }
  360.         } catch (SQLException e) {
  361.             e.printStackTrace();
  362.         }finally {
  363.             try {
  364.               if (rs != null) {
  365.                  
  366.                   rs.close();
  367.                 }
  368.    
  369.               if (prep != null) {
  370.                   prep.close();
  371.                 }
  372.               if (con != null) {
  373.                   con.close();
  374.                 }
  375.             } catch (SQLException ex) {
  376.                 // TODO Auto-generated catch block
  377.                 ex.printStackTrace();
  378.             }
  379.         }
  380.         return false;
  381.     }
  382.  
  383.     public static List<String> getFilterList(Player p) {
  384.         try {
  385.             quietConnect();
  386.             prep = con.prepareStatement("SELECT filter FROM players WHERE uuid = ?");
  387.             prep.setString(1, p.getUniqueId().toString());
  388.             rs = prep.executeQuery();
  389.             if (rs.next()) {
  390.                 if (rs.getString("filter").contains(";")) {
  391.                     List<String> filter = Arrays.asList(rs.getString("filter").split(";"));
  392.                     return filter;
  393.                 } else {
  394.                     List<String> filter = new ArrayList<String>();
  395.                     filter.add(rs.getString("filter"));
  396.                     return filter;
  397.                 }
  398.                
  399.             }
  400.                
  401.         } catch (SQLException e) {
  402.             e.printStackTrace();
  403.         }finally {
  404.             try {
  405.               if (rs != null) {
  406.                  
  407.                   rs.close();
  408.                 }
  409.    
  410.               if (prep != null) {
  411.                   prep.close();
  412.                 }
  413.               if (con != null) {
  414.                   con.close();
  415.                 }
  416.             } catch (SQLException ex) {
  417.                 // TODO Auto-generated catch block
  418.                 ex.printStackTrace();
  419.             }
  420.         }
  421.         return new ArrayList<String>();
  422.     }
  423. }
Advertisement
Add Comment
Please, Sign In to add comment