Search in sources :

Example 1 with DataEntry

use of uk.co.oliwali.HawkEye.entry.DataEntry in project HawkEye by oliverwoodings.

the class SearchQuery method run.

/**
 * Run the search query
 */
@Override
public void run() {
    Util.debug("Beginning search query");
    String sql;
    if (delete)
        sql = "DELETE FROM ";
    else
        sql = "SELECT * FROM ";
    sql += "`" + Config.DbHawkEyeTable + "` WHERE ";
    List<String> args = new LinkedList<String>();
    List<Object> binds = new LinkedList<Object>();
    // Match players from database list
    Util.debug("Building players");
    if (parser.players.size() >= 1) {
        List<Integer> pids = new ArrayList<Integer>();
        List<Integer> npids = new ArrayList<Integer>();
        for (String player : parser.players) {
            for (Map.Entry<String, Integer> entry : DataManager.dbPlayers.entrySet()) {
                if (entry.getKey().toLowerCase().contains(player.toLowerCase()))
                    pids.add(entry.getValue());
                else if (entry.getKey().toLowerCase().contains(player.replace("!", "").toLowerCase()))
                    npids.add(entry.getValue());
            }
        }
        // Include players
        if (pids.size() > 0)
            args.add("player_id IN (" + Util.join(pids, ",") + ")");
        // Exclude players
        if (npids.size() > 0)
            args.add("player_id NOT IN (" + Util.join(npids, ",") + ")");
        if (npids.size() + pids.size() < 1) {
            callBack.error(SearchError.NO_PLAYERS, "No players found matching your specifications");
            return;
        }
    }
    // Match worlds from database list
    Util.debug("Building worlds");
    if (parser.worlds != null) {
        List<Integer> wids = new ArrayList<Integer>();
        List<Integer> nwids = new ArrayList<Integer>();
        for (String world : parser.worlds) {
            for (Map.Entry<String, Integer> entry : DataManager.dbWorlds.entrySet()) {
                if (entry.getKey().toLowerCase().contains(world.toLowerCase()))
                    wids.add(entry.getValue());
                else if (entry.getKey().toLowerCase().contains(world.replace("!", "").toLowerCase()))
                    nwids.add(entry.getValue());
            }
        }
        // Include worlds
        if (wids.size() > 0)
            args.add("world_id IN (" + Util.join(wids, ",") + ")");
        // Exclude worlds
        if (nwids.size() > 0)
            args.add("world_id NOT IN (" + Util.join(nwids, ",") + ")");
        if (nwids.size() + wids.size() < 1) {
            callBack.error(SearchError.NO_WORLDS, "No worlds found matching your specifications");
            return;
        }
    }
    // Compile actions into SQL form
    Util.debug("Building actions");
    if (parser.actions != null && parser.actions.size() > 0) {
        List<Integer> acs = new ArrayList<Integer>();
        for (DataType act : parser.actions) acs.add(act.getId());
        args.add("action IN (" + Util.join(acs, ",") + ")");
    }
    // Add dates
    Util.debug("Building dates");
    if (parser.dateFrom != null) {
        args.add("date >= ?");
        binds.add(parser.dateFrom);
    }
    if (parser.dateTo != null) {
        args.add("date <= ?");
        binds.add(parser.dateTo);
    }
    // Check if location is exact or a range
    Util.debug("Building location");
    if (parser.minLoc != null) {
        args.add("(x BETWEEN " + parser.minLoc.getX() + " AND " + parser.maxLoc.getX() + ")");
        args.add("(y BETWEEN " + parser.minLoc.getY() + " AND " + parser.maxLoc.getY() + ")");
        args.add("(z BETWEEN " + parser.minLoc.getZ() + " AND " + parser.maxLoc.getZ() + ")");
    } else if (parser.loc != null) {
        args.add("x = " + parser.loc.getX());
        args.add("y = " + parser.loc.getY());
        args.add("z = " + parser.loc.getZ());
    }
    // Build the filters into SQL form
    Util.debug("Building filters");
    if (parser.filters != null) {
        for (String filter : parser.filters) {
            args.add("data LIKE ?");
            binds.add("%" + filter + "%");
        }
    }
    // Build WHERE clause
    sql += Util.join(args, " AND ");
    // Add order by
    Util.debug("Ordering by data_id");
    sql += " ORDER BY `data_id` DESC";
    // Check the limits
    Util.debug("Building limits");
    if (Config.MaxLines > 0)
        sql += " LIMIT " + Config.MaxLines;
    // Util.debug("Searching: " + sql);
    // Set up some stuff for the search
    ResultSet res;
    List<DataEntry> results = new ArrayList<DataEntry>();
    JDCConnection conn = DataManager.getConnection();
    PreparedStatement stmnt = null;
    int deleted = 0;
    try {
        // Execute query
        stmnt = conn.prepareStatement(sql);
        Util.debug("Preparing statement");
        for (int i = 0; i < binds.size(); i++) stmnt.setObject(i + 1, binds.get(i));
        Util.debug("Searching: " + stmnt.toString());
        if (delete) {
            Util.debug("Deleting entries");
            deleted = stmnt.executeUpdate();
        } else {
            res = stmnt.executeQuery();
            Util.debug("Getting results");
            // Retrieve results
            while (res.next()) results.add(DataManager.createEntryFromRes(res));
            // If ascending, reverse results
            if (dir == SearchDir.ASC)
                Collections.reverse(results);
        }
    } catch (Exception ex) {
        Util.severe("Error executing MySQL query: " + ex);
        ex.printStackTrace();
        callBack.error(SearchError.MYSQL_ERROR, "Error executing MySQL query: " + ex);
        return;
    } finally {
        try {
            if (stmnt != null)
                stmnt.close();
            conn.close();
        } catch (SQLException ex) {
            Util.severe("Unable to close SQL connection: " + ex);
            callBack.error(SearchError.MYSQL_ERROR, "Unable to close SQL connection: " + ex);
        }
    }
    Util.debug(results.size() + " results found");
    // Run callback
    if (delete)
        ((DeleteCallback) callBack).deleted = deleted;
    else
        callBack.results = results;
    callBack.execute();
    Util.debug("Search complete");
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) LinkedList(java.util.LinkedList) SQLException(java.sql.SQLException) DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) DeleteCallback(uk.co.oliwali.HawkEye.callbacks.DeleteCallback) ResultSet(java.sql.ResultSet) DataType(uk.co.oliwali.HawkEye.DataType) Map(java.util.Map)

Example 2 with DataEntry

use of uk.co.oliwali.HawkEye.entry.DataEntry in project HawkEye by oliverwoodings.

the class MonitorPlayerListener method onPlayerQuit.

@HawkEvent(dataType = DataType.QUIT)
public void onPlayerQuit(PlayerQuitEvent event) {
    Player player = event.getPlayer();
    Location loc = player.getLocation();
    // Check for inventory close
    HawkEye.containerManager.checkInventoryClose(player);
    // Hackish workaround for random NPE given off by the address system
    String ip = "";
    try {
        ip = player.getAddress().getAddress().getHostAddress().toString();
    } catch (Exception e) {
    }
    DataManager.addEntry(new DataEntry(player, DataType.QUIT, loc, Config.LogIpAddresses ? ip : ""));
}
Also used : DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) Player(org.bukkit.entity.Player) Location(org.bukkit.Location) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent)

Example 3 with DataEntry

use of uk.co.oliwali.HawkEye.entry.DataEntry in project HawkEye by oliverwoodings.

the class MonitorPlayerListener method onPlayerTeleport.

@HawkEvent(dataType = DataType.TELEPORT)
public void onPlayerTeleport(PlayerTeleportEvent event) {
    // Check for inventory close
    HawkEye.containerManager.checkInventoryClose(event.getPlayer());
    Location from = event.getFrom();
    Location to = event.getTo();
    if (Util.distance(from, to) > 5)
        DataManager.addEntry(new DataEntry(event.getPlayer(), DataType.TELEPORT, from, to.getWorld().getName() + ": " + to.getX() + ", " + to.getY() + ", " + to.getZ()));
}
Also used : DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) Location(org.bukkit.Location) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent)

Example 4 with DataEntry

use of uk.co.oliwali.HawkEye.entry.DataEntry in project HawkEye by oliverwoodings.

the class MonitorPlayerListener method onPlayerCommandPreprocess.

@HawkEvent(dataType = DataType.COMMAND)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    Player player = event.getPlayer();
    // Check for inventory close
    HawkEye.containerManager.checkInventoryClose(player);
    // Check command filter
    if (Config.CommandFilter.contains(event.getMessage().split(" ")[0]))
        return;
    DataManager.addEntry(new DataEntry(player, DataType.COMMAND, player.getLocation(), event.getMessage()));
}
Also used : DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) Player(org.bukkit.entity.Player) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent)

Example 5 with DataEntry

use of uk.co.oliwali.HawkEye.entry.DataEntry in project HawkEye by oliverwoodings.

the class TptoCommand method execute.

@Override
public boolean execute() {
    if (!Util.isInteger(args.get(0))) {
        Util.sendMessage(sender, "&cPlease supply a entry id!");
        return true;
    }
    DataEntry entry = DataManager.getEntry(Integer.parseInt(args.get(0)));
    if (entry == null) {
        Util.sendMessage(sender, "&cEntry not found");
        return true;
    }
    World world = HawkEye.server.getWorld(entry.getWorld());
    if (world == null) {
        Util.sendMessage(sender, "&cWorld &7" + entry.getWorld() + "&c does not exist!");
        return true;
    }
    Location loc = new Location(world, entry.getX(), entry.getY(), entry.getZ());
    player.teleport(loc);
    Util.sendMessage(sender, "&7Teleported to location of data entry &c" + args.get(0));
    return true;
}
Also used : DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) World(org.bukkit.World) Location(org.bukkit.Location)

Aggregations

DataEntry (uk.co.oliwali.HawkEye.entry.DataEntry)17 Player (org.bukkit.entity.Player)9 HawkEvent (uk.co.oliwali.HawkEye.HawkEvent)9 Location (org.bukkit.Location)7 Block (org.bukkit.block.Block)4 World (org.bukkit.World)3 ItemStack (org.bukkit.inventory.ItemStack)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 DataType (uk.co.oliwali.HawkEye.DataType)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 BlockState (org.bukkit.block.BlockState)1 Entity (org.bukkit.entity.Entity)1 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)1 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)1 DeleteCallback (uk.co.oliwali.HawkEye.callbacks.DeleteCallback)1 SimpleRollbackEntry (uk.co.oliwali.HawkEye.entry.SimpleRollbackEntry)1