Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 227
src/main/java/com/kodari/walkablescale/mixin/EntityMixin.java
package com.kodari.walkablescale.mixin;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.MovementType;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSources;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
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;
@Mixin(Entity.class)
public abstract class EntityMixin {
@Unique
private static final double walkablescale$minimumPlayerScale = 0.100001;
@Unique
private static final double walkablescale$landingTolerance = 1.0E-5;
@Unique
private Box walkablescale$boundsBeforeMove;
@Inject(method = "move", at = @At("HEAD"))
private void walkablescale$captureBounds(MovementType type, Vec3d movement, CallbackInfo ci) {
this.walkablescale$boundsBeforeMove = ((Entity) (Object) this).getBoundingBox();
}
@Inject(method = "move", at = @At("TAIL"))
private void walkablescale$landOnTinyPlayers(MovementType type, Vec3d movement, CallbackInfo ci) {
Entity entity = (Entity) (Object) this;
Box boundsBeforeMove = this.walkablescale$boundsBeforeMove;
if (boundsBeforeMove == null || movement.y >= 0.0 || !(entity instanceof LivingEntity stomper)) {
return;
}
if (stomper.getAttributeValue(EntityAttributes.SCALE) < 1.0
|| !(entity.getEntityWorld() instanceof ServerWorld world)) {
return;
}
Box boundsAfterMove = entity.getBoundingBox();
for (ServerPlayerEntity player : world.getPlayers()) {
if (player == entity
|| player.getAttributeValue(EntityAttributes.SCALE) > walkablescale$minimumPlayerScale) {
continue;
}
Box playerBounds = player.getBoundingBox();
if (boundsAfterMove.maxX <= playerBounds.minX || boundsAfterMove.minX >= playerBounds.maxX
|| boundsAfterMove.maxZ <= playerBounds.minZ || boundsAfterMove.minZ >= playerBounds.maxZ) {
continue;
}
double playerTop = playerBounds.maxY;
if (boundsBeforeMove.minY < playerTop - walkablescale$landingTolerance
|| boundsAfterMove.minY > playerTop) {
continue;
}
boolean landedFromAbove = boundsBeforeMove.minY > playerTop + walkablescale$landingTolerance;
entity.setPosition(entity.getX(), entity.getY() + playerTop - boundsAfterMove.minY, entity.getZ());
entity.setOnGround(true);
if (landedFromAbove) {
DamageSources damageSources = world.getDamageSources();
player.damage(world, damageSources.generic(), 1.0F);
}
}
}
}