Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 28.15 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.logging.Level;
  9.  
  10. /**
  11.  * MySQLSource.java - Used for accessing users and such from a mysql database
  12.  * @author James
  13.  */
  14. public class MySQLSource extends DataSource {
  15.     private String groupst,userst,itemst,kitst,warpst,homest,reservelistt,whitelistt;
  16.     public void initialize() {
  17.         PropertiesFile properties = new PropertiesFile("mysql.properties");
  18.         groupst = properties.getString("groups", "groups");
  19.         userst = properties.getString("users", "users");
  20.         itemst = properties.getString("items", "items");
  21.         kitst = properties.getString("kits", "kits");
  22.         warpst = properties.getString("warps", "warps");
  23.         homest = properties.getString("homes", "homes");
  24.         reservelistt = properties.getString("reservelist", "reservelist");
  25.         whitelistt = properties.getString("whitelist", "whitelist");
  26.         loadGroups();
  27.         loadKits();
  28.         loadHomes();
  29.         loadWarps();
  30.         loadItems();
  31.         //loadBanList();
  32.     }
  33.  
  34.     public void loadGroups() {
  35.         synchronized (groupLock) {
  36.             Connection conn = null;
  37.             PreparedStatement ps = null;
  38.             ResultSet rs = null;
  39.             try {
  40.                 conn = etc.getSQLConnection();
  41.                 groups = new ArrayList<Group>();
  42.                 ps = conn.prepareStatement("SELECT * FROM "+groupst);
  43.                 rs = ps.executeQuery();
  44.                 while (rs.next()) {
  45.                     Group group = new Group();
  46.                     group.Administrator = rs.getBoolean("admin");
  47.                     group.CanModifyWorld = rs.getBoolean("canmodifyworld");
  48.                     group.Commands = rs.getString("commands").split(",");
  49.                     group.DefaultGroup = rs.getBoolean("defaultgroup");
  50.                     group.ID = rs.getInt("id");
  51.                     group.IgnoreRestrictions = rs.getBoolean("ignoresrestrictions");
  52.                     group.InheritedGroups = rs.getString("inheritedgroups").split(",");
  53.                     group.Name = rs.getString("name");
  54.                     group.Prefix = rs.getString("prefix");
  55.                     if (group.InheritedGroups.length == 1)
  56.                         if (group.InheritedGroups[0].equalsIgnoreCase(group.Name))
  57.                             group.InheritedGroups = new String[] { "" };
  58.                     groups.add(group);
  59.                 }
  60.             } catch (SQLException ex) {
  61.                 log.log(Level.SEVERE, "Unable to retreive groups from group table", ex);
  62.             } finally {
  63.                 try {
  64.                     if (ps != null) {
  65.                         ps.close();
  66.                     }
  67.                     if (rs != null) {
  68.                         rs.close();
  69.                     }
  70.                     if (conn != null) {
  71.                         conn.close();
  72.                     }
  73.                 } catch (SQLException ex) {
  74.                 }
  75.             }
  76.         }
  77.     }
  78.  
  79.     public void loadKits() {
  80.         synchronized (kitLock) {
  81.             Connection conn = null;
  82.             PreparedStatement ps = null;
  83.             ResultSet rs = null;
  84.             try {
  85.                 conn = etc.getSQLConnection();
  86.                 kits = new ArrayList<Kit>();
  87.                 ps = conn.prepareStatement("SELECT * FROM "+kitst);
  88.                 rs = ps.executeQuery();
  89.                 while (rs.next()) {
  90.                     Kit kit = new Kit();
  91.                     kit.Delay = rs.getInt("delay");
  92.                     kit.Group = rs.getString("group");
  93.                     kit.ID = rs.getInt("id");
  94.                     kit.Name = rs.getString("name");
  95.                     kit.IDs = new HashMap<String, Integer>();
  96.  
  97.                     String[] ids = rs.getString("items").split(",");
  98.                     for (String str : ids) {
  99.                         String id = "";
  100.                         int amount = 1;
  101.                         if (str.contains(" ")) {
  102.                             id = str.split(" ")[0];
  103.                             amount = Integer.parseInt(str.split(" ")[1]);
  104.                         } else {
  105.                             id = str;
  106.                         }
  107.                         kit.IDs.put(id, amount);
  108.                     }
  109.                     kits.add(kit);
  110.                 }
  111.             } catch (SQLException ex) {
  112.                 log.log(Level.SEVERE, "Unable to retreive kits from kit table", ex);
  113.             } finally {
  114.                 try {
  115.                     if (ps != null) {
  116.                         ps.close();
  117.                     }
  118.                     if (rs != null) {
  119.                         rs.close();
  120.                     }
  121.                     if (conn != null) {
  122.                         conn.close();
  123.                     }
  124.                 } catch (SQLException ex) {
  125.                 }
  126.             }
  127.         }
  128.     }
  129.  
  130.     public void loadHomes() {
  131.         synchronized (homeLock) {
  132.             if (!etc.getInstance().canSaveHomes()) {
  133.                 return;
  134.             }
  135.             Connection conn = null;
  136.             PreparedStatement ps = null;
  137.             ResultSet rs = null;
  138.             try {
  139.                 conn = etc.getSQLConnection();
  140.                 homes = new ArrayList<Warp>();
  141.                 ps = conn.prepareStatement("SELECT * FROM "+homest);
  142.                 rs = ps.executeQuery();
  143.                 while (rs.next()) {
  144.                     Location location = new Location();
  145.                     location.x = rs.getDouble("x");
  146.                     location.y = rs.getDouble("y");
  147.                     location.z = rs.getDouble("z");
  148.                     location.rotX = rs.getFloat("rotX");
  149.                     location.rotY = rs.getFloat("rotY");
  150.                     Warp home = new Warp();
  151.                     home.ID = rs.getInt("id");
  152.                     home.Location = location;
  153.                     home.Name = rs.getString("name");
  154.                     home.Group = rs.getString("group");
  155.                     homes.add(home);
  156.                 }
  157.             } catch (SQLException ex) {
  158.                 log.log(Level.SEVERE, "Unable to retreive homes from home table", ex);
  159.             } finally {
  160.                 try {
  161.                     if (ps != null) {
  162.                         ps.close();
  163.                     }
  164.                     if (rs != null) {
  165.                         rs.close();
  166.                     }
  167.                     if (conn != null) {
  168.                         conn.close();
  169.                     }
  170.                 } catch (SQLException ex) {
  171.                 }
  172.             }
  173.         }
  174.     }
  175.  
  176.     public void loadWarps() {
  177.         synchronized (warpLock) {
  178.             Connection conn = null;
  179.             PreparedStatement ps = null;
  180.             ResultSet rs = null;
  181.             try {
  182.                 conn = etc.getSQLConnection();
  183.                 warps = new ArrayList<Warp>();
  184.                 ps = conn.prepareStatement("SELECT * FROM "+warpst);
  185.                 rs = ps.executeQuery();
  186.                 while (rs.next()) {
  187.                     Location location = new Location();
  188.                     location.x = rs.getDouble("x");
  189.                     location.y = rs.getDouble("y");
  190.                     location.z = rs.getDouble("z");
  191.                     location.rotX = rs.getFloat("rotX");
  192.                     location.rotY = rs.getFloat("rotY");
  193.                     Warp warp = new Warp();
  194.                     warp.ID = rs.getInt("id");
  195.                     warp.Location = location;
  196.                     warp.Name = rs.getString("name");
  197.                     warp.Group = rs.getString("group");
  198.                     warps.add(warp);
  199.                 }
  200.             } catch (SQLException ex) {
  201.                 log.log(Level.SEVERE, "Unable to retreive warps from warp table", ex);
  202.             } finally {
  203.                 try {
  204.                     if (ps != null) {
  205.                         ps.close();
  206.                     }
  207.                     if (rs != null) {
  208.                         rs.close();
  209.                     }
  210.                     if (conn != null) {
  211.                         conn.close();
  212.                     }
  213.                 } catch (SQLException ex) {
  214.                 }
  215.             }
  216.         }
  217.     }
  218.  
  219.     public void loadItems() {
  220.         synchronized (itemLock) {
  221.             Connection conn = null;
  222.             PreparedStatement ps = null;
  223.             ResultSet rs = null;
  224.             try {
  225.                 conn = etc.getSQLConnection();
  226.                 items = new HashMap<String, Integer>();
  227.                 ps = conn.prepareStatement("SELECT * FROM "+itemst);
  228.                 rs = ps.executeQuery();
  229.                 while (rs.next()) {
  230.                     items.put(rs.getString("name"), rs.getInt("itemid"));
  231.                 }
  232.             } catch (SQLException ex) {
  233.                 log.log(Level.SEVERE, "Unable to retreive items from item table", ex);
  234.             } finally {
  235.                 try {
  236.                     if (ps != null) {
  237.                         ps.close();
  238.                     }
  239.                     if (rs != null) {
  240.                         rs.close();
  241.                     }
  242.                     if (conn != null) {
  243.                         conn.close();
  244.                     }
  245.                 } catch (SQLException ex) {
  246.                 }
  247.             }
  248.         }
  249.     }
  250.  
  251.     //Users
  252.     public void addPlayer(Player player) {
  253.         Connection conn = null;
  254.         PreparedStatement ps = null;
  255.         ResultSet rs = null;
  256.         try {
  257.             conn = etc.getSQLConnection();
  258.             ps = conn.prepareStatement("INSERT INTO "+userst+" (name, groups, prefix, commands, admin, canmodifyworld, ignoresrestrictions) VALUES (?,?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
  259.             ps.setString(1, player.getName());
  260.             ps.setString(2, etc.combineSplit(0, player.getGroups(), ","));
  261.             ps.setString(3, player.getPrefix());
  262.             ps.setString(4, etc.combineSplit(0, player.getCommands(), ","));
  263.             ps.setBoolean(5, player.getAdmin());
  264.             ps.setBoolean(6, player.canModifyWorld());
  265.             ps.setBoolean(7, player.ignoreRestrictions());
  266.             ps.executeUpdate();
  267.  
  268.             rs = ps.getGeneratedKeys();
  269.             if (rs.next()) {
  270.                 player.setSqlId(rs.getInt(1));
  271.             }
  272.         } catch (SQLException ex) {
  273.             log.log(Level.SEVERE, "Unable to insert user into users table", ex);
  274.         } finally {
  275.             try {
  276.                 if (ps != null) {
  277.                     ps.close();
  278.                 }
  279.                 if (rs != null) {
  280.                     rs.close();
  281.                 }
  282.                 if (conn != null) {
  283.                     conn.close();
  284.                 }
  285.             } catch (SQLException ex) {
  286.             }
  287.         }
  288.     }
  289.  
  290.     public void modifyPlayer(Player player) {
  291.         Connection conn = null;
  292.         PreparedStatement ps = null;
  293.         try {
  294.             conn = etc.getSQLConnection();
  295.             ps = conn.prepareStatement("UPDATE "+userst+" SET groups = ?, prefix = ?, commands = ?, admin = ?, canmodifyworld = ?, ignoresrestrictions = ? WHERE id = ?");
  296.             ps.setString(1, etc.combineSplit(0, player.getGroups(), ","));
  297.             ps.setString(2, player.getPrefix());
  298.             ps.setString(3, etc.combineSplit(0, player.getCommands(), ","));
  299.             ps.setBoolean(4, player.getAdmin());
  300.             ps.setBoolean(5, player.canModifyWorld());
  301.             ps.setBoolean(6, player.ignoreRestrictions());
  302.             ps.setInt(7, player.getSqlId());
  303.             ps.executeUpdate();
  304.         } catch (SQLException ex) {
  305.             log.log(Level.SEVERE, "Unable to update user in users table", ex);
  306.         } finally {
  307.             try {
  308.                 if (ps != null) {
  309.                     ps.close();
  310.                 }
  311.                 if (conn != null) {
  312.                     conn.close();
  313.                 }
  314.             } catch (SQLException ex) {
  315.             }
  316.         }
  317.     }
  318.  
  319.     public boolean doesPlayerExist(String player) {
  320.         boolean exists = false;
  321.         Connection conn = null;
  322.         PreparedStatement ps = null;
  323.         ResultSet rs = null;
  324.         try {
  325.             conn = etc.getSQLConnection();
  326.             ps = conn.prepareStatement("SELECT * FROM "+userst+" WHERE name = ?");
  327.             ps.setString(1, player);
  328.             rs = ps.executeQuery();
  329.             if (rs.next()) {
  330.                 exists = true;
  331.             }
  332.         } catch (SQLException ex) {
  333.             log.log(Level.SEVERE, "Unable to check if user exists", ex);
  334.         } finally {
  335.             try {
  336.                 if (ps != null) {
  337.                     ps.close();
  338.                 }
  339.                 if (rs != null) {
  340.                     rs.close();
  341.                 }
  342.                 if (conn != null) {
  343.                     conn.close();
  344.                 }
  345.             } catch (SQLException ex) {
  346.             }
  347.         }
  348.         return exists;
  349.     }
  350.  
  351.     //Groups
  352.     public void addGroup(Group group) {
  353.         throw new UnsupportedOperationException("Not supported yet.");
  354.     }
  355.  
  356.     public void modifyGroup(Group group) {
  357.         throw new UnsupportedOperationException("Not supported yet.");
  358.     }
  359.  
  360.     //Kits
  361.     public void addKit(Kit kit) {
  362.         throw new UnsupportedOperationException("Not supported yet.");
  363.     }
  364.  
  365.     public void modifyKit(Kit kit) {
  366.         throw new UnsupportedOperationException("Not supported yet.");
  367.     }
  368.  
  369.     //Homes
  370.     public void addHome(Warp home) {
  371.         Connection conn = null;
  372.         PreparedStatement ps = null;
  373.         ResultSet rs = null;
  374.         try {
  375.             conn = etc.getSQLConnection();
  376.             ps = conn.prepareStatement("INSERT INTO "+homest+" (name, x, y, z, rotX, rotY, `group`) VALUES(?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
  377.             ps.setString(1, home.Name);
  378.             ps.setDouble(2, home.Location.x);
  379.             ps.setDouble(3, home.Location.y);
  380.             ps.setDouble(4, home.Location.z);
  381.             ps.setFloat(5, home.Location.rotX);
  382.             ps.setFloat(6, home.Location.rotY);
  383.             ps.setString(7, home.Group);
  384.             ps.executeUpdate();
  385.  
  386.             rs = ps.getGeneratedKeys();
  387.             if (rs.next()) {
  388.                 home.ID = rs.getInt(1);
  389.                 synchronized (homeLock) {
  390.                     homes.add(home);
  391.                 }
  392.             }
  393.         } catch (SQLException ex) {
  394.             log.log(Level.SEVERE, "Unable to insert home into homes table", ex);
  395.         } finally {
  396.             try {
  397.                 if (ps != null) {
  398.                     ps.close();
  399.                 }
  400.                 if (rs != null) {
  401.                     rs.close();
  402.                 }
  403.                 if (conn != null) {
  404.                     conn.close();
  405.                 }
  406.             } catch (SQLException ex) {
  407.             }
  408.         }
  409.     }
  410.  
  411.     public void changeHome(Warp home) {
  412.         Connection conn = null;
  413.         PreparedStatement ps = null;
  414.         try {
  415.             conn = etc.getSQLConnection();
  416.             ps = conn.prepareStatement("UPDATE "+homest+" SET x = ?, y = ?, z = ?, rotX = ?, rotY = ?, `group` = ? WHERE name = ?");
  417.             ps.setDouble(1, home.Location.x);
  418.             ps.setDouble(2, home.Location.y);
  419.             ps.setDouble(3, home.Location.z);
  420.             ps.setFloat(4, home.Location.rotX);
  421.             ps.setFloat(5, home.Location.rotY);
  422.             ps.setString(6, home.Group);
  423.             ps.setString(7, home.Name);
  424.             ps.executeUpdate();
  425.  
  426.             synchronized (homeLock) {
  427.                 Warp toRem = null;
  428.                 for (Warp h : homes) {
  429.                     if (h.Name.equalsIgnoreCase(home.Name)) {
  430.                         toRem = h;
  431.                     }
  432.                 }
  433.                 if (toRem != null) {
  434.                     homes.remove(toRem);
  435.                 }
  436.                 homes.add(home);
  437.             }
  438.         } catch (SQLException ex) {
  439.             log.log(Level.SEVERE, "Unable to update home in homes table", ex);
  440.         } finally {
  441.             try {
  442.                 if (ps != null) {
  443.                     ps.close();
  444.                 }
  445.                 if (conn != null) {
  446.                     conn.close();
  447.                 }
  448.             } catch (SQLException ex) {
  449.             }
  450.         }
  451.     }
  452.  
  453.     //Warps
  454.     public void addWarp(Warp warp) {
  455.         Connection conn = null;
  456.         PreparedStatement ps = null;
  457.         ResultSet rs = null;
  458.         try {
  459.             conn = etc.getSQLConnection();
  460.             ps = conn.prepareStatement("INSERT INTO "+warpst+" (name, x, y, z, rotX, rotY, `group`) VALUES(?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
  461.             ps.setString(1, warp.Name);
  462.             ps.setDouble(2, warp.Location.x);
  463.             ps.setDouble(3, warp.Location.y);
  464.             ps.setDouble(4, warp.Location.z);
  465.             ps.setFloat(5, warp.Location.rotX);
  466.             ps.setFloat(6, warp.Location.rotY);
  467.             ps.setString(7, warp.Group);
  468.             ps.executeUpdate();
  469.  
  470.             rs = ps.getGeneratedKeys();
  471.             if (rs.next()) {
  472.                 warp.ID = rs.getInt(1);
  473.                 synchronized (warpLock) {
  474.                     warps.add(warp);
  475.                 }
  476.             }
  477.         } catch (SQLException ex) {
  478.             log.log(Level.SEVERE, "Unable to insert warp into warps table", ex);
  479.         } finally {
  480.             try {
  481.                 if (ps != null) {
  482.                     ps.close();
  483.                 }
  484.                 if (rs != null) {
  485.                     rs.close();
  486.                 }
  487.                 if (conn != null) {
  488.                     conn.close();
  489.                 }
  490.             } catch (SQLException ex) {
  491.             }
  492.         }
  493.     }
  494.  
  495.     public void changeWarp(Warp warp) {
  496.         Connection conn = null;
  497.         PreparedStatement ps = null;
  498.         try {
  499.             conn = etc.getSQLConnection();
  500.             ps = conn.prepareStatement("UPDATE "+warpst+" SET x = ?, y = ?, z = ?, rotX = ?, rotY = ?, `group` = ? WHERE name = ?");
  501.             ps.setDouble(1, warp.Location.x);
  502.             ps.setDouble(2, warp.Location.y);
  503.             ps.setDouble(3, warp.Location.z);
  504.             ps.setFloat(4, warp.Location.rotX);
  505.             ps.setFloat(5, warp.Location.rotY);
  506.             ps.setString(6, warp.Group);
  507.             ps.setString(7, warp.Name);
  508.             ps.executeUpdate();
  509.  
  510.             synchronized (warpLock) {
  511.                 Warp toRem = null;
  512.                 for (Warp h : warps) {
  513.                     if (h.Name.equalsIgnoreCase(warp.Name)) {
  514.                         toRem = h;
  515.                     }
  516.                 }
  517.                 if (toRem != null) {
  518.                     warps.remove(toRem);
  519.                 }
  520.                 warps.add(warp);
  521.             }
  522.         } catch (SQLException ex) {
  523.             log.log(Level.SEVERE, "Unable to update warp in warps table", ex);
  524.         } finally {
  525.             try {
  526.                 if (ps != null) {
  527.                     ps.close();
  528.                 }
  529.                 if (conn != null) {
  530.                     conn.close();
  531.                 }
  532.             } catch (SQLException ex) {
  533.             }
  534.         }
  535.     }
  536.  
  537.     public void removeWarp(Warp warp) {
  538.         Connection conn = null;
  539.         PreparedStatement ps = null;
  540.         try {
  541.             conn = etc.getSQLConnection();
  542.             ps = conn.prepareStatement("DELETE FROM "+warpst+" WHERE id = ?");
  543.             ps.setDouble(1, warp.ID);
  544.             ps.executeUpdate();
  545.         } catch (SQLException ex) {
  546.             log.log(Level.SEVERE, "Unable to delete warp from warps table", ex);
  547.         } finally {
  548.             try {
  549.                 if (ps != null) {
  550.                     ps.close();
  551.                 }
  552.                 if (conn != null) {
  553.                     conn.close();
  554.                 }
  555.             } catch (SQLException ex) {
  556.             }
  557.         }
  558.         synchronized (warpLock) {
  559.             warps.remove(warp);
  560.         }
  561.     }
  562.  
  563.     //Whitelist
  564.     public void addToWhitelist(String name) {
  565.         if (isUserOnWhitelist(name))
  566.             return;
  567.        
  568.         Connection conn = null;
  569.         PreparedStatement ps = null;
  570.         try {
  571.             conn = etc.getSQLConnection();
  572.             ps = conn.prepareStatement("INSERT INTO "+whitelistt+" VALUES(?)");
  573.             ps.setString(1, name);
  574.             ps.executeUpdate();
  575.         } catch (SQLException ex) {
  576.             log.log(Level.SEVERE, "Unable to update whitelist", ex);
  577.         } finally {
  578.             try {
  579.                 if (ps != null) {
  580.                     ps.close();
  581.                 }
  582.                 if (conn != null) {
  583.                     conn.close();
  584.                 }
  585.             } catch (SQLException ex) {
  586.             }
  587.         }
  588.     }
  589.  
  590.     public void removeFromWhitelist(String name) {
  591.         if (!isUserOnWhitelist(name))
  592.             return;
  593.        
  594.         Connection conn = null;
  595.         PreparedStatement ps = null;
  596.         try {
  597.             conn = etc.getSQLConnection();
  598.             ps = conn.prepareStatement("DELETE FROM "+whitelistt+" WHERE name = ?");
  599.             ps.setString(1, name);
  600.             ps.executeUpdate();
  601.         } catch (SQLException ex) {
  602.             log.log(Level.SEVERE, "Unable to update whitelist", ex);
  603.         } finally {
  604.             try {
  605.                 if (ps != null) {
  606.                     ps.close();
  607.                 }
  608.                 if (conn != null) {
  609.                     conn.close();
  610.                 }
  611.             } catch (SQLException ex) {
  612.             }
  613.         }
  614.     }
  615.  
  616.     //Reservelist
  617.     public void addToReserveList(String name) {
  618.         if (isUserOnReserveList(name))
  619.             return;
  620.        
  621.         Connection conn = null;
  622.         PreparedStatement ps = null;
  623.         try {
  624.             conn = etc.getSQLConnection();
  625.             ps = conn.prepareStatement("INSERT INTO "+reservelistt+" VALUES(?)");
  626.             ps.setString(1, name);
  627.             ps.executeUpdate();
  628.         } catch (SQLException ex) {
  629.             log.log(Level.SEVERE, "Unable to update reservelist", ex);
  630.         } finally {
  631.             try {
  632.                 if (ps != null) {
  633.                     ps.close();
  634.                 }
  635.                 if (conn != null) {
  636.                     conn.close();
  637.                 }
  638.             } catch (SQLException ex) {
  639.             }
  640.         }
  641.     }
  642.  
  643.     public void removeFromReserveList(String name) {
  644.         if (!isUserOnReserveList(name))
  645.             return;
  646.        
  647.         Connection conn = null;
  648.         PreparedStatement ps = null;
  649.         try {
  650.             conn = etc.getSQLConnection();
  651.             ps = conn.prepareStatement("DELETE FROM "+reservelistt+" WHERE name = ?");
  652.             ps.setString(1, name);
  653.             ps.executeUpdate();
  654.         } catch (SQLException ex) {
  655.             log.log(Level.SEVERE, "Unable to update reservelist", ex);
  656.         } finally {
  657.             try {
  658.                 if (ps != null) {
  659.                     ps.close();
  660.                 }
  661.                 if (conn != null) {
  662.                     conn.close();
  663.                 }
  664.             } catch (SQLException ex) {
  665.             }
  666.         }
  667.     }
  668.  
  669.     public Player getPlayer(String name) {
  670.         Player player = new Player();
  671.         Connection conn = null;
  672.         PreparedStatement ps = null;
  673.         ResultSet rs = null;
  674.         try {
  675.             conn = etc.getSQLConnection();
  676.             ps = conn.prepareStatement("SELECT * FROM "+userst+" WHERE name = ?");
  677.             ps.setString(1, name);
  678.             rs = ps.executeQuery();
  679.             if (rs.next()) {
  680.                 player.setSqlId(rs.getInt("id"));
  681.                 player.setGroups(rs.getString("groups").split(","));
  682.                 player.setCommands(rs.getString("commands").split(","));
  683.                 player.setPrefix(rs.getString("prefix"));
  684.                 player.setAdmin(rs.getBoolean("admin"));
  685.                 player.setCanModifyWorld(rs.getBoolean("canmodifyworld"));
  686.                 player.setIgnoreRestrictions(rs.getBoolean("ignoresrestrictions"));
  687.                 player.setIps(rs.getString("ip").split(","));
  688.             }
  689.         } catch (SQLException ex) {
  690.             log.log(Level.SEVERE, "Unable to retreive users from user table", ex);
  691.         } finally {
  692.             try {
  693.                 if (ps != null) {
  694.                     ps.close();
  695.                 }
  696.                 if (rs != null) {
  697.                     rs.close();
  698.                 }
  699.                 if (conn != null) {
  700.                     conn.close();
  701.                 }
  702.             } catch (SQLException ex) {
  703.             }
  704.         }
  705.         return player;
  706.     }
  707.    
  708.     public void loadBanList() {
  709.         synchronized (banLock) {
  710.             bans = new ArrayList<Ban>();
  711.             Connection conn = null;
  712.             PreparedStatement ps = null;
  713.             ResultSet rs = null;
  714.             try {
  715.                 conn = etc.getSQLConnection();
  716.                 ps = conn.prepareStatement("SELECT * FROM bans");
  717.                 rs = ps.executeQuery();
  718.                 while (rs.next()) {
  719.                     Ban ban = new Ban();
  720.                     ban.setName(rs.getString("name"));
  721.                     ban.setIp(rs.getString("ip"));
  722.                     ban.setReason(rs.getString("reason"));
  723.                     ban.setTimestamp(rs.getInt("length"));
  724.                     bans.add(ban);
  725.                 }
  726.             } catch (SQLException ex) {
  727.                 log.log(Level.SEVERE, "Unable to retreive bans from ban table", ex);
  728.             } finally {
  729.                 try {
  730.                     if (ps != null) {
  731.                         ps.close();
  732.                     }
  733.                     if (rs != null) {
  734.                         rs.close();
  735.                     }
  736.                     if (conn != null) {
  737.                         conn.close();
  738.                     }
  739.                 } catch (SQLException ex) {
  740.                 }
  741.             }
  742.         }
  743.     }
  744.  
  745.     public boolean isUserOnWhitelist(String user) {
  746.         boolean toRet = false;
  747.         Connection conn = null;
  748.         PreparedStatement ps = null;
  749.         ResultSet rs = null;
  750.         try {
  751.             conn = etc.getSQLConnection();
  752.             ps = conn.prepareStatement("SELECT * FROM "+whitelistt+" WHERE name = ?");
  753.             ps.setString(1, user);
  754.             rs = ps.executeQuery();
  755.             if (rs.next()) {
  756.                 toRet = true;
  757.             }
  758.         } catch (SQLException ex) {
  759.             log.log(Level.SEVERE, "Unable to check if user is on whitelist", ex);
  760.         } finally {
  761.             try {
  762.                 if (ps != null) {
  763.                     ps.close();
  764.                 }
  765.                 if (rs != null) {
  766.                     rs.close();
  767.                 }
  768.                 if (conn != null) {
  769.                     conn.close();
  770.                 }
  771.             } catch (SQLException ex) {
  772.             }
  773.         }
  774.         return toRet;
  775.     }
  776.  
  777.     public boolean isUserOnReserveList(String user) {
  778.         boolean toRet = false;
  779.         Connection conn = null;
  780.         PreparedStatement ps = null;
  781.         ResultSet rs = null;
  782.         try {
  783.             conn = etc.getSQLConnection();
  784.             ps = conn.prepareStatement("SELECT * FROM "+reservelistt+" WHERE name = ?");
  785.             ps.setString(1, user);
  786.             rs = ps.executeQuery();
  787.             if (rs.next()) {
  788.                 toRet = true;
  789.             }
  790.         } catch (SQLException ex) {
  791.             log.log(Level.SEVERE, "Unable to check if user is on reservelist", ex);
  792.         } finally {
  793.             try {
  794.                 if (ps != null) {
  795.                     ps.close();
  796.                 }
  797.                 if (rs != null) {
  798.                     rs.close();
  799.                 }
  800.                 if (conn != null) {
  801.                     conn.close();
  802.                 }
  803.             } catch (SQLException ex) {
  804.             }
  805.         }
  806.         return toRet;
  807.     }
  808.    
  809.     public void modifyBan(Ban ban) {
  810.         throw new UnsupportedOperationException("Not supported yet.");
  811.     }
  812. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement