Skip to content

Commit

Permalink
Add last adjustment check
Browse files Browse the repository at this point in the history
  • Loading branch information
CASELOAD7000 authored Sep 30, 2024
1 parent 97f8b47 commit 7f42f82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ public void onPlayerVelocity(PlayerVelocityEvent event) {

Vector velocity = victim.getVelocity();
Double verticalVelocity = playerData.getVerticalVelocity();

if (verticalVelocity == null || !playerData.isOnGround(velocity.getY()))
return;

Long lastAdjustment = playerData.getLastAdjustment();
if (lastAdjustment != null && System.currentTimeMillis() - lastAdjustment < 250)
return;

Vector adjustedVelocity = velocity.clone().setY(
verticalVelocity
);

victim.setVelocity(adjustedVelocity);
playerData.setLastAdjustment(System.currentTimeMillis());
}
}
19 changes: 10 additions & 9 deletions src/main/java/me/caseload/knockbacksync/manager/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.caseload.knockbacksync.util.MathUtil;
import org.bukkit.*;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class PlayerData {
private BukkitTask combatTask;

@Nullable @Setter
private Long ping, previousPing;
private Long ping, previousPing, lastAdjustment;

@Nullable @Setter
private Double verticalVelocity;
Expand Down Expand Up @@ -74,14 +75,10 @@ public void sendPing() {
}

/**
* Determines if the Player is on the ground, either serverside or clientside.
* Determines if the Player is on the ground clientside, but not serverside
* <p>
* Returns <code>ping ≥ (tMax + tFall)</code> and <code>gDist ≤ 1.3</code>
* <p>
* Returns <code>true</code> if:
* <ul>
* <li>Clientside: <code>ping ≥ (tMax + tFall)</code> and <code>gDist ≤ 1.3</code></li>
* <li>Serverside: <code>gDist ≤ 0</code></li>
* </ul>
*
* Where:
* <ul>
* <li><code>ping</code>: Estimated latency</li>
Expand All @@ -104,7 +101,7 @@ public boolean isOnGround(double verticalVelocity) {

double gDist = getDistanceToGround();
if (gDist <= 0)
return true;
return false; // prevent player from taking adjusted knockback when on ground serverside

int tMax = verticalVelocity > 0 ? MathUtil.calculateTimeToMaxVelocity(verticalVelocity) : 0;
double mH = verticalVelocity > 0 ? MathUtil.calculateDistanceTraveled(verticalVelocity, tMax) : 0;
Expand Down Expand Up @@ -172,6 +169,10 @@ public double calculateVerticalVelocity(Player attacker) {
yAxis -= resistanceFactor;
}

// vertical velocity is always 0.4 when you have knockback level higher than 0
if (attacker.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.KNOCKBACK) > 0)
yAxis = 0.4;

return yAxis;
}

Expand Down

0 comments on commit 7f42f82

Please sign in to comment.