Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 5 additions & 33 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

group = "cn.huohuas001.huHoBot"
description = "HuHoBot Allay Adapter"
version = "0.0.9"
version = "0.0.10"

java {
toolchain {
Expand All @@ -17,19 +17,16 @@ java {

repositories {
mavenCentral()
maven("https://jitpack.io/")
maven("https://repo.opencollab.dev/maven-releases/")
maven("https://repo.opencollab.dev/maven-snapshots/")
maven("https://storehouse.okaeri.eu/repository/maven-public/")
}

dependencies {
compileOnly(group = "org.allaymc.allay", name = "api", version = "master-SNAPSHOT")
compileOnly(group = "org.allaymc.allay", name = "api", version = "0.13.0")
compileOnly(group = "org.projectlombok", name = "lombok", version = "1.18.34")

implementation("org.java-websocket:Java-WebSocket:1.5.4")
implementation("com.alibaba.fastjson2:fastjson2:2.0.52")
implementation(group = "com.github.MineBuilders", name = "allaymc-kotlinx", version = "master-SNAPSHOT")
implementation(group = "org.java-websocket", name = "Java-WebSocket", version = "1.5.4")
implementation(group = "com.alibaba.fastjson2", name = "fastjson2", version = "2.0.52")
implementation(group = "eu.okaeri", name = "okaeri-configs-yaml-snakeyaml", version = "5.0.13")

annotationProcessor(group = "org.projectlombok", name = "lombok", version = "1.18.34")
}
Expand Down Expand Up @@ -79,28 +76,3 @@ tasks.clean {
tasks.shadowJar {
archiveClassifier = "AllayMC"
}

tasks.register<Copy>("runServer") {
outputs.upToDateWhen { false }
dependsOn("shadowJar")
val launcherRepo = "https://raw.githubusercontent.com/AllayMC/AllayLauncher/refs/heads/main/scripts"
val cmdWin = "Invoke-Expression (Invoke-WebRequest -Uri \"${launcherRepo}/install_windows.ps1\").Content"
val cmdLinux = "wget -qO- ${launcherRepo}/install_linux.sh | bash"
val cwd = layout.buildDirectory.file("run").get().asFile

val shadowJar = tasks.named("shadowJar", ShadowJar::class).get()
from(shadowJar.archiveFile.get().asFile)
into(cwd.resolve("plugins").apply { mkdirs() })

val isDownloaded = cwd.listFiles()!!.any { it.isFile && it.nameWithoutExtension == "allay" }
val isWindows = System.getProperty("os.name").startsWith("Windows")
fun launch() = exec {
workingDir = cwd
val cmd = if (isDownloaded) "./allay" else if (isWindows) cmdWin else cmdLinux
if (isWindows) commandLine("powershell", "-Command", cmd)
else commandLine("sh", "-c", cmd)
}

// https://github.com/gradle/gradle/issues/18716 // kill it manually by click X...
doLast { launch() }
}
25 changes: 5 additions & 20 deletions src/main/java/cn/huohuas001/huHoBot/Api/BotCustomCommand.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package cn.huohuas001.huHoBot.Api;

import cn.huohuas001.huHoBot.HuHoBot;
import cn.huohuas001.huHoBot.WsClient;
import com.alibaba.fastjson2.JSONObject;
import lombok.AccessLevel;
import lombok.Getter;
import org.allaymc.api.eventbus.event.CancellableEvent;
import org.allaymc.api.eventbus.event.Event;

import java.util.List;

@Getter
public class BotCustomCommand extends Event implements CancellableEvent {
@Getter

private final String command;
@Getter
private final JSONObject data;
@Getter
private final List<String> param;
@Getter(AccessLevel.NONE)
private final String packId;
@Getter
private final boolean runByAdmin;
private boolean cancelled = false;

public BotCustomCommand(String command, JSONObject data, String packId, boolean runByAdmin) {
this.command = command;
Expand All @@ -37,19 +37,4 @@ public void respone(JSONObject msg, String type) {
WsClient client = HuHoBot.getClientManager().getClient();
client.respone(msg.toJSONString(), type, packId);
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean b) {
cancelled = b;
}

@Override
public void cancel() {
setCancelled(true);
}
}
70 changes: 24 additions & 46 deletions src/main/java/cn/huohuas001/huHoBot/Command/HuHoBotCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,66 @@

import cn.huohuas001.huHoBot.HuHoBot;
import cn.huohuas001.huHoBot.NetEvent.bindRequest;
import org.allaymc.api.command.SimpleCommand;
import org.allaymc.api.command.tree.CommandContext;
import org.allaymc.api.command.Command;
import org.allaymc.api.command.tree.CommandTree;
import org.allaymc.api.permission.PermissionGroups;
import org.allaymc.api.utils.TextFormat;

public class HuHoBotCommand extends SimpleCommand {
public class HuHoBotCommand extends Command {
public HuHoBotCommand() {
super("huhobot", "HuHoBot's control command");
getPermissions().forEach(PermissionGroups.OPERATOR::addPermission);

}

private void onReconnect(CommandContext sender) {
if (HuHoBot.getPlugin().reconnect()) {
sender.addOutput(TextFormat.GOLD + "重连机器人成功.");
} else {
sender.addOutput(TextFormat.DARK_RED + "重连机器人失败:已在连接状态.");
}

}

private void onDisconnect(CommandContext sender) {
if (HuHoBot.getPlugin().disConnectServer()) {
sender.addOutput(TextFormat.GOLD + "已断开机器人连接.");
}
}

private void onBind(CommandContext sender, String args) {
bindRequest obj = HuHoBot.getPlugin().bindRequestObj;
if(obj.confirmBind(args)){
sender.addOutput(TextFormat.GOLD + "已向服务器发送确认绑定请求,请等待服务端下发配置文件.");
}else{
sender.addOutput(TextFormat.DARK_RED + "绑定码错误,请重新输入.");
}
}

private void onReload(CommandContext sender) {
HuHoBot.reloadConfig();
sender.addOutput(TextFormat.GOLD + "已重载配置文件.");
}

private void onHelp(CommandContext sender) {
sender.addOutput(TextFormat.AQUA + "HuHoBot 操作相关命令");
sender.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot reconnect - 重新连接服务器");
sender.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot disconnect - 断开服务器连接");
sender.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot reload - 重载配置文件");
sender.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot bind <code:String> - 确认绑定");
}

@Override
public void prepareCommandTree(CommandTree tree) {
tree.getRoot()
.key("reconnect")
.exec(context -> {
onReconnect(context);
if (HuHoBot.getPlugin().reconnect()) {
context.addOutput(TextFormat.GOLD + "重连机器人成功.");
} else {
context.addOutput(TextFormat.DARK_RED + "重连机器人失败:已在连接状态.");
}

return context.success();
})
.root()
.key("disconnect")
.exec(context -> {
onDisconnect(context);
if (HuHoBot.getPlugin().disConnectServer()) {
context.addOutput(TextFormat.GOLD + "已断开机器人连接.");
}
return context.success();
})
.root()
.key("bind")
.str("code")
.exec(context -> {
String Code = context.getResult(1);
onBind(context, Code);
bindRequest obj = HuHoBot.getPlugin().bindRequestObj;
if (obj.confirmBind(Code)) {
context.addOutput(TextFormat.GOLD + "已向服务器发送确认绑定请求,请等待服务端下发配置文件.");
} else {
context.addOutput(TextFormat.DARK_RED + "绑定码错误,请重新输入.");
}
return context.success();
})
.root()
.key("help")
.exec(context -> {
onHelp(context);
context.addOutput(TextFormat.AQUA + "HuHoBot 操作相关命令");
context.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot reconnect - 重新连接服务器");
context.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot disconnect - 断开服务器连接");
context.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot reload - 重载配置文件");
context.addOutput(TextFormat.GOLD + ">" + TextFormat.DARK_GRAY + "/huhobot bind <code:String> - 确认绑定");
return context.success();
})
.root()
.key("reload")
.exec(context -> {
onReload(context);
HuHoBot.reloadConfig();
context.addOutput(TextFormat.GOLD + "已重载配置文件.");
return context.success();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.allaymc.api.eventbus.EventHandler;
import org.allaymc.api.eventbus.event.player.PlayerChatEvent;

public class onChat {
public class GameEventListener {
@EventHandler
private void onChatEvent(PlayerChatEvent event){
private void onPlayerChat(PlayerChatEvent event) {
String message = event.getMessage();
String playerName = event.getPlayer().getDisplayName();

Expand Down
51 changes: 30 additions & 21 deletions src/main/java/cn/huohuas001/huHoBot/HuHoBot.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package cn.huohuas001.huHoBot;

import cn.huohuas001.huHoBot.Command.HuHoBotCommand;
import cn.huohuas001.huHoBot.GameEvent.onChat;
import cn.huohuas001.huHoBot.GameEvent.GameEventListener;
import cn.huohuas001.huHoBot.NetEvent.*;
import cn.huohuas001.huHoBot.Settings.PluginConfig;
import com.alibaba.fastjson2.JSONObject;
import eu.okaeri.configs.ConfigManager;
import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.command.CommandResult;
import org.allaymc.api.command.CommandSender;
import org.allaymc.api.plugin.Plugin;
import org.allaymc.api.registry.Registries;
import org.allaymc.api.server.Server;
import org.allaymc.api.utils.Utils;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HuHoBot extends Plugin {
private static HuHoBot instance;
private static final String CONFIG_FILE_NAME = "plugins/HuHoBot/config.yml";
private static HuHoBot instance;
@Getter
private static PluginConfig config;
@Getter
Expand All @@ -31,9 +31,18 @@ public class HuHoBot extends Plugin {

public bindRequest bindRequestObj;

public static HuHoBot getPlugin() {
return instance;
}

public static void reloadConfig() {
config.load();
config.save();
}

@Override
public void onEnable() {
Server.getInstance().getEventBus().registerListener(new onChat());
Server.getInstance().getEventBus().registerListener(new GameEventListener());
}

@Override
Expand All @@ -43,8 +52,18 @@ public void onLoad() {
// 创建配置管理器
config = ConfigManager.create(
PluginConfig.class,
Utils.createConfigInitializer(Path.of(CONFIG_FILE_NAME)
)
it -> {
// Specify configurer implementation, optionally additional serdes packages
it.withConfigurer(new YamlSnakeYamlConfigurer());
// Specify Path, File or pathname
it.withBindFile(Path.of(CONFIG_FILE_NAME));
// Automatic removal of undeclared keys
it.withRemoveOrphans(true);
// Save the file if it does not exist
it.saveDefaults();
// Load and save to update comments/new fields
it.load(true);
}
);

// 初始化默认值
Expand Down Expand Up @@ -74,7 +93,6 @@ private void registerEvent(String eventName, EventRunner event) {
eventList.put(eventName, event);
}


/**
* 统一事件注册
*/
Expand All @@ -95,7 +113,7 @@ private void totalRegEvent() {
registerEvent("bindRequest", bindRequestObj);
}

public void onWsMsg(JSONObject data){
public void onWsMsg(JSONObject data) {
JSONObject header = data.getJSONObject("header");
JSONObject body = data.getJSONObject("body");

Expand All @@ -105,30 +123,21 @@ public void onWsMsg(JSONObject data){
EventRunner event = eventList.get(type);
if (event != null) {
event.EventCall(packId, body);
}else{
} else {
log.error("在处理消息是遇到错误: 未知的消息类型{}", type);
log.error("此错误具有不可容错性!请检查插件是否为最新!");
log.info("正在断开连接...");
clientManager.shutdownClient();
}
}

public static HuHoBot getPlugin(){
return instance;
}

public static void reloadConfig(){
config.load();
config.save();
}

public void runCommand(String command, String packId) {
CommandSender orginalSender = Server.getInstance();
CommandResult result = Registries.COMMANDS.execute(orginalSender, command);
String resultTextBuilder = "暂不支持返回值.";
if(result.isSuccess()){
if (result.isSuccess()) {
clientManager.getClient().respone("已执行,命令回调如下:\n" + resultTextBuilder, "success", packId);
}else{
} else {
clientManager.getClient().respone("已执行,命令回调如下:\n" + resultTextBuilder, "error", packId);
}

Expand Down Expand Up @@ -178,5 +187,5 @@ public boolean disConnectServer() {
return clientManager.shutdownClient();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AddAllowList extends EventRunner {
@Override
public boolean run() {
String XboxId = body.getString("xboxid");
if(Server.getInstance().getPlayerService().addToWhitelist(XboxId)){
if (Server.getInstance().getPlayerManager().addToWhitelist(XboxId)) {
PluginConfig config = this.getConfig();
String name = config.getServerName();
respone(name + "已接受添加名为" + XboxId + "的白名单请求", "success");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/huohuas001/huHoBot/NetEvent/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public boolean run() {
String msg = body.getString("msg");
PluginConfig config = this.getConfig();
String message = config.getChatConfig().getFromGroup().replace("{nick}", nick).replace("{msg}", msg);
if(config.getChatConfig().isPostChat()){
Server.getInstance().broadcastText(message);
if (config.getChatConfig().isPostChat()) {
Server.getInstance().getMessageChannel().broadcastMessage(message);
}
return true;
}
Expand Down
Loading