more stuff + now ik why hashing break

well thats because yaml moment. i chaanged the key
and it workdde fine so yeah yaml moment
it sucks
This commit is contained in:
ChomeNS 2023-03-23 19:20:06 +07:00
parent 7e1931f9b5
commit 3f28d95135
6 changed files with 127 additions and 133 deletions

View file

@ -13,9 +13,8 @@ import org.apache.commons.lang3.RandomStringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors; import java.util.Timer;
import java.util.concurrent.ScheduledExecutorService; import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class Bot { public class Bot {
private final ArrayList<SessionListener> listeners = new ArrayList<>(); private final ArrayList<SessionListener> listeners = new ArrayList<>();
@ -30,8 +29,6 @@ public class Bot {
@Getter private Session session; @Getter private Session session;
@Getter private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(69);
@Getter @Setter private ConsolePlugin console; @Getter @Setter private ConsolePlugin console;
@Getter @Setter private LoggerPlugin logger; // in ConsolePlugin @Getter @Setter private LoggerPlugin logger; // in ConsolePlugin
@Getter private final ChatPlugin chat; @Getter private final ChatPlugin chat;
@ -50,14 +47,14 @@ public class Bot {
this.allBots = allBots; this.allBots = allBots;
this.config = config; this.config = config;
chat = new ChatPlugin(this); this.chat = new ChatPlugin(this);
selfCare = new SelfCarePlugin(this); this.selfCare = new SelfCarePlugin(this);
position = new PositionPlugin(this); this.position = new PositionPlugin(this);
core = new CorePlugin(this); this.core = new CorePlugin(this);
commandHandler = new CommandHandlerPlugin(); this.commandHandler = new CommandHandlerPlugin();
chatCommandHandler = new ChatCommandHandlerPlugin(this); this.chatCommandHandler = new ChatCommandHandlerPlugin(this);
hashing = new HashingPlugin(this); this.hashing = new HashingPlugin(this);
music = new MusicPlayerPlugin(this); this.music = new MusicPlayerPlugin(this);
reconnect(); reconnect();
} }
@ -121,9 +118,13 @@ public class Bot {
if (reconnectDelay < 0) return; // to disable reconnecting if (reconnectDelay < 0) return; // to disable reconnecting
Runnable task = () -> reconnect(); final Timer timer = new Timer();
timer.schedule(new TimerTask() {
executor.schedule(task, reconnectDelay, TimeUnit.MILLISECONDS); @Override
public void run() {
reconnect();
}
}, reconnectDelay, 1);
} }
}); });

View file

@ -18,11 +18,7 @@ import com.nukkitx.math.vector.Vector3i;
import lombok.Getter; import lombok.Getter;
import me.chayapak1.chomensbot_mabe.Bot; import me.chayapak1.chomensbot_mabe.Bot;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class CorePlugin extends PositionPlugin.PositionListener { public class CorePlugin extends PositionPlugin.PositionListener {
private final Bot bot; private final Bot bot;
@ -107,7 +103,14 @@ public class CorePlugin extends PositionPlugin.PositionListener {
if (!ready) { if (!ready) {
ready = true; ready = true;
bot.executor().schedule(this::refill, bot.config().core().refillInterval(), TimeUnit.MILLISECONDS);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
refill();
}
}, 1, bot.config().core().refillInterval());
for (Listener listener : listeners) listener.ready(); for (Listener listener : listeners) listener.ready();
} }
} }

View file

@ -1,13 +1,12 @@
package me.chayapak1.chomensbot_mabe.plugins; package me.chayapak1.chomensbot_mabe.plugins;
import com.google.common.hash.Hashing;
import lombok.Getter; import lombok.Getter;
import me.chayapak1.chomensbot_mabe.Bot; import me.chayapak1.chomensbot_mabe.Bot;
import me.chayapak1.chomensbot_mabe.util.Hexadecimal;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.util.Timer;
import java.security.NoSuchAlgorithmException; import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class HashingPlugin { public class HashingPlugin {
private final Bot bot; private final Bot bot;
@ -18,28 +17,27 @@ public class HashingPlugin {
public HashingPlugin (Bot bot) { public HashingPlugin (Bot bot) {
this.bot = bot; this.bot = bot;
bot.executor().scheduleAtFixedRate(this::update, 1000 * 2, 500, TimeUnit.MILLISECONDS); final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
update();
}
}, 1, 1000);
} }
public void update () { public void update () {
final String normalHashKey = bot.config().keys().get("normalKey"); final String normalHashKey = bot.config().keys().get("normalKey");
final String ownerHashKey = bot.config().keys().get("ownerKey"); final String ownerHashKey = bot.config().keys().get("ownerKey");
try { final String hashValue = (System.currentTimeMillis() / 10_000) + normalHashKey;
MessageDigest md = MessageDigest.getInstance("SHA-256"); hash = Hashing.sha256()
String time = String.valueOf(System.currentTimeMillis() / 10000); .hashString(hashValue, StandardCharsets.UTF_8)
.toString().substring(0, 16);
// messy final String ownerHashValue = (System.currentTimeMillis() / 10_000) + ownerHashKey;
String normalHashInput = time + normalHashKey; ownerHash = Hashing.sha256()
byte[] normalHashByteHash = md.digest(normalHashInput.getBytes(StandardCharsets.UTF_8)); .hashString(ownerHashValue, StandardCharsets.UTF_8)
hash = Hexadecimal.encode(normalHashByteHash).substring(0, 16); .toString().substring(0, 16);
String ownerHashInput = time + ownerHashKey;
byte[] ownerHashByteHash = md.digest(ownerHashInput.getBytes(StandardCharsets.UTF_8));
ownerHash = Hexadecimal.encode(ownerHashByteHash).substring(0, 16);
bot.logger().log("normal hash input " + normalHashInput + " owner " + ownerHashInput);
bot.logger().log(hash + " " + ownerHash);
} catch (NoSuchAlgorithmException ignored) {}
} }
} }

View file

@ -18,13 +18,13 @@ 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.Timer;
import java.util.concurrent.TimeUnit; import java.util.TimerTask;
public class MusicPlayerPlugin extends SessionAdapter { public class MusicPlayerPlugin extends SessionAdapter {
private final Bot bot; private final Bot bot;
private ScheduledFuture<?> futurePlayTask; private TimerTask 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");
@ -85,69 +85,73 @@ public class MusicPlayerPlugin extends SessionAdapter {
} }
public void coreReady () { public void coreReady () {
final Runnable playTask = () -> { playTask = new TimerTask() {
if (loaderThread != null && !loaderThread.isAlive()) { @Override
if (loaderThread.exception != null) { public void run() {
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;
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;
bot.core().run("minecraft:bossbar add " + bossbarName + " \"\"");
bot.core().run("minecraft:bossbar set " + bossbarName + " players " + SELECTOR);
bot.core().run("minecraft:bossbar set " + bossbarName + " name " + GsonComponentSerializer.gson().serialize(generateBossbar()));
bot.core().run("minecraft:bossbar set " + bossbarName + " color yellow");
bot.core().run("minecraft:bossbar set " + bossbarName + " visible true");
bot.core().run("minecraft:bossbar set " + bossbarName + " value " + (int) Math.floor(currentSong.time));
bot.core().run("minecraft:bossbar set " + bossbarName + " max " + currentSong.length);
if (currentSong.paused) return;
handlePlaying();
if (currentSong.finished()) {
removeBossbar();
bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
if (loop == 1) {
currentSong.setTime(0);
return;
}
if (loop == 2) {
skip();
return;
} }
songQueue.remove(); if (currentSong == null) {
if (songQueue.size() == 0) return;
if (songQueue.size() == 0) { currentSong = songQueue.get(0); // songQueue.poll();
stopPlaying(); bot.chat().tellraw(Component.translatable("Now playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
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();
} }
if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return;
else ticksUntilPausedBossbar = 20;
bot.core().run("minecraft:bossbar add " + bossbarName + " \"\"");
bot.core().run("minecraft:bossbar set " + bossbarName + " players " + SELECTOR);
bot.core().run("minecraft:bossbar set " + bossbarName + " name " + GsonComponentSerializer.gson().serialize(generateBossbar()));
bot.core().run("minecraft:bossbar set " + bossbarName + " color yellow");
bot.core().run("minecraft:bossbar set " + bossbarName + " visible true");
bot.core().run("minecraft:bossbar set " + bossbarName + " value " + (int) Math.floor(currentSong.time));
bot.core().run("minecraft:bossbar set " + bossbarName + " max " + currentSong.length);
if (currentSong.paused) return;
handlePlaying();
if (currentSong.finished()) {
removeBossbar();
bot.chat().tellraw(Component.translatable("Finished playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD)));
if (loop == 1) {
currentSong.setTime(0);
return;
}
if (loop == 2) {
skip();
return;
}
songQueue.remove();
if (songQueue.size() == 0) {
stopPlaying();
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();
}
}
} }
}; };
futurePlayTask = bot.executor().scheduleAtFixedRate(playTask, 50, 50, TimeUnit.MILLISECONDS); final Timer timer = new Timer();
timer.schedule(playTask, 1, 50);
if (currentSong != null) currentSong.play(); if (currentSong != null) currentSong.play();
} }
@ -220,7 +224,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
@Override @Override
public void disconnected (DisconnectedEvent event) { public void disconnected (DisconnectedEvent event) {
futurePlayTask.cancel(false); playTask.cancel();
if (currentSong != null) currentSong.pause(); // nice. if (currentSong != null) currentSong.pause(); // nice.
} }

View file

@ -22,13 +22,13 @@ import me.chayapak1.chomensbot_mabe.Bot;
import me.chayapak1.chomensbot_mabe.Configuration; import me.chayapak1.chomensbot_mabe.Configuration;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.concurrent.ScheduledFuture; import java.util.Timer;
import java.util.concurrent.TimeUnit; import java.util.TimerTask;
public class SelfCarePlugin extends SessionAdapter { public class SelfCarePlugin extends SessionAdapter {
private final Bot bot; private final Bot bot;
private ScheduledFuture<?> futureTask; private TimerTask checkTask;
@Getter @Setter boolean visibility = false; @Getter @Setter boolean visibility = false;
@ -108,21 +108,25 @@ public class SelfCarePlugin extends SessionAdapter {
muted = false; muted = false;
prefix = false; prefix = false;
final Runnable task = () -> { checkTask = new TimerTask() {
final Session session = bot.session(); @Override
final PacketProtocol protocol = session.getPacketProtocol(); public void run() {
if ( final Session session = bot.session();
!session.isConnected() || final PacketProtocol protocol = session.getPacketProtocol();
( if (
protocol instanceof MinecraftProtocol && !session.isConnected() ||
((MinecraftProtocol) protocol).getState() != ProtocolState.GAME (
) protocol instanceof MinecraftProtocol &&
) return; ((MinecraftProtocol) protocol).getState() != ProtocolState.GAME
)
) return;
check(); check();
}
}; };
futureTask = bot.executor().scheduleAtFixedRate(task, bot.config().selfCare().checkInterval(), 500, TimeUnit.MILLISECONDS); final Timer timer = new Timer();
timer.schedule(checkTask, 1, bot.config().selfCare().checkInterval());
} }
public void packetReceived (ClientboundGameEventPacket packet) { public void packetReceived (ClientboundGameEventPacket packet) {
@ -152,6 +156,6 @@ public class SelfCarePlugin extends SessionAdapter {
@Override @Override
public void disconnected (DisconnectedEvent event) { public void disconnected (DisconnectedEvent event) {
futureTask.cancel(true); checkTask.cancel();
} }
} }

View file

@ -1,16 +0,0 @@
package me.chayapak1.chomensbot_mabe.util;
public interface Hexadecimal {
static String encode (byte b) {
return "" + Character.forDigit((b >> 4) & 0xF, 16) + Character.forDigit((b & 0xF), 16);
}
static String encode (byte[] array) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) sb.append(encode(array[i]));
return sb.toString();
}
// TODO: Decode
}