add global ticker

the performance is the same its still using like 100 - 300 heap usage
doesn't really do anyhting.,.,,.
real
This commit is contained in:
Chayapak 2023-05-01 10:08:48 +07:00
parent 7232defa7f
commit 8155ada338
9 changed files with 172 additions and 127 deletions

View file

@ -4,9 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="50f184fa-7bed-4956-baf5-7586ff26ea08" name="Changes" comment="fix mabe mabe&#10;untested"> <list default="true" id="50f184fa-7bed-4956-baf5-7586ff26ea08" name="Changes" comment="add bot.profile() mabe" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@ -57,19 +55,19 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"RunOnceActivity.OpenProjectViewOnStart": "true", &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"git-widget-placeholder": "master", &quot;git-widget-placeholder&quot;: &quot;master&quot;,
"jdk.selected.JAVA_MODULE": "17", &quot;jdk.selected.JAVA_MODULE&quot;: &quot;17&quot;,
"last_opened_file_path": "/home/chayapak1/projects/chipmunkmod", &quot;last_opened_file_path&quot;: &quot;/home/chayapak1/projects/chipmunkmod&quot;,
"project.structure.last.edited": "Modules", &quot;project.structure.last.edited&quot;: &quot;Modules&quot;,
"project.structure.proportion": "0.0", &quot;project.structure.proportion&quot;: &quot;0.0&quot;,
"project.structure.side.proportion": "0.0", &quot;project.structure.side.proportion&quot;: &quot;0.0&quot;,
"settings.editor.selected.configurable": "reference.settings.ide.settings.new.ui" &quot;settings.editor.selected.configurable&quot;: &quot;reference.settings.ide.settings.new.ui&quot;
} }
}]]></component> }</component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS"> <key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/.idea" /> <recent name="$PROJECT_DIR$/.idea" />
@ -111,20 +109,6 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1680245437032</updated> <updated>1680245437032</updated>
</task> </task>
<task id="LOCAL-00099" summary="sex">
<created>1682167370618</created>
<option name="number" value="00099" />
<option name="presentableId" value="LOCAL-00099" />
<option name="project" value="LOCAL" />
<updated>1682167370618</updated>
</task>
<task id="LOCAL-00100" summary="probably improve logquery?">
<created>1682168732774</created>
<option name="number" value="00100" />
<option name="presentableId" value="LOCAL-00100" />
<option name="project" value="LOCAL" />
<updated>1682168732774</updated>
</task>
<task id="LOCAL-00101" summary="forgor to set the thread to null"> <task id="LOCAL-00101" summary="forgor to set the thread to null">
<created>1682169054135</created> <created>1682169054135</created>
<option name="number" value="00101" /> <option name="number" value="00101" />
@ -454,7 +438,21 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1682854469605</updated> <updated>1682854469605</updated>
</task> </task>
<option name="localTasksCounter" value="148" /> <task id="LOCAL-00148" summary="patch skiddie exploit">
<created>1682901042864</created>
<option name="number" value="00148" />
<option name="presentableId" value="LOCAL-00148" />
<option name="project" value="LOCAL" />
<updated>1682901042865</updated>
</task>
<task id="LOCAL-00149" summary="add bot.profile() mabe">
<created>1682905214835</created>
<option name="number" value="00149" />
<option name="presentableId" value="LOCAL-00149" />
<option name="project" value="LOCAL" />
<updated>1682905214836</updated>
</task>
<option name="localTasksCounter" value="150" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
@ -469,8 +467,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="change stuff a bit" />
<MESSAGE value="fix fallback name?" />
<MESSAGE value="make time output shorter?&#10;mabe" /> <MESSAGE value="make time output shorter?&#10;mabe" />
<MESSAGE value="chat queue delay in config" /> <MESSAGE value="chat queue delay in config" />
<MESSAGE value="new 1.19.4 update i guess" /> <MESSAGE value="new 1.19.4 update i guess" />
@ -494,7 +490,9 @@
<MESSAGE value="packet queue farded it" /> <MESSAGE value="packet queue farded it" />
<MESSAGE value="i forgor" /> <MESSAGE value="i forgor" />
<MESSAGE value="fix mabe mabe&#10;untested" /> <MESSAGE value="fix mabe mabe&#10;untested" />
<option name="LAST_COMMIT_MESSAGE" value="fix mabe mabe&#10;untested" /> <MESSAGE value="patch skiddie exploit" />
<MESSAGE value="add bot.profile() mabe" />
<option name="LAST_COMMIT_MESSAGE" value="add bot.profile() mabe" />
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>

View file

@ -46,6 +46,7 @@ public class Bot {
@Getter @Setter private LoggerPlugin logger; // in ConsolePlugin @Getter @Setter private LoggerPlugin logger; // in ConsolePlugin
@Getter @Setter private DiscordPlugin discord; // same for this one too @Getter @Setter private DiscordPlugin discord; // same for this one too
@Getter private TickPlugin tick;
@Getter private ChatPlugin chat; @Getter private ChatPlugin chat;
@Getter private PositionPlugin position; @Getter private PositionPlugin position;
@Getter private SelfCarePlugin selfCare; @Getter private SelfCarePlugin selfCare;
@ -89,6 +90,7 @@ public class Bot {
} }
public void ready () { public void ready () {
this.tick = new TickPlugin(this);
this.chat = new ChatPlugin(this); this.chat = new ChatPlugin(this);
this.position = new PositionPlugin(this); this.position = new PositionPlugin(this);
this.selfCare = new SelfCarePlugin(this); this.selfCare = new SelfCarePlugin(this);

View file

@ -1,6 +1,5 @@
package land.chipmunk.chayapak.chomens_bot.plugins; package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.BossBar; import land.chipmunk.chayapak.chomens_bot.data.BossBar;
import lombok.Getter; import lombok.Getter;
@ -9,14 +8,10 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class BossbarManagerPlugin extends Bot.Listener { public class BossbarManagerPlugin extends Bot.Listener {
private final Bot bot; private final Bot bot;
private ScheduledFuture<?> tickTask;
private final Map<String, BossBar> bossBars = new HashMap<>(); private final Map<String, BossBar> bossBars = new HashMap<>();
@Getter @Setter private boolean enabled = true; @Getter @Setter private boolean enabled = true;
@ -28,25 +23,16 @@ public class BossbarManagerPlugin extends Bot.Listener {
public BossbarManagerPlugin (Bot bot) { public BossbarManagerPlugin (Bot bot) {
this.bot = bot; this.bot = bot;
bot.core().addListener(new CorePlugin.Listener() { bot.tick().addListener(new TickPlugin.Listener() {
@Override @Override
public void ready() { public void onTick() {
BossbarManagerPlugin.this.ready(); tick();
} }
}); });
bot.addListener(this); bot.addListener(this);
} }
public void ready () {
tickTask = bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS);
}
@Override
public void disconnected(DisconnectedEvent event) {
tickTask.cancel(true);
}
public void tick () { public void tick () {
if (!enabled) return; if (!enabled) return;
for (Map.Entry<String, BossBar> _bossBar : bossBars.entrySet()) { for (Map.Entry<String, BossBar> _bossBar : bossBars.entrySet()) {

View file

@ -22,7 +22,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
// please ignore my shitcode // please ignore my shitcode
public class DiscordPlugin { public class DiscordPlugin {
@ -71,7 +70,12 @@ public class DiscordPlugin {
if (channelAlreadyAddedListeners) return; if (channelAlreadyAddedListeners) return;
bot.executor().scheduleAtFixedRate(() -> onDiscordTick(channelId), 0, 50, TimeUnit.MILLISECONDS); bot.tick().addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
onDiscordTick(channelId);
}
});
bot.chat().addListener(new ChatPlugin.ChatListener() { bot.chat().addListener(new ChatPlugin.ChatListener() {
@Override @Override

View file

@ -10,7 +10,6 @@ import net.kyori.adventure.text.format.NamedTextColor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class FilterPlugin extends PlayersPlugin.PlayerListener { public class FilterPlugin extends PlayersPlugin.PlayerListener {
@ -23,7 +22,12 @@ public class FilterPlugin extends PlayersPlugin.PlayerListener {
bot.players().addListener(this); bot.players().addListener(this);
bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS); bot.tick().addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
tick();
}
});
} }
private FilteredPlayer getPlayer (String name) { private FilteredPlayer getPlayer (String name) {

View file

@ -5,7 +5,6 @@ import land.chipmunk.chayapak.chomens_bot.Bot;
import lombok.Getter; import lombok.Getter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
public class HashingPlugin { public class HashingPlugin {
private final Bot bot; private final Bot bot;
@ -16,7 +15,12 @@ public class HashingPlugin {
public HashingPlugin (Bot bot) { public HashingPlugin (Bot bot) {
this.bot = bot; this.bot = bot;
bot.executor().scheduleAtFixedRate(this::update, 0, 1, TimeUnit.SECONDS); bot.tick().addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
update();
}
});
} }
public void update () { public void update () {

View file

@ -17,14 +17,10 @@ import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class MusicPlayerPlugin extends Bot.Listener { public class MusicPlayerPlugin extends Bot.Listener {
private final Bot bot; private final Bot bot;
private ScheduledFuture<?> playTask;
public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]"; public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]";
public static File SONG_DIR = new File("songs"); public static File SONG_DIR = new File("songs");
static { static {
@ -90,76 +86,77 @@ public class MusicPlayerPlugin extends Bot.Listener {
} }
public void coreReady () { public void coreReady () {
final Runnable task = () -> { bot.tick().addListener(new TickPlugin.Listener() {
if (loaderThread != null && !loaderThread.isAlive()) { @Override
if (loaderThread.exception != null) { public void onTick() {
bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED)); if (loaderThread != null && !loaderThread.isAlive()) {
} else { if (loaderThread.exception != null) {
songQueue.add(loaderThread.song); bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED));
bot.chat().tellraw(Component.translatable("Added %s to the song queue", Component.empty().append(loaderThread.song.name).color(NamedTextColor.GOLD))); } else {
} songQueue.add(loaderThread.song);
loaderThread = null; bot.chat().tellraw(Component.translatable("Added %s to the song queue", Component.empty().append(loaderThread.song.name).color(NamedTextColor.GOLD)));
} }
loaderThread = null;
if (currentSong == null) {
if (songQueue.size() == 0) return;
addBossBar();
currentSong = songQueue.get(0); // songQueue.poll();
bot.chat().tellraw(Component.translatable("Now playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
currentSong.play();
}
if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return;
else ticksUntilPausedBossbar = 20;
BossBar bossBar = bot.bossbar().get(bossbarName);
if (bossBar == null) bossBar = addBossBar();
bossBar.players(SELECTOR);
bossBar.name(generateBossbar());
bossBar.color(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW);
bossBar.visible(true);
bossBar.style(BossBarStyle.PROGRESS);
bossBar.value((int) Math.floor(currentSong.time * speed));
bossBar.max(currentSong.length);
if (currentSong.paused) return;
handlePlaying();
if (currentSong.finished()) {
if (loop == Loop.CURRENT) {
currentSong.setTime(0);
return;
} }
bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); if (currentSong == null) {
if (songQueue.size() == 0) return;
if (loop == Loop.ALL) { addBossBar();
skip();
return;
}
songQueue.remove(); currentSong = songQueue.get(0); // songQueue.poll();
bot.chat().tellraw(Component.translatable("Now playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
if (songQueue.size() == 0) {
stopPlaying();
removeBossBar();
bot.chat().tellraw(Component.text("Finished playing every song in the queue"));
return;
}
if (currentSong.size() > 0) {
currentSong = songQueue.get(0);
currentSong.setTime(0);
currentSong.play(); currentSong.play();
} }
}
};
playTask = bot.executor().scheduleAtFixedRate(task, 50, 50, TimeUnit.MILLISECONDS); if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return;
else ticksUntilPausedBossbar = 20;
BossBar bossBar = bot.bossbar().get(bossbarName);
if (bossBar == null) bossBar = addBossBar();
bossBar.players(SELECTOR);
bossBar.name(generateBossbar());
bossBar.color(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW);
bossBar.visible(true);
bossBar.style(BossBarStyle.PROGRESS);
bossBar.value((int) Math.floor(currentSong.time * speed));
bossBar.max(currentSong.length);
if (currentSong.paused) return;
handlePlaying();
if (currentSong.finished()) {
if (loop == Loop.CURRENT) {
currentSong.setTime(0);
return;
}
bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
if (loop == Loop.ALL) {
skip();
return;
}
songQueue.remove();
if (songQueue.size() == 0) {
stopPlaying();
removeBossBar();
bot.chat().tellraw(Component.text("Finished playing every song in the queue"));
return;
}
if (currentSong.size() > 0) {
currentSong = songQueue.get(0);
currentSong.setTime(0);
currentSong.play();
}
}
}
});
if (currentSong != null) currentSong.play(); if (currentSong != null) currentSong.play();
} }
@ -248,8 +245,6 @@ public class MusicPlayerPlugin extends Bot.Listener {
@Override @Override
public void disconnected (DisconnectedEvent event) { public void disconnected (DisconnectedEvent event) {
playTask.cancel(true);
if (currentSong != null) currentSong.pause(); // nice. if (currentSong != null) currentSong.pause(); // nice.
} }

View file

@ -13,7 +13,6 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit;
public class TPSPlugin extends Bot.Listener { public class TPSPlugin extends Bot.Listener {
private final Bot bot; private final Bot bot;
@ -35,7 +34,12 @@ public class TPSPlugin extends Bot.Listener {
bot.core().addListener(new CorePlugin.Listener() { bot.core().addListener(new CorePlugin.Listener() {
@Override @Override
public void ready() { public void ready() {
bot.executor().scheduleAtFixedRate(() -> updateTPSBar(), 0, 50, TimeUnit.MILLISECONDS); bot.tick().addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
updateTPSBar();
}
});
} }
} }
); );

View file

@ -0,0 +1,48 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import land.chipmunk.chayapak.chomens_bot.Bot;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class TickPlugin extends Bot.Listener {
private final Bot bot;
private ScheduledFuture<?> tickTask;
private final List<Listener> listeners = new ArrayList<>();
public TickPlugin (Bot bot) {
this.bot = bot;
bot.addListener(this);
}
@Override
public void connected(ConnectedEvent event) {
tickTask = bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS);
}
private void tick () {
for (Listener listener : listeners) {
listener.onTick();
}
}
@Override
public void disconnected (DisconnectedEvent event) {
tickTask.cancel(true);
}
public void addListener (Listener listener) {
listeners.add(listener);
}
public static class Listener {
public void onTick () {}
}
}