初始化提交

This commit is contained in:
2022-11-06 18:37:18 +08:00
commit 0a77aef84e
55 changed files with 3550 additions and 0 deletions

View File

@@ -0,0 +1,201 @@
package cn.hamster3.service.common.entity;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* 消息内容
*/
@SuppressWarnings("unused")
public class ServiceMessageInfo {
/**
* 消息发送者
*/
private final ServiceSenderInfo senderInfo;
/**
* 接受该消息的目标服务器
* <p>
* 一旦设定该值,则此条消息将会由 HamsterService-Server 过滤
* <p>
* 仅服务器名称匹配的子端才能接收到这条消息
* <p>
* 若不设定值为null则该消息会广播给所有子端
*/
private final String toServer;
/**
* 消息标签
* <p>
* 一般用这个来判断消息由哪个插件发出
*/
private String tag;
/**
* 消息动作
* <p>
* 一般用这个来判断插件应该如何处理这条消息
*/
private String action;
/**
* 消息内容
* <p>
* 这里是消息的附加参数
*/
private JsonElement content;
public ServiceMessageInfo(@NotNull ServiceSenderInfo senderInfo, @NotNull String tag, @NotNull String action) {
this(senderInfo, tag, action, null);
}
public ServiceMessageInfo(@NotNull ServiceSenderInfo senderInfo, @NotNull String tag, @NotNull String action, @Nullable JsonElement content) {
this(senderInfo, null, tag, action, content);
}
public ServiceMessageInfo(@NotNull ServiceSenderInfo senderInfo, @Nullable String toServer, @NotNull String tag, @NotNull String action, @Nullable JsonElement content) {
this.senderInfo = senderInfo;
this.toServer = toServer;
this.tag = tag;
this.action = action;
this.content = content;
}
public ServiceMessageInfo(@NotNull JsonObject object) {
senderInfo = new ServiceSenderInfo(object.getAsJsonObject("senderInfo"));
if (object.has("toServer")) {
toServer = object.get("toServer").getAsString();
} else {
toServer = null;
}
tag = object.get("tag").getAsString();
action = object.get("action").getAsString();
content = object.get("content");
}
/**
* 序列化至Json
*
* @return json对象
*/
@NotNull
public JsonObject saveToJson() {
JsonObject object = new JsonObject();
object.add("senderInfo", senderInfo.saveToJson());
if (toServer != null) {
object.addProperty("toServer", toServer);
}
object.addProperty("tag", tag);
object.addProperty("action", action);
object.add("content", content);
return object;
}
/**
* 获取消息发送者
*
* @return 发送者
*/
@NotNull
public ServiceSenderInfo getSenderInfo() {
return senderInfo;
}
/**
* 获取定向发送的接受者
* <p>
* 如果返回为 null 则代表广播消息
*
* @return 定向发送的接受者
*/
public String getToServer() {
return toServer;
}
/**
* 获取消息标签
*
* @return 消息标签
*/
@NotNull
public String getTag() {
return tag;
}
/**
* 设置消息标签
*
* @param tag 消息标签
*/
public void setTag(@NotNull String tag) {
this.tag = tag;
}
/**
* 获取消息动作
*
* @return 消息动作
*/
@NotNull
public String getAction() {
return action;
}
/**
* 设置消息动作
*
* @param action 消息动作
*/
public void setAction(@NotNull String action) {
this.action = action;
}
/**
* 获取消息内容
*
* @return 消息内容
*/
public JsonElement getContent() {
return content;
}
/**
* 设置消息内容
*
* @param content 消息内容
*/
public void setContent(@Nullable JsonElement content) {
this.content = content;
}
/**
* 以字符串形式获取消息内容
*
* @return 消息内容
*/
public String getContentAsString() {
return content.getAsString();
}
/**
* 以 JsonObject 对象获取消息内容
*
* @return 消息内容
*/
public JsonObject getContentAsJsonObject() {
return content.getAsJsonObject();
}
/**
* 以 JsonArray 对象获取消息内容
*
* @return 消息内容
*/
public JsonArray getContentAsJsonArray() {
return content.getAsJsonArray();
}
@Override
public String toString() {
return saveToJson().toString();
}
}

View File

@@ -0,0 +1,82 @@
package cn.hamster3.service.common.entity;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
/**
* 消息发送者信息
*/
@SuppressWarnings("unused")
public class ServiceSenderInfo {
private final ServiceSenderType type;
private final String name;
private final String nickName;
/**
* 缓存用的 jsonInfo
*/
private JsonObject jsonInfo;
public ServiceSenderInfo(@NotNull ServiceSenderType type, @NotNull String name, @NotNull String nickName) {
this.type = type;
this.name = name;
this.nickName = nickName;
}
public ServiceSenderInfo(@NotNull JsonObject object) {
type = ServiceSenderType.valueOf(object.get("type").getAsString());
name = object.get("name").getAsString();
nickName = object.get("nickName").getAsString();
}
/**
* 序列化至Json
*
* @return json对象
*/
@NotNull
public JsonObject saveToJson() {
if (jsonInfo == null) {
jsonInfo = new JsonObject();
jsonInfo.addProperty("type", type.name());
jsonInfo.addProperty("name", name);
jsonInfo.addProperty("nickName", nickName);
}
return jsonInfo;
}
@NotNull
public ServiceSenderType getType() {
return type;
}
@NotNull
public String getName() {
return name;
}
@NotNull
public String getNickName() {
return nickName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ServiceSenderInfo that = (ServiceSenderInfo) o;
return name.equals(that.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return saveToJson().toString();
}
}

View File

@@ -0,0 +1,20 @@
package cn.hamster3.service.common.entity;
/**
* 消息发送者类型
*/
@SuppressWarnings("unused")
public enum ServiceSenderType {
/**
* Bukkit服务器
*/
BUKKIT,
/**
* BungeeCord 等代理服务器
*/
PROXY,
/**
* 消息中心服务器名称为ServiceCentre
*/
SERVICE_CENTRE
}