Skip to content

Commit

Permalink
Add support for /tick rate on Fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
Axionize committed Oct 16, 2024
1 parent 10877e1 commit 51d4f63
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class PlayerData {
public static final long PING_OFFSET = 25;
private static final int PLUGIN_IDENTIFIER = 0x80000000; // Bit 31 set to 1 (negative)
private static final int ID_MASK = 0x7FFF; // 15-bit mask
public static float TICK_RATE = 20.0F;
private static Field playerField;

static {
Expand Down Expand Up @@ -185,7 +186,7 @@ public boolean isOnGround(double verticalVelocity) {
if (tFall == -1)
return false; // reached the max tick limit, not safe to predict

return getEstimatedPing() >= tMax + tFall / 20.0 * 1000 && gDist <= 1.3;
return getEstimatedPing() >= tMax + tFall / TICK_RATE * 1000 && gDist <= 1.3;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.caseload.knockbacksync.listener.fabric.FabricPlayerDamageListener;
import me.caseload.knockbacksync.listener.fabric.FabricPlayerJoinQuitListener;
import me.caseload.knockbacksync.listener.fabric.FabricPlayerKnockbackListener;
import me.caseload.knockbacksync.listener.fabric.FabricTickRateChangeListener;
import me.caseload.knockbacksync.permission.FabricPermissionChecker;
import me.caseload.knockbacksync.permission.PermissionChecker;
import me.caseload.knockbacksync.scheduler.FabricSchedulerAdapter;
Expand Down Expand Up @@ -87,6 +88,7 @@ protected void registerPlatformListeners() {
new FabricPlayerJoinQuitListener().register();
new FabricPlayerDamageListener().register();
new FabricPlayerKnockbackListener().register();
new FabricTickRateChangeListener().register();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.Vec3;

public interface PlayerVelocityCallback {
Event<PlayerVelocityCallback> EVENT = EventFactory.createArrayBacked(PlayerVelocityCallback.class,
public interface PlayerVelocityEvent {
Event<PlayerVelocityEvent> EVENT = EventFactory.createArrayBacked(PlayerVelocityEvent.class,
(listeners) -> (player, velocity) -> {
for (PlayerVelocityCallback listener : listeners) {
for (PlayerVelocityEvent listener : listeners) {
InteractionResult result = listener.onVelocityChange(player, velocity);

if (result != InteractionResult.PASS) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.caseload.knockbacksync.callback;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;

public interface TickRateChangeEvent {
Event<TickRateChangeEvent> EVENT = EventFactory.createArrayBacked(TickRateChangeEvent.class,
(listeners) -> (oldTickRate, newTickRate) -> {
for (TickRateChangeEvent listener : listeners) {
listener.onTickRateChange(oldTickRate, newTickRate);
}
});

void onTickRateChange(float oldTickRate, float newTickRate);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.caseload.knockbacksync.listener.fabric;

import com.github.retrooper.packetevents.util.Vector3d;
import me.caseload.knockbacksync.callback.PlayerVelocityCallback;
import me.caseload.knockbacksync.callback.PlayerVelocityEvent;
import me.caseload.knockbacksync.listener.PlayerKnockbackListener;
import me.caseload.knockbacksync.player.FabricPlayer;
import net.minecraft.world.InteractionResult;
Expand All @@ -11,7 +11,7 @@
public class FabricPlayerKnockbackListener extends PlayerKnockbackListener {

public void register() {
PlayerVelocityCallback.EVENT.register((player, velocity) -> {
PlayerVelocityEvent.EVENT.register((player, velocity) -> {
DamageSource lastDamageSource = player.getLastDamageSource();
if (lastDamageSource == null)
return InteractionResult.PASS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.caseload.knockbacksync.listener.fabric;

import me.caseload.knockbacksync.callback.TickRateChangeEvent;
import me.caseload.knockbacksync.player.PlayerData;

public class FabricTickRateChangeListener {
public void register() {
TickRateChangeEvent.EVENT.register((oldTickRate, newTickRate) -> {
if (oldTickRate != newTickRate) {
PlayerData.TICK_RATE = newTickRate;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.caseload.knockbacksync.mixin;

import me.caseload.knockbacksync.callback.PlayerVelocityCallback;
import me.caseload.knockbacksync.callback.PlayerVelocityEvent;
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionResult;
Expand All @@ -20,7 +20,7 @@ private void onAttack(Entity target, CallbackInfo ci) {
if (target instanceof ServerPlayer serverPlayer && target.hurtMarked) {
Vec3 velocity = target.getDeltaMovement();

InteractionResult result = PlayerVelocityCallback.EVENT.invoker().onVelocityChange(serverPlayer, velocity);
InteractionResult result = PlayerVelocityEvent.EVENT.invoker().onVelocityChange(serverPlayer, velocity);

if (result == InteractionResult.FAIL) {
target.hurtMarked = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.caseload.knockbacksync.mixin;

import me.caseload.knockbacksync.callback.PlayerVelocityCallback;
import me.caseload.knockbacksync.callback.PlayerVelocityEvent;
import net.minecraft.server.level.ServerEntity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionResult;
Expand All @@ -27,7 +27,7 @@ private void onSendChanges(CallbackInfo ci) {
ServerPlayer player = (ServerPlayer) this.entity;
Vec3 velocity = player.getDeltaMovement();

InteractionResult result = PlayerVelocityCallback.EVENT.invoker().onVelocityChange(player, velocity);
InteractionResult result = PlayerVelocityEvent.EVENT.invoker().onVelocityChange(player, velocity);

if (result == InteractionResult.FAIL) {
this.entity.hurtMarked = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.caseload.knockbacksync.mixin;

import me.caseload.knockbacksync.callback.TickRateChangeEvent;
import org.spongepowered.asm.mixin.Mixin;

import net.minecraft.world.TickRateManager;
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.CallbackInfo;

@Mixin(TickRateManager.class)
public class TickRateManagerMixin {
@Shadow
protected float tickrate;

@Inject(method = "setTickRate", at = @At("HEAD"))
private void onSetTickRate(float tickRate, CallbackInfo ci) {
float oldTickRate = this.tickrate;
TickRateChangeEvent.EVENT.invoker().onTickRateChange(oldTickRate, tickRate);
}
}
3 changes: 2 additions & 1 deletion fabric/src/main/resources/knockbacksync.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"compatibilityLevel": "JAVA_21",
"mixins": [
"PlayerMixin",
"ServerEntityMixin"
"ServerEntityMixin",
"TickRateManagerMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 51d4f63

Please sign in to comment.