diff --git a/README.md b/README.md index 1ebfdd82..b10b0661 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ public class Main implements CommandExecutor,Listener{ top.qscraft dodoopenjava - 3.1.13-SNAPSHOT-1 + 3.2-SNAPSHOT ``` @@ -109,7 +109,7 @@ public class Main implements CommandExecutor,Listener{ } dependencies { - implementation 'top.qscraft:dodoopenjava:3.1.13-SNAPSHOT-1' + implementation 'top.qscraft:dodoopenjava:3.2-SNAPSHOT' } ``` ### 教程(过于古老,无参考价值,改日重写) diff --git a/pom.xml b/pom.xml index 72a071dc..817572ba 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ io.github.minecraftchampions.dodoopenjava DodoOpenJava - 3.1.13-SNAPSHOT + 3.2-SNAPSHOT jar DodoOpenJava diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/ApiResultsLogger.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/ApiResultsLogger.java index 867df454..6fd6a73e 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/ApiResultsLogger.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/ApiResultsLogger.java @@ -9,6 +9,7 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentSkipListMap; /** @@ -21,23 +22,42 @@ public class ApiResultsLogger { private final ConcurrentSkipListMap> resultsLogMap = new ConcurrentSkipListMap<>(); + /** + * 添加API执行结果记录 + * + * @param result result + */ public void addResult(@NonNull Result result) { - long timestamp = result.getTimestamp(); - Set results = resultsLogMap.get(timestamp); - if (results == null) { - resultsLogMap.put(timestamp, new LinkedHashSet<>(Set.of(result))); - return; - } - synchronized (results) { - results.add(result); - } + CompletableFuture.runAsync(() -> { + long timestamp = result.getTimestamp(); + Set results = resultsLogMap.get(timestamp); + if (results == null) { + resultsLogMap.put(timestamp, new LinkedHashSet<>(Set.of(result))); + return; + } + synchronized (results) { + results.add(result); + } + }); } - public ConcurrentSkipListMap> getAllLogs() { + /** + * 获取日志 + * + * @return 日志 + */ + public CompletableFuture>> getAllLogs() { return getLogs(null, null); } - public ConcurrentSkipListMap> getLogs(Date startDate, Date endDate) { + /** + * 获取指定时间段到另一个指定时间段的日志 + * + * @param startDate 开始 + * @param endDate 结束 + * @return 日志 + */ + public CompletableFuture>> getLogs(Date startDate, Date endDate) { long startTimestamp; long endTimestamp; if (startDate == null) { @@ -50,19 +70,27 @@ public ConcurrentSkipListMap> getLogs(Date startDate } else { endTimestamp = endDate.getTime(); } - return getLogs(startTimestamp,endTimestamp); + return getLogs(startTimestamp, endTimestamp); } - public ConcurrentSkipListMap> getLogs(long startTimestamp, long endTimestamp) { - Map> subMap = resultsLogMap.subMap(startTimestamp, true, endTimestamp, true); - ConcurrentSkipListMap> deepCopiedMap = new ConcurrentSkipListMap<>(); - for (Map.Entry> entry : subMap.entrySet()) { - deepCopiedMap.put(entry.getKey(), (LinkedHashSet)entry.getValue().clone()); - } - return deepCopiedMap; + /** + * 获取指定时间段到另一个指定时间段的日志 + * + * @param startTimestamp 开始 + * @param endTimestamp 结束 + * @return 日志 + */ + public CompletableFuture>> getLogs(long startTimestamp, long endTimestamp) { + return CompletableFuture.supplyAsync(() -> { + Map> subMap = resultsLogMap.subMap(startTimestamp, true, endTimestamp, true); + ConcurrentSkipListMap> deepCopiedMap = new ConcurrentSkipListMap<>(); + for (Map.Entry> entry : subMap.entrySet()) { + deepCopiedMap.put(entry.getKey(), (LinkedHashSet) entry.getValue().clone()); + } + return deepCopiedMap; + }); } - @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/Bot.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/Bot.java index 1d4a0b75..1ea3eda0 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/Bot.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/Bot.java @@ -1,6 +1,5 @@ package io.github.minecraftchampions.dodoopenjava; -import io.github.minecraftchampions.dodoopenjava.card.Card; import io.github.minecraftchampions.dodoopenjava.command.CommandExecutor; import io.github.minecraftchampions.dodoopenjava.command.CommandManager; import io.github.minecraftchampions.dodoopenjava.event.EventManager; @@ -8,6 +7,8 @@ import io.github.minecraftchampions.dodoopenjava.event.Listener; import io.github.minecraftchampions.dodoopenjava.event.WebSocketEventTrigger; import io.github.minecraftchampions.dodoopenjava.message.Message; +import io.github.minecraftchampions.dodoopenjava.message.card.CardMessage; +import io.github.minecraftchampions.dodoopenjava.message.text.TextMessage; import io.github.minecraftchampions.dodoopenjava.utils.BaseUtil; import lombok.Getter; import lombok.NonNull; @@ -52,10 +53,20 @@ public String getAuthorization() { return BaseUtil.Authorization(clientId, token); } + /** + * 启用日志记录器 + */ public void enableApiResultsLogger() { DodoOpenJava.enableApiResultsLogger(this); } + /** + * 卸载日志记录器 + */ + public void disableApiResultsLogger() { + DodoOpenJava.disableApiResultsLogger(this); + } + /** * 注册事件监听器 * @@ -71,6 +82,9 @@ public synchronized void registerListener(@NonNull Listener listener) { getEventManager().registerListener(listener); } + /** + * 移除事件监听器 + */ public synchronized void removeEventTrigger() { if (eventTrigger != null) { eventTrigger.close(); @@ -223,7 +237,6 @@ public Result removeBotInvite(String dodoSourceId) { public Result setBotIslandLeave(String islandSourceId) { return io.github.minecraftchampions.dodoopenjava.api.v2.BotApi.setBotIslandLeave(bot.getAuthorization(), islandSourceId); } - } public class ChannelApi { @@ -251,7 +264,6 @@ public Result getChannelInfo(String channelId) { public Result deleteChannel(String islandSourceId, String channelId) { return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelApi.deleteChannel(bot.getAuthorization(), islandSourceId, channelId); } - } public class ChannelArticleApi { @@ -264,7 +276,6 @@ public Result removeChannelArticle(int type, String id, String channelId) { public Result addChannelArticle(String channelId, String title, String content, String imageUrl) { return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelArticleApi.addChannelArticle(bot.getAuthorization(), channelId, title, content, imageUrl); } - } public class ChannelMessageApi { @@ -329,7 +340,7 @@ public Result addChannelMessageReaction(String messageId, String id) { } @SneakyThrows - public Result editChannelCardMessage(String messageId, Card messageBody) { + public Result editChannelCardMessage(String messageId, CardMessage messageBody) { return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.editChannelCardMessage(bot.getAuthorization(), messageId, messageBody); } @@ -354,8 +365,8 @@ public Result sendTextMessage(String channelId, String content) { } @SneakyThrows - public Result sendTextMessage(String channelId, Message message) { - return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.sendTextMessage(bot.getAuthorization(), channelId, message); + public Result sendTextMessage(String channelId, TextMessage textMessage) { + return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.sendTextMessage(bot.getAuthorization(), channelId, textMessage); } @SneakyThrows @@ -369,7 +380,7 @@ public Result sendAtTextMessage(String channelId, String dodoId, String message) } @SneakyThrows - public Result sendCardMessage(String channelId, Card messageBody) { + public Result sendCardMessage(String channelId, CardMessage messageBody) { return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.sendCardMessage(bot.getAuthorization(), channelId, messageBody); } @@ -378,9 +389,30 @@ public Result getChannelMessageReactionMemberList(String messageId, int type, St return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.getChannelMessageReactionMemberList(bot.getAuthorization(), messageId, type, id, pageSize, maxId); } + @SneakyThrows + public Result sendMessage(String channelId, Message messageBody) { + return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.sendMessage(bot.getAuthorization(), channelId, messageBody); + } } public class ChannelVoiceApi { + @SneakyThrows + public Result getChannelVoiceMemberStatus(String islandSourceId, String dodoSourceId) { + return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelVoiceApi.getChannelVoiceMemberStatus(bot.getAuthorization(), islandSourceId, dodoSourceId); + + } + + @SneakyThrows + public Result moveChannelVoiceMember(String islandSourceId, String dodoSourceId, String channelId) { + return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelVoiceApi.moveChannelVoiceMember(bot.getAuthorization(), islandSourceId, dodoSourceId, channelId); + + } + + @SneakyThrows + public Result editChannelVoiceMember(int operateType, String dodoSourceId, String channelId) { + return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelVoiceApi.editChannelVoiceMember(bot.getAuthorization(), operateType, dodoSourceId, channelId); + + } } public class EventApi { @@ -388,7 +420,6 @@ public class EventApi { public Result getWebSocketConnection() { return io.github.minecraftchampions.dodoopenjava.api.v2.EventApi.getWebSocketConnection(bot.getAuthorization()); } - } public class GiftApi { @@ -416,7 +447,6 @@ public Result getGiftMemberList(String targetId, int targetType, String giftId, public Result getGiftAccount(String islandSourceId) { return io.github.minecraftchampions.dodoopenjava.api.v2.GiftApi.getGiftAccount(bot.getAuthorization(), islandSourceId); } - } public class IntegralApi { @@ -429,7 +459,6 @@ public Result getIntegralInfo(String islandSourceId, String dodoSourceId) { public Result setIntegralEdit(String islandSourceId, String dodoSourceId, int operateType, long integral) { return io.github.minecraftchampions.dodoopenjava.api.v2.IntegralApi.setIntegralEdit(bot.getAuthorization(), islandSourceId, dodoSourceId, operateType, integral); } - } public class IslandApi { @@ -457,7 +486,6 @@ public Result getIslandMuteList(String islandSourceId) { public Result getIslandBanList(String islandSourceId) { return io.github.minecraftchampions.dodoopenjava.api.v2.IslandApi.getIslandBanList(bot.getAuthorization(), islandSourceId); } - } public class MemberApi { diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/DodoOpenJava.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/DodoOpenJava.java index ccf29977..8b79692e 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/DodoOpenJava.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/DodoOpenJava.java @@ -5,9 +5,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.HashMap; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; /** * API @@ -18,7 +18,7 @@ public class DodoOpenJava { private static final HashSet bots = new HashSet<>(); @Getter - private static final HashMap logMap = new HashMap<>(); + private static final ConcurrentHashMap logMap = new ConcurrentHashMap<>(); public static final Logger LOGGER = LoggerFactory.getLogger(DodoOpenJava.class); @@ -39,23 +39,60 @@ public static Set getBots() { return (HashSet) bots.clone(); } + /** + * 卸载Bot + * + * @param bot bot + */ public synchronized static void disableBot(Bot bot) { bots.remove(bot); bot.getCommandManager().unregisterAllCommands(); bot.getEventManager().unregisterAllListeners(); bot.removeEventTrigger(); - logMap.remove(bot.getAuthorization()); + bot.disableApiResultsLogger(); } + /** + * 启用日志服务 + * + * @param bot bot + */ public static void enableApiResultsLogger(Bot bot) { enableApiResultsLogger(bot.getAuthorization()); } - public static synchronized void enableApiResultsLogger(String authorization) { - if (logMap.containsKey(authorization)) { - LOGGER.warn("已经调用过DodoOpenJava#enableApiResultsLogger"); - return; + /** + * 启用日志服务 + * + * @param authorization authorization + */ + public static void enableApiResultsLogger(String authorization) { + synchronized (logMap) { + if (logMap.containsKey(authorization)) { + LOGGER.warn("已经调用过DodoOpenJava#enableApiResultsLogger"); + return; + } + logMap.put(authorization, new ApiResultsLogger(authorization)); + } + } + + /** + * 卸载日志服务 + * + * @param bot bot + */ + public static void disableApiResultsLogger(Bot bot) { + enableApiResultsLogger(bot.getAuthorization()); + } + + /** + * 卸载日志服务 + * + * @param authorization authorization + */ + public static void disableApiResultsLogger(String authorization) { + synchronized (logMap) { + logMap.remove(authorization); } - logMap.put(authorization, new ApiResultsLogger(authorization)); } } diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/Result.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/Result.java index f1413405..c178f303 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/Result.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/Result.java @@ -103,6 +103,7 @@ private Result(@NonNull JSONObject jsonObject) { /** * 初始化 + * * @param jsonObject jsonObject * @return Result */ @@ -112,6 +113,7 @@ public static Result of(@NonNull JSONObject jsonObject) { /** * 如果成功 + * * @param consumer consumer * @return this */ @@ -122,6 +124,7 @@ public Result ifSuccess(@NonNull Consumer consumer) { /** * 如果失败 + * * @param consumer consumer * @return this */ @@ -132,6 +135,7 @@ public Result ifFailure(@NonNull Consumer consumer) { /** * 获取 + * * @return jsonObject */ public JSONObject getJSONObjectData() { @@ -144,6 +148,7 @@ public JSONObject getJSONObjectData() { /** * 获取 + * * @return jsonArray */ public JSONArray getJSONArrayData() { @@ -153,4 +158,4 @@ public JSONArray getJSONArrayData() { throw new RuntimeException(); } } -} +} \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/api/v2/ChannelMessageApi.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/api/v2/ChannelMessageApi.java index 899c6c55..0c877b51 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/api/v2/ChannelMessageApi.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/api/v2/ChannelMessageApi.java @@ -2,8 +2,9 @@ import io.github.minecraftchampions.dodoopenjava.DodoOpenJava; import io.github.minecraftchampions.dodoopenjava.Result; -import io.github.minecraftchampions.dodoopenjava.card.Card; import io.github.minecraftchampions.dodoopenjava.message.Message; +import io.github.minecraftchampions.dodoopenjava.message.card.CardMessage; +import io.github.minecraftchampions.dodoopenjava.message.text.TextMessage; import io.github.minecraftchampions.dodoopenjava.utils.BaseUtil; import io.github.minecraftchampions.dodoopenjava.utils.NetUtil; import org.json.JSONObject; @@ -33,28 +34,28 @@ public static Result sendTextMessage(String clientId, String token, String chann /** * 发送文本消息 * - * @param clientId 机器人唯一标识 - * @param token 机器人鉴权Token - * @param message 发送的消息 - * @param channelId 频道号 + * @param clientId 机器人唯一标识 + * @param token 机器人鉴权Token + * @param textMessage 发送的消息 + * @param channelId 频道号 * @return JSON对象 * @throws IOException 失败后抛出 */ - public static Result sendTextMessage(String clientId, String token, String channelId, Message message) throws IOException { - return sendTextMessage(BaseUtil.Authorization(clientId, token), channelId, message); + public static Result sendTextMessage(String clientId, String token, String channelId, TextMessage textMessage) throws IOException { + return sendTextMessage(BaseUtil.Authorization(clientId, token), channelId, textMessage); } /** * 发送文本消息 * * @param authorization authorization - * @param message 发送的消息 + * @param textMessage 发送的消息 * @param channelId 频道号 * @return JSON对象 * @throws IOException 失败后抛出 */ - public static Result sendTextMessage(String authorization, String channelId, Message message) throws IOException { - return sendTextMessage(authorization, channelId, message.toString()); + public static Result sendTextMessage(String authorization, String channelId, TextMessage textMessage) throws IOException { + return sendTextMessage(authorization, channelId, textMessage.toString()); } /** @@ -689,7 +690,7 @@ public static Result removeChannelMessageBotReaction(String authorization, Strin * @return JSON对象 * @throws IOException 失败后抛出 */ - public static Result sendCardMessage(String clientId, String token, String channelId, Card messageBody) throws IOException { + public static Result sendCardMessage(String clientId, String token, String channelId, CardMessage messageBody) throws IOException { return sendCardMessage(BaseUtil.Authorization(clientId, token), channelId, messageBody); } @@ -702,7 +703,7 @@ public static Result sendCardMessage(String clientId, String token, String chann * @return JSON对象 * @throws IOException 失败后抛出 */ - public static Result sendCardMessage(String authorization, String channelId, Card messageBody) throws IOException { + public static Result sendCardMessage(String authorization, String channelId, CardMessage messageBody) throws IOException { String url = DodoOpenJava.BASEURL + "channel/message/send"; JSONObject jsonObject = new JSONObject(); jsonObject.put("channelId", channelId) @@ -711,6 +712,38 @@ public static Result sendCardMessage(String authorization, String channelId, Car return NetUtil.sendRequest(jsonObject.toString(), url, authorization); } + /** + * 发送消息 + * + * @param clientId 机器人唯一标识 + * @param token 机器人鉴权Token + * @param channelId 频道号 + * @param messageBody 消息 + * @return JSON对象 + * @throws IOException 失败后抛出 + */ + public static Result sendMessage(String clientId, String token, String channelId, Message messageBody) throws IOException { + return sendMessage(BaseUtil.Authorization(clientId, token), channelId, messageBody); + } + + /** + * 发送消息 + * + * @param authorization authorization + * @param channelId 频道号 + * @param messageBody 消息 + * @return JSON对象 + * @throws IOException 失败后抛出 + */ + public static Result sendMessage(String authorization, String channelId, Message messageBody) throws IOException { + String url = DodoOpenJava.BASEURL + "channel/message/send"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put("channelId", channelId) + .put("messageType", messageBody.getType()) + .put("messageBody", messageBody.toMessage()); + return NetUtil.sendRequest(jsonObject.toString(), url, authorization); + } + /** * 编辑卡片消息 * @@ -721,7 +754,7 @@ public static Result sendCardMessage(String authorization, String channelId, Car * @return JSON对象 * @throws IOException 失败后抛出 */ - public static Result editChannelCardMessage(String clientId, String token, String messageId, Card messageBody) throws IOException { + public static Result editChannelCardMessage(String clientId, String token, String messageId, CardMessage messageBody) throws IOException { return editChannelCardMessage(BaseUtil.Authorization(clientId, token), messageId, messageBody); } @@ -734,7 +767,7 @@ public static Result editChannelCardMessage(String clientId, String token, Strin * @return JSON对象 * @throws IOException 失败后抛出 */ - public static Result editChannelCardMessage(String authorization, String messageId, Card messageBody) throws IOException { + public static Result editChannelCardMessage(String authorization, String messageId, CardMessage messageBody) throws IOException { String url = DodoOpenJava.BASEURL + "channel/message/edit"; JSONObject jsonObject = new JSONObject(); jsonObject.put("messageId", messageId) diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/package-info.java deleted file mode 100644 index 0382a87f..00000000 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * 组件列表 - */ -package io.github.minecraftchampions.dodoopenjava.card.component; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/package-info.java deleted file mode 100644 index 795ace68..00000000 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * 关于卡片消息的一些枚举 - */ -package io.github.minecraftchampions.dodoopenjava.card.enums; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/card/package-info.java deleted file mode 100644 index 45a63526..00000000 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * 关于卡片消息 - * @author qscbm187531 - */ -package io.github.minecraftchampions.dodoopenjava.card; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/command/CommandManager.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/command/CommandManager.java index c96f2f6a..5e5a688e 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/command/CommandManager.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/command/CommandManager.java @@ -5,8 +5,9 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; -import java.util.HashMap; +import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; /** * 命令系统的相关方法 @@ -14,10 +15,10 @@ @Getter @RequiredArgsConstructor public class CommandManager { - private final HashMap commands = new HashMap<>(); + private final Map commands = new ConcurrentHashMap<>(); - private final HashMap commandAliasesMapping = new HashMap<>(); + private final Map commandAliasesMapping = new ConcurrentHashMap<>(); private final @NonNull Bot bot; @@ -36,7 +37,7 @@ private synchronized void init() { * * @param command 命令实例 */ - public synchronized void registerCommand(@NonNull CommandExecutor command) { + public void registerCommand(@NonNull CommandExecutor command) { String commandName = command.getMainCommand(); Set aliases = command.getCommandAliases(); if (aliases.contains(commandName)) { @@ -69,7 +70,7 @@ public synchronized void registerCommand(@NonNull CommandExecutor command) { * * @param command 命令实例 */ - public synchronized void unregisterCommand(@NonNull CommandExecutor command) { + public void unregisterCommand(@NonNull CommandExecutor command) { commands.remove(command.getMainCommand()); for (String str : command.getCommandAliases()) { commandAliasesMapping.remove(str); @@ -79,7 +80,7 @@ public synchronized void unregisterCommand(@NonNull CommandExecutor command) { /** * 注销所有命令 */ - public synchronized void unregisterAllCommands() { + public void unregisterAllCommands() { commands.clear(); commandAliasesMapping.clear(); } @@ -95,12 +96,14 @@ public synchronized void unregisterAllCommands() { public boolean trigger(@NonNull CommandSender sender, @NonNull String commandName, boolean personalMessage, @NonNull String... args) { CommandExecutor command; - if (commands.containsKey(commandName)) { - command = commands.get(commandName); - } else if (commandAliasesMapping.containsKey(commandName)) { - command = commands.get(commandAliasesMapping.get(commandName)); - } else { - return false; + synchronized (commands) { + if (commands.containsKey(commandName)) { + command = commands.get(commandName); + } else if (commandAliasesMapping.containsKey(commandName)) { + command = commands.get(commandAliasesMapping.get(commandName)); + } else { + return false; + } } if (!command.allowPersonalChat() && personalMessage) { return false; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/event/EventManager.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/event/EventManager.java index c815040c..fd65c095 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/event/EventManager.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/event/EventManager.java @@ -7,17 +7,18 @@ import java.lang.reflect.Modifier; import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; /** * 事件的相关方法(包含监听器注册等) */ public class EventManager { - private final HashMap, ArrayList>> handlers = new HashMap<>(); + private final Map, ArrayList>> handlers = new ConcurrentHashMap<>(); /** * 注册事件监听器 @@ -35,7 +36,7 @@ public void registerListeners(@NonNull Listener... listeners) { * * @param listener 事件监听器 */ - public synchronized void registerListener(@NonNull Listener listener) { + public void registerListener(@NonNull Listener listener) { for (Method method : listener.getClass().getDeclaredMethods()) { if (!Modifier.isPublic(method.getModifiers())) continue; @@ -53,10 +54,13 @@ public synchronized void registerListener(@NonNull Listener listener) { method.setAccessible(true); if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>()); - if (Modifier.isStatic(method.getModifiers())) { - handlers.get(eventClass).add(new SimpleEntry<>(method, null)); - } else { - handlers.get(eventClass).add(new SimpleEntry<>(method, listener)); + List> list = handlers.get(eventClass); + synchronized (list) { + if (Modifier.isStatic(method.getModifiers())) { + list.add(new SimpleEntry<>(method, null)); + } else { + list.add(new SimpleEntry<>(method, listener)); + } } } } @@ -65,7 +69,9 @@ public synchronized void registerListener(@NonNull Listener listener) { * 注销所有事件监听器 */ public synchronized void unregisterAllListeners() { - handlers.clear(); + synchronized (handlers) { + handlers.clear(); + } } /** @@ -73,11 +79,13 @@ public synchronized void unregisterAllListeners() { * * @param listener listener */ - public synchronized void unregisterListeners(@NonNull Listener listener) { - Set> set = handlers.keySet(); - for (Class clazz : set) { - List> list = handlers.get(clazz).stream().filter(e -> e.getKey().getDeclaringClass() == listener.getClass()).toList(); - handlers.get(clazz).removeAll(list); + public void unregisterListeners(@NonNull Listener listener) { + synchronized (handlers) { + Set> set = handlers.keySet(); + for (Class clazz : set) { + List> list = handlers.get(clazz).stream().filter(e -> e.getKey().getDeclaringClass() == listener.getClass()).toList(); + handlers.get(clazz).removeAll(list); + } } } @@ -91,6 +99,18 @@ public void fireEvent(@NonNull Event event) throws RuntimeException { if (event.getEventType() == null || event.getEventName().isEmpty()) { throw new RuntimeException("未知的Event"); } + synchronized (handlers) { + fireEvent(event, handlers); + } + } + + /** + * 触发事件 + * + * @param event 事件 + * @param handlers 储存 + */ + public static void fireEvent(@NonNull Event event, @NonNull Map, ArrayList>> handlers) { boolean isAsync = event.isAsynchronous(); if (!handlers.containsKey(event.eventType)) { return; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/event/WebHookEventTrigger.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/event/WebHookEventTrigger.java index 546f5394..68879891 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/event/WebHookEventTrigger.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/event/WebHookEventTrigger.java @@ -138,7 +138,7 @@ public void handle(HttpExchange httpExchange) { String error = """ { "status": -9999, - "message": "错误的请求" + "textMessage": "错误的请求" } """; httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, error.getBytes(StandardCharsets.UTF_8).length); @@ -165,29 +165,29 @@ public void handle(HttpExchange httpExchange) { String event = OpenSecretUtil.WebHookDecrypt(payload, SecretKey); JSONObject jsonObject = new JSONObject(Objects.requireNonNull(event)); if (jsonObject.getInt("type") == 2) { - String message = "{\n" + + String textMessage = "{\n" + " \"status\": 0,\n" + - " \"message\": \"\",\n" + + " \"textMessage\": \"\",\n" + " \"data\": {\n" + " \"checkCode\": \"" + jsonObject.getJSONObject("data").getString("checkCode") + "\"\n" + " }\n" + "}"; - httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, message.getBytes(StandardCharsets.UTF_8).length); + httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, textMessage.getBytes(StandardCharsets.UTF_8).length); OutputStream responseBody = httpExchange.getResponseBody(); OutputStreamWriter writer = new OutputStreamWriter(responseBody, StandardCharsets.UTF_8); - writer.write(message); + writer.write(textMessage); writer.close(); responseBody.close(); } else { - String message = """ + String textMessage = """ { "status": 0, - "message": "" + "textMessage": "" }"""; - httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, message.getBytes(StandardCharsets.UTF_8).length); + httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, textMessage.getBytes(StandardCharsets.UTF_8).length); OutputStream responseBody = httpExchange.getResponseBody(); OutputStreamWriter writer = new OutputStreamWriter(responseBody, StandardCharsets.UTF_8); - writer.write(message); + writer.write(textMessage); writer.close(); responseBody.close(); DisposeEvent thread = new DisposeEvent(jsonObject); @@ -197,7 +197,7 @@ public void handle(HttpExchange httpExchange) { String error = """ { "status": -9999, - "message": "处理失败" + "textMessage": "处理失败" } """; httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, error.getBytes(StandardCharsets.UTF_8).length); diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/event/WebSocketEventTrigger.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/event/WebSocketEventTrigger.java index 28b5d0ae..bac2b636 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/event/WebSocketEventTrigger.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/event/WebSocketEventTrigger.java @@ -22,7 +22,6 @@ import org.json.JSONObject; import java.net.URI; -import java.net.URISyntaxException; import java.nio.ByteBuffer; /** @@ -49,6 +48,10 @@ public WebSocketEventTrigger(Bot bot) { this.bot = bot; } + private int reacquireCount = 0; + + private final int reacquireMaxCount = 50; + @Override public void start() { v2(); @@ -71,6 +74,7 @@ public void reconnectWebsocket() { public synchronized void v2() { bot.getApi().V2.eventApi.getWebSocketConnection() .ifSuccess(result -> { + reacquireCount = 0; wssLo = result.getJSONObjectData().getString("endpoint"); try { mWebSocket = new WsListenerC(new URI(wssLo)); @@ -89,7 +93,15 @@ public synchronized void v2() { } }) .ifFailure(result -> { - DodoOpenJava.LOGGER.warn("获取websocket连接错误" + result.getMessage() + ";尝试重连"); + reacquireCount++; + if (reacquireCount > reacquireMaxCount) { + DodoOpenJava.LOGGER.error("获取websocket连接错误" + result.getMessage() + + ";\n当前重连次数:" + reacquireCount + ",已超过最大重连次数:" + reacquireMaxCount + + ",已取消重连"); + return; + } + DodoOpenJava.LOGGER.warn("获取websocket连接错误" + result.getMessage() + + ";\n已尝试重新获取,当前重新获取次数:" + reacquireCount); v2(); }); } @@ -145,23 +157,29 @@ public void onMessage(ByteBuffer bf) { return; } JSONObject jsontext = new JSONObject(message); - switch (jsontext.getJSONObject("data").getString("eventType")) { - case "1001" -> eventManager.fireEvent(new PersonalMessageEvent(jsontext)); - case "2001" -> eventManager.fireEvent(new MessageEvent(jsontext)); - case "3001" -> eventManager.fireEvent(new MessageReactionEvent(jsontext)); - case "3002" -> eventManager.fireEvent(new CardMessageButtonClickEvent(jsontext)); - case "3003" -> eventManager.fireEvent(new CardMessageFormSubmitEvent(jsontext)); - case "3004" -> eventManager.fireEvent(new CardMessageListSubmitEvent(jsontext)); - case "4001" -> eventManager.fireEvent(new MemberJoinEvent(jsontext)); - case "4002" -> eventManager.fireEvent(new MemberLeaveEvent(jsontext)); - case "5001" -> eventManager.fireEvent(new ChannelVoiceMemberJoinEvent(jsontext)); - case "5002" -> eventManager.fireEvent(new ChannelVoiceMemberLeaveEvent(jsontext)); - case "6001" -> eventManager.fireEvent(new ChannelArticlePublishEvent(jsontext)); - case "6002" -> eventManager.fireEvent(new ChannelArticleCommentEvent(jsontext)); - case "7001" -> eventManager.fireEvent(new GiftSendEvent(jsontext)); - case "8001" -> eventManager.fireEvent(new IntegralChangeEvent(jsontext)); - case "9001" -> eventManager.fireEvent(new GoodsPurchaseEvent(jsontext)); - default -> DodoOpenJava.LOGGER.warn("未知的事件!"); + try { + switch (jsontext.getJSONObject("data").getString("eventType")) { + case "1001" -> eventManager.fireEvent(new PersonalMessageEvent(jsontext)); + case "2001" -> eventManager.fireEvent(new MessageEvent(jsontext)); + case "3001" -> eventManager.fireEvent(new MessageReactionEvent(jsontext)); + case "3002" -> eventManager.fireEvent(new CardMessageButtonClickEvent(jsontext)); + case "3003" -> eventManager.fireEvent(new CardMessageFormSubmitEvent(jsontext)); + case "3004" -> eventManager.fireEvent(new CardMessageListSubmitEvent(jsontext)); + case "4001" -> eventManager.fireEvent(new MemberJoinEvent(jsontext)); + case "4002" -> eventManager.fireEvent(new MemberLeaveEvent(jsontext)); + case "5001" -> eventManager.fireEvent(new ChannelVoiceMemberJoinEvent(jsontext)); + case "5002" -> eventManager.fireEvent(new ChannelVoiceMemberLeaveEvent(jsontext)); + case "6001" -> eventManager.fireEvent(new ChannelArticlePublishEvent(jsontext)); + case "6002" -> eventManager.fireEvent(new ChannelArticleCommentEvent(jsontext)); + case "7001" -> eventManager.fireEvent(new GiftSendEvent(jsontext)); + case "8001" -> eventManager.fireEvent(new IntegralChangeEvent(jsontext)); + case "9001" -> eventManager.fireEvent(new GoodsPurchaseEvent(jsontext)); + default -> DodoOpenJava.LOGGER.warn("未知的事件!"); + } + } catch (Exception e) { + DodoOpenJava.LOGGER.warn("Websocket消息接收发生未知错误;消息内容:" + message + + ";\n错误内容:" + e.getLocalizedMessage()); + sendPing(); } } diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/Message.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/Message.java index 4beb8d40..be225b96 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/Message.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/Message.java @@ -1,147 +1,9 @@ package io.github.minecraftchampions.dodoopenjava.message; -import io.github.minecraftchampions.dodoopenjava.utils.BaseUtil; -import lombok.*; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; +import org.json.JSONObject; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import java.io.StringReader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +public abstract class Message { + public abstract JSONObject toMessage(); -/** - * 消息组件 - */ -@EqualsAndHashCode -@AllArgsConstructor(access = AccessLevel.PROTECTED) -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class Message { - private String text; - - @NonNull - private MessageBuilder messageBuilder; - - /** - * 获取构造器 - * - * @return builder - */ - public static MessageBuilder builder() { - return new MessageBuilder(); - } - - /** - * 序列化 - * - * @return 序列化后的文本 - */ - public String serialize() { - List list = messageBuilder.getComponents(); - StringBuilder sb = new StringBuilder(); - for (MessageComponent component : list) { - String content = BaseUtil.replaceXmlSpecialCharacters(component.getContent()); - List styles = component.getStyles(); - if (component instanceof MessageComponent.LinkComponent linkComponent) { - sb.append("<").append("link url=\"") - .append(linkComponent.getLink()) - .append("\">"); - if (styles.contains(MessageStyle.normal) && styles.size() == 1) { - sb.append(content); - } else { - for (MessageStyle style : styles) { - sb.append("<").append(style.toString()) - .append(">"); - } - sb.append(content); - for (int i = styles.size() - 1; i >= 0; i--) { - sb.append(""); - } - } - sb.append(""); - } else { - if (styles.contains(MessageStyle.normal) && styles.size() == 1) { - sb.append(content); - } else { - for (MessageStyle style : styles) { - sb.append("<").append(style.toString()) - .append(">"); - } - sb.append(content); - for (int i = styles.size() - 1; i >= 0; i--) { - sb.append(""); - } - } - } - } - return sb.toString(); - } - - /** - * 反序列化 - * - * @param str 需要被反序列化的文本 - * @return Message - */ - @SneakyThrows - public static Message deserialize(String str) { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder documentBuilder = factory.newDocumentBuilder(); - String input = "" + str + ""; - Document document = documentBuilder.parse(new InputSource(new StringReader(input))); - NodeList nodes = document.getFirstChild().getChildNodes(); - List list = new ArrayList<>(); - List nodeList = BaseUtil.getAllTextNodes(document.getFirstChild()); - for (Node node : nodeList) { - String content = node.getTextContent(); - List styleList = new ArrayList<>(); - boolean isLink = false; - String link = ""; - if (!node.getParentNode().getNodeName().equals("root")) { - while (!node.getParentNode().getNodeName().equals("root")) { - if (isLink) { - throw new RuntimeException("link组件外面不能套别的组件"); - } - Node parentNode = node.getParentNode(); - String nodeName = parentNode.getNodeName(); - if (nodeName.equals("link")) { - isLink = true; - link = parentNode.getAttributes().item(0).getNodeValue(); - } else { - styleList.add(MessageStyle.valueOf(nodeName)); - } - node = parentNode; - } - } - Collections.reverse(styleList); - MessageComponent messageComponent; - if (isLink) { - messageComponent = MessageComponent.LinkComponent.newComponent() - .link(link).content(content).style(styleList.toArray(MessageStyle[]::new)); - } else { - messageComponent = MessageComponent.newComponent() - .content(content).style(styleList.toArray(MessageStyle[]::new)); - } - list.add(messageComponent); - } - return Message.builder().append(list).build(); - } - - /** - * 转换为 markdown - * - * @return markdown - */ - @Override - public String toString() { - return text; - } + public abstract int getType(); } diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageBuilder.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageBuilder.java deleted file mode 100644 index f4244395..00000000 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageBuilder.java +++ /dev/null @@ -1,61 +0,0 @@ -package io.github.minecraftchampions.dodoopenjava.message; - -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import java.util.ArrayList; -import java.util.List; - -/** - * 消息构造器 - */ -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class MessageBuilder { - private final List components = new ArrayList<>(); - - public MessageBuilder append(MessageComponent component) { - this.components.add(component); - return this; - } - - public MessageBuilder append(MessageComponent... components) { - this.components.addAll(List.of(components)); - return this; - } - - public MessageBuilder append(List components) { - this.components.addAll(components); - return this; - } - - public MessageBuilder prepend(MessageComponent component) { - this.components.add(0, component); - return this; - } - - public MessageBuilder insert(int index, MessageComponent component) { - this.components.add(0, component); - return this; - } - - public MessageBuilder newLine() { - this.components.add(MessageComponent.newComponent().content("\n")); - return this; - } - - public MessageBuilder link(String link, String context, String contextStyle) { - this.components.add(MessageComponent.LinkComponent.newComponent() - .content(context).style(MessageStyle.cite).link(link)); - return this; - } - - public Message build() { - StringBuilder sb = new StringBuilder(); - for (MessageComponent component : this.components) { - sb.append(component.toString()); - } - return new Message(sb.toString(), this); - } -} diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/Card.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/CardMessage.java similarity index 84% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/Card.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/CardMessage.java index 18f30bb5..d717a608 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/Card.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/CardMessage.java @@ -1,7 +1,8 @@ -package io.github.minecraftchampions.dodoopenjava.card; +package io.github.minecraftchampions.dodoopenjava.message.card; -import io.github.minecraftchampions.dodoopenjava.card.component.CardComponent; -import io.github.minecraftchampions.dodoopenjava.card.enums.Theme; +import io.github.minecraftchampions.dodoopenjava.message.Message; +import io.github.minecraftchampions.dodoopenjava.message.card.component.CardComponent; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.Theme; import lombok.AllArgsConstructor; import lombok.NonNull; import org.json.JSONObject; @@ -13,7 +14,7 @@ * 卡片消息 */ @AllArgsConstructor -public class Card { +public class CardMessage extends Message { private final JSONObject JsonCard; /** @@ -28,7 +29,7 @@ public JSONObject toJSONObject() { /** * 初始化卡片 */ - public Card() { + public CardMessage() { JsonCard = new JSONObject(""" { "content": "", @@ -120,4 +121,15 @@ public void removeComponent(int index) { public String toString() { return toJSONObject().toString(); } + + + @Override + public JSONObject toMessage() { + return toJSONObject(); + } + + @Override + public int getType() { + return 6; + } } \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ButtonGroupComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ButtonGroupComponent.java similarity index 93% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ButtonGroupComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ButtonGroupComponent.java index 5b016b39..e7774857 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ButtonGroupComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ButtonGroupComponent.java @@ -1,7 +1,7 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; -import io.github.minecraftchampions.dodoopenjava.card.enums.ButtonAction; -import io.github.minecraftchampions.dodoopenjava.card.enums.Color; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.ButtonAction; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.Color; import lombok.NonNull; import org.json.JSONArray; import org.json.JSONObject; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/CardComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/CardComponent.java similarity index 80% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/CardComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/CardComponent.java index 8d0552d6..f2b5f06f 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/CardComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/CardComponent.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; import lombok.Getter; import org.json.JSONObject; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/CountdownComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/CountdownComponent.java similarity index 89% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/CountdownComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/CountdownComponent.java index 1728f779..9880af5b 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/CountdownComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/CountdownComponent.java @@ -1,6 +1,6 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; -import io.github.minecraftchampions.dodoopenjava.card.enums.Style; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.Style; import lombok.NonNull; /** diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/DividerComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/DividerComponent.java similarity index 72% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/DividerComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/DividerComponent.java index ea0b5e93..2576d990 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/DividerComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/DividerComponent.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; /** * 分割线组件 diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/FormComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/FormComponent.java similarity index 95% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/FormComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/FormComponent.java index 07bd198e..6b013484 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/FormComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/FormComponent.java @@ -1,6 +1,6 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; -import io.github.minecraftchampions.dodoopenjava.card.enums.Rows; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.Rows; import lombok.NonNull; import org.json.JSONArray; import org.json.JSONObject; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/HeaderComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/HeaderComponent.java similarity index 85% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/HeaderComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/HeaderComponent.java index 9f88517e..0aa9cd6a 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/HeaderComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/HeaderComponent.java @@ -1,6 +1,6 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; -import io.github.minecraftchampions.dodoopenjava.card.enums.TextType; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.TextType; import lombok.NonNull; import org.json.JSONObject; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ImageComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ImageComponent.java similarity index 86% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ImageComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ImageComponent.java index e40d082a..b1dc2afc 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ImageComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ImageComponent.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; import lombok.NonNull; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ImageGroupComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ImageGroupComponent.java similarity index 96% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ImageGroupComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ImageGroupComponent.java index 682a0995..3d2f9399 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ImageGroupComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ImageGroupComponent.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; import lombok.NonNull; import org.json.JSONArray; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ListSelectorComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ListSelectorComponent.java similarity index 97% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ListSelectorComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ListSelectorComponent.java index 1a51c45a..f0679dc4 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/ListSelectorComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/ListSelectorComponent.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; import io.github.minecraftchampions.dodoopenjava.utils.MapUtil; import lombok.NonNull; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/RemarkComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/RemarkComponent.java similarity index 96% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/RemarkComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/RemarkComponent.java index 97e39280..2e7035de 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/RemarkComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/RemarkComponent.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; import lombok.NonNull; import org.json.JSONArray; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/SectionAndButtonComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/SectionAndButtonComponent.java similarity index 92% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/SectionAndButtonComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/SectionAndButtonComponent.java index c5b30810..1323333d 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/SectionAndButtonComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/SectionAndButtonComponent.java @@ -1,6 +1,6 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; -import io.github.minecraftchampions.dodoopenjava.card.enums.Align; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.Align; import lombok.NonNull; /** diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/SectionComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/SectionComponent.java similarity index 93% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/SectionComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/SectionComponent.java index 89362003..b5c6f4b9 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/SectionComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/SectionComponent.java @@ -1,8 +1,8 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; -import io.github.minecraftchampions.dodoopenjava.card.enums.Cols; -import io.github.minecraftchampions.dodoopenjava.card.enums.SectionType; -import io.github.minecraftchampions.dodoopenjava.card.enums.TextType; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.Cols; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.SectionType; +import io.github.minecraftchampions.dodoopenjava.message.card.enums.TextType; import lombok.NonNull; import org.json.JSONObject; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/VideoComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/VideoComponent.java similarity index 94% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/VideoComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/VideoComponent.java index 6296d492..28816e89 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/component/VideoComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/VideoComponent.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.component; +package io.github.minecraftchampions.dodoopenjava.message.card.component; import lombok.NonNull; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/package-info.java new file mode 100644 index 00000000..44bb08b3 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/component/package-info.java @@ -0,0 +1,4 @@ +/** + * 组件列表 + */ +package io.github.minecraftchampions.dodoopenjava.message.card.component; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Align.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Align.java similarity index 83% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Align.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Align.java index c80726a0..ae50dc12 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Align.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Align.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/ButtonAction.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/ButtonAction.java similarity index 87% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/ButtonAction.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/ButtonAction.java index f0ce999c..ac307c34 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/ButtonAction.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/ButtonAction.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Color.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Color.java similarity index 89% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Color.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Color.java index 71ebabe3..2445521d 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Color.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Color.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Cols.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Cols.java similarity index 85% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Cols.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Cols.java index 95e2267e..a3659890 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Cols.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Cols.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Rows.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Rows.java similarity index 84% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Rows.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Rows.java index 8ed0273f..e9440283 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Rows.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Rows.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/SectionType.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/SectionType.java similarity index 86% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/SectionType.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/SectionType.java index 183b460f..4a821d34 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/SectionType.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/SectionType.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Style.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Style.java similarity index 78% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Style.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Style.java index 8fef4f2c..1f63396f 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Style.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Style.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/TextType.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/TextType.java similarity index 83% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/TextType.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/TextType.java index 6d59eefc..804f399c 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/TextType.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/TextType.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Theme.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Theme.java similarity index 91% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Theme.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Theme.java index f5bca35d..3ad1c3ee 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/card/enums/Theme.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/Theme.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.card.enums; +package io.github.minecraftchampions.dodoopenjava.message.card.enums; import lombok.Getter; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/package-info.java new file mode 100644 index 00000000..fee6706f --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/enums/package-info.java @@ -0,0 +1,4 @@ +/** + * 关于卡片消息的一些枚举 + */ +package io.github.minecraftchampions.dodoopenjava.message.card.enums; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/package-info.java new file mode 100644 index 00000000..df62377b --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/card/package-info.java @@ -0,0 +1,4 @@ +/** + * 卡片消息组件 + */ +package io.github.minecraftchampions.dodoopenjava.message.card; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/file/FileMessage.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/file/FileMessage.java new file mode 100644 index 00000000..95b6cd75 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/file/FileMessage.java @@ -0,0 +1,58 @@ +package io.github.minecraftchampions.dodoopenjava.message.file; + +import io.github.minecraftchampions.dodoopenjava.message.Message; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.json.JSONObject; + +/** + * 文件消息组件 + */ +@RequiredArgsConstructor +public class FileMessage extends Message { + @NonNull + private String url; + + @NonNull + private String name; + + @NonNull + private long size; + + @Override + public JSONObject toMessage() { + return null; + } + + @Override + public int getType() { + return 5; + } + + /** + * 设置文件链接 + * + * @param url 链接 + */ + public void setUrl(@NonNull String url) { + this.url = url; + } + + /** + * 设置文件名 + * + * @param name 文件名 + */ + public void setName(@NonNull String name) { + this.name = name; + } + + /** + * 设置文件大小 + * + * @param size 文件大小 + */ + public void setSize(long size) { + this.size = size; + } +} diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/file/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/file/package-info.java new file mode 100644 index 00000000..03884893 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/file/package-info.java @@ -0,0 +1,4 @@ +/** + * 文件消息组件 + */ +package io.github.minecraftchampions.dodoopenjava.message.file; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/image/ImageMessage.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/image/ImageMessage.java new file mode 100644 index 00000000..150dcc74 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/image/ImageMessage.java @@ -0,0 +1,91 @@ +package io.github.minecraftchampions.dodoopenjava.message.image; + +import io.github.minecraftchampions.dodoopenjava.message.Message; +import lombok.AllArgsConstructor; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.json.JSONObject; + +/** + * 图片消息组件 + */ +@RequiredArgsConstructor +@AllArgsConstructor +public class ImageMessage extends Message { + @NonNull + private String url; + + @NonNull + private Integer width; + + @NonNull + private Integer height; + + private Integer isOriginal; + + @Override + public JSONObject toMessage() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("url", url); + jsonObject.put("width", width); + jsonObject.put("height", height); + if (isOriginal != null) { + jsonObject.put("isOriginal", isOriginal); + } + return jsonObject; + } + + @Override + public int getType() { + return 2; + } + + /** + * 设置图片链接 + * + * @param url 图片 + */ + public void setUrl(@NonNull String url) { + this.url = url; + } + + /** + * 设置宽度 + * + * @param width 宽度 + */ + public void setWidth(@NonNull Integer width) { + this.width = width; + } + + /** + * 设置高度 + * + * @param height 高度 + */ + public void setHeight(@NonNull Integer height) { + this.height = height; + } + + /** + * 设置是否原图 + * + * @param isOriginal 0否1是 + */ + public void setIsOriginal(@NonNull Integer isOriginal) { + this.isOriginal = isOriginal; + } + + /** + * 设置是否原图 + * + * @param isOriginal ture/false + */ + public void setIsOriginal(boolean isOriginal) { + if (isOriginal) { + this.isOriginal = 1; + } else { + this.isOriginal = 0; + } + } +} diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/image/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/image/package-info.java new file mode 100644 index 00000000..08948ed4 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/image/package-info.java @@ -0,0 +1,4 @@ +/** + * 图片消息组件 + */ +package io.github.minecraftchampions.dodoopenjava.message.image; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/share/ShareMessage.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/share/ShareMessage.java new file mode 100644 index 00000000..7c5f5aa5 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/share/ShareMessage.java @@ -0,0 +1,34 @@ +package io.github.minecraftchampions.dodoopenjava.message.share; + +import io.github.minecraftchampions.dodoopenjava.message.Message; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.json.JSONObject; + +/** + * 分享消息组件 + */ +@RequiredArgsConstructor +public class ShareMessage extends Message { + @NonNull + private String jumpUrl; + + /** + * 设置跳转url + * + * @param jumpUrl url + */ + public void setJumpUrl(@NonNull String jumpUrl) { + this.jumpUrl = jumpUrl; + } + + @Override + public JSONObject toMessage() { + return new JSONObject().put("jumpUrl", jumpUrl); + } + + @Override + public int getType() { + return 4; + } +} diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/share/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/share/package-info.java new file mode 100644 index 00000000..6977c325 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/share/package-info.java @@ -0,0 +1,4 @@ +/** + * 分享消息组件 + */ +package io.github.minecraftchampions.dodoopenjava.message.share; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessage.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessage.java new file mode 100644 index 00000000..42480832 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessage.java @@ -0,0 +1,160 @@ +package io.github.minecraftchampions.dodoopenjava.message.text; + +import io.github.minecraftchampions.dodoopenjava.message.Message; +import io.github.minecraftchampions.dodoopenjava.utils.BaseUtil; +import lombok.*; +import org.json.JSONObject; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * 消息组件 + */ +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class TextMessage extends Message { + private String text; + + @NonNull + private TextMessageBuilder messageBuilder; + + /** + * 获取构造器 + * + * @return builder + */ + public static TextMessageBuilder builder() { + return new TextMessageBuilder(); + } + + /** + * 序列化 + * + * @return 序列化后的文本 + */ + public String serialize() { + List list = messageBuilder.getComponents(); + StringBuilder sb = new StringBuilder(); + for (TextMessageComponent component : list) { + String content = BaseUtil.replaceXmlSpecialCharacters(component.getContent()); + List styles = component.getStyles(); + if (component instanceof TextMessageComponent.LinkComponent linkComponent) { + sb.append("<").append("link url=\"") + .append(linkComponent.getLink()) + .append("\">"); + if (styles.contains(TextMessageStyle.normal) && styles.size() == 1) { + sb.append(content); + } else { + for (TextMessageStyle style : styles) { + sb.append("<").append(style.toString()) + .append(">"); + } + sb.append(content); + for (int i = styles.size() - 1; i >= 0; i--) { + sb.append(""); + } + } + sb.append(""); + } else { + if (styles.contains(TextMessageStyle.normal) && styles.size() == 1) { + sb.append(content); + } else { + for (TextMessageStyle style : styles) { + sb.append("<").append(style.toString()) + .append(">"); + } + sb.append(content); + for (int i = styles.size() - 1; i >= 0; i--) { + sb.append(""); + } + } + } + } + return sb.toString(); + } + + /** + * 反序列化 + * + * @param str 需要被反序列化的文本 + * @return Message + */ + @SneakyThrows + public static TextMessage deserialize(String str) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = factory.newDocumentBuilder(); + String input = "" + str + ""; + Document document = documentBuilder.parse(new InputSource(new StringReader(input))); + NodeList nodes = document.getFirstChild().getChildNodes(); + List list = new ArrayList<>(); + List nodeList = BaseUtil.getAllTextNodes(document.getFirstChild()); + for (Node node : nodeList) { + String content = node.getTextContent(); + List styleList = new ArrayList<>(); + boolean isLink = false; + String link = ""; + if (!node.getParentNode().getNodeName().equals("root")) { + while (!node.getParentNode().getNodeName().equals("root")) { + if (isLink) { + throw new RuntimeException("link组件外面不能套别的组件"); + } + Node parentNode = node.getParentNode(); + String nodeName = parentNode.getNodeName(); + if (nodeName.equals("link")) { + isLink = true; + link = parentNode.getAttributes().item(0).getNodeValue(); + } else { + styleList.add(TextMessageStyle.valueOf(nodeName)); + } + node = parentNode; + } + } + Collections.reverse(styleList); + TextMessageComponent textMessageComponent; + if (isLink) { + textMessageComponent = TextMessageComponent.LinkComponent.newComponent() + .link(link).content(content).style(styleList.toArray(TextMessageStyle[]::new)); + } else { + textMessageComponent = TextMessageComponent.newComponent() + .content(content).style(styleList.toArray(TextMessageStyle[]::new)); + } + list.add(textMessageComponent); + } + return TextMessage.builder().append(list).build(); + } + + /** + * 转换为 markdown + * + * @return markdown + */ + @Override + public String toString() { + return text; + } + + @Override + public JSONObject toMessage() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("content", text); + return jsonObject; + } + + @Override + public int getType() { + return 1; + } +} diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageBuilder.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageBuilder.java new file mode 100644 index 00000000..c932f992 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageBuilder.java @@ -0,0 +1,77 @@ +package io.github.minecraftchampions.dodoopenjava.message.text; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +/** + * 消息构造器 + */ +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class TextMessageBuilder { + private final List components = new ArrayList<>(); + + public TextMessageBuilder append(TextMessageComponent component) { + synchronized (this.components) { + this.components.add(component); + } + return this; + } + + public TextMessageBuilder append(TextMessageComponent... components) { + synchronized (this.components) { + this.components.addAll(List.of(components)); + } + return this; + } + + public TextMessageBuilder append(List components) { + synchronized (this.components) { + this.components.addAll(components); + } + return this; + } + + public TextMessageBuilder prepend(TextMessageComponent component) { + synchronized (this.components) { + this.components.add(0, component); + } + return this; + } + + public TextMessageBuilder insert(int index, TextMessageComponent component) { + synchronized (this.components) { + this.components.add(0, component); + } + return this; + } + + public TextMessageBuilder newLine() { + synchronized (this.components) { + this.components.add(TextMessageComponent.newComponent().content("\n")); + } + return this; + } + + public TextMessageBuilder link(String link, String context, String contextStyle) { + synchronized (this.components) { + this.components.add(TextMessageComponent.LinkComponent.newComponent() + .content(context).style(TextMessageStyle.cite).link(link)); + } + return this; + } + + public TextMessage build() { + StringBuilder sb = new StringBuilder(); + synchronized (this.components) { + for (TextMessageComponent component : this.components) { + sb.append(component.toString()); + } + } + return new TextMessage(sb.toString(), this); + } +} diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageComponent.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageComponent.java similarity index 53% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageComponent.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageComponent.java index c0956e15..37103482 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageComponent.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageComponent.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.message; +package io.github.minecraftchampions.dodoopenjava.message.text; import lombok.Getter; @@ -7,64 +7,63 @@ import java.util.List; @Getter -public class MessageComponent { - private MessageComponent() { - styles.add(MessageStyle.normal); +public class TextMessageComponent { + private TextMessageComponent() { + styles.add(TextMessageStyle.normal); } - public static MessageComponent newComponent() { - return new MessageComponent(); + public static TextMessageComponent newComponent() { + return new TextMessageComponent(); } protected String content; - protected List styles = new ArrayList<>(); + protected List styles = new ArrayList<>(); - public MessageComponent content(String str) { + public synchronized TextMessageComponent content(String str) { this.content = str; return this; } - public MessageComponent style(MessageStyle... styles) { - this.styles = new ArrayList<>(); - this.styles.addAll(Arrays.asList(styles)); + public TextMessageComponent style(TextMessageStyle... styles) { + this.styles = new ArrayList<>(Arrays.asList(styles)); return this; } @Override public String toString() { String str = this.content; - for (MessageStyle style : styles) { + for (TextMessageStyle style : styles) { str = String.format(style.getRegex(), str); } return str; } @Getter - public static class LinkComponent extends MessageComponent { + public static class LinkComponent extends TextMessageComponent { protected String link; public static LinkComponent newComponent() { return new LinkComponent(); } - public LinkComponent link(String link) { + public synchronized LinkComponent link(String link) { this.link = link; return this; } @Override - public LinkComponent content(String str) { + public synchronized LinkComponent content(String str) { this.content = str; return this; } @Override - public LinkComponent style(MessageStyle... styles) { + public synchronized LinkComponent style(TextMessageStyle... styles) { this.styles = new ArrayList<>(); - List list = Arrays.asList(styles); - if (list.contains(MessageStyle.code) || - list.contains(MessageStyle.cite)) + List list = Arrays.asList(styles); + if (list.contains(TextMessageStyle.code) || + list.contains(TextMessageStyle.cite)) throw new RuntimeException("LinkComponent不能传入code与cite style"); this.styles.addAll(list); @@ -74,7 +73,7 @@ public LinkComponent style(MessageStyle... styles) { @Override public String toString() { String str = this.content; - for (MessageStyle style : styles) { + for (TextMessageStyle style : styles) { str = String.format(style.getRegex(), str); } return "[" + diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageStyle.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageStyle.java similarity index 84% rename from src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageStyle.java rename to src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageStyle.java index 1790b283..aa7aa4bf 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/MessageStyle.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/TextMessageStyle.java @@ -1,4 +1,4 @@ -package io.github.minecraftchampions.dodoopenjava.message; +package io.github.minecraftchampions.dodoopenjava.message.text; import lombok.Getter; @@ -6,7 +6,7 @@ import java.util.Set; @Getter -public enum MessageStyle { +public enum TextMessageStyle { bold("**%s**"), italic("*%s*"), underline("__%s__"), @@ -18,7 +18,7 @@ public enum MessageStyle { private final String regex; - MessageStyle(String regex) { + TextMessageStyle(String regex) { this.regex = regex; } diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/package-info.java new file mode 100644 index 00000000..443ac1fa --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/text/package-info.java @@ -0,0 +1,4 @@ +/** + * 文字消息组件 + */ +package io.github.minecraftchampions.dodoopenjava.message.text; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/video/VideoMessage.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/video/VideoMessage.java new file mode 100644 index 00000000..7f3ec3d4 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/video/VideoMessage.java @@ -0,0 +1,80 @@ +package io.github.minecraftchampions.dodoopenjava.message.video; + +import io.github.minecraftchampions.dodoopenjava.message.Message; +import lombok.AllArgsConstructor; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import org.json.JSONObject; + +/** + * 视频消息组件 + */ +@RequiredArgsConstructor +@AllArgsConstructor +public class VideoMessage extends Message { + @NonNull + private String url; + + private String coverUrl; + + private Long duration; + + private Long size; + + /** + * 设置视频时长 + * + * @param duration 时长 + */ + public void setDuration(long duration) { + this.duration = duration; + } + + /** + * 设置视频大小 + * + * @param size 大小 + */ + public void setSize(long size) { + this.size = size; + } + + /** + * 设置视频链接 + * + * @param url 链接 + */ + public void setUrl(@NonNull String url) { + this.url = url; + } + + /** + * 设置视频大小 + * + * @param coverUrl 大小 + */ + public void setCover(@NonNull String coverUrl) { + this.coverUrl = coverUrl; + } + + @Override + public JSONObject toMessage() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("url", url); + if (coverUrl != null) { + jsonObject.put("coverUrl", coverUrl); + } + if (size != null) { + jsonObject.put("size", size); + } + if (duration != null) { + jsonObject.put("duration", duration); + } + return jsonObject; + } + + @Override + public int getType() { + return 3; + } +} diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/message/video/package-info.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/video/package-info.java new file mode 100644 index 00000000..3e22e2a6 --- /dev/null +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/message/video/package-info.java @@ -0,0 +1,4 @@ +/** + * 视频消息组件 + */ +package io.github.minecraftchampions.dodoopenjava.message.video; \ No newline at end of file diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/JsonData.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/JsonData.java index 3a984224..b5002cfc 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/JsonData.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/JsonData.java @@ -47,8 +47,8 @@ public JsonData() { GroupManager.setGroupsFile(Group); UserManager.setUsersFile(User); //获取文件内容 - JSONObject groupJson = getGroupFile(); - JSONObject userJson = getUserFile(); + JSONObject groupJson = getGroupFileData(); + JSONObject userJson = getUserFileData(); //键数组 Set groupSet = groupJson.getJSONObject("Groups").keySet(); List groups = new ArrayList<>(); @@ -146,20 +146,20 @@ public void saveToFile() throws IOException { } /** - * 获取权限组文件 + * 获取权限组文件数据 * * @return JSON对象 */ - public JSONObject getGroupFile() { + public JSONObject getGroupFileData() { return new JSONObject(Objects.requireNonNull(ConfigUtil.readFile(Group))); } /** - * 获取用户文件 + * 获取用户文件数据 * * @return JSON对象 */ - public JSONObject getUserFile() { + public JSONObject getUserFileData() { return new JSONObject(Objects.requireNonNull(ConfigUtil.readFile(User))); } } diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/PermData.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/PermData.java index 1e5f1606..cc7d871b 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/PermData.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/PermData.java @@ -1,11 +1,14 @@ package io.github.minecraftchampions.dodoopenjava.permissions.data; +import lombok.Getter; + import java.io.File; import java.io.IOException; /** * 存储实例 */ +@Getter public abstract class PermData { public File User; public File Group; diff --git a/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/YamlData.java b/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/YamlData.java index 3ea251f1..25638dcd 100644 --- a/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/YamlData.java +++ b/src/main/java/io/github/minecraftchampions/dodoopenjava/permissions/data/YamlData.java @@ -24,7 +24,7 @@ public class YamlData extends PermData { */ @Override public void saveToFile() throws IOException { - FileConfiguration groupFile = getGroupFile(); + FileConfiguration groupFile = getGroupFileData(); Map map = GroupManager.getGroups(); map.forEach((key, value) -> { List perms = value.getPermissions(); @@ -35,7 +35,7 @@ public void saveToFile() throws IOException { groupFile.set("Groups." + key + ".extend", extend); }); groupFile.save(Group); - FileConfiguration userFile = getUserFile(); + FileConfiguration userFile = getUserFileData(); Map hashMap = UserManager.getUsers(); hashMap.forEach((key, value) -> { List perms = value.getPermissions(); @@ -74,11 +74,11 @@ public YamlData() { GroupManager.setGroupsFile(Group); UserManager.setUsersFile(User); GroupManager.setGroups(new TreeMap<>()); - for (int i = 0; i < getGroupFile().getConfigurationSection("Groups").getKeys(false).size(); i++) { - String name = getGroupFile().getConfigurationSection("Groups").getKeys(false).stream().toList().get(i); - List perms = getGroupFile().getStringList("Groups." + name + ".perms"); - Boolean isDefault = getGroupFile().getBoolean("Groups." + name + ".isDefault"); - List extendsGroup = getGroupFile().getStringList("Groups." + name + "extend"); + for (int i = 0; i < getGroupFileData().getConfigurationSection("Groups").getKeys(false).size(); i++) { + String name = getGroupFileData().getConfigurationSection("Groups").getKeys(false).stream().toList().get(i); + List perms = getGroupFileData().getStringList("Groups." + name + ".perms"); + Boolean isDefault = getGroupFileData().getBoolean("Groups." + name + ".isDefault"); + List extendsGroup = getGroupFileData().getStringList("Groups." + name + "extend"); io.github.minecraftchampions.dodoopenjava.permissions.Group group = new Group(name); for (String perm : perms) { group.addPermission(perm); @@ -93,10 +93,10 @@ public YamlData() { } UserManager.setUsers(new TreeMap<>()); - for (int i = 0; i < getUserFile().getConfigurationSection("Users").getKeys(false).size(); i++) { - String DodoId = getUserFile().getConfigurationSection("Users").getKeys(false).stream().toList().get(i); - List perms = getUserFile().getStringList("Users." + DodoId + ".perms"); - String group = getUserFile().getString("Users." + DodoId + ".Group"); + for (int i = 0; i < getUserFileData().getConfigurationSection("Users").getKeys(false).size(); i++) { + String DodoId = getUserFileData().getConfigurationSection("Users").getKeys(false).stream().toList().get(i); + List perms = getUserFileData().getStringList("Users." + DodoId + ".perms"); + String group = getUserFileData().getString("Users." + DodoId + ".Group"); User user = new User(DodoId); for (String perm : perms) { user.addPermission(perm); @@ -106,11 +106,11 @@ public YamlData() { } } - public FileConfiguration getGroupFile() { + public FileConfiguration getGroupFileData() { return ConfigUtil.load(Group); } - public FileConfiguration getUserFile() { + public FileConfiguration getUserFileData() { return ConfigUtil.load(User); } } \ No newline at end of file