Search in sources :

Example 1 with HawkEvent

use of uk.co.oliwali.HawkEye.HawkEvent 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 HawkEvent

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

the class MonitorBlockListener method onBlockPlace.

@HawkEvent(dataType = DataType.BLOCK_PLACE)
public void onBlockPlace(BlockPlaceEvent event) {
    Block block = event.getBlock();
    if (block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST)
        return;
    DataManager.addEntry(new BlockChangeEntry(event.getPlayer(), DataType.BLOCK_PLACE, block.getLocation(), event.getBlockReplacedState(), block.getState()));
}
Also used : BlockChangeEntry(uk.co.oliwali.HawkEye.entry.BlockChangeEntry) Block(org.bukkit.block.Block) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent)

Example 3 with HawkEvent

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

the class MonitorEntityListener method onEntityDeath.

/**
	 * Uses the lastAttacker field in the players {@link PlayerSession} to log the death and cause
	 */
@HawkEvent(dataType = { DataType.PVP_DEATH, DataType.MOB_DEATH, DataType.OTHER_DEATH, DataType.ENTITY_KILL })
public void onEntityDeath(EntityDeathEvent event) {
    Entity entity = event.getEntity();
    if (entity instanceof Player) {
        //Player death
        Player victim = (Player) entity;
        //Mob or PVP death
        if (victim.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) (victim.getLastDamageCause())).getDamager();
            if (damager instanceof Player) {
                if (!Config.isLogged(DataType.PVP_DEATH) && !Config.LogDeathDrops)
                    return;
                DataManager.addEntry(new DataEntry(victim, DataType.PVP_DEATH, victim.getLocation(), Util.getEntityName(damager)));
            } else {
                if (!Config.isLogged(DataType.MOB_DEATH) && !Config.LogDeathDrops)
                    return;
                DataManager.addEntry(new DataEntry(victim, DataType.MOB_DEATH, victim.getLocation(), Util.getEntityName(damager)));
            }
        //Other death
        } else {
            if (!Config.isLogged(DataType.OTHER_DEATH) && !Config.LogDeathDrops)
                return;
            EntityDamageEvent dEvent = victim.getLastDamageCause();
            String cause = dEvent == null ? "Unknown" : victim.getLastDamageCause().getCause().name();
            String[] words = cause.split("_");
            for (int i = 0; i < words.length; i++) words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1).toLowerCase();
            cause = Util.join(Arrays.asList(words), " ");
            DataManager.addEntry(new DataEntry(victim, DataType.OTHER_DEATH, victim.getLocation(), cause));
        }
        //Log item drops
        if (Config.LogDeathDrops) {
            String data = null;
            for (ItemStack stack : event.getDrops()) {
                if (stack.getData() != null)
                    data = stack.getAmount() + "x " + stack.getTypeId() + ":" + stack.getData().getData();
                else
                    data = stack.getAmount() + "x " + stack.getTypeId();
                DataManager.addEntry(new DataEntry(victim, DataType.ITEM_DROP, victim.getLocation(), data));
            }
        }
    } else {
        //Mob Death
        if (!Config.isLogged(DataType.ENTITY_KILL))
            return;
        if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
            Entity damager = ((EntityDamageByEntityEvent) entity.getLastDamageCause()).getDamager();
            //Only interested in player kills
            if (!(damager instanceof Player))
                return;
            Player player = (Player) damager;
            DataManager.addEntry(new DataEntry(player, DataType.ENTITY_KILL, entity.getLocation(), Util.getEntityName(entity)));
        }
    }
}
Also used : DataEntry(uk.co.oliwali.HawkEye.entry.DataEntry) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) ItemStack(org.bukkit.inventory.ItemStack) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) HawkEvent(uk.co.oliwali.HawkEye.HawkEvent)

Example 4 with HawkEvent

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

use of uk.co.oliwali.HawkEye.HawkEvent 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)

Aggregations

HawkEvent (uk.co.oliwali.HawkEye.HawkEvent)12 DataEntry (uk.co.oliwali.HawkEye.entry.DataEntry)9 Player (org.bukkit.entity.Player)8 Location (org.bukkit.Location)5 ItemStack (org.bukkit.inventory.ItemStack)3 Block (org.bukkit.block.Block)2 BlockChangeEntry (uk.co.oliwali.HawkEye.entry.BlockChangeEntry)2 Method (java.lang.reflect.Method)1 BlockState (org.bukkit.block.BlockState)1 Entity (org.bukkit.entity.Entity)1 Event (org.bukkit.event.Event)1 EventException (org.bukkit.event.EventException)1 Listener (org.bukkit.event.Listener)1 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)1 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)1 MaterialData (org.bukkit.material.MaterialData)1 EventExecutor (org.bukkit.plugin.EventExecutor)1 PluginManager (org.bukkit.plugin.PluginManager)1 DataType (uk.co.oliwali.HawkEye.DataType)1 SimpleRollbackEntry (uk.co.oliwali.HawkEye.entry.SimpleRollbackEntry)1