Skip to content

Commit

Permalink
use executors instead
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jul 7, 2024
1 parent a7598cc commit fec390e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Config {
public final Sound elytra_too_fast_sound;
public final Component cmd_say_format;
public final Duration tickData_cache_duration;
public final long elytra_speed_calc_period;
public final int nether_ceiling_max_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 @@ -63,6 +64,8 @@ The time in ticks (1 sec = 20 ticks) a checked tps will be cached\s
A server restart is required when changing a command's enable status!""");

// Elytra Speed
this.elytra_speed_calc_period = getInt("elytra.elytra-speed.check-period-ticks", 10,
"The period in ticks players will be checked to determine their speed.");
this.elytra_calculate_3D = getBoolean("elytra.elytra-speed.calculate-3D-speed", false, """
If set to false, will only calculate 2-Dimensional speed without taking height\s
changes into consideration.""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.github.retrooper.packetevents.protocol.PacketSide;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPlayerFlying;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import me.xginko.aef.modules.AEFModule;
import me.xginko.aef.utils.LocationUtil;
import org.bukkit.Location;
Expand All @@ -26,22 +25,25 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ElytraHelper extends AEFModule implements Consumer<ScheduledTask>, PacketListener, Listener {
public class ElytraHelper extends AEFModule implements Runnable, PacketListener, Listener {

private static ElytraHelper instance;
private final Map<UUID, PlayerData> playerDataMap;
private final PacketListenerAbstract packetListener;
private ScheduledTask scheduledTask;
private final long checkPeriod;
private final ScheduledExecutorService executorService;
private ScheduledFuture<?> scheduledTask;

public ElytraHelper() {
super("elytra.elytra-speed");
instance = this;
playerDataMap = new ConcurrentHashMap<>();
packetListener = asAbstract(PacketListenerPriority.MONITOR);
checkPeriod = config.getInt(configPath + ".check-period-ticks", 10);
executorService = Executors.newScheduledThreadPool(1);
}

public static ElytraHelper getInstance() {
Expand All @@ -52,8 +54,7 @@ public static ElytraHelper getInstance() {
public void enable() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
PacketEvents.getAPI().getEventManager().registerListener(packetListener);
scheduledTask = plugin.getServer().getGlobalRegionScheduler()
.runAtFixedRate(plugin, this, checkPeriod, checkPeriod);
scheduledTask = executorService.scheduleAtFixedRate(this, 50L, config.elytra_speed_calc_period * 50L, TimeUnit.MILLISECONDS);
}

@Override
Expand All @@ -65,13 +66,14 @@ public boolean shouldEnable() {
public void disable() {
HandlerList.unregisterAll(this);
PacketEvents.getAPI().getEventManager().unregisterListener(packetListener);
if (scheduledTask != null) scheduledTask.cancel();
if (scheduledTask != null) scheduledTask.cancel(true);
executorService.shutdown();
}

@Override
public void accept(ScheduledTask task) {
public void run() {
for (Map.Entry<UUID, PlayerData> entry : playerDataMap.entrySet()) {
entry.getValue().calcSpeedAvg(config.elytra_calculate_3D, checkPeriod);
entry.getValue().calcSpeedAvg(config.elytra_calculate_3D, config.elytra_speed_calc_period);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ 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;
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,
elytra_enable_at_spawn, elytra_enable_global, elytra_enable_netherceiling,
elytra_actionbar_enabled, elytra_show_chunkage, elytra_play_too_fast_sound,
elytra_actionbar_enabled, elytra_show_chunk_age, elytra_play_too_fast_sound,
elytra_teleport_back, elytra_calculate_3D;

public Config() throws Exception {
Expand Down Expand Up @@ -69,12 +70,14 @@ public Config() throws Exception {
"A server restart is required when changing a command's enable status!");

// Elytra Speed
this.elytra_speed_calc_period = getInt("elytra.elytra-speed.check-period-ticks", 10,
"The period in ticks players will be checked to determine their speed.");
this.elytra_calculate_3D = getBoolean("elytra.elytra-speed.calculate-3D-speed", false,
"If set to false, will only calculate 2-Dimensional speed\n" +
"without taking height changes into consideration.");
this.elytra_actionbar_enabled = getBoolean("elytra.elytra-speed.display-actionbar", true,
"Display info in Actionbar while flying.");
this.elytra_show_chunkage = getBoolean("elytra.elytra-speed.display-chunk-info-in-actionbar", true,
this.elytra_show_chunk_age = getBoolean("elytra.elytra-speed.display-chunk-info-in-actionbar", true,
"Inform flying player if they are in old or new chunks.");
this.elytra_play_too_fast_sound = getBoolean("elytra.elytra-speed.play-sound-when-too-fast", true,
"Plays XP pickup sound to alert players when theyre going\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void onPlayerMove(PlayerMoveEvent event) {

if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_spawn_TooFastChunkInfo
.replace("%neworold%", lang.elytra_spawn_New)
.replace("%chunks%", lang.elytra_spawn_Chunks)
Expand All @@ -114,7 +114,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
} else {
if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_spawn_YouAreFlyingIn
.replace("%neworold%", lang.elytra_spawn_New_Color+lang.elytra_spawn_New.toUpperCase())
.replace("%chunks%", lang.elytra_spawn_Chunks)
Expand All @@ -139,7 +139,7 @@ private void onPlayerMove(PlayerMoveEvent event) {

if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_spawn_TooFastChunkInfo
.replace("%neworold%", lang.elytra_spawn_Old)
.replace("%chunks%", lang.elytra_spawn_Chunks)
Expand All @@ -151,7 +151,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
} else {
if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_spawn_YouAreFlyingIn
.replace("%neworold%", lang.elytra_spawn_Old_Color+lang.elytra_spawn_Old.toUpperCase())
.replace("%chunks%", lang.elytra_spawn_Chunks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void onPlayerMove(PlayerMoveEvent event) {

if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_global_TooFastChunkInfo
.replace("%neworold%", lang.elytra_global_New)
.replace("%chunks%", lang.elytra_global_Chunks)
Expand All @@ -120,7 +120,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
} else {
if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_global_YouAreFlyingIn
.replace("%neworold%", lang.elytra_global_New_Color+lang.elytra_global_New.toUpperCase())
.replace("%chunks%", lang.elytra_global_Chunks)
Expand Down Expand Up @@ -148,7 +148,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
if (global_EnableBursting) {
player.sendActionBar(lang.elytra_global_TooFastLowTPS);
} else {
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_global_TooFastChunkInfo
.replace("%neworold%", lang.elytra_global_New)
.replace("%chunks%", lang.elytra_global_Chunks)
Expand All @@ -160,7 +160,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
} else {
if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_global_YouAreFlyingIn
.replace("%neworold%", lang.elytra_global_New_Color+lang.elytra_global_New.toUpperCase())
.replace("%chunks%", lang.elytra_global_Chunks)
Expand Down Expand Up @@ -188,7 +188,7 @@ private void onPlayerMove(PlayerMoveEvent event) {

if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_global_TooFastChunkInfo
.replace("%neworold%", lang.elytra_global_Old)
.replace("%chunks%", lang.elytra_global_Chunks)
Expand All @@ -199,7 +199,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
} else {
if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_global_YouAreFlyingIn
.replace("%neworold%", lang.elytra_global_Old_Color+lang.elytra_global_Old.toUpperCase())
.replace("%chunks%", lang.elytra_global_Chunks)
Expand Down Expand Up @@ -227,7 +227,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
if (global_EnableBursting) {
player.sendActionBar(lang.elytra_global_TooFastLowTPS);
} else {
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_global_TooFastChunkInfo
.replace("%neworold%", lang.elytra_global_Old)
.replace("%chunks%", lang.elytra_global_Chunks)
Expand All @@ -239,7 +239,7 @@ private void onPlayerMove(PlayerMoveEvent event) {
} else {
if (!config.elytra_actionbar_enabled) return;
LanguageCache lang = AnarchyExploitFixes.getLang(player.getLocale());
if (config.elytra_show_chunkage) {
if (config.elytra_show_chunk_age) {
player.sendActionBar(lang.elytra_global_YouAreFlyingIn
.replace("%neworold%", lang.elytra_global_Old_Color+lang.elytra_global_Old.toUpperCase())
.replace("%chunks%", lang.elytra_global_Chunks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.NumberConversions;
import org.jetbrains.annotations.NotNull;

Expand All @@ -31,23 +30,27 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ElytraHelper extends AEFModule implements Disableable, Runnable, PacketListener, Listener {

private static ElytraHelper instance;
private final Map<UUID, PlayerData> playerDataMap;
private final NewChunksListener newChunksListener;
private final PacketListenerAbstract packetListener;
private BukkitTask scheduledTask;
private final long checkPeriod;
private final ScheduledExecutorService executorService;
private ScheduledFuture<?> scheduledTask;

public ElytraHelper() {
super("elytra.elytra-speed");
instance = this;
playerDataMap = new ConcurrentHashMap<>();
newChunksListener = ChunkUtil.canGetInhabitedTime() ? null : new NewChunksListener();
packetListener = asAbstract(PacketListenerPriority.MONITOR);
checkPeriod = config.getInt(configPath + ".check-period-ticks", 10);
executorService = Executors.newScheduledThreadPool(1);
}

public static ElytraHelper getInstance() {
Expand All @@ -58,14 +61,15 @@ public static ElytraHelper getInstance() {
public void enable() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
PacketEvents.getAPI().getEventManager().registerListener(packetListener);
scheduledTask = plugin.getServer().getScheduler().runTaskTimer(plugin, this, checkPeriod, checkPeriod);
scheduledTask = executorService.scheduleAtFixedRate(this, 50L, config.elytra_speed_calc_period * 50L, TimeUnit.MILLISECONDS);
}

@Override
public void disable() {
HandlerList.unregisterAll(this);
PacketEvents.getAPI().getEventManager().unregisterListener(packetListener);
if (scheduledTask != null) scheduledTask.cancel();
if (scheduledTask != null) scheduledTask.cancel(true);
executorService.shutdown();
}

@Override
Expand All @@ -76,7 +80,7 @@ public boolean shouldEnable() {
@Override
public void run() {
for (Map.Entry<UUID, PlayerData> entry : playerDataMap.entrySet()) {
entry.getValue().calcSpeedAvg(config.elytra_calculate_3D, checkPeriod);
entry.getValue().calcSpeedAvg(config.elytra_calculate_3D, config.elytra_speed_calc_period);
}
}

Expand Down
Loading

0 comments on commit fec390e

Please sign in to comment.