public static void setOwner(Player p, String clanName) { saveClans(); if (clanExists(clanName)) { if (!(MySQL.mySQLenabled())) { ClansFile.getData().set("Clans." + getClanNumber(clanName) + ".Owner", p.getUniqueId()); ClansFile.saveData(); ClansFile.reloadData(); loadClans(); } else { try { Connection conn = MySQL.getConnection(); String INSERT = "INSERT INTO CODClans (clan, owner) VALUES(?, ?) ON DUPLICATE KEY UPDATE owner=?"; PreparedStatement ps = conn.prepareStatement(INSERT); ps.setString(1, clanName); ps.setString(2, p.getUniqueId().toString()); ps.setString(3, p.getUniqueId().toString()); ps.executeUpdate(); ps.close(); conn.close(); loadClans(); } catch (Exception e) { e.printStackTrace(); } } } }
public static boolean clanExists(String clan) { if (!(MySQL.mySQLenabled())) { ArrayList<String> c = new ArrayList<String>(); ClansFile.saveData(); ClansFile.reloadData(); int i = 0; while (ClansFile.getData().get("Clans." + i) != null) { c.add(ClansFile.getData().getString("Clans." + i + ".Name")); i++; } if (c.isEmpty()) { return false; } if (c.contains(clan)) { return true; } return false; } else { try { Connection conn = MySQL.getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT clan FROM CODClans"); ResultSet rs = ps.executeQuery(); while (rs.next()) { if (rs.getString("clan").equals(clan)) return true; } rs.close(); ps.close(); conn.close(); return false; } catch (Exception e) { e.printStackTrace(); } return false; } }
public static void saveClans() { if (!(MySQL.mySQLenabled())) { HashMap<Integer, String> Owner = new HashMap<Integer, String>(); int next = 0; while (ClansFile.getData().get("Clans." + next) != null) { Owner.put(next, ClansFile.getData().getString("Clans." + next + ".Owner")); next++; } ClansFile.getData().set("Clans", null); ClansFile.saveData(); ClansFile.reloadData(); for (int i = 0; i < clans.size(); i++) { ClansFile.getData().set("Clans." + i + ".Name", clans.get(i)); if (clan.get(clans.get(i)) != null) { ClansFile.getData().set(("Clans." + i + ".Players"), clan.get(clans.get(i))); } if (admins.get(clans.get(i)) != null) { ClansFile.getData().set("Clans." + i + ".Admins", admins.get(clans.get(i))); } ClansFile.getData().set(("Clans." + i + ".Owner"), Owner.get(i)); } ClansFile.saveData(); ClansFile.reloadData(); loadClans(); } else { try { HashMap<String, String> Owner = new HashMap<String, String>(); Connection conn = MySQL.getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT clan,owner FROM CODClans"); ResultSet rs = ps.executeQuery(); while (rs.next()) { Owner.put(rs.getString("clan"), rs.getString("owner")); } ps = conn.prepareStatement("DELETE FROM CODClans"); ps.executeUpdate(); for (int i = 0; i < clans.size(); i++) { String INSERT = "INSERT INTO CODClans (clan, owner, players, admins) VALUES(?, ?, ?, ?) ON DUPLICATE KEY UPDATE owner=?,players=?,admins=?"; ps = conn.prepareStatement(INSERT); ps.setString(1, clans.get(i)); if (clan.get(clans.get(i)) != null) { Clob clob = MySQL.stringToClob(MySQL.listToString(clan.get(clans.get(i))), conn); ps.setClob(3, clob); ps.setClob(6, clob); } else { Clob clob = MySQL.stringToClob(MySQL.listToString(new ArrayList<String>()), conn); ps.setClob(3, clob); ps.setClob(6, clob); } if (admins.get(clans.get(i)) != null) { Clob clob = MySQL.stringToClob(MySQL.listToString(admins.get(clans.get(i))), conn); ps.setClob(4, clob); ps.setClob(7, clob); } else { Clob clob = MySQL.stringToClob(MySQL.listToString(new ArrayList<String>()), conn); ps.setClob(4, clob); ps.setClob(7, clob); } ps.setString(2, Owner.get(clans.get(i))); ps.setString(5, Owner.get(clans.get(i))); ps.executeUpdate(); ps.close(); conn.close(); } } catch (Exception e) { e.printStackTrace(); } } }