private static final void mysqlFind( final Plugin plugin, final Location location, final int radius, final WorldManager manager, final ArrayList<Player> players) { PreparedStatement ps = null; ResultSet rs = null; Connection conn = null; HashMap<BBPlayerInfo, Integer> modifications = new HashMap<BBPlayerInfo, Integer>(); try { conn = ConnectionManager.getConnection(); if (conn == null) return; // TODO maybe more customizable actions? String actionString = "action IN('" + Action.BLOCK_BROKEN.ordinal() + "', '" + Action.BLOCK_PLACED.ordinal() + "', '" + Action.LEAF_DECAY.ordinal() + "', '" + Action.TNT_EXPLOSION.ordinal() + "', '" + Action.CREEPER_EXPLOSION.ordinal() + "', '" + Action.MISC_EXPLOSION.ordinal() + "', '" + Action.LAVA_FLOW.ordinal() + "', '" + Action.BLOCK_BURN.ordinal() + "')"; /* * org.h2.jdbc.JdbcSQLException: Column "ID" must be in the GROUP BY * list; SQL statement: */ if (BBSettings.usingDBMS(DBMS.H2) || BBSettings.usingDBMS(DBMS.POSTGRES)) { ps = conn.prepareStatement( "SELECT player, count(player) AS modifications FROM " + BBDataTable.getInstance().getTableName() + " WHERE " + actionString + " AND rbacked = '0' AND x < ? AND x > ? AND y < ? AND y > ? AND z < ? AND z > ? AND world = ? GROUP BY id,player ORDER BY id DESC"); } else { ps = conn.prepareStatement( "SELECT player, count(player) AS modifications FROM " + BBDataTable.getInstance().getTableName() + " WHERE " + actionString + " AND rbacked = '0' AND x < ? AND x > ? AND y < ? AND y > ? AND z < ? AND z > ? AND world = ? GROUP BY player ORDER BY id DESC"); } ps.setInt(1, location.getBlockX() + radius); ps.setInt(2, location.getBlockX() - radius); ps.setInt(3, location.getBlockY() + radius); ps.setInt(4, location.getBlockY() - radius); ps.setInt(5, location.getBlockZ() + radius); ps.setInt(6, location.getBlockZ() - radius); ps.setInt(7, manager.getWorld(location.getWorld().getName())); rs = ps.executeQuery(); conn.commit(); int size = 0; while (rs.next()) { BBPlayerInfo player = BBUsersTable.getInstance().getUserByID(rs.getInt("player")); int mods = rs.getInt("modifications"); modifications.put(player, mods); size++; } if (size > 0) { StringBuilder playerList = new StringBuilder(); for (Entry<BBPlayerInfo, Integer> entry : modifications.entrySet()) { if (entry.getKey() != null) { playerList.append(entry.getKey().getName()); playerList.append(" ("); playerList.append(entry.getValue()); playerList.append("), "); } } if (playerList.indexOf(",") != -1) { playerList.delete(playerList.lastIndexOf(","), playerList.length()); } // TODO Put into sync'd runnable for (Player player : players) { player.sendMessage( BigBrother.premessage + playerList.length() + " player(s) have modified this area:"); player.sendMessage(playerList.toString()); } } else { for (Player player : players) { player.sendMessage(BigBrother.premessage + "No modifications in this area."); } } } catch (SQLException ex) { BBLogging.severe("Find SQL Exception", ex); } finally { ConnectionManager.cleanup("Find SQL", conn, ps, rs); } }
private static final void mysqlFind( final Plugin plugin, final String playerName, final Location location, final int radius, final WorldManager manager, final ArrayList<Player> players) { BBPlayerInfo hunted = BBUsersTable.getInstance().getUserByName(playerName); PreparedStatement ps = null; ResultSet rs = null; HashMap<Integer, Integer> creations = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> destructions = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> explosions = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> burns = new HashMap<Integer, Integer>(); Connection conn = null; try { conn = ConnectionManager.getConnection(); if (conn == null) return; // TODO maybe more customizable actions? String actionString = "action IN('" + Action.BLOCK_BROKEN.ordinal() + "', '" + Action.BLOCK_PLACED.ordinal() + "', '" + Action.LEAF_DECAY.ordinal() + "', '" + Action.TNT_EXPLOSION.ordinal() + "', '" + Action.CREEPER_EXPLOSION.ordinal() + "', '" + Action.MISC_EXPLOSION.ordinal() + "', '" + Action.LAVA_FLOW.ordinal() + "', '" + Action.BLOCK_BURN.ordinal() + "')"; ps = conn.prepareStatement( "SELECT action, type FROM " + BBDataTable.getInstance().getTableName() + " WHERE " + actionString + " AND rbacked = 0 AND x < ? AND x > ? AND y < ? AND y > ? AND z < ? AND z > ? AND player = ? AND world = ? order by date desc"); ps.setInt(1, location.getBlockX() + radius); ps.setInt(2, location.getBlockX() - radius); ps.setInt(3, location.getBlockY() + radius); ps.setInt(4, location.getBlockY() - radius); ps.setInt(5, location.getBlockZ() + radius); ps.setInt(6, location.getBlockZ() - radius); ps.setInt(7, hunted.getID()); ps.setInt(8, manager.getWorld(location.getWorld().getName())); rs = ps.executeQuery(); conn.commit(); int size = 0; while (rs.next()) { Action action = Action.values()[rs.getInt("action")]; int type = rs.getInt("type"); switch (action) { case BLOCK_BROKEN: case LEAF_DECAY: if (destructions.containsKey(type)) { destructions.put(type, destructions.get(type) + 1); size++; } else { destructions.put(type, 1); size++; } break; case BLOCK_PLACED: if (creations.containsKey(type)) { creations.put(type, creations.get(type) + 1); size++; } else { creations.put(type, 1); size++; } break; case TNT_EXPLOSION: case CREEPER_EXPLOSION: case MISC_EXPLOSION: if (explosions.containsKey(type)) { explosions.put(type, explosions.get(type) + 1); size++; } else { explosions.put(type, 1); size++; } case BLOCK_BURN: if (burns.containsKey(type)) { burns.put(type, burns.get(type) + 1); size++; } else { burns.put(type, 1); size++; } break; case LAVA_FLOW: if (creations.containsKey(type)) { creations.put(type, creations.get(type) + 1); size++; } else { creations.put(type, 1); size++; } break; } } if (size > 0) { StringBuilder creationList = new StringBuilder(); creationList.append(ChatColor.AQUA.toString()); creationList.append("Placed Blocks: "); creationList.append(ChatColor.WHITE.toString()); for (Entry<Integer, Integer> entry : creations.entrySet()) { creationList.append(Material.getMaterial(entry.getKey())); creationList.append(" ("); creationList.append(entry.getValue()); creationList.append("), "); } if (creationList.toString().contains(",")) { creationList.delete(creationList.lastIndexOf(","), creationList.length()); } StringBuilder brokenList = new StringBuilder(); brokenList.append(ChatColor.RED.toString()); brokenList.append("Broken Blocks: "); brokenList.append(ChatColor.WHITE.toString()); for (Entry<Integer, Integer> entry : destructions.entrySet()) { brokenList.append(Material.getMaterial(entry.getKey())); brokenList.append(" ("); brokenList.append(entry.getValue()); brokenList.append("), "); } if (brokenList.toString().contains(",")) { brokenList.delete(brokenList.lastIndexOf(","), brokenList.length()); } StringBuilder explodeList = new StringBuilder(); explodeList.append(ChatColor.RED.toString()); explodeList.append("Exploded Blocks: "); explodeList.append(ChatColor.WHITE.toString()); for (Entry<Integer, Integer> entry : explosions.entrySet()) { explodeList.append(Material.getMaterial(entry.getKey())); explodeList.append(" ("); explodeList.append(entry.getValue()); explodeList.append("), "); } if (explodeList.toString().contains(",")) { explodeList.delete(explodeList.lastIndexOf(","), explodeList.length()); } StringBuilder burnList = new StringBuilder(); burnList.append(ChatColor.RED.toString()); burnList.append("Burned Blocks: "); burnList.append(ChatColor.WHITE.toString()); for (Entry<Integer, Integer> entry : burns.entrySet()) { burnList.append(Material.getMaterial(entry.getKey())); burnList.append(" ("); burnList.append(entry.getValue()); burnList.append("), "); } if (burnList.toString().contains(",")) { burnList.delete(burnList.lastIndexOf(","), burnList.length()); } for (Player player : players) { player.sendMessage( BigBrother.premessage + playerName + " has made " + size + " modifications"); if (creations.entrySet().size() > 0) { player.sendMessage(creationList.toString()); } if (destructions.entrySet().size() > 0) { player.sendMessage(brokenList.toString()); } if (explosions.entrySet().size() > 0) { player.sendMessage(explodeList.toString()); } if (burns.entrySet().size() > 0) { player.sendMessage(burnList.toString()); } } } else { for (Player player : players) { player.sendMessage( BigBrother.premessage + playerName + " has no modifications in this area."); } } } catch (SQLException ex) { BBLogging.severe("Find SQL Exception", ex); } finally { ConnectionManager.cleanup("Find SQL", conn, ps, rs); } }
/** Reload from the database. */ public void refresh() { BBPlayerInfo clone; BBLogging.debug("BBPlayerInfo.refresh(): " + name + "#" + Integer.valueOf(id)); if (id > -1) { clone = BBUsersTable.getInstance().getUserFromDB(id); } else { clone = BBUsersTable.getInstance().getUserFromDB(name); } id = clone.id; flags = clone.flags; name = clone.name; }
private void chestCheck(String player, Block block) { if (block.getState() instanceof Chest) { Chest chest = (Chest) block.getState(); BBPlayerInfo pi = BBUsersTable.getInstance().getUserByName(player); bystanders.add( new DeltaChest( player, chest, pi.getOldChestContents(), chest.getInventory().getContents())); } }
private void setFlag(PlayerField fld, boolean on) { if (!on) { flags &= ~(1 << fld.ordinal()); } else { flags |= (1 << fld.ordinal()); } if (id != -1) { BBUsersTable.getInstance().addOrUpdatePlayer(this); } }
public static BBUsersTable getInstance() { if (instance == null) { BBLogging.debug("BBDB.dbms=" + BBDB.dbms.toString()); if (BBDB.usingDBMS(DBMS.POSTGRES)) { instance = new BBUsersPostgreSQL(); } else { instance = new BBUsersMySQL(); } instance.loadCache(); } return instance; }
/** * For caching a new player. * * @param name */ public BBPlayerInfo(String name) { this.name = name; setNew(true); // Only really used to determine if we need to INSERT or // UPDATE. if (BBSettings.autoWatch) { setWatched(true); } BBUsersTable.getInstance().addOrUpdatePlayer(this); refresh(); // Get ID# setNew(false); BBLogging.debug("New user: "******" -> #" + id); }
@Override public ArrayList<Action> getBlockHistory(Block block, WorldManager manager) { PreparedStatement ps = null; ResultSet rs = null; ArrayList<Action> blockList = new ArrayList<Action>(); try { // TODO maybe more customizable actions? ps = BBDB.prepare( "SELECT bbdata.id, date, player, action, x, y, z, type, data, rbacked, bbworlds.name AS world" + " FROM " + BBDataTable.getInstance().getTableName() + " AS bbdata" + " INNER JOIN " + BBWorldsTable.getInstance().getTableName() + " AS bbworlds" + " ON bbworlds.id = bbdata.world" + " WHERE rbacked = false AND x = ? AND y = ? AND z = ? AND bbdata.world = ? ORDER BY bbdata.id ASC;"); ps.setInt(1, block.getX()); ps.setInt(2, block.getY()); ps.setInt(3, block.getZ()); ps.setInt(4, manager.getWorld(block.getWorld().getName())); rs = ps.executeQuery(); BBDB.commit(); while (rs.next()) { String data = rs.getString("data"); Action newBlock = ActionProvider.findAndProvide( rs.getInt("action"), BBUsersTable.getInstance().getUserByID(rs.getInt("player")), rs.getString("world"), rs.getInt("x"), rs.getInt("y"), rs.getInt("z"), rs.getInt("type"), data); newBlock.date = rs.getLong("date"); blockList.add(newBlock); } } catch (SQLException ex) { BBLogging.severe("Find SQL Exception", ex); } finally { BBDB.cleanup("Find", ps, rs); } return blockList; }
public String undoStatement(Rollback rollback, WorldManager manager) { StringBuilder statement = new StringBuilder("UPDATE "); statement.append(" " + BBDataTable.getInstance().getTableName() + " AS bbdata,"); statement.append(" " + BBUsersTable.getInstance().getTableName() + " AS usr "); statement.append(" SET rbacked = '0'"); statement.append(" WHERE "); statement.append(" bbdata.player = usr.id AND "); statement.append(getActionString()); if (!rollback.rollbackAll) { statement.append(" AND "); statement.append(getPlayerString(rollback.players)); } if (rollback.blockTypes.size() > 0) { statement.append(" AND "); statement.append(getBlockString(rollback.blockTypes)); } if (rollback.time != 0) { statement.append(" AND "); statement.append("date > "); statement.append("'"); statement.append(rollback.time); statement.append("'"); } if (rollback.radius != 0) { statement.append(" AND "); statement.append("x < "); statement.append("'"); statement.append(rollback.center.getBlockX() + rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("x > "); statement.append("'"); statement.append(rollback.center.getBlockX() - rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("y < "); statement.append("'"); statement.append(rollback.center.getBlockY() + rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("y > "); statement.append("'"); statement.append(rollback.center.getBlockY() - rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("z < "); statement.append("'"); statement.append(rollback.center.getBlockZ() + rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("z > "); statement.append("'"); statement.append(rollback.center.getBlockZ() - rollback.radius); statement.append("'"); statement.append(" AND world = "); statement.append("'"); statement.append(rollback.server.getWorlds().indexOf(rollback.center.getWorld())); statement.append("'"); statement.append(" AND world = '"); statement.append(manager.getWorld(rollback.center.getWorld().getName())); statement.append("'"); } statement.append(" AND rbacked = '1'"); statement.append(";"); return statement.toString(); }
public String create(Rollback rollback, WorldManager manager) { StringBuilder statement = new StringBuilder( "SELECT bbdata.id, date, player, action, x, y, z, type, data, rbacked, bbworlds.name AS \"world\""); statement.append(" FROM"); statement.append(" " + BBDataTable.getInstance().getTableName() + " AS bbdata,"); statement.append(" " + BBWorldsTable.getInstance().getTableName() + " AS bbworlds, "); statement.append(" " + BBUsersTable.getInstance().getTableName() + " AS usr "); statement.append(" WHERE "); statement.append(" bbworlds.id = bbdata.world AND bbdata.player = usr.id AND "); statement.append(getActionString()); if (!rollback.rollbackAll) { statement.append(" AND "); statement.append(getPlayerString(rollback.players)); } if (rollback.blockTypes.size() > 0) { statement.append(" AND "); statement.append(getBlockString(rollback.blockTypes)); } if (rollback.time != 0) { statement.append(" AND "); statement.append("date > "); statement.append("'"); statement.append(rollback.time); statement.append("'"); } if (rollback.radius != 0) { statement.append(" AND "); statement.append("x < "); statement.append("'"); statement.append(rollback.center.getBlockX() + rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("x > "); statement.append("'"); statement.append(rollback.center.getBlockX() - rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("y < "); statement.append("'"); statement.append(rollback.center.getBlockY() + rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("y > "); statement.append("'"); statement.append(rollback.center.getBlockY() - rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("z < "); statement.append("'"); statement.append(rollback.center.getBlockZ() + rollback.radius); statement.append("'"); statement.append(" AND "); statement.append("z > "); statement.append("'"); statement.append(rollback.center.getBlockZ() - rollback.radius); statement.append("'"); statement.append(" AND bbdata.world = '"); statement.append(manager.getWorld(rollback.center.getWorld().getName())); statement.append("'"); } statement.append(" AND rbacked = '0'"); statement.append(" ORDER BY bbdata.id DESC"); statement.append(";"); return statement.toString(); }