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);
}
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;
}
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;
}
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);
}
Aggregations