fix a very major problem and code improvement

This commit is contained in:
Chayapak 2023-06-26 16:55:20 +07:00
parent a4213669e3
commit a1d1717175
2 changed files with 41 additions and 15 deletions

View file

@ -76,7 +76,7 @@ public class CorePlugin extends PositionPlugin.Listener {
bot.position().addListener(this); bot.position().addListener(this);
if (bot.options().coreRateLimit().limit() != 0 && bot.options().coreRateLimit().reset() != 0) { if (hasRateLimit() && hasReset()) {
bot.executor().scheduleAtFixedRate( bot.executor().scheduleAtFixedRate(
() -> commandsPerSecond = 0, () -> commandsPerSecond = 0,
0, 0,
@ -105,6 +105,18 @@ public class CorePlugin extends PositionPlugin.Listener {
}); });
} }
public boolean hasRateLimit () {
return bot.options().coreRateLimit().limit() > 0;
}
public boolean hasReset () {
return bot.options().coreRateLimit().reset() > 0;
}
public boolean isRateLimited () {
return commandsPerSecond > bot.options().coreRateLimit().limit();
}
private void forceRun (String command) { private void forceRun (String command) {
bot.session().send(new ServerboundSetCommandBlockPacket( bot.session().send(new ServerboundSetCommandBlockPacket(
absoluteCorePosition(), absoluteCorePosition(),
@ -122,10 +134,11 @@ public class CorePlugin extends PositionPlugin.Listener {
if (!ready) return; if (!ready) return;
if (bot.options().useCore()) { if (bot.options().useCore()) {
if (bot.options().coreRateLimit().limit() != 0 && commandsPerSecond > bot.options().coreRateLimit().limit()) return; if (isRateLimited() && hasRateLimit()) return;
forceRun(command); forceRun(command);
commandsPerSecond++;
if (hasRateLimit()) commandsPerSecond++;
} else if (command.length() < 256) { } else if (command.length() < 256) {
bot.chat().send("/" + command); bot.chat().send("/" + command);
} }

View file

@ -16,7 +16,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.Instant; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -41,8 +41,6 @@ public class MusicPlayerPlugin extends Bot.Listener {
private int ticksUntilPausedBossbar = 20; private int ticksUntilPausedBossbar = 20;
private long nextBossBarUpdate = Instant.now().toEpochMilli();
private final String bossbarName = "music"; private final String bossbarName = "music";
public MusicPlayerPlugin (Bot bot) { public MusicPlayerPlugin (Bot bot) {
@ -120,17 +118,13 @@ public class MusicPlayerPlugin extends Bot.Listener {
if (bossBar == null && bot.bossbar().enabled()) bossBar = addBossBar(); if (bossBar == null && bot.bossbar().enabled()) bossBar = addBossBar();
final long currentTime = Instant.now().toEpochMilli(); if (bot.bossbar().enabled()) {
if (currentTime >= nextBossBarUpdate && bossBar != null) {
bossBar.setTitle(generateBossbar()); bossBar.setTitle(generateBossbar());
bossBar.setColor(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW); bossBar.setColor(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW);
bossBar.setValue((int) Math.floor(currentSong.time * speed)); bossBar.setValue((int) Math.floor(((double) currentSong.time / 1000) * speed));
nextBossBarUpdate = currentTime + 500;
} }
if (currentSong.paused || bot.core().commandsPerSecond() > bot.options().coreRateLimit().limit()) return; if (currentSong.paused || bot.core().isRateLimited()) return;
handlePlaying(); handlePlaying();
@ -198,7 +192,7 @@ public class MusicPlayerPlugin extends Bot.Listener {
BossBarColor.WHITE, BossBarColor.WHITE,
BossBarDivision.NONE, BossBarDivision.NONE,
true, true,
(int) currentSong.length, (int) currentSong.length / 1000,
0, 0,
bot bot
); );
@ -216,7 +210,26 @@ public class MusicPlayerPlugin extends Bot.Listener {
Component component = Component.empty() Component component = Component.empty()
.append(Component.empty().append(currentSong.name).color(pitch > 0 ? NamedTextColor.LIGHT_PURPLE : NamedTextColor.GREEN)) .append(Component.empty().append(currentSong.name).color(pitch > 0 ? NamedTextColor.LIGHT_PURPLE : NamedTextColor.GREEN))
.append(Component.text(" | ").color(NamedTextColor.DARK_GRAY)) .append(Component.text(" | ").color(NamedTextColor.DARK_GRAY))
.append(Component.translatable("%s / %s", formatTime((long) (currentSong.time * speed)).color(NamedTextColor.GRAY), formatTime(currentSong.length).color(NamedTextColor.GRAY)).color(NamedTextColor.DARK_GRAY)); .append(
Component
.translatable("%s / %s",
formatTime((long) (currentSong.time * speed)).color(NamedTextColor.GRAY),
formatTime(currentSong.length).color(NamedTextColor.GRAY)).color(NamedTextColor.DARK_GRAY)
);
if (!bot.core().hasRateLimit()) {
final DecimalFormat formatter = new DecimalFormat("#,###");
component = component
.append(Component.text(" | ").color(NamedTextColor.DARK_GRAY))
.append(
Component.translatable(
"%s / %s",
Component.text(formatter.format(currentSong.position), NamedTextColor.GRAY),
Component.text(formatter.format(currentSong.size()), NamedTextColor.GRAY)
).color(NamedTextColor.DARK_GRAY)
);
}
if (currentSong.paused) { if (currentSong.paused) {
return component return component