From 0ba3fd64a7b1bedd5ff48537d5b5f97aa0590e7f Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 16 Sep 2024 17:51:41 +0100 Subject: [PATCH] Fixed earth ponies breaking blocks in adventure mode and protected regions. Fixes #434 --- .../unicopia/ability/EarthPonyGrowAbility.java | 5 ++++- .../unicopia/ability/EarthPonyKickAbility.java | 15 +++++++++++---- .../unicopia/ability/EarthPonyStompAbility.java | 15 ++++++++------- .../unicopia/entity/player/PlayerPhysics.java | 6 +++--- .../unicopia/entity/player/Pony.java | 2 +- .../server/world/BlockDestructionManager.java | 3 ++- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyGrowAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyGrowAbility.java index 744ee4a5..386fa26a 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyGrowAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyGrowAbility.java @@ -14,6 +14,7 @@ import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.recipe.TransformCropsRecipe; import com.minelittlepony.unicopia.recipe.URecipes; import com.minelittlepony.unicopia.server.world.BlockDestructionManager; +import com.minelittlepony.unicopia.server.world.ModificationType; import com.minelittlepony.unicopia.util.TraceHelper; import com.minelittlepony.unicopia.util.VecHelper; @@ -70,7 +71,9 @@ public class EarthPonyGrowAbility implements Ability { for (BlockPos pos : BlockPos.iterate( data.pos().add(-2, -2, -2), data.pos().add( 2, 2, 2))) { - count += applySingle(player, player.asWorld(), player.asWorld().getBlockState(pos), pos); + if (player.canModifyAt(pos, ModificationType.PHYSICAL)) { + count += applySingle(player, player.asWorld(), player.asWorld().getBlockState(pos), pos); + } } } else { count = 1; diff --git a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java index ca76579f..f8e4e861 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyKickAbility.java @@ -17,6 +17,7 @@ import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.particle.UParticles; import com.minelittlepony.unicopia.server.world.BlockDestructionManager; +import com.minelittlepony.unicopia.server.world.ModificationType; import com.minelittlepony.unicopia.util.*; import net.minecraft.block.BeehiveBlock; @@ -98,7 +99,7 @@ public class EarthPonyKickAbility implements Ability { } BlockPos pos = kickLocation.pos(); - EarthPonyStompAbility.stompBlock(w, pos, 10 * (1 + player.getLevel().getScaled(5)) * w.getBlockState(pos).calcBlockBreakingDelta(player.asEntity(), w, pos)); + EarthPonyStompAbility.stompBlock(player, w, pos, 10 * (1 + player.getLevel().getScaled(5)) * w.getBlockState(pos).calcBlockBreakingDelta(player.asEntity(), w, pos)); player.setAnimation(Animation.KICK, Animation.Recipient.ANYONE); }); } @@ -165,10 +166,16 @@ public class EarthPonyKickAbility implements Ability { if (BlockDestructionManager.of(player.getWorld()).getBlockDestruction(pos) + 4 >= BlockDestructionManager.MAX_DAMAGE) { if (player.getWorld().random.nextInt(30) == 0) { - tree.logs().forEach(player.getWorld(), (w, state, p) -> w.breakBlock(p, true)); + tree.logs().forEach(player.getWorld(), (w, state, p) -> { + if (iplayer.canModifyAt(p, ModificationType.PHYSICAL)) { + w.breakBlock(p, true); + } + }); tree.leaves().forEach(player.getWorld(), (w, state, p) -> { - Block.dropStacks(w.getBlockState(p), w, p); - w.setBlockState(p, Blocks.AIR.getDefaultState(), Block.NOTIFY_ALL); + if (iplayer.canModifyAt(p, ModificationType.PHYSICAL)) { + Block.dropStacks(w.getBlockState(p), w, p); + w.setBlockState(p, Blocks.AIR.getDefaultState(), Block.NOTIFY_ALL); + } }); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java index de4c8be8..e1761d25 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/EarthPonyStompAbility.java @@ -19,6 +19,7 @@ import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.particle.UParticles; import com.minelittlepony.unicopia.server.world.BlockDestructionManager; +import com.minelittlepony.unicopia.server.world.ModificationType; import com.minelittlepony.unicopia.util.PosHelper; import com.minelittlepony.unicopia.util.VecHelper; @@ -166,7 +167,7 @@ public class EarthPonyStompAbility implements Ability { double radius = rad + heavyness * 0.3; - spawnEffectAround(player, center, radius, rad); + spawnEffectAround(iplayer, player, center, radius, rad); ParticleUtils.spawnParticle(player.getWorld(), UParticles.GROUND_POUND, player.getX(), player.getY() - 1, player.getZ(), 0, 0, 0); BlockState steppingState = player.getSteppingBlockState(); @@ -198,17 +199,17 @@ public class EarthPonyStompAbility implements Ability { return true; } - public static void spawnEffectAround(Entity source, BlockPos center, double radius, double range) { + public static void spawnEffectAround(Pony pony, Entity source, BlockPos center, double radius, double range) { BlockPos.stream(new BlockBox(center).expand(MathHelper.ceil(radius))).forEach(i -> { double dist = Math.sqrt(i.getSquaredDistance(source.getX(), source.getY(), source.getZ())); if (dist <= radius) { - spawnEffect(source.getWorld(), i, dist, range); + spawnEffect(pony, source.getWorld(), i, dist, range); } }); } - public static void spawnEffect(World w, BlockPos pos, double dist, double rad) { + public static void spawnEffect(Pony pony, World w, BlockPos pos, double dist, double rad) { if (w.getBlockState(pos.up()).isAir()) { BlockState state = w.getBlockState(pos); @@ -216,18 +217,18 @@ public class EarthPonyStompAbility implements Ability { float scaledHardness = (1 - hardness / 70); float damage = hardness < 0 ? 0 : MathHelper.clamp((int)((1 - dist / rad) * 9 * scaledHardness), 0, BlockDestructionManager.MAX_DAMAGE - 1); - stompBlock(w, pos, damage); + stompBlock(pony, w, pos, damage); } } - public static void stompBlock(World w, BlockPos pos, float damage) { + public static void stompBlock(Pony pony, World w, BlockPos pos, float damage) { BlockState state = w.getBlockState(pos); if (state.isAir() || damage <= 0) { return; } - if (BlockDestructionManager.of(w).damageBlock(pos, damage) >= BlockDestructionManager.MAX_DAMAGE) { + if (BlockDestructionManager.of(w).damageBlock(pos, damage) >= BlockDestructionManager.MAX_DAMAGE && pony.canModifyAt(pos, ModificationType.PHYSICAL)) { w.breakBlock(pos, true); if (w instanceof ServerWorld) { diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java index d1339e66..ca77955d 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java @@ -829,8 +829,8 @@ public class PlayerPhysics extends EntityPhysics implements Tickab .reduce(0, (u, pos) -> { if (pony.canModifyAt(pos, ModificationType.PHYSICAL)) { if (isEarthPonySmash) { - BlockDestructionManager.of(entity.getWorld()).damageBlock(pos, (int)entity.getWorld().getRandom().nextTriangular(5, 3)); - if (BlockDestructionManager.of(entity.getWorld()).getBlockDestruction(pos) >= 9) { + float destruction = BlockDestructionManager.of(entity.getWorld()).damageBlock(pos, (int)entity.getWorld().getRandom().nextTriangular(5, 3)); + if (destruction >= BlockDestructionManager.MAX_DAMAGE - 1 && pony.canModifyAt(pos, ModificationType.PHYSICAL)) { entity.getWorld().breakBlock(pos, true); } } else { @@ -862,7 +862,7 @@ public class PlayerPhysics extends EntityPhysics implements Tickab if (isFlying()) { playSound(USounds.ENTITY_PLAYER_PEGASUS_DASH, 1, 1); } else { - playSound(USounds.ENTITY_PLAYER_EARTHPONY_DASH, 2, 0.3F); + playSound(USounds.ENTITY_PLAYER_EARTHPONY_DASH, 2, 1.3F); } } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index 4cc2b251..9d09beaa 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -703,7 +703,7 @@ public class Pony extends Living implements Copyable, Update if (EffectUtils.hasExtraDefenses(entity)) { double radius = distance / 10; if (radius > 0) { - EarthPonyStompAbility.spawnEffectAround(entity, entity.getSteppingPos(), radius, radius); + EarthPonyStompAbility.spawnEffectAround(this, entity, entity.getSteppingPos(), radius, radius); } } diff --git a/src/main/java/com/minelittlepony/unicopia/server/world/BlockDestructionManager.java b/src/main/java/com/minelittlepony/unicopia/server/world/BlockDestructionManager.java index 974abaef..25464839 100644 --- a/src/main/java/com/minelittlepony/unicopia/server/world/BlockDestructionManager.java +++ b/src/main/java/com/minelittlepony/unicopia/server/world/BlockDestructionManager.java @@ -17,6 +17,7 @@ import net.minecraft.nbt.NbtCompound; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; import net.minecraft.world.World; public class BlockDestructionManager implements Tickable { @@ -54,7 +55,7 @@ public class BlockDestructionManager implements Tickable { if (amount == 0) { return getBlockDestruction(pos); } - amount = Math.max(getBlockDestruction(pos), 0) + amount; + amount = MathHelper.clamp(Math.max(getBlockDestruction(pos), 0) + amount, 0, MAX_DAMAGE); setBlockDestruction(pos, amount); return amount; }