Skip to content

Commit

Permalink
Starting on putting bumping into a mixin. Currently not functional; b…
Browse files Browse the repository at this point in the history
…umps walls along the X axis even when it should be the Z axis. Examine Entity.adjustMovementForCollisions for answers??
  • Loading branch information
floral-qua-floral committed Nov 28, 2024
1 parent 4ac6957 commit 9d15965
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
85 changes: 83 additions & 2 deletions mod/src/main/java/com/floralquafloral/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,15 +22,26 @@
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;
import org.spongepowered.asm.mixin.injection.Inject;
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)
Expand Down Expand Up @@ -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<VoxelShape> bumpBlocksOnCollision(World world, Entity entity, Box movingEntityBoundingBox, Operation<Iterable<VoxelShape>> 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<Pair<BlockPos, VoxelShape>> blockCollisions =
() -> new BlockCollisionSpliterator<>(world, entity, movingEntityBoundingBox, false, (pos, voxelShape) -> new Pair<>(pos, voxelShape));

EnumMap<Direction, Set<BlockPos>> bumpBlocks = new EnumMap<>(Direction.class);
for(Direction direction : Direction.values()) {
bumpBlocks.put(direction, new HashSet<>());
}

for(Pair<BlockPos, VoxelShape> 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<BlockPos> 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<Void> original) {
// if(entity instanceof ServerPlayerEntity mario) {
//// world.breakBlock(blockPos, true, mario);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9d15965

Please sign in to comment.