Skip to content

Commit

Permalink
dog teleport lag spike fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Goby56 committed Jan 20, 2024
1 parent b8d12c4 commit 3cb624b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/goby56/wakes/duck/ProducesWake.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.goby56.wakes.duck;

import com.goby56.wakes.particle.custom.SplashPlaneParticle;
import net.minecraft.client.particle.Particle;
import net.minecraft.util.math.Vec3d;

public interface ProducesWake {
Expand All @@ -14,4 +13,6 @@ public interface ProducesWake {
double getVerticalVelocity();
void setSplashPlane(SplashPlaneParticle particle);

void setRecentlyTeleported(boolean b);

}
23 changes: 23 additions & 0 deletions src/main/java/com/goby56/wakes/mixin/TameableTeleportMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.goby56.wakes.mixin;

import com.goby56.wakes.duck.ProducesWake;
import net.minecraft.entity.ai.goal.FollowOwnerGoal;
import net.minecraft.entity.passive.TameableEntity;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(FollowOwnerGoal.class)
public class TameableTeleportMixin {
@Shadow private TameableEntity tameable;

@Inject(at = @At("TAIL"), method = "tryTeleportTo")
private void onTeleport(int x, int y, int z, CallbackInfoReturnable<Boolean> cir) {
if (cir.getReturnValue()) {
((ProducesWake) tameable).setRecentlyTeleported(true);
}
}
}
27 changes: 23 additions & 4 deletions src/main/java/com/goby56/wakes/mixin/WakeSpawnerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
import com.goby56.wakes.duck.ProducesWake;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.network.packet.s2c.play.PositionFlag;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Set;

@Mixin(Entity.class)
public abstract class WakeSpawnerMixin implements ProducesWake {
Expand All @@ -32,6 +38,7 @@ public abstract class WakeSpawnerMixin implements ProducesWake {
@Unique private double verticalNumericalVelocity = 0;
@Unique private Float producingWaterLevel = null;
@Unique private SplashPlaneParticle splashPlane;
@Unique private boolean hasRecentlyTeleported = false;

@Override
public boolean onWaterSurface() {
Expand All @@ -54,8 +61,7 @@ public double getVerticalVelocity() {

@Override
public Vec3d getPrevPos() {
if (this.prevPosOnSurface == null) return null;
return new Vec3d(this.prevPosOnSurface.x, this.prevPosOnSurface.y, this.prevPosOnSurface.z);
return this.prevPosOnSurface;
}

@Override
Expand All @@ -73,7 +79,19 @@ public void setSplashPlane(SplashPlaneParticle particle) {
this.splashPlane = particle;
}

@Inject(at = @At("HEAD"), method = "tick")
@Override
public void setRecentlyTeleported(boolean b) {
this.hasRecentlyTeleported = b;
}

// TODO FIX PLAYER TELEPORTATION CAUSING LONG WAKES
// @Inject(at = @At("TAIL"), method = "teleport(Lnet/minecraft/server/world/ServerWorld;DDDLjava/util/Set;FF)Z")
// private void onTeleport(ServerWorld world, double destX, double destY, double destZ, Set<PositionFlag> flags, float yaw, float pitch, CallbackInfoReturnable<Boolean> cir) {
// this.setRecentlyTeleported(true);
// System.out.printf("%s wants to teleport\n", this);
// }

@Inject(at = @At("TAIL"), method = "tick")
private void tick(CallbackInfo info) {
this.onWaterSurface = this.isTouchingWater() && !this.isSubmergedInWater();
Entity thisEntity = ((Entity) (Object) this);
Expand All @@ -86,7 +104,7 @@ private void tick(CallbackInfo info) {
return;
}

if (this.onWaterSurface) {
if (this.onWaterSurface && this.horizontalNumericalVelocity > 1e-3 && !this.hasRecentlyTeleported) {
if (this.producingWaterLevel == null)
this.producingWaterLevel = WakesUtils.getWaterLevel(this.world, thisEntity);

Expand All @@ -99,6 +117,7 @@ private void tick(CallbackInfo info) {
this.producingWaterLevel = null;
this.prevPosOnSurface = null;
}
this.setRecentlyTeleported(false);
}

@Inject(at = @At("TAIL"), method = "onSwimmingStart")
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/goby56/wakes/utils/WakesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ public static void placeWakeTrail(Entity entity) {
}
}

// TODO FIX ENTERING BOAT CREATES LONG WAKE
// if (velocity < WakesClient.CONFIG_INSTANCE.minimumProducerVelocity) {
// ((ProducesWake) entity).setPrevPos(null);
// }

Vec3d prevPos = producer.getPrevPos();
if (prevPos == null) {
return;
Expand Down
19 changes: 10 additions & 9 deletions src/main/resources/wakes.mixins.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"required": true,
"package": "com.goby56.wakes.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"WakeSpawnerMixin"
],
"injectors": {
"defaultRequire": 1
}
"required": true,
"package": "com.goby56.wakes.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"WakeSpawnerMixin",
"TameableTeleportMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 3cb624b

Please sign in to comment.