Skip to content

Commit

Permalink
correct elytra speed calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Aug 7, 2024
1 parent 63f5b2b commit 940ce81
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ElytraHelper extends AEFModule implements Runnable, PacketListener,
private final PacketListenerAbstract packetListener;
private ScheduledExecutorService executorService;
private ScheduledFuture<?> scheduledTask;
private final long speed_as_ticks = config.elytra_speed_calc_period / 50L;

public ElytraHelper() {
super("elytra.elytra-speed");
Expand Down Expand Up @@ -74,7 +75,7 @@ public void disable() {
@Override
public void run() {
for (Map.Entry<UUID, PlayerData> entry : playerDataMap.entrySet()) {
entry.getValue().calcSpeedAvg(config.elytra_calculate_3D, config.elytra_speed_calc_period);
entry.getValue().calcSpeedAvg(config.elytra_calculate_3D, speed_as_ticks);
}
}

Expand Down Expand Up @@ -160,9 +161,9 @@ public void updateLatestPosition(Location location) {
latest.setZ((float) location.getZ());
}

public void calcSpeedAvg(boolean using3D, long period) {
public void calcSpeedAvg(boolean using3D, long tickPeriod) {
// Blockdistance per tick
speedAvg = Math.abs(using3D ? LocationUtil.getRelDistance3D(previous, latest) : LocationUtil.getRelDistance2D(previous, latest)) / ((double) period / 50);
speedAvg = Math.abs(using3D ? LocationUtil.getRelDistance3D(previous, latest) : LocationUtil.getRelDistance2D(previous, latest)) / tickPeriod;
previous = latest.clone();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Config {
public final String cmd_say_format;
public final Sound elytra_too_fast_sound;
public final Duration tps_cache_duration;
public final long elytra_speed_calc_period, elytra_old_chunk_limit;
public final long elytra_speed_calc_period_millis, elytra_old_chunk_limit;
public final int nether_ceiling_max_y, nether_floor_min_y, overworld_floor_min_y, elytra_spawn_radius;
public final boolean auto_lang, packets_disabled, connectionMsgsAreOnByDefault,
cmd_say_enabled, cmd_help_enabled, cmd_toggleConMsgs_enabled,
Expand Down Expand Up @@ -74,7 +74,7 @@ public Config() throws Exception {
"Time in ticks that a chunk has to have been inhabited to count as old chunk.\n" +
"Note that the time is incremented once per tick per player within mob spawning\n" +
"distance of a chunk.");
this.elytra_speed_calc_period = getInt("elytra.elytra-speed.check-period-millis", 500,
this.elytra_speed_calc_period_millis = getInt("elytra.elytra-speed.check-period-millis", 500,
"The period in millis players will be checked to determine their speed.\n" +
"If you have lagging players with consistent high ping, you can increase this number.");
this.elytra_calculate_3D = getBoolean("elytra.elytra-speed.calculate-3D-speed", false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class ElytraHelper extends AEFModule implements Runnable, PacketListener,
private NewChunksListener newChunksListener;
private ScheduledExecutorService executorService;
private ScheduledFuture<?> scheduledTask;
private final long speed_as_ticks = config.elytra_speed_calc_period_millis / 50L;

public ElytraHelper() {
super("elytra.elytra-speed");
Expand All @@ -60,7 +61,7 @@ public void enable() {
PacketEvents.getAPI().getEventManager().registerListener(packetListener);
executorService = Executors.newScheduledThreadPool(1);
scheduledTask = executorService.scheduleAtFixedRate(
this, 50L, config.elytra_speed_calc_period, TimeUnit.MILLISECONDS);
this, 50L, config.elytra_speed_calc_period_millis, TimeUnit.MILLISECONDS);
if (!ChunkUtil.canGetInhabitedTime())
newChunksListener = new NewChunksListener();
}
Expand All @@ -82,7 +83,7 @@ public boolean shouldEnable() {
@Override
public void run() {
for (Map.Entry<UUID, PlayerData> entry : playerDataMap.entrySet()) {
entry.getValue().calcSpeedAvg(config.elytra_calculate_3D, config.elytra_speed_calc_period);
entry.getValue().calcSpeedAvg(config.elytra_calculate_3D, speed_as_ticks);
}
}

Expand Down Expand Up @@ -162,9 +163,9 @@ public void updateLatestPosition(Location location) {
latest.setZ((float) location.getZ());
}

public void calcSpeedAvg(boolean using3D, long period) {
public void calcSpeedAvg(boolean using3D, long tickPeriod) {
// Blockdistance per tick
speedAvg = Math.abs(using3D ? LocationUtil.getRelDistance3D(previous, latest) : LocationUtil.getRelDistance2D(previous, latest)) / ((double) period / 50);
speedAvg = Math.abs(using3D ? LocationUtil.getRelDistance3D(previous, latest) : LocationUtil.getRelDistance2D(previous, latest)) / tickPeriod;
previous = latest.clone();
}
};
Expand Down

0 comments on commit 940ce81

Please sign in to comment.