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);
if (bot.options().coreRateLimit().limit() != 0 && bot.options().coreRateLimit().reset() != 0) {
if (hasRateLimit() && hasReset()) {
bot.executor().scheduleAtFixedRate(
() -> commandsPerSecond = 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) {
bot.session().send(new ServerboundSetCommandBlockPacket(
absoluteCorePosition(),
@ -122,10 +134,11 @@ public class CorePlugin extends PositionPlugin.Listener {
if (!ready) return;
if (bot.options().useCore()) {
if (bot.options().coreRateLimit().limit() != 0 && commandsPerSecond > bot.options().coreRateLimit().limit()) return;
if (isRateLimited() && hasRateLimit()) return;
forceRun(command);
commandsPerSecond++;
if (hasRateLimit()) commandsPerSecond++;
} else if (command.length() < 256) {
bot.chat().send("/" + command);
}

View file

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