private void loadFromDatabase() { final ResultSet result = this.connection.getSQL().getResult("SELECT * FROM `cp_bans`;", false, true); try { while (result != null && result.next()) { final CraftoPlayer player = CraftoPlayer.getPlayer(result.getInt("player_id")); final CraftoPlayer bannedBy = CraftoPlayer.getPlayer(result.getInt("banned_by")); if (player != null && bannedBy != null) { final BanRecord record = new BanRecord( player, bannedBy, result.getString("reason"), result.getTimestamp("created_at"), result.getLong("duration")); this.map.put(record.getPlayer().getUniqueId(), record); } } } catch (final SQLException e) { int id = -1; try { id = result.getInt("id"); } catch (final Exception ignoreMe) { } CraftoMessenger.exception( "BanModule/BanDatabase.java/createTable()", "SQL Exception at BanRecord: " + id, e); } }
public void deleteRecord(final BanRecord record) { Validate.notNull(record, "The record cannot be null!"); this.map.remove(record.getPlayer().getUniqueId()); this.module .getPlugin() .getScheduler() .registerNewQuery( "DELETE FROM `cp_bans` WHERE `player_id` = " + record.getPlayer().getId()); }
public void insertRecord(final BanRecord record) { Validate.notNull(record, "The record cannot be null!"); if (record.getDuration() < 0 || record.getDuration() > 10000) { // Only insert the database if the ban is permanent or lasts longer than 10 // seconds! CraftoMessenger.debug("Inserting ban record to database..."); final String query = "INSERT INTO `cp_bans` (`player_id`, `banned_by`, `reason`, `duration`, `created_at`) VALUES (" + record.getPlayer().getId() + ", " + record.getBannedBy().getId() + ", " + (record.getReason().isPresent() ? "'" + record.getReason().get() + "'" : "NULL") + ", " + record.getDuration() + ", '" + record.getCreationTime().toString() + "') ON DUPLICATE KEY UPDATE id=id;"; this.module.getPlugin().getScheduler().registerNewQuery(query); } else { CraftoMessenger.debug("Skipping inserting ban record to database."); } // But always insert into ram this.map.put(record.getPlayer().getUniqueId(), record); }