Skip to content

Commit

Permalink
Accurate swimming and crawling
Browse files Browse the repository at this point in the history
  • Loading branch information
baguchi committed Nov 15, 2023
1 parent 621f289 commit af110b9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import baguchan.better_with_aquatic.api.ISwiming;
import baguchan.better_with_aquatic.util.MathUtil;
import com.mojang.nbt.CompoundTag;
import net.minecraft.core.block.Block;
import net.minecraft.core.block.BlockFluid;
import net.minecraft.core.block.material.Material;
import net.minecraft.core.entity.EntityLiving;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.util.helper.MathHelper;
import net.minecraft.core.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -24,16 +27,23 @@ public EntityPlayerMixin(World world) {
}

public void setSwimming(boolean p_20274_) {
this.swimming = p_20274_;
if (p_20274_) {

if (p_20274_ && !this.swimming) {
this.heightOffset = 0.5f;
this.setSize(0.6F, 0.6F);
} else {
float center = this.bbWidth / 2.0f;
float heightOfMob = this.bbHeight;
this.bb.setBounds(x - (double) center, y - (double) this.heightOffset + (double) this.ySlideOffset, z - (double) center, x + (double) center, y - (double) this.heightOffset + (double) this.ySlideOffset + (double) heightOfMob, z + (double) center);

} else if (!p_20274_ && this.swimming) {
this.heightOffset = 1.62f;
this.ySlideOffset = 0.0f;
this.setSize(0.6F, 1.8F);
float center = this.bbWidth / 2.0f;
float heightOfMob = this.bbHeight;
this.bb.setBounds(x - (double) center, y, z - (double) center, x + (double) center, y + (double) heightOfMob, z + (double) center);
}

this.swimming = p_20274_;
}

public boolean isSwimming() {
Expand All @@ -57,10 +67,10 @@ public float getSwimAmount(float p_20999_) {
public void moveEntityWithHeading(float moveStrafing, float moveForward, CallbackInfo ci) {
if (this.isSwimming() && this.isInWater() && !this.isPassenger()) {
double d3 = this.getLookAngle().yCoord;
double d4 = d3 < -0.2 ? 0.04 : 0.08;
double d4 = d3 < -0.2 ? 0.04 : 0.1;
if (d3 <= 0.0
|| this.isJumping
|| this.world.getBlockMaterial((int) (this.x), (int) (this.y + 1.0 - 0.1), (int) (this.z)) == Material.water) {
|| this.world.getBlockMaterial(MathHelper.floor_double(this.x), MathHelper.floor_double(this.y + 1.0 - 0.5), MathHelper.floor_double(this.z)) == Material.water) {
this.yd += (d3 - yd) * d4;
}
this.xd += (this.getLookAngle().xCoord - xd) * 0.025F;
Expand All @@ -71,11 +81,13 @@ public void moveEntityWithHeading(float moveStrafing, float moveForward, Callbac

@Inject(method = "onLivingUpdate", at = @At("HEAD"))
public void onLivingUpdate(CallbackInfo ci) {
if (!(this.world.getBlockId((int) (this.x), (int) this.y + 1, (int) (this.z)) != 0 && this.world.getBlockMaterial((int) (this.x), (int) this.y + 1, (int) (this.z)) != Material.water) && this.isVisuallyCrawling()) {
Block block = this.world.getBlock((int) this.x, MathHelper.floor_double(this.y + 1), (int) this.z);

if ((block instanceof BlockFluid || block == null) && this.isVisuallyCrawling()) {
this.setSwimming(false);
}

if (this.isSwimming() && this.isInWater() && this.moveForward == 0 && this.moveStrafing == 0) {
if ((block instanceof BlockFluid || block == null) && this.isSwimming() && this.isInWater() && this.moveForward == 0 && this.moveStrafing == 0) {
this.setSwimming(false);
}
this.updateSwimAmount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.EntityPlayerSP;
import net.minecraft.core.entity.player.EntityPlayer;
import net.minecraft.core.util.helper.MathHelper;
import net.minecraft.core.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
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.CallbackInfoReturnable;

@Mixin(value = EntityPlayerSP.class, remap = false)
public abstract class EntityPlayerSPMixin extends EntityPlayer implements ISwiming {
Expand All @@ -31,4 +36,59 @@ public void tick() {
}
}
}

@Shadow
private boolean isBlockTranslucent(int i, int j, int k) {
return false;
}

@Inject(method = "checkInTile", at = @At(value = "HEAD"), cancellable = true)
protected void checkInTile(double d, double d1, double d2, CallbackInfoReturnable<Boolean> cir) {
if (!this.noPhysics && this.isSwimming()) {

int i = MathHelper.floor_double(d);
int j = MathHelper.floor_double(d1);
int k = MathHelper.floor_double(d2);
double d3 = d - (double) i;
double d4 = d2 - (double) k;
if (this.isBlockTranslucent(i, j, k)) {
boolean flag = !this.isBlockTranslucent(i - 1, j, k);
boolean flag1 = !this.isBlockTranslucent(i + 1, j, k);
boolean flag2 = !this.isBlockTranslucent(i, j, k - 1);
boolean flag3 = !this.isBlockTranslucent(i, j, k + 1);
int byte0 = -1;
double d5 = 9999.0;
if (flag && d3 < d5) {
d5 = d3;
byte0 = 0;
}
if (flag1 && 1.0 - d3 < d5) {
d5 = 1.0 - d3;
byte0 = 1;
}
if (flag2 && d4 < d5) {
d5 = d4;
byte0 = 4;
}
if (flag3 && 1.0 - d4 < d5) {
double d6 = 1.0 - d4;
byte0 = 5;
}
float f = 0.1f;
if (byte0 == 0) {
this.xd = -f;
}
if (byte0 == 1) {
this.xd = f;
}
if (byte0 == 4) {
this.zd = -f;
}
if (byte0 == 5) {
this.zd = f;
}
}
cir.setReturnValue(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import baguchan.better_with_aquatic.api.ISwiming;
import net.minecraft.client.input.KeyboardInput;
import net.minecraft.client.option.GameSettings;
import net.minecraft.core.block.material.Material;
import net.minecraft.core.entity.player.EntityPlayer;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -16,6 +17,7 @@
public class KeyboardInputMixin {

private int sprintTime = 20;
private boolean pressedSprint = false;
@Shadow
@Final
private GameSettings gameSettings;
Expand All @@ -26,20 +28,22 @@ public class KeyboardInputMixin {
@Inject(method = "tick", at = @At(value = "TAIL"))
public void tick(EntityPlayer entityplayer, CallbackInfo ci) {

if (entityplayer instanceof ISwiming) {
if (entityplayer.isInWater()) {
if (entityplayer instanceof ISwiming) {
if (entityplayer.isUnderLiquid(Material.water) && !entityplayer.isSneaking()) {
if (keys[0]) {
this.sprintTime = 0;
if (this.sprintTime < 9) {
if (this.sprintTime < 9 && !this.pressedSprint) {
if (BetterWithAquatic.isEnableSwim()) {
((ISwiming) entityplayer).setSwimming(true);
}
} else {
this.pressedSprint = true;
}
this.sprintTime = 0;
} else {
if (this.sprintTime < 20) {
this.sprintTime++;
}
((ISwiming) entityplayer).setSwimming(false);
this.pressedSprint = false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ protected void preRenderCallback(EntityPlayer entity, float ticksExisted, float
if (swimAmount > 0.0F) {
float f3 = entity.isInWater() ? -90.0F - entity.xRot : -90.0F;
float f5 = MathUtil.lerp(swimAmount, 0.0F, f3);
GL11.glRotatef(f5, 1F, 0, 0);
if (((ISwiming) entity).isSwimming() && entity.isInWater()) {
GL11.glTranslatef(0.0F, -1.0F, 0.3F);
GL11.glRotatef(f5, 1F, 0, 0);
}
GL11.glTranslatef(0.0F, -1.0F, 0.3F);

}
}
}
Expand Down

0 comments on commit af110b9

Please sign in to comment.