9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 01:49:30 +00:00

refactor(block): 优化弹跳方块行为并修复相关问题

This commit is contained in:
jhqwqmc
2025-09-10 16:09:16 +08:00
parent 647e585c86
commit c99f693f91
4 changed files with 17 additions and 18 deletions

View File

@@ -2,11 +2,13 @@ package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.util.LocationUtils;
import net.momirealms.craftengine.core.block.BlockBehavior;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.craftengine.core.world.Vec3d;
import java.util.Map;
import java.util.concurrent.Callable;
@@ -25,13 +27,7 @@ public class BouncingBlockBehavior extends BukkitBlockBehavior {
@Override
public void fallOn(Object thisBlock, Object[] args, Callable<Object> superMethod) {
Object entity = args[3];
Object finalFallDistance;
if (VersionHelper.isOrAbove1_21_5()) {
double fallDistance = (double) args[4];
finalFallDistance = fallDistance * 0.5;
} else {
finalFallDistance = (float) args[4] * 0.5F;
}
Object finalFallDistance = VersionHelper.isOrAbove1_21_5() ? (double) args[4] * 0.5 : (float) args[4] * 0.5F;
FastNMS.INSTANCE.method$Entity$causeFallDamage(
entity, finalFallDistance, 1.0F,
FastNMS.INSTANCE.method$DamageSources$fall(FastNMS.INSTANCE.method$Entity$damageSources(entity))
@@ -49,16 +45,12 @@ public class BouncingBlockBehavior extends BukkitBlockBehavior {
}
private void bounceUp(Object entity) {
Object deltaMovement = FastNMS.INSTANCE.method$Entity$getDeltaMovement(entity);
if (FastNMS.INSTANCE.field$Vec3$y(deltaMovement) < 0.0) {
Vec3d deltaMovement = LocationUtils.fromVec(FastNMS.INSTANCE.method$Entity$getDeltaMovement(entity));
if (deltaMovement.y < 0.0) {
double d = CoreReflections.clazz$LivingEntity.isInstance(entity) ? 1.0 : 0.8;
FastNMS.INSTANCE.method$Entity$setDeltaMovement(
entity,
FastNMS.INSTANCE.field$Vec3$x(deltaMovement),
-FastNMS.INSTANCE.field$Vec3$y(deltaMovement) * this.bounceHeight * d,
FastNMS.INSTANCE.field$Vec3$z(deltaMovement)
);
if (this.syncPlayerSelf) {
double y = -deltaMovement.y * this.bounceHeight * d;
FastNMS.INSTANCE.method$Entity$setDeltaMovement(entity, deltaMovement.x, y, deltaMovement.z);
if (CoreReflections.clazz$Player.isInstance(entity) && this.syncPlayerSelf && y > 0.032) { // 防抖
FastNMS.INSTANCE.field$Entity$hurtMarked(entity, true);
}
}

View File

@@ -347,7 +347,11 @@ public class UnsafeCompositeBlockBehavior extends BukkitBlockBehavior {
@Override
public void updateEntityMovementAfterFallOn(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
for (AbstractBlockBehavior behavior : this.behaviors) {
behavior.updateEntityMovementAfterFallOn(thisBlock, args, superMethod);
if (behavior instanceof BouncingBlockBehavior bouncingBlockBehavior) {
bouncingBlockBehavior.updateEntityMovementAfterFallOn(thisBlock, args, superMethod);
return;
}
}
super.updateEntityMovementAfterFallOn(thisBlock, args, superMethod);
}
}

View File

@@ -29,7 +29,7 @@ public final class LocationUtils {
return new Vec3d(
FastNMS.INSTANCE.field$Vec3$x(vec),
FastNMS.INSTANCE.field$Vec3$y(vec),
FastNMS.INSTANCE.field$Vec3$y(vec)
FastNMS.INSTANCE.field$Vec3$z(vec)
);
}

View File

@@ -121,6 +121,7 @@ public abstract class BlockBehavior {
// 1.21+ BlockState state, ServerLevel level, BlockPos pos, Explosion explosion, BiConsumer<ItemStack, BlockPos> dropConsumer
public void onExplosionHit(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
superMethod.call();
}
// LevelAccessor level, BlockPos pos, BlockState state, FluidState fluidState
@@ -181,10 +182,12 @@ public abstract class BlockBehavior {
// 1.20.1~1.21.4 Level world, BlockState state, BlockPos pos, Entity entity, float fallDistance
// 1.21.5+ Level level, BlockState state, BlockPos pos, Entity entity, double fallDistance
public void fallOn(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
superMethod.call();
}
// BlockGetter level, Entity entity
public void updateEntityMovementAfterFallOn(Object thisBlock, Object[] args, Callable<Object> superMethod) throws Exception {
superMethod.call();
}
public ImmutableBlockState updateStateForPlacement(BlockPlaceContext context, ImmutableBlockState state) {