初始化提交

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,101 @@
package cn.hamster3.service.common.data;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
@SuppressWarnings("unused")
public class ServiceBlockPos {
@NotNull
private String serverName;
@NotNull
private String worldName;
private int x;
private int y;
private int z;
public ServiceBlockPos(@NotNull String serverName, @NotNull String worldName, int x, int y, int z) {
this.serverName = serverName;
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
}
public ServiceBlockPos(@NotNull JsonObject object) {
serverName = object.get("serverName").getAsString();
worldName = object.get("worldName").getAsString();
x = object.get("x").getAsInt();
y = object.get("y").getAsInt();
z = object.get("z").getAsInt();
}
@NotNull
public JsonObject saveToJson() {
JsonObject object = new JsonObject();
object.addProperty("serverName", serverName);
object.addProperty("worldName", worldName);
object.addProperty("x", x);
object.addProperty("y", y);
object.addProperty("z", z);
return object;
}
@NotNull
public String getServerName() {
return serverName;
}
public void setServerName(@NotNull String serverName) {
this.serverName = serverName;
}
@NotNull
public String getWorldName() {
return worldName;
}
public void setWorldName(@NotNull String worldName) {
this.worldName = worldName;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ServiceBlockPos that = (ServiceBlockPos) o;
return x == that.x && y == that.y && z == that.z && serverName.equals(that.serverName) && worldName.equals(that.worldName);
}
@Override
public int hashCode() {
return Objects.hash(serverName, worldName, x, y, z);
}
}

View File

@@ -0,0 +1,148 @@
package cn.hamster3.service.common.data;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
@SuppressWarnings("unused")
public class ServiceLocation {
@NotNull
private String serverName;
@NotNull
private String worldName;
private double x;
private double y;
private double z;
private float yaw;
private float pitch;
public ServiceLocation(@NotNull String serverName, @NotNull String worldName, double x, double y, double z) {
this.serverName = serverName;
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
}
public ServiceLocation(@NotNull String serverName, @NotNull String worldName, double x, double y, double z, float yaw, float pitch) {
this.serverName = serverName;
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
public ServiceLocation(@NotNull JsonObject object) {
serverName = object.get("serverName").getAsString();
worldName = object.get("worldName").getAsString();
x = object.get("x").getAsDouble();
y = object.get("y").getAsDouble();
z = object.get("z").getAsDouble();
yaw = object.get("yaw").getAsFloat();
pitch = object.get("pitch").getAsFloat();
}
@NotNull
public JsonObject saveToJson() {
JsonObject object = new JsonObject();
object.addProperty("serverName", serverName);
object.addProperty("worldName", worldName);
object.addProperty("x", x);
object.addProperty("y", y);
object.addProperty("z", z);
object.addProperty("yaw", yaw);
object.addProperty("pitch", pitch);
return object;
}
@NotNull
public String getServerName() {
return serverName;
}
public void setServerName(@NotNull String serverName) {
this.serverName = serverName;
}
@NotNull
public String getWorldName() {
return worldName;
}
public void setWorldName(@NotNull String worldName) {
this.worldName = worldName;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public int getBlockX() {
return (int) x;
}
public int getBlockY() {
return (int) y;
}
public int getBlockZ() {
return (int) z;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public float getYaw() {
return yaw;
}
public void setYaw(float yaw) {
this.yaw = yaw;
}
public float getPitch() {
return pitch;
}
public void setPitch(float pitch) {
this.pitch = pitch;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ServiceLocation that = (ServiceLocation) o;
return Double.compare(that.x, x) == 0 && Double.compare(that.y, y) == 0 && Double.compare(that.z, z) == 0 && Float.compare(that.yaw, yaw) == 0 && Float.compare(that.pitch, pitch) == 0 && serverName.equals(that.serverName) && worldName.equals(that.worldName);
}
@Override
public int hashCode() {
return Objects.hash(serverName, worldName, x, y, z, yaw, pitch);
}
}

View File

@@ -0,0 +1,153 @@
package cn.hamster3.service.common.data;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
/**
* 玩家信息
*/
@SuppressWarnings("unused")
public class ServicePlayerInfo {
/**
* 玩家的uuid
*/
private final UUID uuid;
/**
* 玩家的名称
*/
private final String playerName;
/**
* 玩家所在的 bukkit 服务器名称
*/
private final String bukkitServer;
/**
* 玩家所在的 代理 服务器名称
*/
private final String proxyServer;
/**
* 玩家是否在线
*/
private boolean online;
public ServicePlayerInfo(@NotNull UUID uuid, @NotNull String playerName, @Nullable String bukkitServer, @Nullable String proxyServer) {
this.uuid = uuid;
this.playerName = playerName;
this.bukkitServer = bukkitServer;
this.proxyServer = proxyServer;
online = true;
}
public ServicePlayerInfo(@NotNull UUID uuid, @NotNull String playerName, @Nullable String bukkitServer, @Nullable String proxyServer, boolean online) {
this.uuid = uuid;
this.playerName = playerName;
this.bukkitServer = bukkitServer;
this.proxyServer = proxyServer;
this.online = online;
}
public ServicePlayerInfo(JsonObject object) {
uuid = UUID.fromString(object.get("uuid").getAsString());
playerName = object.get("playerName").getAsString();
online = object.get("online").getAsBoolean();
if (object.has("bukkitServer")) {
JsonElement bukkitServerJson = object.get("bukkitServer");
if (!bukkitServerJson.isJsonNull()) {
bukkitServer = bukkitServerJson.getAsString();
} else {
bukkitServer = null;
}
} else {
bukkitServer = null;
}
if (object.has("proxyServer")) {
JsonElement proxyServerJson = object.get("proxyServer");
if (!proxyServerJson.isJsonNull()) {
proxyServer = proxyServerJson.getAsString();
} else {
proxyServer = null;
}
} else {
proxyServer = null;
}
}
public JsonObject saveToJson() {
JsonObject object = new JsonObject();
object.addProperty("uuid", uuid.toString());
object.addProperty("playerName", playerName);
object.addProperty("online", online);
object.addProperty("bukkitServer", bukkitServer);
object.addProperty("proxyServer", proxyServer);
return object;
}
/**
* 获取玩家的UUID
*
* @return 玩家的UUID
*/
@NotNull
public UUID getUuid() {
return uuid;
}
/**
* 获取玩家的名称
*
* @return 玩家名称
*/
@NotNull
public String getPlayerName() {
return playerName;
}
public boolean isOnline() {
return online;
}
public void setOnline(boolean online) {
this.online = online;
}
/**
* 获取玩家所在的子服
*
* @return 子服名称
*/
public String getBukkitServer() {
return bukkitServer;
}
/**
* 获取玩家所在的代理节点
*
* @return 代理节点名称
*/
public String getProxyServer() {
return proxyServer;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ServicePlayerInfo that = (ServicePlayerInfo) o;
return uuid.equals(that.uuid);
}
@Override
public int hashCode() {
return uuid.hashCode();
}
@Override
public String toString() {
return saveToJson().toString();
}
}

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
}

View File

@@ -0,0 +1,106 @@
package cn.hamster3.service.common.util;
import com.google.gson.*;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import java.util.ArrayList;
import java.util.Collections;
public class ComponentUtils {
private static final Gson gson = new GsonBuilder()
.setLenient()// json宽松
.enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式
.serializeNulls() //智能null
.setPrettyPrinting()// 调教格式
.create();
private ComponentUtils() {
}
public static BaseComponent[] parseComponentFromJson(JsonElement json) {
if (json.isJsonPrimitive()) {
return new ComponentBuilder(json.getAsString()).create();
}
if (json.isJsonArray()) {
JsonArray array = json.getAsJsonArray();
ArrayList<BaseComponent> list = new ArrayList<>();
for (JsonElement element : array) {
Collections.addAll(list, parseComponentFromJson(element));
}
BaseComponent[] components = new BaseComponent[list.size()];
for (int i = 0; i < list.size(); i++) {
components[i] = list.get(i);
}
return components;
}
if (json.isJsonObject()) {
JsonObject object = json.getAsJsonObject();
ComponentBuilder builder = new ComponentBuilder(object.get("text").getAsString());
if (object.has("color")) {
builder.color(ChatColor.of(object.get("color").getAsString()));
}
if (object.has("bold")) {
builder.bold(object.get("bold").getAsBoolean());
}
if (object.has("italic")) {
builder.italic(object.get("italic").getAsBoolean());
}
if (object.has("underlined")) {
builder.underlined(object.get("underlined").getAsBoolean());
}
if (object.has("strikethrough")) {
builder.strikethrough(object.get("strikethrough").getAsBoolean());
}
if (object.has("obfuscated")) {
builder.obfuscated(object.get("obfuscated").getAsBoolean());
}
if (object.has("insertion")) {
builder.insertion(object.get("insertion").getAsString());
}
if (object.has("clickEvent")) {
builder.event(parseClickEvent(object.getAsJsonObject("clickEvent")));
}
if (object.has("hoverEvent")) {
builder.event(parseHoverEvent(object.getAsJsonObject("hoverEvent")));
}
if (object.has("extra")) {
builder.append(parseComponentFromJson(object.get("extra")));
}
return builder.create();
}
throw new IllegalArgumentException("非法json字符串: " + json);
}
private static ClickEvent parseClickEvent(JsonObject object) {
return new ClickEvent(
ClickEvent.Action.valueOf(
object
.get("action")
.getAsString()
.toUpperCase()
),
object.get("value").getAsString()
);
}
@SuppressWarnings("deprecation")
private static HoverEvent parseHoverEvent(JsonObject object) {
return new HoverEvent(
HoverEvent.Action.valueOf(
object
.get("action")
.getAsString()
.toUpperCase()
),
parseComponentFromJson(object.get("value"))
);
}
public static Gson getGson() {
return gson;
}
}

View File

@@ -0,0 +1,38 @@
package cn.hamster3.service.common.util;
import java.util.logging.Logger;
@SuppressWarnings("unused")
public abstract class ServiceLogUtils {
private static Logger logger;
public static void setLogger(Logger logger) {
ServiceLogUtils.logger = logger;
}
public static void info(String info) {
logger.info(info);
}
public static void info(String info, Object... params) {
logger.info(String.format(info, params));
}
public static void warning(String warning) {
logger.warning(warning);
}
public static void warning(String warning, Object... params) {
logger.warning(String.format(warning, params));
}
public static void error(Throwable e, String message) {
warning(message);
e.printStackTrace();
}
public static void error(Throwable e, String message, Object... args) {
warning(message, args);
e.printStackTrace();
}
}