Search in sources :

Example 1 with BlockDamageSource

use of org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource in project SpongeCommon by SpongePowered.

the class DamageEventHandler method generateCauseFor.

public static void generateCauseFor(DamageSource damageSource) {
    if (damageSource instanceof EntityDamageSourceIndirect) {
        net.minecraft.entity.Entity source = damageSource.getTrueSource();
        if (!(source instanceof EntityPlayer) && source != null) {
            final IMixinEntity mixinEntity = EntityUtil.toMixin(source);
            mixinEntity.getNotifierUser().ifPresent(notifier -> Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier));
            mixinEntity.getCreatorUser().ifPresent(owner -> Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner));
        }
    } else if (damageSource instanceof EntityDamageSource) {
        net.minecraft.entity.Entity source = damageSource.getTrueSource();
        if (!(source instanceof EntityPlayer) && source != null) {
            final IMixinEntity mixinEntity = EntityUtil.toMixin(source);
            // TODO only have a UUID, want a user
            mixinEntity.getNotifierUser().ifPresent(notifier -> Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier));
            mixinEntity.getCreatorUser().ifPresent(creator -> Sponge.getCauseStackManager().addContext(EventContextKeys.CREATOR, creator));
        }
    } else if (damageSource instanceof BlockDamageSource) {
        Location<org.spongepowered.api.world.World> location = ((BlockDamageSource) damageSource).getLocation();
        BlockPos blockPos = ((IMixinLocation) (Object) location).getBlockPos();
        final IMixinChunk mixinChunk = (IMixinChunk) ((net.minecraft.world.World) location.getExtent()).getChunkFromBlockCoords(blockPos);
        mixinChunk.getBlockNotifier(blockPos).ifPresent(notifier -> Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier));
        mixinChunk.getBlockOwner(blockPos).ifPresent(owner -> Sponge.getCauseStackManager().addContext(EventContextKeys.CREATOR, owner));
    }
    Sponge.getCauseStackManager().pushCause(damageSource);
}
Also used : EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Item(net.minecraft.item.Item) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) NBTTagList(net.minecraft.nbt.NBTTagList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) EntityDamageSourceIndirect(net.minecraft.util.EntityDamageSourceIndirect) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ItemArmor(net.minecraft.item.ItemArmor) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) NbtDataUtil(org.spongepowered.common.data.util.NbtDataUtil) Location(org.spongepowered.api.world.Location) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) Predicate(java.util.function.Predicate) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) EntityUtil(org.spongepowered.common.entity.EntityUtil) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(org.spongepowered.api.world.World) Optional(java.util.Optional) MobEffects(net.minecraft.init.MobEffects) EventContext(org.spongepowered.api.event.cause.EventContext) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) Iterables(com.google.common.collect.Iterables) Enchantment(net.minecraft.enchantment.Enchantment) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) EntityEquipmentSlot(net.minecraft.inventory.EntityEquipmentSlot) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) Multimap(com.google.common.collect.Multimap) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Chunk(net.minecraft.world.chunk.Chunk) Entity(net.minecraft.entity.Entity) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) BlockPos(net.minecraft.util.math.BlockPos) DamageSource(net.minecraft.util.DamageSource) IBlockState(net.minecraft.block.state.IBlockState) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EntityLivingBase(net.minecraft.entity.EntityLivingBase) MathHelper(net.minecraft.util.math.MathHelper) EntityDamageSource(net.minecraft.util.EntityDamageSource) EnumCreatureAttribute(net.minecraft.entity.EnumCreatureAttribute) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) Entity(net.minecraft.entity.Entity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) EntityDamageSourceIndirect(net.minecraft.util.EntityDamageSourceIndirect) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) World(org.spongepowered.api.world.World) EntityDamageSource(net.minecraft.util.EntityDamageSource) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with BlockDamageSource

use of org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource in project modules-extra by CubeEngine.

the class Observe method cause.

public static Map<String, Object> cause(Object cause, boolean doRecursion) {
    if (cause instanceof EntityDamageSource) {
        Entity source = ((EntityDamageSource) cause).getSource();
        Map<String, Object> sourceCause = Observe.cause(source);
        Map<String, Object> indirectCause = null;
        if (cause instanceof IndirectEntityDamageSource) {
            indirectCause = Observe.cause(((IndirectEntityDamageSource) cause).getIndirectSource());
            if (sourceCause == null) {
                return indirectCause;
            }
        }
        // TODO indirectCause
        return sourceCause;
    } else if (cause instanceof BlockDamageSource) {
        return Observe.cause(((BlockDamageSource) cause).getBlockSnapshot());
    } else if (cause instanceof DamageSource) {
        return Observe.damageCause(((DamageSource) cause));
    }
    if (cause instanceof Player) {
        return playerCause(((Player) cause));
    } else if (cause instanceof BlockSnapshot) {
        return blockCause(((BlockSnapshot) cause));
    } else if (cause instanceof PrimedTNT) {
        return tntCause(((PrimedTNT) cause));
    } else if (cause instanceof Entity) {
        return entityCause(((Entity) cause), doRecursion);
    }
    // TODO other causes that interest us
    return null;
}
Also used : PrimedTNT(org.spongepowered.api.entity.explosive.PrimedTNT) Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource)

Example 3 with BlockDamageSource

use of org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource in project SpongeCommon by SpongePowered.

the class SpongeBlockDamageSourceBuilder method build.

@SuppressWarnings("ConstantConditions")
@Override
public BlockDamageSource build() throws IllegalStateException {
    checkState(this.location != null);
    checkState(this.blockSnapshot != null);
    checkState(this.damageType != null);
    final MinecraftBlockDamageSource damageSource = new MinecraftBlockDamageSource(this.damageType.name(), this.location);
    final DamageSourceAccessor accessor = (DamageSourceAccessor) (Object) damageSource;
    if (this.absolute) {
        accessor.invoker$bypassMagic();
    }
    if (this.bypasses) {
        accessor.invoker$bypassArmor();
    }
    if (this.scales) {
        damageSource.setScalesWithDifficulty();
    }
    if (this.explosion) {
        damageSource.setExplosion();
    }
    if (this.magical) {
        damageSource.setMagic();
    }
    if (this.creative) {
        accessor.invoker$bypassInvul();
    }
    if (this.exhaustion != null) {
        accessor.accessor$exhaustion(this.exhaustion.floatValue());
    }
    if (this.fire) {
        accessor.invoker$setIsFire();
    }
    return (BlockDamageSource) (Object) damageSource;
}
Also used : MinecraftBlockDamageSource(org.spongepowered.common.util.MinecraftBlockDamageSource) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) MinecraftBlockDamageSource(org.spongepowered.common.util.MinecraftBlockDamageSource) DamageSourceAccessor(org.spongepowered.common.accessor.world.damagesource.DamageSourceAccessor)

Example 4 with BlockDamageSource

use of org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource 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);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) AABB(net.minecraft.world.phys.AABB) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Registry(net.minecraft.core.Registry) ChunkSource(net.minecraft.world.level.chunk.ChunkSource) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Player(net.minecraft.world.entity.player.Player) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Enchantment(net.minecraft.world.item.enchantment.Enchantment) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) MobEffects(net.minecraft.world.effect.MobEffects) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) CombatRules(net.minecraft.world.damagesource.CombatRules) Server(org.spongepowered.api.Server) CauseStackManager(org.spongepowered.api.event.CauseStackManager) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Cause(org.spongepowered.api.event.Cause) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) Mth(net.minecraft.util.Mth) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) Player(net.minecraft.world.entity.player.Player) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ServerLocation(org.spongepowered.api.world.server.ServerLocation) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) Entity(net.minecraft.world.entity.Entity) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) BlockPos(net.minecraft.core.BlockPos) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)

Aggregations

BlockDamageSource (org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource)4 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 DoubleUnaryOperator (java.util.function.DoubleUnaryOperator)2 Predicate (java.util.function.Predicate)2 PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)2 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)1 Lists (com.google.common.collect.Lists)1 Multimap (com.google.common.collect.Multimap)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 IBlockState (net.minecraft.block.state.IBlockState)1 BlockPos (net.minecraft.core.BlockPos)1 Registry (net.minecraft.core.Registry)1 Enchantment (net.minecraft.enchantment.Enchantment)1