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

View file

@ -18,11 +18,7 @@ import com.nukkitx.math.vector.Vector3i;
import lombok.Getter;
import me.chayapak1.chomensbot_mabe.Bot;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.*;
public class CorePlugin extends PositionPlugin.PositionListener {
private final Bot bot;
@ -107,7 +103,14 @@ public class CorePlugin extends PositionPlugin.PositionListener {
if (!ready) {
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();
}
}

View file

@ -1,13 +1,12 @@
package me.chayapak1.chomensbot_mabe.plugins;
import com.google.common.hash.Hashing;
import lombok.Getter;
import me.chayapak1.chomensbot_mabe.Bot;
import me.chayapak1.chomensbot_mabe.util.Hexadecimal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
import java.util.Timer;
import java.util.TimerTask;
public class HashingPlugin {
private final Bot bot;
@ -18,28 +17,27 @@ public class HashingPlugin {
public HashingPlugin (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 () {
final String normalHashKey = bot.config().keys().get("normalKey");
final String ownerHashKey = bot.config().keys().get("ownerKey");
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
String time = String.valueOf(System.currentTimeMillis() / 10000);
final String hashValue = (System.currentTimeMillis() / 10_000) + normalHashKey;
hash = Hashing.sha256()
.hashString(hashValue, StandardCharsets.UTF_8)
.toString().substring(0, 16);
// messy
String normalHashInput = time + normalHashKey;
byte[] normalHashByteHash = md.digest(normalHashInput.getBytes(StandardCharsets.UTF_8));
hash = Hexadecimal.encode(normalHashByteHash).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) {}
final String ownerHashValue = (System.currentTimeMillis() / 10_000) + ownerHashKey;
ownerHash = Hashing.sha256()
.hashString(ownerHashValue, StandardCharsets.UTF_8)
.toString().substring(0, 16);
}
}

View file

@ -18,13 +18,13 @@ import java.net.URL;
import java.nio.file.Path;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.Timer;
import java.util.TimerTask;
public class MusicPlayerPlugin extends SessionAdapter {
private final Bot bot;
private ScheduledFuture<?> futurePlayTask;
private TimerTask playTask;
public static final String SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]";
public static File SONG_DIR = new File("songs");
@ -85,69 +85,73 @@ public class MusicPlayerPlugin extends SessionAdapter {
}
public void coreReady () {
final Runnable playTask = () -> {
if (loaderThread != null && !loaderThread.isAlive()) {
if (loaderThread.exception != null) {
bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED));
} else {
songQueue.add(loaderThread.song);
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;
playTask = new TimerTask() {
@Override
public void run() {
if (loaderThread != null && !loaderThread.isAlive()) {
if (loaderThread.exception != null) {
bot.chat().tellraw(Component.translatable("Failed to load song: %s", loaderThread.exception.message()).color(NamedTextColor.RED));
} else {
songQueue.add(loaderThread.song);
bot.chat().tellraw(Component.translatable("Added %s to the song queue", Component.empty().append(loaderThread.song.name).color(NamedTextColor.GOLD)));
}
loaderThread = null;
}
songQueue.remove();
if (currentSong == null) {
if (songQueue.size() == 0) return;
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 = 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 (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();
}
@ -220,7 +224,7 @@ public class MusicPlayerPlugin extends SessionAdapter {
@Override
public void disconnected (DisconnectedEvent event) {
futurePlayTask.cancel(false);
playTask.cancel();
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 net.kyori.adventure.text.Component;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.Timer;
import java.util.TimerTask;
public class SelfCarePlugin extends SessionAdapter {
private final Bot bot;
private ScheduledFuture<?> futureTask;
private TimerTask checkTask;
@Getter @Setter boolean visibility = false;
@ -108,21 +108,25 @@ public class SelfCarePlugin extends SessionAdapter {
muted = false;
prefix = false;
final Runnable task = () -> {
final Session session = bot.session();
final PacketProtocol protocol = session.getPacketProtocol();
if (
!session.isConnected() ||
(
protocol instanceof MinecraftProtocol &&
((MinecraftProtocol) protocol).getState() != ProtocolState.GAME
)
) return;
checkTask = new TimerTask() {
@Override
public void run() {
final Session session = bot.session();
final PacketProtocol protocol = session.getPacketProtocol();
if (
!session.isConnected() ||
(
protocol instanceof MinecraftProtocol &&
((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) {
@ -152,6 +156,6 @@ public class SelfCarePlugin extends SessionAdapter {
@Override
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
}