use of net.minecraft.world.damagesource.DamageSource in project MagicPlugin by elBukkit.
the class CompatibilityUtils method magicDamage.
@Override
public void magicDamage(Damageable target, double amount, Entity source) {
try {
if (target == null || target.isDead())
return;
// Might need to config-drive this, or just go back to defaulting to normal damage
if (!USE_MAGIC_DAMAGE || target instanceof Witch || target instanceof Enderman || target instanceof ArmorStand || !(target instanceof LivingEntity)) {
damage(target, amount, source);
return;
}
net.minecraft.world.entity.Entity targetHandle = ((CraftEntity) target).getHandle();
if (targetHandle == null)
return;
net.minecraft.world.entity.Entity sourceHandle = source == null ? null : ((CraftEntity) source).getHandle();
// Bukkit won't allow magic damage from anything but a potion..
if (sourceHandle != null && source instanceof LivingEntity) {
Location location = target.getLocation();
ThrownPotion potion = getOrCreatePotionEntity(location);
net.minecraft.world.entity.Entity potionHandle = ((CraftEntity) potion).getHandle();
potion.setShooter((LivingEntity) source);
DamageSource magicSource = DamageSource.indirectMagic(potionHandle, sourceHandle);
// This is a bit of hack that lets us damage the ender dragon, who is a weird and annoying collection
// of various non-living entity pieces.
((EntityDamageSource) magicSource).setThorns();
try (EnteredStateTracker.Touchable damaging = isDamaging.enter()) {
damaging.touch();
targetHandle.hurt(magicSource, (float) amount);
}
} else {
try (EnteredStateTracker.Touchable damaging = isDamaging.enter()) {
damaging.touch();
targetHandle.hurt(DamageSource.MAGIC, (float) amount);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of net.minecraft.world.damagesource.DamageSource in project SpongeCommon by SpongePowered.
the class EntityPerformingDropsTransaction method captureState.
@Override
protected void captureState() {
super.captureState();
final Entity entity = this.destroyingEntity;
this.worldSupplier = VolumeStreamUtils.createWeaklyReferencedSupplier((ServerLevel) entity.level, "ServerLevel");
final CompoundTag tag = new CompoundTag();
entity.saveWithoutId(tag);
this.entityTag = tag;
@Nullable final DamageSource lastAttacker;
if (entity instanceof LivingEntity) {
final CombatEntry entry = ((CombatTrackerAccessor) ((LivingEntity) entity).getCombatTracker()).invoker$getMostSignificantFall();
if (entry != null) {
lastAttacker = ((CombatEntryAccessor) entry).accessor$source();
} else {
lastAttacker = null;
}
} else {
lastAttacker = null;
}
final WeakReference<@Nullable DamageSource> ref = new WeakReference<>(lastAttacker);
this.lastAttacker = () -> {
@Nullable final DamageSource damageSource = ref.get();
// Yes, I know, we're effectively
if (damageSource == null) {
return Optional.empty();
}
return Optional.of(damageSource);
};
}
use of net.minecraft.world.damagesource.DamageSource in project SpongeCommon by SpongePowered.
the class DamageEventUtil method generateCauseFor.
/**
* This applies various contexts based on the type of {@link DamageSource}, whether
* it's provided by sponge or vanilla. This is not stack neutral, which is why it requires
* a {@link CauseStackManager.StackFrame} reference to push onto the stack.
*/
public static void generateCauseFor(final DamageSource damageSource, final CauseStackManager.StackFrame frame) {
if (damageSource instanceof EntityDamageSource) {
final net.minecraft.world.entity.Entity source = damageSource.getEntity();
if (!(source instanceof Player) && source instanceof CreatorTrackedBridge) {
final CreatorTrackedBridge creatorBridge = (CreatorTrackedBridge) source;
creatorBridge.tracker$getCreatorUUID().ifPresent(creator -> frame.addContext(EventContextKeys.CREATOR, creator));
creatorBridge.tracker$getNotifierUUID().ifPresent(notifier -> frame.addContext(EventContextKeys.NOTIFIER, notifier));
}
} else if (damageSource instanceof BlockDamageSource) {
final ServerLocation location = ((BlockDamageSource) damageSource).location();
final BlockPos blockPos = VecHelper.toBlockPos(location);
final LevelChunkBridge chunkBridge = (LevelChunkBridge) ((net.minecraft.world.level.Level) location.world()).getChunkAt(blockPos);
chunkBridge.bridge$getBlockCreatorUUID(blockPos).ifPresent(creator -> frame.addContext(EventContextKeys.CREATOR, creator));
chunkBridge.bridge$getBlockNotifierUUID(blockPos).ifPresent(notifier -> frame.addContext(EventContextKeys.NOTIFIER, notifier));
}
frame.pushCause(damageSource);
}
use of net.minecraft.world.damagesource.DamageSource in project SpongeCommon by SpongePowered.
the class DamageEventUtil method createEnchantmentModifiers.
public static Optional<List<DamageFunction>> createEnchantmentModifiers(final LivingEntity living, final DamageSource damageSource) {
if (damageSource.isBypassMagic()) {
return Optional.empty();
}
final Iterable<net.minecraft.world.item.ItemStack> inventory = living.getArmorSlots();
final int damageProtection = EnchantmentHelper.getDamageProtection(inventory, damageSource);
if (damageProtection <= 0) {
return Optional.empty();
}
final List<DamageFunction> modifiers = new ArrayList<>();
final DoubleUnaryOperator enchantmentFunction = incomingDamage -> -(incomingDamage - CombatRules.getDamageAfterMagicAbsorb((float) incomingDamage, damageProtection));
try (final CauseStackManager.StackFrame frame = ((Server) living.getServer()).causeStackManager().pushCauseFrame()) {
frame.pushCause(living);
final DamageModifier enchantmentModifier = DamageModifier.builder().cause(frame.currentCause()).type(DamageModifierTypes.ARMOR_ENCHANTMENT).build();
modifiers.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
}
return Optional.of(modifiers);
}
use of net.minecraft.world.damagesource.DamageSource in project SpongeCommon by SpongePowered.
the class EntityMixin method impl$createLavaBlockDamageSource.
/*
@Inject(method = "move",
at = @At("HEAD"),
cancellable = true)
private void impl$onSpongeMoveEntity(final MoverType type, final Vec3d vec3d, final CallbackInfo ci) {
if (!this.world.isClientSide && !SpongeHooks.checkEntitySpeed(((Entity) (Object) this), vec3d.x(), vec3d.y(), vec3d.z())) {
ci.cancel();
}
}
*/
@Redirect(method = "lavaHurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;hurt(Lnet/minecraft/world/damagesource/DamageSource;F)Z"))
private boolean impl$createLavaBlockDamageSource(final Entity entity, final DamageSource source, final float damage) {
if (this.level.isClientSide) {
// Short circuit
return entity.hurt(source, damage);
}
try {
final AABB bb = this.shadow$getBoundingBox().inflate(-0.10000000149011612D, -0.4000000059604645D, -0.10000000149011612D);
final ServerLocation location = DamageEventUtil.findFirstMatchingBlock((Entity) (Object) this, bb, block -> block.getMaterial() == Material.LAVA);
final DamageSource lava = MinecraftBlockDamageSource.ofFire("lava", location, false);
// Bridge to bypass issue with using accessor mixins within mixins
((DamageSourceBridge) lava).bridge$setLava();
return entity.hurt(DamageSource.LAVA, damage);
} finally {
// Since "source" is already the DamageSource.LAVA object, we can simply re-use it here.
((DamageSourceBridge) source).bridge$setLava();
}
}
Aggregations