From 9d15965162788e8cc91528f0bfdc1c69135fc862 Mon Sep 17 00:00:00 2001 From: Floral <49110090+floral-qua-floral@users.noreply.github.com> Date: Thu, 28 Nov 2024 02:07:27 -0500 Subject: [PATCH] Starting on putting bumping into a mixin. Currently not functional; bumps walls along the X axis even when it should be the Z axis. Examine Entity.adjustMovementForCollisions for answers?? --- .../floralquafloral/mixin/EntityMixin.java | 85 ++++++++++++++++++- .../states/AttackInterceptionHandler.java | 22 +++-- .../action/baseactions/airborne/Kick.java | 3 + 3 files changed, 101 insertions(+), 9 deletions(-) diff --git a/mod/src/main/java/com/floralquafloral/mixin/EntityMixin.java b/mod/src/main/java/com/floralquafloral/mixin/EntityMixin.java index 10850ab..d4e61e7 100644 --- a/mod/src/main/java/com/floralquafloral/mixin/EntityMixin.java +++ b/mod/src/main/java/com/floralquafloral/mixin/EntityMixin.java @@ -1,15 +1,19 @@ package com.floralquafloral.mixin; import com.floralquafloral.MarioQuaMario; -import com.floralquafloral.mariodata.MarioAuthoritativeData; import com.floralquafloral.mariodata.MarioDataManager; import com.floralquafloral.mariodata.MarioPlayerData; +import com.floralquafloral.mariodata.moveable.MarioMainClientData; import com.floralquafloral.mariodata.moveable.MarioServerData; import com.floralquafloral.registries.RegistryManager; import com.floralquafloral.registries.states.action.ParsedAction; import com.floralquafloral.registries.states.character.ParsedCharacter; import com.floralquafloral.registries.states.powerup.ParsedPowerUp; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import com.llamalad7.mixinextras.sugar.Local; import net.minecraft.block.BlockState; +import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityPose; import net.minecraft.entity.MovementType; @@ -18,8 +22,15 @@ import net.minecraft.nbt.NbtElement; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; +import net.minecraft.util.Pair; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Box; +import net.minecraft.util.math.Direction; import net.minecraft.util.math.Vec3d; +import net.minecraft.util.shape.VoxelShape; +import net.minecraft.world.BlockCollisionSpliterator; +import net.minecraft.world.World; +import org.joml.Vector3d; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; @@ -27,6 +38,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import java.util.EnumMap; +import java.util.HashSet; +import java.util.Set; + @Mixin(Entity.class) public abstract class EntityMixin { @Inject(at = @At("HEAD"), method = "setSwimming(Z)V", cancellable = true) @@ -85,8 +100,74 @@ private void executeStompsOnServer(MovementType movementType, Vec3d movement, Ca } } + @WrapOperation(method = "findCollisionsForMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getBlockCollisions(Lnet/minecraft/entity/Entity;Lnet/minecraft/util/math/Box;)Ljava/lang/Iterable;")) + private static Iterable bumpBlocksOnCollision(World world, Entity entity, Box movingEntityBoundingBox, Operation> original) { + // This doesn't work! Fix it! + if(false && entity instanceof ClientPlayerEntity) { + MarioMainClientData data = MarioMainClientData.getInstance(); + if(data != null) { + Vec3d marioVelocity = data.getMario().getVelocity(); // is this adequate? + Vector3d mutableMarioVelocity = new Vector3d(marioVelocity.x, marioVelocity.y, marioVelocity.z); + + boolean bumpCeilings = true || marioVelocity.y != 0 && data.getAction().BUMPING_RULE.CEILINGS != 0; + boolean bumpFloors = true || marioVelocity.y != 0 && data.getAction().BUMPING_RULE.FLOORS != 0; + boolean bumpWalls = true || (marioVelocity.x != 0 || marioVelocity.z != 0) && data.getAction().BUMPING_RULE.WALLS != 0; + Iterable> blockCollisions = + () -> new BlockCollisionSpliterator<>(world, entity, movingEntityBoundingBox, false, (pos, voxelShape) -> new Pair<>(pos, voxelShape)); + + EnumMap> bumpBlocks = new EnumMap<>(Direction.class); + for(Direction direction : Direction.values()) { + bumpBlocks.put(direction, new HashSet<>()); + } + + for(Pair collidedBlock : blockCollisions) { + if((bumpCeilings || bumpFloors) && collidedBlock.getRight().calculateMaxDistance(Direction.Axis.Y, movingEntityBoundingBox, marioVelocity.y) != 0) { + if(bumpCeilings && marioVelocity.y > 0) { + // Mario was moving up and bumped this block on the Y axis! + bumpBlocks.get(Direction.UP).add(collidedBlock.getLeft().toImmutable()); + } + else if(bumpFloors && marioVelocity.y < 0) { + // Mario was moving down and bumped this block on the Y axis! + bumpBlocks.get(Direction.DOWN).add(collidedBlock.getLeft().toImmutable()); + } + } + else if(bumpWalls) { + boolean xAxisFirst = Math.abs(marioVelocity.x) > Math.abs(marioVelocity.z); + if(xAxisFirst && collidedBlock.getRight().calculateMaxDistance(Direction.Axis.X, movingEntityBoundingBox, marioVelocity.x) != 0) { + MarioQuaMario.LOGGER.info("Collided along X axis: {}", collidedBlock.getRight().calculateMaxDistance(Direction.Axis.X, movingEntityBoundingBox, marioVelocity.x)); + if(marioVelocity.x > 0) + bumpBlocks.get(Direction.EAST).add(collidedBlock.getLeft().toImmutable()); + else + bumpBlocks.get(Direction.WEST).add(collidedBlock.getLeft().toImmutable()); + } + else if(collidedBlock.getRight().calculateMaxDistance(Direction.Axis.Z, movingEntityBoundingBox, marioVelocity.z) != 0) { + if(marioVelocity.z > 0) + bumpBlocks.get(Direction.SOUTH).add(collidedBlock.getLeft().toImmutable()); + else + bumpBlocks.get(Direction.NORTH).add(collidedBlock.getLeft().toImmutable()); + } + else if(!xAxisFirst && collidedBlock.getRight().calculateMaxDistance(Direction.Axis.X, movingEntityBoundingBox, marioVelocity.x) != 0) { + MarioQuaMario.LOGGER.info("Collided along X axis: {}", collidedBlock.getRight().calculateMaxDistance(Direction.Axis.X, movingEntityBoundingBox, marioVelocity.x)); + if(marioVelocity.x > 0) + bumpBlocks.get(Direction.EAST).add(collidedBlock.getLeft().toImmutable()); + else + bumpBlocks.get(Direction.WEST).add(collidedBlock.getLeft().toImmutable()); + } + } + } + + for(Direction direction : Direction.values()) { + Set bumpBlocksInThisDirection = bumpBlocks.get(direction); + if(!bumpBlocksInThisDirection.isEmpty()) + MarioQuaMario.LOGGER.info("BUMP {}:\t{}\t{}", direction.getName(), bumpBlocksInThisDirection.size(), bumpBlocksInThisDirection); + } + } + } + + return original.call(world, entity, movingEntityBoundingBox); + } -// @WrapOperation(method = "checkBlockCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;onEntityCollision(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/entity/Entity;)V")) + // @WrapOperation(method = "checkBlockCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;onEntityCollision(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/entity/Entity;)V")) // private void executeBumpsOnClient(BlockState instance, World world, BlockPos blockPos, Entity entity, Operation original) { // if(entity instanceof ServerPlayerEntity mario) { //// world.breakBlock(blockPos, true, mario); diff --git a/mod/src/main/java/com/floralquafloral/registries/states/AttackInterceptionHandler.java b/mod/src/main/java/com/floralquafloral/registries/states/AttackInterceptionHandler.java index 0257de5..29e233a 100644 --- a/mod/src/main/java/com/floralquafloral/registries/states/AttackInterceptionHandler.java +++ b/mod/src/main/java/com/floralquafloral/registries/states/AttackInterceptionHandler.java @@ -49,14 +49,11 @@ public static boolean attemptInterceptions( ) { for(MarioAttackInterceptingStateDefinition.AttackInterceptionDefinition interception : interceptions) { if(interception.shouldIntercept(data, attackCooldownProgress, targetEntity, targetBlock)) { - long seed = Random.create().nextLong(); interception.executeTravellers(data, targetEntity, targetBlock); data.applyModifiedVelocity(); - interception.executeClients(data, true, seed, targetEntity, targetBlock); - Identifier actionTargetID = interception.getActionTarget(); - if(actionTargetID != null) - data.setActionTransitionless(Objects.requireNonNull(RegistryManager.ACTIONS.get(actionTargetID))); + long seed = Random.create().nextLong(); + performInterceptionClient(data, data, interception, targetEntity, targetBlock, seed); ClientPlayNetworking.send(new InterceptedAttackC2SPayload( targetEntity == null ? -1 : targetEntity.getId(), @@ -117,9 +114,20 @@ public static void performInterceptionClient( isFromAction ? data.getAction().INTERCEPTIONS : data.getPowerUp().INTERCEPTIONS; MarioAttackInterceptingStateDefinition.AttackInterceptionDefinition interception = interceptions.get(interceptionIndex); - Entity targetEntity = targetEntityID == -1 ? null : data.getMario().getWorld().getEntityById(targetEntityID); + performInterceptionClient( + data, clientData, interception, + targetEntityID == -1 ? null : data.getMario().getWorld().getEntityById(targetEntityID), + targetBlock.equals(NONEXISTENT_BLOCKPOS) ? null : targetBlock, + seed + ); + } - interception.executeClients(clientData, false, seed, targetEntity, targetBlock.equals(NONEXISTENT_BLOCKPOS) ? null : targetBlock); + private static void performInterceptionClient( + MarioPlayerData data, MarioClientSideData clientData, + MarioAttackInterceptingStateDefinition.AttackInterceptionDefinition interception, + @Nullable Entity targetEntity, @Nullable BlockPos targetBlock, long seed + ) { + interception.executeClients(clientData, false, seed, targetEntity, targetBlock); Identifier actionTargetID = interception.getActionTarget(); if(actionTargetID != null) diff --git a/mod/src/main/java/com/floralquafloral/registries/states/action/baseactions/airborne/Kick.java b/mod/src/main/java/com/floralquafloral/registries/states/action/baseactions/airborne/Kick.java index 0bbdeff..0e2725a 100644 --- a/mod/src/main/java/com/floralquafloral/registries/states/action/baseactions/airborne/Kick.java +++ b/mod/src/main/java/com/floralquafloral/registries/states/action/baseactions/airborne/Kick.java @@ -12,6 +12,9 @@ import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Hand; import net.minecraft.util.Identifier; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.hit.EntityHitResult; +import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable;