use of org.spongepowered.api.event.cause.entity.damage.source.DamageSource in project Skree by Skelril.
the class PlayerCombatParser method parse.
default default void parse(DamageEntityEvent event) {
Entity entity = event.getTargetEntity();
if (!(entity instanceof Living)) {
return;
}
Optional<DamageSource> optDamageSource = event.getCause().first(DamageSource.class);
if (optDamageSource.isPresent()) {
Entity srcEntity = null;
Entity indirectSrcEntity = null;
if (optDamageSource.get() instanceof IndirectEntityDamageSource) {
srcEntity = ((IndirectEntityDamageSource) optDamageSource.get()).getIndirectSource();
indirectSrcEntity = ((IndirectEntityDamageSource) optDamageSource.get()).getSource();
} else if (optDamageSource.get() instanceof EntityDamageSource) {
srcEntity = ((EntityDamageSource) optDamageSource.get()).getSource();
}
if (!(srcEntity instanceof Living)) {
if (entity instanceof Player) {
processNonLivingAttack(optDamageSource.get(), (Player) entity);
}
return;
}
Living living = (Living) srcEntity;
if (verify(living)) {
if (entity instanceof Player && living instanceof Player) {
processPvP((Player) living, (Player) entity);
processPvP((Player) living, (Player) entity, indirectSrcEntity);
} else if (entity instanceof Player) {
processMonsterAttack(living, (Player) entity);
} else if (living instanceof Player) {
processPlayerAttack((Player) living, (Living) entity);
}
}
}
}
use of org.spongepowered.api.event.cause.entity.damage.source.DamageSource in project Skree by Skelril.
the class MainWorldWrapper method createFor.
private PlayerCombatParser createFor(Cancellable event) {
return new PlayerCombatParser() {
@Override
public void processPvP(Player attacker, Player defender, @Nullable Entity indirectSource) {
Optional<PvPService> optService = Sponge.getServiceManager().provide(PvPService.class);
if (optService.isPresent()) {
PvPService service = optService.get();
if (service.getPvPState(attacker).allowByDefault() && service.getPvPState(defender).allowByDefault()) {
return;
}
}
if (!(indirectSource instanceof Snowball) || !lobby.contains(attacker)) {
attacker.sendMessage(Text.of(TextColors.RED, "PvP is opt-in only in the main world!"));
event.setCancelled(true);
}
}
@Override
public void processNonLivingAttack(DamageSource attacker, Player defender) {
if (attacker.getType() == DamageTypes.VOID) {
defender.setLocation(defender.getWorld().getSpawnLocation());
defender.offer(Keys.FALL_DISTANCE, 0F);
event.setCancelled(true);
}
}
};
}
use of org.spongepowered.api.event.cause.entity.damage.source.DamageSource in project Skree by Skelril.
the class JungleRaidEffectListener method createFor.
private PlayerCombatParser createFor(Cancellable event, JungleRaidInstance inst) {
return new PlayerCombatParser() {
@Override
public void processPvP(Player attacker, Player defender, @Nullable Entity indirectSource) {
final boolean isDamageEntityEvent = event instanceof DamageEntityEvent;
// Do Death Touch before anything else
if (inst.isFlagEnabled(JungleRaidFlag.DEATH_TOUCH) && isDamageEntityEvent) {
((DamageEntityEvent) event).setBaseDamage(Math.pow(defender.get(Keys.MAX_HEALTH).orElse(20D), 3));
return;
}
Optional<JungleRaidClass> optClass = inst.getClass(attacker);
if (optClass.isPresent()) {
JungleRaidClass jrClass = optClass.get();
if (jrClass == JungleRaidClass.SNIPER) {
Optional<ItemStack> optHeld = attacker.getItemInHand(HandTypes.MAIN_HAND);
boolean hasWoodenSword = optHeld.isPresent() && optHeld.get().getItem() == ItemTypes.WOODEN_SWORD;
if (indirectSource != null || !hasWoodenSword) {
double distSq = attacker.getLocation().getPosition().distanceSquared(defender.getLocation().getPosition());
double targetDistSq = Math.pow(70, 2);
double ratio = Math.min(distSq, targetDistSq) / targetDistSq;
if (isDamageEntityEvent) {
// Handle damage modification
((DamageEntityEvent) event).setBaseDamage(((DamageEntityEvent) event).getBaseDamage() * ratio);
} else {
// Disable the arrow fire in the Impact event
if (ratio < .7 && indirectSource != null) {
indirectSource.offer(Keys.FIRE_TICKS, 0);
}
}
}
}
}
if (inst.isFlagEnabled(JungleRaidFlag.TITAN_MODE) && attacker.getUniqueId().equals(inst.getFlagData().titan) && isDamageEntityEvent) {
((DamageEntityEvent) event).setBaseDamage(((DamageEntityEvent) event).getBaseDamage() * 2);
}
}
@Override
public void processNonLivingAttack(DamageSource attacker, Player defender) {
if (!(event instanceof DamageEntityEvent)) {
return;
}
if (attacker.getType() == DamageTypes.FALL) {
BlockType belowType = defender.getLocation().add(0, -1, 0).getBlockType();
if (inst.isFlagEnabled(JungleRaidFlag.TRAMPOLINE)) {
Vector3d oldVel = defender.getVelocity();
Vector3d newVel = new Vector3d(oldVel.getX(), 0, oldVel.getZ());
defender.setVelocity(new Vector3d(0, .1, 0).mul(((DamageEntityEvent) event).getBaseDamage()).add(newVel));
event.setCancelled(true);
} else if (belowType == BlockTypes.LEAVES || belowType == BlockTypes.LEAVES2) {
if (Probability.getChance(2)) {
Vector3d oldVel = defender.getVelocity();
Vector3d newVel = new Vector3d(oldVel.getX() > 0 ? -.5 : .5, 0, oldVel.getZ() > 0 ? -.5 : .5);
defender.setVelocity(new Vector3d(0, .1, 0).mul(((DamageEntityEvent) event).getBaseDamage()).add(newVel));
}
event.setCancelled(true);
}
} else if (attacker.getType() == DamageTypes.CUSTOM) {
if (inst.isFlagEnabled(JungleRaidFlag.EXPLOSIVE_ARROWS) || inst.isFlagEnabled(JungleRaidFlag.GRENADES)) {
((DamageEntityEvent) event).setBaseDamage(Math.min(((DamageEntityEvent) event).getBaseDamage(), 2));
}
}
}
};
}
use of org.spongepowered.api.event.cause.entity.damage.source.DamageSource in project Skree by Skelril.
the class PatientXListener method onEntityDamageEvent.
@Listener(order = Order.LAST)
public void onEntityDamageEvent(DamageEntityEvent event) {
Entity defender = event.getTargetEntity();
Optional<PatientXInstance> optInst = manager.getApplicableZone(defender);
if (!optInst.isPresent())
return;
PatientXInstance inst = optInst.get();
DamageSource dmgSource = event.getCause().first(DamageSource.class).get();
if (defender instanceof Player && manager.getBlockedDamage().contains(dmgSource.getType())) {
// Explosive damage formula: (1 × 1 + 1) × 8 × power + 1
// Use 49, snowball power is 3
double ratio = event.getBaseDamage() / 49;
// Nullify all modifiers
for (Tuple<DamageModifier, Function<? super Double, Double>> modifier : event.getModifiers()) {
event.setDamage(modifier.getFirst(), (a) -> 0D);
}
event.setBaseDamage(ratio * inst.getDifficulty());
}
}
Aggregations