From 6e24ca9c6f584be40c2ed7347102c28a5aaaa2b9 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 28 Dec 2021 03:32:59 +0200 Subject: [PATCH] Observe spawn protection and mob griefing rules when trying to apply spells that affect blocks --- .../unicopia/ability/magic/Caster.java | 9 +++++++++ .../ability/magic/spell/effect/CatapultSpell.java | 6 ++++-- .../magic/spell/effect/DarkVortexSpell.java | 15 ++++++++------- .../ability/magic/spell/effect/FireSpell.java | 2 +- .../ability/magic/spell/effect/IceSpell.java | 2 +- .../ability/magic/spell/effect/InfernoSpell.java | 2 +- .../ability/magic/spell/effect/ScorchSpell.java | 2 +- 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/Caster.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/Caster.java index 298656a5..4b48b15e 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/Caster.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/Caster.java @@ -15,8 +15,10 @@ import com.minelittlepony.unicopia.util.VecHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.sound.SoundEvent; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.GameRules; import net.minecraft.world.World; /** @@ -58,6 +60,13 @@ public interface Caster extends Owned, Levelled, Affi return getEntity().getBlockPos(); } + default boolean canModifyAt(BlockPos pos) { + if (getMaster() instanceof PlayerEntity) { + return !getWorld().canPlayerModifyAt((PlayerEntity)getMaster(), pos); + } + return getWorld().getGameRules().getBoolean(GameRules.DO_MOB_GRIEFING); + } + default void playSound(SoundEvent sound, float volume, float pitch) { getWorld().playSound(null, getEntity().getX(), getEntity().getY(), getEntity().getZ(), sound, getEntity().getSoundCategory(), volume, pitch); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/CatapultSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/CatapultSpell.java index 2a4361f6..d9ff1006 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/CatapultSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/CatapultSpell.java @@ -44,7 +44,7 @@ public class CatapultSpell extends AbstractSpell implements ProjectileSpell { @Override public void onImpact(MagicProjectileEntity projectile, BlockPos pos, BlockState state) { - if (!projectile.isClient()) { + if (!projectile.isClient() && projectile.canModifyAt(pos)) { createBlockEntity(projectile.world, pos, e -> apply(projectile, e)); } } @@ -92,7 +92,9 @@ public class CatapultSpell extends AbstractSpell implements ProjectileSpell { EntityHitResult result = (EntityHitResult)ray; Optional.ofNullable(result.getEntity()).ifPresent(apply); } else if (ray.getType() == HitResult.Type.BLOCK) { - createBlockEntity(caster.getWorld(), ((BlockHitResult)ray).getBlockPos(), apply); + if (caster.canModifyAt(((BlockHitResult)ray).getBlockPos())) { + createBlockEntity(caster.getWorld(), ((BlockHitResult)ray).getBlockPos(), apply); + } } } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/DarkVortexSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/DarkVortexSpell.java index a846284f..d64020c3 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/DarkVortexSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/DarkVortexSpell.java @@ -22,6 +22,7 @@ import net.minecraft.nbt.NbtCompound; import net.minecraft.particle.ParticleTypes; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; +import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; /** @@ -135,7 +136,7 @@ public class DarkVortexSpell extends AttractiveSpell { if (radius > 5) { Vec3d origin = getOrigin(source); PosHelper.getAllInRegionMutable(source.getOrigin(), new Sphere(false, radius)).forEach(i -> { - if (!source.getWorld().getFluidState(i).isEmpty()) { + if (!canAffect(source, i)) { return; } if (source.getOrigin().isWithinDistance(i, getEventHorizonRadius())) { @@ -153,6 +154,12 @@ public class DarkVortexSpell extends AttractiveSpell { return super.applyEntities(source); } + protected boolean canAffect(Caster source, BlockPos pos) { + return source.canModifyAt(pos) + && source.getWorld().getFluidState(pos).isEmpty() + && source.getWorld().getBlockState(pos).getHardness(source.getWorld(), pos) >= 0; + } + // 1. force decreases with distance: distance scale 1 -> 0 // 2. max force (at dist 0) is taken from accumulated mass // 3. force reaches 0 at distance of drawDropOffRange @@ -226,9 +233,3 @@ public class DarkVortexSpell extends AttractiveSpell { accumulatedMass = compound.getFloat("accumulatedMass"); } } - - - - - - diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireSpell.java index 2b0a7071..a452fb84 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireSpell.java @@ -59,7 +59,7 @@ public class FireSpell extends AbstractAreaEffectSpell implements ProjectileSpel } return PosHelper.getAllInRegionMutable(source.getOrigin(), new Sphere(false, Math.max(0, 4 + getTraits().get(Trait.POWER)))).reduce(false, - (r, i) -> applyBlocks(source.getWorld(), i), + (r, i) -> source.canModifyAt(i) && applyBlocks(source.getWorld(), i), (a, b) -> a || b) || applyEntities(null, source.getWorld(), source.getOriginVector()); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/IceSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/IceSpell.java index d5c7c3e7..dae2eef7 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/IceSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/IceSpell.java @@ -45,7 +45,7 @@ public class IceSpell extends AbstractSpell { PosHelper.getAllInRegionMutable(source.getOrigin(), effect_range) .forEach(i -> { - if (applyBlockSingle(owner, source.getWorld(), i)) { + if (source.canModifyAt(i) && applyBlockSingle(owner, source.getWorld(), i)) { ParticleUtils.spawnParticle(source.getWorld(), ParticleTypes.SPLASH, new Vec3d( i.getX() + source.getWorld().random.nextFloat(), i.getY() + 1, diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/InfernoSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/InfernoSpell.java index 3bf3ec26..5f762935 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/InfernoSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/InfernoSpell.java @@ -44,7 +44,7 @@ public class InfernoSpell extends FireSpell { for (int i = 0; i < radius; i++) { BlockPos pos = new BlockPos(shape.computePoint(w.random).add(origin)); - if (converter.convert(w, pos)) { + if (source.canModifyAt(pos) && converter.convert(w, pos)) { playEffect(w, pos); } } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ScorchSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ScorchSpell.java index 45ad1612..cf22bf0f 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ScorchSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ScorchSpell.java @@ -29,7 +29,7 @@ public class ScorchSpell extends FireSpell { public boolean tick(Caster source, Situation situation) { BlockPos pos = PosHelper.findSolidGroundAt(source.getWorld(), source.getOrigin(), source.getPhysics().getGravitySignum()); - if (StateMaps.FIRE_AFFECTED.convert(source.getWorld(), pos)) { + if (source.canModifyAt(pos) && StateMaps.FIRE_AFFECTED.convert(source.getWorld(), pos)) { source.spawnParticles(new Sphere(false, Math.max(1, getTraits().get(Trait.POWER))), 5, p -> { source.addParticle(ParticleTypes.SMOKE, PosHelper.offset(p, pos), Vec3d.ZERO); });