
NetworkJS
一个强大的 KubeJS 扩展,为你的 Minecraft 服务器脚本启用互联网连接和 Discord 集成。
NetworkJS
一个强大的 KubeJS 扩展模组,为你的 Minecraft 服务器脚本启用互联网连接和 Discord 集成功能。
注意:这是我第一个 Java 模组!我仍在学习,非常欢迎反馈和贡献。:D
功能特性
- HTTP/FETCH 支持 - 从你的 KubeJS 脚本发起 HTTP 请求
- Discord 机器人集成 - 发送消息、嵌入内容,并监听 Discord 事件
- 服务器工具 - 向玩家发送彩色消息并获取服务器信息
- 易于集成 - 与 KubeJS 无缝协作,支持全局函数和类访问两种方式
安装方法
API 参考
全局函数
// HTTP 请求 - 现在可作为全局函数使用!
fetch('https://api.example.com/data')
fetchAsync('https://api.example.com/data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key: 'value' })
})
全局类
DiscordBot - Discord 集成
const config = {
token: "your-bot-token",
guild: "your-guild-id",
channels: {
chat: "channel-id",
announcements: "channel-id"
},
sanitizeMessages: true
};
const bot = new DiscordBot(config);
// 发送消息
bot.sendMessage("chat", "Hello from Minecraft!");
// 发送嵌入内容
bot.sendEmbed("announcements", {
title: "Server Status",
description: "Server is online!",
color: 0x00ff00
});
// 监听 Discord 消息
bot.onMessage(function(discordMessage) {
console.log("Message from " + discordMessage.getAuthor() + ": " + discordMessage.getContent());
});
// 设置机器人活动状态
bot.setActivity("Minecraft Server Online");
Server - Minecraft 服务器工具
// 向所有玩家发送彩色消息
Server.sendRawMessage("&aWelcome to the server!");
// 向指定玩家发送消息
Server.sendRawMessageToPlayer("PlayerName", "&bHello there!");
// 获取服务器信息
const playerCount = Server.getPlayerCount();
const playerNames = Server.getPlayerNames(); // 返回字符串数组
FetchBinding - HTTP 请求(旧版)
// 你仍然可以使用基于类的方式
const response = FetchBinding.fetch("https://api.example.com");
console.log("Status:", response.getStatus());
console.log("Response:", response.text());
// 带选项
const options = new FetchOptions("POST", {"Content-Type": "application/json"}, '{"data": "value"}');
const response = FetchBinding.fetch("https://api.example.com", options);
// 异步请求
const futureResponse = FetchBinding.fetchAsync("https://api.example.com");
完整的 API 导出
以下类和函数在你的 KubeJS 脚本中全局可用:
| 导出 | 类型 | 描述 |
|---|---|---|
fetch() |
函数 | 发起 HTTP 请求(全局函数) |
fetchAsync() |
函数 | 发起异步 HTTP 请求(全局函数) |
DiscordBot |
类 | Discord 机器人功能 |
Server |
类 | 服务器工具 |
FetchBinding |
类 | HTTP 请求工具(旧版) |
FetchOptions |
类 | HTTP 请求选项 |
FetchResponse |
类 | HTTP 响应对象 |
使用示例
Discord-Minecraft 桥接
const discordConfig = {
token: "your-bot-token",
guild: "your-guild-id",
channels: { chat: "chat-channel-id" }
};
const bot = new DiscordBot(discordConfig);
// 将 Discord 消息转发到 Minecraft
bot.onMessage(function(msg) {
if (msg.isFromConfiguredChannel() && !msg.isBot()) {
Server.sendRawMessage(`&7[Discord] &f${msg.getAuthor()}: ${msg.getContent()}`);
}
});
// 将 Minecraft 聊天转发到 Discord
PlayerEvents.chat(event => {
const playerName = event.player.name.getString();
const message = event.message;
bot.sendMessage("chat", `**${playerName}**: ${message}`);
});
服务器状态 API
// 使用 fetch 创建一个简单的 API 端点
ServerEvents.loaded(event => {
const statusData = {
online: true,
players: Server.getPlayerCount(),
playerList: Server.getPlayerNames()
};
// 发送到你的状态 API
fetchAsync('https://your-api.com/server-status', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(statusData)
});
});
环境要求
- Minecraft 1.21.1
- NeoForge 21.1.200+
- KubeJS 2101.7.1+
构建
git clone https://github.com/SSnowly/NetworkJS.git
cd NetworkJS
./gradlew build
构建的 jar 文件将位于 build/libs/networkjs-1.21.1-{version}.jar
许可证
本项目基于 MIT 许可证授权 - 详情请参阅 LICENSE 文件。
贡献
作为一名 Java 开发新手,我一直在寻求改进!欢迎:
- 提交 bug 或功能请求的 issue
- 提交包含改进的 pull request
- 分享关于代码结构的反馈
- 帮助完善文档
支持
- 提交 issue 反馈 bug
- 查看 KubeJS Wiki 获取 KubeJS 通用帮助
由我倾心制作 <3
正在加载版本记录…
正在加载评论…
评论在新手盒子客户端中发表,这里同步展示。