Skip to content

Commit

Permalink
ExtraToolsKubeJS Update
Browse files Browse the repository at this point in the history
  • Loading branch information
KenRouKoro committed Dec 2, 2022
1 parent b59b2ab commit 9f561ef
Show file tree
Hide file tree
Showing 19 changed files with 482 additions and 23 deletions.
12 changes: 6 additions & 6 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/modules/KubejsExtraTools.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/modules/KubejsExtraTools.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 84 additions & 1 deletion KubejsExtraTools/README.MD
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Kubejs Koro Extra Tools
## 简介
这个mod为KubeJS加入了许多好玩(X)的功能支持,具体如下:

1. HttpServer
2. HttpClient
3. 文件写入
4. 文件读取
5. 正则表达式支持
6. 异步支持
7. Html处理支持
8. MQTT客户端
9. WebSocket客户端

所以理论上汝能用这个mod写一个爬虫!
或着对着小爱来一句:“小爱同学,备份服务器。”(逃

所以理论上汝能用这个mod写一个爬虫!(逃

**此文档部分源自Hutool文档**
## 教程
Expand Down Expand Up @@ -64,6 +69,84 @@ HttpClient.setMaxRedirectCount(10);//设置允许重定向的次数
HttpClient.isHttps("https://baidu.com")//判断一个url是否为https请求

```
### WebSocket
#### 通用方法
```javascript
WebSocket.creatConnect("ws://127.0.0.1:8620");//创建websocket连接对象
WebSocket.connect();//启动连接
WebSocket.sendMessage();//发送信息,这个方法是线程安全的
WebSocket.close();//关闭WebSocket连接,并清理环境
```
#### 事件
```javascript
//用法
WebSocketEvents.event("event_name",event=>{});
```
#### 事件列表
1. onClose WebSocket连接关闭事件
2. onError WebSocket连接错误事件
3. onMessage WebSocket收到信息事件
4. onOpen WebSocket连接启动事件
#### event内容
使用getter方法进行访问
```java
protected String message;
protected ServerHandshake serverHandshake;
protected int code;
protected String reason;
protected boolean remote;
```

### MQTT
tcp连接支持两种类型的连接:
TCP连接:tcp://
ssl/TLS保护的tcp连接:ssl://
例如:
tcp://localhost:1883
ssl://localhost:8883
如果未指定端口,则:
tcp://“URI默认为1883
ssl://URI默认为8883
#### 通用方法
```javascript
MQTT.creatConnect("broker","clientId");//创建MQTT连接对象,broker是MQTTserverURI
MQTT.setServerURIs(["URI1","URI2"]);//设置多个MQTT服务器
MQTT.setCleanSession(true);//设置CleanSession
MQTT.setPassword("password");//设置密码
MQTT.setUsername("username");//设置用户名
MQTT.setAutomaticReconnect(true);//设置自动重连
MQTT.setMaxInflight(10);//设置最大等待数据
MQTT.setKeepalive(10);//设置Keepalive
MQTT.setWillPayloadAndTopic(2,"topic","payload",false);//设置服务质量(QOS),主题,负载,retain
MQTT.connect();//启动连接
MQTT.disconnect();//关闭连接

```
#### 事件
```javascript
MQTTEvents.event("event_name",()=>{});
```
#### 事件列表
1. disconnected 连接关闭事件
2. mqttErrorOccurred 连接错误事件
3. messageArrived 收到订阅信息事件
4. deliveryComplete 交付完成事件
5. connectComplete 连接完成事件
6. authPacketArrived 身份验证数据包到达事件

#### event内容
使用getter方法进行访问
```java
MqttDisconnectResponse disconnectResponse;
String topic;
MqttMessage message;
IMqttToken token;
boolean reconnect;
String serverURI;
int reasonCode;
MqttProperties properties;
```

### FileWriter
```javascript
var writer = FileWriter.create("test.properties");
Expand Down
12 changes: 12 additions & 0 deletions KubejsExtraTools/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ repositories {
}
}
maven { url 'https://maven.blamejared.com' }
maven { url 'https://repo.eclipse.org/content/repositories/paho-releases/'}
mavenLocal()
}
configurations {
includeModImplementation
includeImplementation

include.extendsFrom includeImplementation
include.extendsFrom includeModImplementation

implementation.extendsFrom includeImplementation
modImplementation.extendsFrom includeModImplementation
}
dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
Expand All @@ -46,6 +56,8 @@ dependencies {
//ws依赖
implementation "org.java-websocket:Java-WebSocket:1.5.3"
modImplementation "cn.korostudio:koro-utils-lib:1.0.0"
//MQTT依赖
includeImplementation "org.eclipse.paho:org.eclipse.paho.mqttv5.client:1.2.5"

}

Expand Down
2 changes: 1 addition & 1 deletion KubejsExtraTools/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minecraft_version=1.19.2
yarn_mappings=1.19.2+build.28
loader_version=0.14.11
# Mod Properties
mod_version=.0.0.1
mod_version=.0.0.2
maven_group=cn.korostudio
archives_base_name=kubejs-extra-tools
# Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import cn.hutool.http.server.action.Action;
import cn.korostudio.kubejsextratools.kubejs.bindings.HttpClientBindings;
import cn.korostudio.kubejsextratools.kubejs.bindings.HttpServerBindings;
import cn.korostudio.kubejsextratools.kubejs.bindings.MQTTBindings;
import cn.korostudio.kubejsextratools.kubejs.bindings.WebSocketBindings;
import cn.korostudio.kubejsextratools.kubejs.events.HttpServerEvents;
import cn.korostudio.kubejsextratools.kubejs.events.MQTTEvents;
import cn.korostudio.kubejsextratools.kubejs.events.WebSocketEvents;
import dev.latvian.mods.kubejs.KubeJSPlugin;
import dev.latvian.mods.kubejs.script.BindingsEvent;
import dev.latvian.mods.kubejs.script.ScriptType;
Expand Down Expand Up @@ -42,11 +46,15 @@ public void registerBindings(BindingsEvent event) {
event.add("FileReader", FileReader.class);
event.add("Assert", Assert.class);
event.add("Charset", Charset.class);
event.add("WebSocket", WebSocketBindings.class);
event.add("MQTT", MQTTBindings.class);
}

@Override
public void registerEvents() {
HttpServerEvents.GROUP.register();
WebSocketEvents.GROUP.register();
MQTTEvents.GROUP.register();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package cn.korostudio.kubejsextratools.kubejs.bindings;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.server.SimpleServer;
import cn.hutool.http.server.action.Action;
import cn.korostudio.kubejsextratools.kubejs.data.HttpServer;
import cn.korostudio.kubejsextratools.kubejs.events.HttpServerEvents;
import cn.korostudio.kubejsextratools.kubejs.events.PathEventJS;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.script.ScriptManager;
import dev.latvian.mods.rhino.BaseFunction;
import dev.latvian.mods.rhino.Function;
import dev.latvian.mods.rhino.FunctionObject;
import cn.korostudio.kubejsextratools.kubejs.events.eventJS.PathEventJS;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.Arrays;
@Slf4j
public class HttpServerBindings {
public static HttpServer httpServer = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cn.korostudio.kubejsextratools.kubejs.bindings;

import cn.korostudio.kubejsextratools.kubejs.mqtt.MQTTCallback;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.mqttv5.client.*;
import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;

@Slf4j
public class MQTTBindings {

protected static MqttClient mqttClient = null;
protected static MemoryPersistence persistence = null;
protected static MqttConnectionOptions connOpts;

public static void creatConnect(String broker,String clientId) {
if(mqttClient!=null){
return;
}
persistence = new MemoryPersistence();
try {
mqttClient = new MqttClient(broker, clientId, persistence);
connOpts = new MqttConnectionOptions();
mqttClient.setCallback(new MQTTCallback());
} catch (MqttException e) {
log.error("clientId为空或长度大于65535个字符!", e);
}
}
public static void setServerURIs(String[] urIs){
if (connOpts == null){
return;
}
connOpts.setServerURIs(urIs);
}
public static void setCleanSession(boolean b){
if (connOpts == null){
return;
}
connOpts.setCleanStart(b);
}
public static void setPassword(String password){
if (connOpts == null){
return;
}
connOpts.setPassword(password.getBytes());
}
public static void setUsername(String username){
if (connOpts == null){
return;
}
connOpts.setUserName(username);
}
public static void setAutomaticReconnect(boolean b){
if (connOpts == null){
return;
}
connOpts.setAutomaticReconnect(b);
}
public static void setMaxInflight(int maxInflight){
if (connOpts == null){
return;
}
connOpts.setReceiveMaximum(maxInflight);
}
public static void setKeepalive(int keepalive){
if (connOpts == null){
return;
}
connOpts.setKeepAliveInterval(keepalive);
}
public static void setWillPayloadAndTopic(int qos,String topic,String payload,boolean retain){
if (connOpts == null){
return;
}
MqttMessage willMessage = new MqttMessage(payload.getBytes(), qos, retain, null);
connOpts.setWill(topic, willMessage);
}
public static void connect(){
if (mqttClient==null){
return;
}
try {
mqttClient.connect(connOpts);
} catch (MqttException e) {
log.error("MQTT 连接失败!",e);
}
}
public static void disconnect(){
if (mqttClient==null){
return;
}
try {
mqttClient.disconnect();
} catch (MqttException e) {
log.error("MQTT 关闭异常!",e);
}
}
}
Loading

0 comments on commit 9f561ef

Please sign in to comment.