Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ZCore extends JavaPlugin {
- private Connection conn;
- public void onEnable(){
- checkTable();
- try {
- loadData();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Bukkit.getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
- }
- public void onDisable(){
- try {
- saveData();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private void checkTable(){
- openConnection();
- StringBuilder sb = new StringBuilder();
- sb.append("create table if not exists users(");
- sb.append("uuid varchar(100) not null,");
- sb.append("name varchar(50) not null,");
- sb.append("rank int not null,");
- sb.append("kills int not null,");
- sb.append("deaths int not null,");
- sb.append("recs int not null,");
- sb.append("badges int not null,");
- sb.append("stone int not null,");
- sb.append("apples int not null,");
- sb.append("money int not null,");
- sb.append("level int not null,");
- sb.append("points int not null");
- sb.append("primary key(uuid));");
- try {
- conn.createStatement().executeUpdate(sb.toString());
- } catch (SQLException e) {
- e.printStackTrace();
- }
- closeConnection();
- }
- private void loadData() throws SQLException{
- openConnection();
- int i = 0;
- ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM `users`");
- while(rs.next()){
- User u = User.get(UUID.fromString(rs.getString("uuid")));
- u.setName(rs.getString("name"));
- u.setKills(rs.getInt("kills"));
- u.setDeaths(rs.getInt("deaths"));
- u.setRecs(rs.getInt("recs"));
- u.setBadges(rs.getInt("badges"));
- u.setMoney(rs.getInt("money"));
- u.setApples(rs.getInt("apples"));
- u.setLevel(rs.getInt("level"));
- u.setPoints(rs.getInt("points"));
- u.setHome(rs.getString("home"));
- u.setNoblesse(rs.getBoolean("noblesse"));
- u.setGod(rs.getBoolean("god"));
- i++;
- }
- Bukkit.getConsoleSender().sendMessage("§a§lLoaded §6§l" + i + " §a§lusers");
- closeConnection();
- }
- private void saveData() throws SQLException{
- openConnection();
- int i = 0;
- for(User u : UserUtils.getUsers()){
- StringBuilder sb = new StringBuilder();
- sb.append("INSERT INTO users (uuid, name, rank, kills, deaths, recs, badges, money, apples, stone, points, home, noblesse, god) VALUES (");
- sb.append("'" + u.getName() +"',");
- sb.append("'" + u.getUuid().toString() +"',");
- sb.append("'" + u.getName() +"',");
- sb.append("'" + u.getKills() +"',");
- sb.append("'" + u.getDeaths() +"'");
- sb.append("'" + u.getRecs() +"'");
- sb.append("'" + u.getBadges() +"'");
- sb.append("'" + u.getMoney() +"'");
- sb.append("'" + u.getApples() +"'");
- sb.append("'" + u.getStone() +"'");
- sb.append("'" + u.getPoints() +"'");
- sb.append("'" + u.getHome() +"'");
- sb.append("'" + u.hasNoblesse() +"'");
- sb.append("'" + u.hasGod() +"'");
- sb.append(") ON DUPLICATE KEY UPDATE ");
- sb.append("uuid='" + u.getUuid().toString() +"',");
- sb.append("name='" + u.getName() +"',");
- sb.append("kills='" + u.getKills() +"',");
- sb.append("deaths='" + u.getDeaths() +"'");
- sb.append("recs='" + u.getRecs() +"'");
- sb.append("badges='" + u.getBadges() +"'");
- sb.append("money='" + u.getMoney() +"'");
- sb.append("apples='" + u.getApples() +"'");
- sb.append("stone='" + u.getStone() +"'");
- sb.append("points='" + u.getPoints() +"'");
- sb.append("home='" + u.getHome() +"'");
- if(u.hasGod()) {
- sb.append("god='" + Boolean.valueOf(true) +"'");
- } else {
- sb.append("god='" + Boolean.valueOf(false) +"'");
- }
- if(u.hasNoblesse()) {
- sb.append("god='" + Boolean.valueOf(true) +"'");
- }
- sb.append("noblesse='" + Boolean.valueOf(false) +"'");
- conn.createStatement().executeUpdate(sb.toString());
- i++;
- }
- Bukkit.getConsoleSender().sendMessage("§a§lSaved §6§l" + i + " §a§lusers");
- closeConnection();
- }
- private synchronized void openConnection(){
- if(!isConnected()){
- try{
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/youtube?user=root&password=");
- } catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
- private synchronized void closeConnection(){
- if(isConnected()){
- try{
- conn.close();
- } catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected() {
- try{
- if(conn == null) return false;
- if(conn.isClosed()) return false;
- } catch(SQLException e){
- e.printStackTrace();
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement