use of org.bukkit.event.Listener in project VoxelGamesLibv2 by VoxelGamesLib.
the class LoggedPluginManager method registerEvents.
/**
* Registers all the events in the given listener class.
*
* @param listener - listener to register
* @param plugin - plugin to register
*/
public void registerEvents(Listener listener, Plugin plugin) {
if (!plugin.isEnabled()) {
throw new IllegalPluginAccessException("Plugin attempted to register " + listener + " while not enabled");
}
// Just in case Bukkit decides to validate the parameters in the future
EventExecutor nullExecutor = (arg0, arg1) -> {
throw new IllegalStateException("This method should never be called!");
};
for (Entry<Class<? extends Event>, Set<RegisteredListener>> entry : plugin.getPluginLoader().createRegisteredListeners(listener, plugin).entrySet()) {
Collection<RegisteredListener> listeners = entry.getValue();
Collection<RegisteredListener> modified = Lists.newArrayList();
// Use our plugin specific logger instead
for (final RegisteredListener delegate : listeners) {
RegisteredListener customListener = new RegisteredListener(delegate.getListener(), nullExecutor, delegate.getPriority(), delegate.getPlugin(), delegate.isIgnoringCancelled()) {
@Override
public void callEvent(Event event) throws EventException {
try {
delegate.callEvent(event);
} catch (AuthorNagException e) {
// Let Bukkit handle that one
throw e;
} catch (Throwable e) {
customHandler(event, e);
}
}
};
modified.add(customListener);
}
getEventListeners(getRegistrationClass(entry.getKey())).registerAll(modified);
}
}
use of org.bukkit.event.Listener in project VoxelGamesLibv2 by VoxelGamesLib.
the class AbstractPhase method disable.
@Override
public void disable() {
// disable timer
duration = Duration.between(startTime, LocalDateTime.now());
log.finer("disable phase " + getName());
// only disable features that have been started to avoid errors
for (Feature feature : startedFeatures) {
log.finer("disable " + feature.getName());
try {
feature.disable();
} catch (Exception ex) {
log.severe("error while stopping " + feature.getName());
ex.printStackTrace();
return;
}
if (feature instanceof Listener) {
eventHandler.unregister((Listener) feature, getGame());
}
if (feature instanceof FeatureCommandImplementor) {
AbstractFeatureCommand cmd = injector.getInstance(((FeatureCommandImplementor) feature).getCommandClass());
commandHandler.unregister(cmd, this);
}
}
for (VictoryCondition victoryCondition : victoryConditions) {
if (victoryCondition instanceof Listener) {
eventHandler.registerEvents((Listener) victoryCondition, getGame());
}
}
phaseTickables.values().forEach(tickable -> {
tickable.disable();
if (tickable instanceof Ability) {
((Ability) tickable).unregister();
}
});
startedFeatures.clear();
}
use of org.bukkit.event.Listener in project MagicPlugin by elBukkit.
the class Mage method onPlayerDeath.
public void onPlayerDeath(EntityDeathEvent event) {
Player player = getPlayer();
if (!player.hasMetadata("arena")) {
lastDeathLocation = player.getLocation();
}
List<Listener> active = new ArrayList<>(deathListeners);
for (Listener listener : active) {
callEvent(listener, event);
}
}
use of org.bukkit.event.Listener in project MagicPlugin by elBukkit.
the class Mage method onDamage.
public void onDamage(EntityDamageEvent event) {
String damageType = currentDamageType;
currentDamageType = null;
LivingEntity entity = getLivingEntity();
if (entity == null) {
return;
}
// Send on to any registered spells
List<Listener> active = new ArrayList<>(damageListeners);
for (Listener listener : active) {
callEvent(listener, event);
if (event.isCancelled())
break;
}
EntityDamageEvent.DamageCause cause = event.getCause();
if (cause == EntityDamageEvent.DamageCause.FALL) {
if (fallProtectionCount > 0 && fallProtection > 0 && fallProtection > System.currentTimeMillis()) {
event.setCancelled(true);
fallProtectionCount--;
if (fallingSpell != null) {
double scale = 1;
LivingEntity li = getLivingEntity();
if (li != null) {
scale = event.getDamage() / li.getMaxHealth();
}
fallingSpell.playEffects("land", (float) scale, getLocation().getBlock().getRelative(BlockFace.DOWN));
}
if (fallProtectionCount <= 0) {
fallProtection = 0;
fallingSpell = null;
}
return;
} else {
fallingSpell = null;
}
}
if (isSuperProtected()) {
event.setCancelled(true);
if (entity.getFireTicks() > 0) {
entity.setFireTicks(0);
}
return;
}
if (event.isCancelled()) {
return;
}
// First check for damage reduction
double reduction = 0;
Double overallProtection = protection.get("overall");
if (overallProtection != null) {
reduction = overallProtection * controller.getMaxDamageReduction("overall");
}
// Apply weaknesses
double multiplier = 1;
Double overallWeakness = weakness.get("overall");
if (overallWeakness != null && overallWeakness > 0) {
double defendMultiplier = controller.getMaxDefendMultiplier("overall");
if (defendMultiplier > 1) {
defendMultiplier = 1 + (defendMultiplier - 1) * overallWeakness;
multiplier *= defendMultiplier;
}
}
if (cause == EntityDamageEvent.DamageCause.FIRE_TICK) {
// Also put out fire if they have maxed out fire protection.
double damageReductionFire = getProtection("fire");
if (damageReductionFire >= 1 && entity.getFireTicks() > 0) {
entity.setFireTicks(0);
}
}
if (damageType == null) {
switch(cause) {
case CONTACT:
case ENTITY_ATTACK:
damageType = "physical";
break;
case FIRE:
case FIRE_TICK:
case LAVA:
damageType = "fire";
break;
case BLOCK_EXPLOSION:
case ENTITY_EXPLOSION:
damageType = "explosion";
break;
default:
damageType = cause.name().toLowerCase();
break;
}
}
lastDamageType = damageType;
double protection = getProtection(damageType);
double maxReduction = controller.getMaxDamageReduction(damageType);
reduction += protection * maxReduction;
if (reduction >= 1) {
event.setCancelled(true);
sendDebugMessage(ChatColor.RED + "Damage nullified by " + ChatColor.BLUE + damageType + " (" + cause + ")", 8);
return;
}
double damage = event.getDamage();
sendDebugMessage(ChatColor.RED + "Damaged by " + ChatColor.BLUE + (damageType == null ? "generic" : damageType) + " (" + cause + ")" + ChatColor.RED + " for " + ChatColor.DARK_RED + damage, 10);
if (reduction > 0) {
damage = (1.0 - reduction) * damage;
sendDebugMessage(ChatColor.DARK_RED + "Damage type " + ChatColor.BLUE + damageType + " reduced by " + ChatColor.AQUA + reduction + ChatColor.DARK_RED + " to " + ChatColor.RED + damage, 9);
event.setDamage(damage);
}
double weakness = getWeakness(damageType);
double maxMultiplier = controller.getMaxDefendMultiplier(damageType);
if (maxMultiplier > 1 && weakness > 0) {
weakness = 1 + (maxMultiplier - 1) * weakness;
multiplier *= weakness;
}
if (multiplier > 1) {
damage = multiplier * damage;
sendDebugMessage(ChatColor.DARK_RED + "Damage type " + ChatColor.BLUE + damageType + " multiplied by " + ChatColor.AQUA + multiplier + ChatColor.DARK_RED + " to " + ChatColor.RED + damage, 9);
event.setDamage(damage);
}
if (damage > 0) {
for (Iterator<Batch> iterator = pendingBatches.iterator(); iterator.hasNext(); ) {
Batch batch = iterator.next();
if (!(batch instanceof SpellBatch))
continue;
SpellBatch spellBatch = (SpellBatch) batch;
Spell spell = spellBatch.getSpell();
double cancelOnDamage = spell.cancelOnDamage();
if (cancelOnDamage > 0 && cancelOnDamage < damage) {
spell.cancel();
batch.finish();
iterator.remove();
}
}
}
}
use of org.bukkit.event.Listener in project MyPet by xXKeyleXx.
the class FactionsHook method onEnable.
@SuppressWarnings("unchecked")
@Override
public boolean onEnable() {
try {
Class engineClass = ReflectionUtil.getClass("com.massivecraft.factions.engine.EngineMain");
Method getMethod = engineClass.getDeclaredMethod("get");
engine = getMethod.invoke(null);
engineMethod = engineClass.getDeclaredMethod("canCombatDamageHappen", EntityDamageByEntityEvent.class, boolean.class);
engineMethod.setAccessible(true);
apiVersion = ApiVersion.V1;
return true;
} catch (Throwable ignored) {
}
try {
Class engineClass = ReflectionUtil.getClass("com.massivecraft.factions.engine.EngineCombat");
Method getMethod = engineClass.getDeclaredMethod("get");
engine = getMethod.invoke(null);
engineMethod = engineClass.getDeclaredMethod("canCombatDamageHappen", EntityDamageByEntityEvent.class, boolean.class);
engineMethod.setAccessible(true);
apiVersion = ApiVersion.V2;
return true;
} catch (Throwable ignored) {
}
try {
Class engineClass = ReflectionUtil.getClass("com.massivecraft.factions.engine.EngineCanCombatHappen");
Method getMethod = engineClass.getDeclaredMethod("get");
engine = getMethod.invoke(null);
engineMethod = engineClass.getDeclaredMethod("canCombatDamageHappen", EntityDamageByEntityEvent.class, boolean.class);
engineMethod.setAccessible(true);
apiVersion = ApiVersion.V3;
return true;
} catch (Throwable ignored) {
}
try {
for (RegisteredListener rl : EntityDamageEvent.getHandlerList().getRegisteredListeners()) {
Listener l = rl.getListener();
if (l.getClass().getName().equalsIgnoreCase("com.massivecraft.factions.listeners.FactionsEntityListener")) {
engine = l;
engineMethod = l.getClass().getDeclaredMethod("canDamagerHurtDamagee", EntityDamageByEntityEvent.class, boolean.class);
engineMethod.setAccessible(true);
apiVersion = ApiVersion.Savage;
return true;
}
}
} catch (Throwable ignored) {
}
MyPetApi.getLogger().warning("Factions was found but no suitable MyPet hook was provided. Please report this to the MyPet developer.");
MyPetApi.getLogger().warning("Factions version: " + MyPetApi.getPluginHookManager().getPluginInstance("Factions").get().getDescription().getVersion());
return false;
}
Aggregations