Volver al índice
src/main/java/ar/com/companeros/world/DownStepTask.java
package ar.com.companeros.world;
import ar.com.companeros.action.ToolResult;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.FallingBlock;
import net.minecraft.world.phys.AABB;
/** Quita un solo apoyo comprobado y espera un descenso físico de exactamente un bloque. */
public final class DownStepTask implements PlayerOperations.Operation {
// La decisión de cavar y el horario pertenecen al agente; esta herramienta sólo ejecuta un paso.
private final ServerPlayer player;
private final UUID invocation;
private final BlockPos target;
private final Map<String, Integer> before;
private final long startedAt = System.currentTimeMillis();
private PlayerOperations.Operation mining;
private ToolResult result;
private long firstTick = -1, landedAt = -1;
private boolean mined;
private final RiskGate risks;
public DownStepTask(ServerPlayer player, UUID invocation) {
this.player = player; this.invocation = invocation;
risks = new RiskGate(player, "dig_down_step");
target = BlockPos.containing(player.getX(), player.getY() - .1, player.getZ()).immutable();
before = PlayerCrafting.counts(player);
}
// Se comprueba el suelo antes de cada golpe. No se rellena una cueva ni se altera la gravedad.
@Override public ToolResult tick(long tick) {
if (result != null) return result;
if (firstTick < 0) firstTick = tick;
if (!player.isAlive() || player.isRemoved() || player.hasDisconnected())
return finish(ToolResult.Status.CANCELLED, ToolResult.Reason.CONDITIONS_CHANGED, "body_unavailable");
if (tick - firstTick > 1270)
return finish(ToolResult.Status.BLOCKED, ToolResult.Reason.TIMEOUT, "down_step_not_completed mined=" + mined);
String unsafe = openingRisk();
if (unsafe != null) return finish(ToolResult.Status.BLOCKED, ToolResult.Reason.PATH_BLOCKED, unsafe);
if (!mined) {
AABB body = player.getBoundingBox();
if (body.minX < target.getX() || body.maxX > target.getX() + 1
|| body.minZ < target.getZ() || body.maxZ > target.getZ() + 1
|| Math.abs(player.getY() - (target.getY() + 1)) > .12)
return finish(ToolResult.Status.BLOCKED, ToolResult.Reason.CONDITIONS_CHANGED,
"stand_wholly_on_target_before_down_step=" + target.toShortString());
var ground = player.level().getBlockState(target);
if (!Block.isShapeFullBlock(ground.getCollisionShape(player.level(), target))
|| ground.hasBlockEntity() || ground.getBlock() instanceof FallingBlock)
return finish(ToolResult.Status.BLOCKED, ToolResult.Reason.PATH_BLOCKED,
"current_support_is_not_full_stable_soil=" + target.toShortString());
if (mining == null) mining = PlayerOperations.mine(player, invocation, target);
ToolResult outcome = mining.tick(tick);
if (!outcome.finished()) return running();
if (!outcome.succeeded()) return finish(outcome.status(), outcome.reason(),
"native_mining=" + outcome.actualEffects());
mined = true;
}
// El bloque y sus objetos son los de la rotura nativa; recoger sólo al tocar realmente el cuerpo.
for (ItemEntity item : player.serverLevel().getEntitiesOfClass(ItemEntity.class,
player.getBoundingBox().inflate(.65), ItemEntity::isAlive)) item.playerTouch(player);
if (Math.abs(player.getY() - target.getY()) <= .08 && player.onGround()) {
if (landedAt < 0) landedAt = tick;
if (tick - landedAt >= 12) return finish(ToolResult.Status.SUCCEEDED, ToolResult.Reason.NONE,
"native_block_broken_and_landed_one_step=" + target.toShortString());
}
return running();
}
// Se inspecciona sólo la celda que se abrirá y sus vecinas cargadas: un suelo vacío no es una apuesta.
private String openingRisk() {
for (int x = -1; x <= 1; x++) for (int z = -1; z <= 1; z++) for (int y = -1; y <= 1; y++) {
BlockPos cell = target.offset(x, y, z);
if (!PlayerOperations.loaded(player, cell)) return "unloaded_ground_neighbor=" + cell.toShortString();
var state = player.level().getBlockState(cell);
if (!state.getFluidState().isEmpty()) {
String warning = risks.warning("fluid", target, "fluid_beside_down_step=" + cell.toShortString());
if (warning != null) return warning;
}
if (state.is(Blocks.FIRE) || state.is(Blocks.SOUL_FIRE) || state.is(Blocks.CACTUS)
|| state.is(Blocks.MAGMA_BLOCK) || state.is(Blocks.POWDER_SNOW)) {
String warning = risks.warning("hazard", target, "hazard_beside_down_step=" + cell.toShortString());
if (warning != null) return warning;
}
}
BlockPos floor = target.below();
var support = player.level().getBlockState(floor);
if (!Block.isShapeFullBlock(support.getCollisionShape(player.level(), floor))
|| support.getBlock() instanceof FallingBlock)
return risks.warning("floor", target, "no_solid_floor_one_block_below=" + floor.toShortString());
return null;
}
private ToolResult running() {
return new ToolResult(invocation, ToolResult.Status.RUNNING, ToolResult.Reason.NONE, Map.of(), List.of(), List.of(),
GlobalPos.of(player.level().dimension(), player.blockPosition()), startedAt, System.currentTimeMillis());
}
private ToolResult finish(ToolResult.Status status, ToolResult.Reason reason, String evidence) {
if (result == null) {
if (mining != null) mining.cancel();
result = new ToolResult(invocation, status, reason, PlayerCrafting.delta(before, PlayerCrafting.counts(player)),
List.of("dig_down_step mined=" + mined + " actual=" + player.blockPosition().toShortString() + " " + evidence),
List.of(), GlobalPos.of(player.level().dimension(), player.blockPosition()), startedAt, System.currentTimeMillis());
}
return result;
}
@Override public void cancel() { finish(ToolResult.Status.CANCELLED, ToolResult.Reason.CANCELLED, "cancelled"); }
}