|
| 1 | +/* |
| 2 | + * This file is part of Sponge, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.forge.mixin.core.world.level; |
| 26 | + |
| 27 | +import net.minecraft.core.BlockPos; |
| 28 | +import net.minecraft.server.level.ServerLevel; |
| 29 | +import net.minecraft.world.entity.Entity; |
| 30 | +import net.minecraft.world.level.ServerExplosion; |
| 31 | +import net.minecraft.world.phys.AABB; |
| 32 | +import net.minecraft.world.phys.Vec3; |
| 33 | +import org.spongepowered.api.event.Cause; |
| 34 | +import org.spongepowered.api.event.SpongeEventFactory; |
| 35 | +import org.spongepowered.api.event.world.ExplosionEvent; |
| 36 | +import org.spongepowered.api.world.explosion.Explosion; |
| 37 | +import org.spongepowered.api.world.server.ServerLocation; |
| 38 | +import org.spongepowered.api.world.server.ServerWorld; |
| 39 | +import org.spongepowered.asm.mixin.Final; |
| 40 | +import org.spongepowered.asm.mixin.Mixin; |
| 41 | +import org.spongepowered.asm.mixin.Shadow; |
| 42 | +import org.spongepowered.asm.mixin.injection.At; |
| 43 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 44 | +import org.spongepowered.common.SpongeCommon; |
| 45 | +import org.spongepowered.common.event.ShouldFire; |
| 46 | +import org.spongepowered.common.event.tracking.PhaseTracker; |
| 47 | +import org.spongepowered.common.util.VecHelper; |
| 48 | + |
| 49 | +import java.util.Collections; |
| 50 | +import java.util.List; |
| 51 | +import java.util.stream.Collectors; |
| 52 | + |
| 53 | +@Mixin(ServerExplosion.class) |
| 54 | +public abstract class ServerExplosionMixin_Forge { |
| 55 | + |
| 56 | + // @formatter:off |
| 57 | + @Shadow @Final private ServerLevel level; |
| 58 | + |
| 59 | + @Shadow protected abstract boolean shadow$interactsWithBlocks(); |
| 60 | + // @formatter:on |
| 61 | + |
| 62 | + /** |
| 63 | + * These two fields are used in the mixin below, but require a redirect specific for SpongeVanilla |
| 64 | + * and SpongeForge as NeoForge makes a change to the method being used here. |
| 65 | + * |
| 66 | + * @see org.spongepowered.common.mixin.core.world.level.ServerExplosionMixin |
| 67 | + */ |
| 68 | + private boolean impl$shouldDamageEntities; |
| 69 | + private double impl$knockbackMultiplier; |
| 70 | + private List<BlockPos> impl$affectedBlocks; |
| 71 | + |
| 72 | + @Redirect(method = "hurtEntities", at = @At(value = "INVOKE", |
| 73 | + target = "Lnet/minecraft/server/level/ServerLevel;getEntities(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/phys/AABB;)Ljava/util/List;")) |
| 74 | + private List<Entity> forge$throwEventToFilterEntitiesOnHurt(final ServerLevel instance, final Entity sourceEntity, final AABB aabb) { |
| 75 | + final List<Entity> entities; |
| 76 | + if (this.impl$shouldDamageEntities) { |
| 77 | + // filter out invulnerable entities before event |
| 78 | + entities = instance.getEntities(sourceEntity, aabb).stream() |
| 79 | + .filter(e -> !e.ignoreExplosion((net.minecraft.world.level.Explosion) this)) |
| 80 | + .toList(); |
| 81 | + } else { |
| 82 | + entities = Collections.emptyList(); |
| 83 | + } |
| 84 | + |
| 85 | + if (ShouldFire.EXPLOSION_EVENT_DETONATE) { |
| 86 | + final var apiWorld = (ServerWorld) this.level; |
| 87 | + final var apiEntities = entities.stream().map(org.spongepowered.api.entity.Entity.class::cast).toList(); |
| 88 | + final var apiBlockPositions = this.impl$affectedBlocks.stream().map(bp -> ServerLocation.of(apiWorld, VecHelper.toVector3i(bp))).toList(); |
| 89 | + final Cause cause = PhaseTracker.getCauseStackManager().currentCause(); |
| 90 | + final ExplosionEvent.Detonate event = SpongeEventFactory.createExplosionEventDetonate(cause, apiBlockPositions, apiEntities, (Explosion) this, apiWorld); |
| 91 | + if (SpongeCommon.post(event)) { |
| 92 | + this.impl$affectedBlocks.clear(); // no blocks affected |
| 93 | + return Collections.emptyList(); // no entities affected |
| 94 | + } |
| 95 | + if (this.shadow$interactsWithBlocks()) { |
| 96 | + this.impl$affectedBlocks = event.affectedLocations().stream().map(VecHelper::toBlockPos).collect(Collectors.toList()); |
| 97 | + } |
| 98 | + if (this.impl$shouldDamageEntities) { |
| 99 | + return event.entities().stream().map(Entity.class::cast).toList(); |
| 100 | + } |
| 101 | + } |
| 102 | + return entities; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + @Redirect(method = "hurtEntities", at = @At(value = "NEW", target = "(DDD)Lnet/minecraft/world/phys/Vec3;")) |
| 107 | + private Vec3 forge$useKnockbackMultiplier(final double $$0, final double $$1, final double $$2) { |
| 108 | + // Honor our knockback value from event |
| 109 | + return new Vec3($$0 * this.impl$knockbackMultiplier, |
| 110 | + $$1 * this.impl$knockbackMultiplier, |
| 111 | + $$2 * this.impl$knockbackMultiplier); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments