Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.base.Preconditions;
import me.caseload.knockbacksync.BukkitBase;
import me.caseload.knockbacksync.Platform;
import me.caseload.knockbacksync.manager.PlayerDataManager;
import me.caseload.knockbacksync.world.FoliaWorld;
import me.caseload.knockbacksync.world.PlatformWorld;
import me.caseload.knockbacksync.world.SpigotWorld;
Expand Down Expand Up @@ -190,7 +191,24 @@ public int getMainHandKnockbackLevel() {

@Override
public void setVelocity(Vector3d adjustedVelocity) {
bukkitPlayer.setVelocity(new Vector(adjustedVelocity.x, adjustedVelocity.y, adjustedVelocity.z));
PlayerData data = PlayerDataManager.getPlayerData(this.getUser());

if(data == null){
// Fallback to normal behavior in the absence of a guard
bukkitPlayer.setVelocity(new Vector(adjustedVelocity.x, adjustedVelocity.y, adjustedVelocity.z));
return;
}

if(data.isVelocityGuard()){
return; // Already applying velocity
}

data.setVelocityGuard(true);
try {
this.bukkitPlayer.setVelocity(new Vector(adjustedVelocity.x, adjustedVelocity.y, adjustedVelocity.z));
} finally {
data.setVelocityGuard(false);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public void onPlayerVelocity(PlatformPlayer victim, Vector3d velocity) {
PlayerData victimPlayerData = PlayerDataManager.getPlayerData(user);
if (victimPlayerData == null)
return;
if (victimPlayerData.isVelocityGuard()) { // Prevent velocity setter from looping infinitely
return;
}

if (victimPlayerData.getNotNullPing() < PlayerData.PING_OFFSET)
return;
Expand Down Expand Up @@ -50,6 +53,8 @@ else if (victimPlayerData.isOffGroundSyncEnabled())
else
return;

victimPlayerData.setVelocityGuard(true);;
victim.setVelocity(adjustedVelocity);
victimPlayerData.setVelocityGuard(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class PlayerData {
public static final long PING_OFFSET = 25;

private static Field playerField;
@Getter @Setter
private boolean velocityGuard = false; // Used to prevent infinite recursion while setting velocity

public final Queue<Pair<Integer, Long>> transactionsSent = new ConcurrentLinkedQueue<>();
public final Queue<Pair<Long, Long>> keepaliveMap = new ConcurrentLinkedQueue<>();
Expand Down