Search in sources :

Example 1 with DataType

use of uk.co.oliwali.HawkEye.DataType in project HawkEye by oliverwoodings.

the class HawkEyeListener method registerEvents.

public final void registerEvents() {
    PluginManager pm = plugin.getServer().getPluginManager();
    Method[] methods = this.getClass().getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        final HawkEvent he = method.getAnnotation(HawkEvent.class);
        if (he == null)
            continue;
        boolean register = false;
        for (DataType dt : he.dataType()) {
            if (Config.isLogged(dt))
                register = true;
        }
        if (!register)
            continue;
        Class<?>[] params = method.getParameterTypes();
        if (!Event.class.isAssignableFrom(params[0]) || params.length != 1) {
            continue;
        }
        final Class<? extends Event> eventClass = params[0].asSubclass(Event.class);
        method.setAccessible(true);
        EventExecutor executor = new EventExecutor() {

            public void execute(Listener listener, Event event) throws EventException {
                try {
                    if (!eventClass.isAssignableFrom(event.getClass())) {
                        return;
                    }
                    Util.debug(DebugLevel.HIGH, "Calling event: " + event.getEventName());
                    method.invoke(listener, event);
                    Util.debug(DebugLevel.HIGH, "Event call complete: " + event.getEventName());
                } catch (Exception ex) {
                    throw new EventException(ex.getCause());
                }
            }
        };
        Util.debug("Registering listener for " + eventClass.getName());
        pm.registerEvent(eventClass, this, he.priority(), executor, plugin, he.ignoreCancelled());
    }
}
Also used : Listener(org.bukkit.event.Listener) EventException(org.bukkit.event.EventException) Method(java.lang.reflect.Method) EventException(org.bukkit.event.EventException) PluginManager(org.bukkit.plugin.PluginManager) EventExecutor(org.bukkit.plugin.EventExecutor) DataType(uk.co.oliwali.HawkEye.DataType) Event(org.bukkit.event.Event) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent)

Example 2 with DataType

use of uk.co.oliwali.HawkEye.DataType in project HawkEye by oliverwoodings.

the class DataManager method createEntryFromRes.

/**
	 * Creates a {@link DataEntry} from the inputted {ResultSet}
	 * @param res
	 * @return returns a {@link DataEntry}
	 * @throws SQLException
	 */
public static DataEntry createEntryFromRes(ResultSet res) throws Exception {
    DataType type = DataType.fromId(res.getInt("action"));
    DataEntry entry = (DataEntry) type.getEntryClass().newInstance();
    entry.setPlayer(DataManager.getPlayer(res.getInt("player_id")));
    entry.setDate(res.getString("date"));
    entry.setDataId(res.getInt("data_id"));
    entry.setType(DataType.fromId(res.getInt("action")));
    entry.interpretSqlData(res.getString("data"));
    entry.setPlugin(res.getString("plugin"));
    entry.setWorld(DataManager.getWorld(res.getInt("world_id")));
    entry.setX(res.getInt("x"));
    entry.setY(res.getInt("y"));
    entry.setZ(res.getInt("z"));
    return entry;
}
Also used : DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) DataType(uk.co.oliwali.HawkEye.DataType)

Example 3 with DataType

use of uk.co.oliwali.HawkEye.DataType 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 4 with DataType

use of uk.co.oliwali.HawkEye.DataType in project HawkEye by oliverwoodings.

the class RollbackCommand method execute.

@Override
public boolean execute() {
    //Check if player already has a rollback processing
    if (session.doingRollback()) {
        Util.sendMessage(sender, "&cYou already have a rollback command processing!");
        return true;
    }
    //Parse arguments
    SearchParser parser = null;
    try {
        parser = new SearchParser(player, args);
        parser.loc = null;
        //Check that supplied actions can rollback
        if (parser.actions.size() > 0) {
            for (DataType type : parser.actions) if (!type.canRollback())
                throw new IllegalArgumentException("You cannot rollback that action type: &7" + type.getConfigName());
        } else //If none supplied, add in all rollback types
        {
            for (DataType type : DataType.values()) if (type.canRollback())
                parser.actions.add(type);
        }
    } catch (IllegalArgumentException e) {
        Util.sendMessage(sender, "&c" + e.getMessage());
        return true;
    }
    //Create new SearchQuery with data
    new SearchQuery(new RollbackCallback(session, RollbackType.GLOBAL), parser, SearchDir.DESC);
    return true;
}
Also used : SearchQuery(uk.co.oliwali.HawkEye.database.SearchQuery) DataType(uk.co.oliwali.HawkEye.DataType) SearchParser(uk.co.oliwali.HawkEye.SearchParser) RollbackCallback(uk.co.oliwali.HawkEye.callbacks.RollbackCallback)

Example 5 with DataType

use of uk.co.oliwali.HawkEye.DataType in project HawkEye by oliverwoodings.

the class RollbackCommand method moreHelp.

@Override
public void moreHelp() {
    List<String> acs = new ArrayList<String>();
    for (DataType type : DataType.values()) if (type.canRollback())
        acs.add(type.getConfigName());
    Util.sendMessage(sender, "&7There are 6 parameters you can use - &ca: p: w: r: f: t:");
    Util.sendMessage(sender, "&6Action &ca:&7 - list of actions separated by commas. Select from the following: &8" + Util.join(acs, " "));
    Util.sendMessage(sender, "&6Player &cp:&7 - list of players. &6World &cw:&7 - list of worlds");
    Util.sendMessage(sender, "&6Filter &cf:&7 - list of keywords (e.g. block id)");
    Util.sendMessage(sender, "&6Radius &cr:&7 - radius to search around given location");
    Util.sendMessage(sender, "&6Time &ct:&7 - time bracket in the following format:");
    Util.sendMessage(sender, "&7  -&c t:10h45m10s &7-back specified amount of time");
    Util.sendMessage(sender, "&7  -&c t:2011-06-02,10:45:10 &7-from given date");
    Util.sendMessage(sender, "&7  -&c t:2011-06-02,10:45:10,2011-07-04,18:15:00 &7-between dates");
}
Also used : ArrayList(java.util.ArrayList) DataType(uk.co.oliwali.HawkEye.DataType)

Aggregations

DataType (uk.co.oliwali.HawkEye.DataType)11 SearchParser (uk.co.oliwali.HawkEye.SearchParser)5 SearchQuery (uk.co.oliwali.HawkEye.database.SearchQuery)5 ArrayList (java.util.ArrayList)4 RollbackCallback (uk.co.oliwali.HawkEye.callbacks.RollbackCallback)3 DataEntry (uk.co.oliwali.HawkEye.entry.DataEntry)2 IncompleteRegionException (com.sk89q.worldedit.IncompleteRegionException)1 LocalPlayer (com.sk89q.worldedit.LocalPlayer)1 LocalWorld (com.sk89q.worldedit.LocalWorld)1 BukkitPlayer (com.sk89q.worldedit.bukkit.BukkitPlayer)1 Region (com.sk89q.worldedit.regions.Region)1 Method (java.lang.reflect.Method)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Event (org.bukkit.event.Event)1 EventException (org.bukkit.event.EventException)1 Listener (org.bukkit.event.Listener)1