mirror of
https://github.com/System-End/Discord-MC-Chat.git
synced 2026-04-20 00:25:18 +00:00
重写完成至接收控制台日志和玩家发送聊天消息
This commit is contained in:
parent
96b8a17837
commit
d7c734629e
7 changed files with 170 additions and 5 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -18,6 +18,4 @@ classes/
|
|||
|
||||
# Fabric
|
||||
|
||||
run/
|
||||
|
||||
temp/
|
||||
run/
|
||||
|
|
@ -26,10 +26,14 @@ public class Config {
|
|||
public String channelId = "";
|
||||
public String consoleLogChannelId = "";
|
||||
|
||||
public boolean useUuidInsteadOfName = true;
|
||||
|
||||
public String worldName = "world";
|
||||
|
||||
public String avatarAPI = "https://mc-heads.net/avatar/%player%.png";
|
||||
|
||||
public boolean multiServerMode = false;
|
||||
|
||||
public List<String> adminsIds = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,9 @@ import net.dv8tion.jda.api.JDA;
|
|||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import net.dv8tion.jda.api.utils.ChunkingFilter;
|
||||
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
||||
import net.fabricmc.api.DedicatedServerModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
|
|
@ -16,6 +19,7 @@ import top.xujiayao.mcdiscordchat.utils.Texts;
|
|||
import top.xujiayao.mcdiscordchat.utils.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
/**
|
||||
* @author Xujiayao
|
||||
|
|
@ -25,6 +29,7 @@ public class Main implements DedicatedServerModInitializer {
|
|||
public static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("MCDiscordChat");
|
||||
public static final File CONFIG_FILE = new File(FabricLoader.getInstance().getConfigDir().toFile(), "mcdiscordchat.json");
|
||||
public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");
|
||||
public static String VERSION;
|
||||
public static Config CONFIG;
|
||||
public static JDA JDA;
|
||||
|
|
@ -47,6 +52,9 @@ public class Main implements DedicatedServerModInitializer {
|
|||
|
||||
try {
|
||||
JDA = JDABuilder.createDefault(CONFIG.generic.botToken)
|
||||
.setChunkingFilter(ChunkingFilter.ALL)
|
||||
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
||||
.enableIntents(GatewayIntent.GUILD_MEMBERS)
|
||||
// .addEventListeners(...)
|
||||
.build();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
package top.xujiayao.mcdiscordchat.minecraft.mixins;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.text.Text;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import static top.xujiayao.mcdiscordchat.Main.CONFIG;
|
||||
import static top.xujiayao.mcdiscordchat.Main.CONSOLE_LOG_CHANNEL;
|
||||
import static top.xujiayao.mcdiscordchat.Main.SIMPLE_DATE_FORMAT;
|
||||
import static top.xujiayao.mcdiscordchat.Main.TEXTS;
|
||||
|
||||
/**
|
||||
* @author Xujiayao
|
||||
*/
|
||||
@Mixin(MinecraftServer.class)
|
||||
public class MixinMinecraftServer {
|
||||
|
||||
@Inject(method = "sendSystemMessage", at = @At("HEAD"))
|
||||
private void sendSystemMessage(Text message, UUID sender, CallbackInfo ci) {
|
||||
if (!CONFIG.generic.consoleLogChannelId.isEmpty()) {
|
||||
CONSOLE_LOG_CHANNEL.sendMessage(TEXTS.consoleLogMessage()
|
||||
.replace("%timestamp%", SIMPLE_DATE_FORMAT.format(new Date()))
|
||||
.replace("%message%", message.getString())).queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
package top.xujiayao.mcdiscordchat.minecraft.mixins;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.dv8tion.jda.api.entities.Emote;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.minecraft.network.MessageType;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.filter.TextStream.Message;
|
||||
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.text.TranslatableText;
|
||||
import net.minecraft.util.Formatting;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static top.xujiayao.mcdiscordchat.Main.CHANNEL;
|
||||
import static top.xujiayao.mcdiscordchat.Main.CONFIG;
|
||||
import static top.xujiayao.mcdiscordchat.Main.HTTP_CLIENT;
|
||||
import static top.xujiayao.mcdiscordchat.Main.JDA;
|
||||
import static top.xujiayao.mcdiscordchat.Main.LOGGER;
|
||||
|
||||
/**
|
||||
* @author Xujiayao
|
||||
*/
|
||||
@Mixin(ServerPlayNetworkHandler.class)
|
||||
public class MixinServerPlayNetworkHandler {
|
||||
|
||||
@Shadow
|
||||
private ServerPlayerEntity player;
|
||||
|
||||
@Final
|
||||
@Shadow
|
||||
private MinecraftServer server;
|
||||
|
||||
@Inject(method = "handleMessage", at = @At("HEAD"), cancellable = true)
|
||||
private void handleMessage(Message message, CallbackInfo ci) {
|
||||
// if (config.generic.bannedMinecraft.contains(playerEntity.getEntityName())) {
|
||||
// return Optional.empty();
|
||||
// }
|
||||
|
||||
String contentToDiscord = message.getRaw();
|
||||
String contentToMinecraft = message.getRaw();
|
||||
|
||||
Text text = new TranslatableText("chat.type.text", player.getDisplayName(), contentToDiscord);
|
||||
JsonObject json = new Gson().fromJson(Text.Serializer.toJson(text), JsonObject.class);
|
||||
json.getAsJsonArray("with").remove(1);
|
||||
|
||||
if (StringUtils.countMatches(contentToDiscord, ":") >= 2) {
|
||||
String[] emoteNames = StringUtils.substringsBetween(contentToDiscord, ":", ":");
|
||||
for (String emoteName : emoteNames) {
|
||||
List<Emote> emotes = JDA.getEmotesByName(emoteName, true);
|
||||
if (!emotes.isEmpty()) {
|
||||
contentToDiscord = StringUtils.replaceIgnoreCase(contentToDiscord, (":" + emoteName + ":"), emotes.get(0).getAsMention());
|
||||
contentToMinecraft = StringUtils.replaceIgnoreCase(contentToMinecraft, (":" + emoteName + ":"), (Formatting.YELLOW + ":" + emoteName + ":" + Formatting.WHITE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (contentToDiscord.contains("@")) {
|
||||
String[] memberNames = StringUtils.substringsBetween(contentToDiscord, "@", " ");
|
||||
if (!StringUtils.substringAfterLast(contentToDiscord, "@").contains(" ")) {
|
||||
memberNames = ArrayUtils.add(memberNames, StringUtils.substringAfterLast(contentToDiscord, "@"));
|
||||
}
|
||||
for (String memberName : memberNames) {
|
||||
for (Member member : CHANNEL.getMembers()) {
|
||||
if (member.getUser().getName().equalsIgnoreCase(memberName)
|
||||
|| (member.getNickname() != null && member.getNickname().equalsIgnoreCase(memberName))) {
|
||||
contentToDiscord = StringUtils.replaceIgnoreCase(contentToDiscord, ("@" + memberName), member.getAsMention());
|
||||
contentToMinecraft = StringUtils.replaceIgnoreCase(contentToMinecraft, ("@" + memberName), (Formatting.YELLOW + "@" + member.getEffectiveName() + Formatting.WHITE));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Change colour for emoji too
|
||||
|
||||
json.getAsJsonArray("with").add(contentToMinecraft);
|
||||
Text finalText = Text.Serializer.fromJson(json.toString());
|
||||
server.getPlayerManager().broadcast(finalText, MessageType.CHAT, this.player.getUuid());
|
||||
ci.cancel();
|
||||
|
||||
JsonObject body = new JsonObject();
|
||||
body.addProperty("content", contentToDiscord);
|
||||
//body.addProperty("username", (CONFIG.generic.multiServerMode ? ("[" + config.multiServer.serverDisplayName + "] " + playerEntity.getEntityName() : playerEntity.getEntityName()));
|
||||
body.addProperty("username", player.getEntityName());
|
||||
body.addProperty("avatar_url", CONFIG.generic.avatarAPI.replace("%player%", (CONFIG.generic.useUuidInsteadOfName ? player.getUuid().toString() : player.getEntityName())));
|
||||
|
||||
try {
|
||||
Request request = new Request.Builder()
|
||||
.url(CONFIG.generic.webhookURL)
|
||||
.post(RequestBody.create(body.toString(), MediaType.get("application/json")))
|
||||
.build();
|
||||
|
||||
HTTP_CLIENT.newCall(request).execute();
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(ExceptionUtils.getStackTrace(e));
|
||||
}
|
||||
}
|
||||
|
||||
// @Inject(at = @At(value = "TAIL"), method = "executeCommand")
|
||||
// private void onCommandExecuted(String input, CallbackInfo ci) {
|
||||
// CommandExecutionCallback.EVENT.invoker().onExecuted(input, this.player.getCommandSource());
|
||||
// }
|
||||
}
|
||||
|
|
@ -124,13 +124,16 @@ public class Utils {
|
|||
" \"top.xujiayao.mcdiscordchat.Main\"\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" \"mixins\": [\n" +
|
||||
" \"mcdiscordchat.mixins.json\"\n" +
|
||||
" ],\n" +
|
||||
" \"depends\": {\n" +
|
||||
" \"fabricloader\": \">=0.13.3\",\n" +
|
||||
" \"fabric\": \"*\",\n" +
|
||||
" \"minecraft\": \"1.18.x\",\n" +
|
||||
" \"java\": \">=17\"\n" +
|
||||
" }\n" +
|
||||
"}\n", JsonObject.class);
|
||||
"}", JsonObject.class);
|
||||
|
||||
VERSION = json.get("version").getAsString();
|
||||
VERSION = VERSION.substring(VERSION.indexOf("-") + 1);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@
|
|||
"mixins": [
|
||||
"MixinPlayerManager",
|
||||
"MixinPlayerAdvancementTracker",
|
||||
"MixinServerPlayerEntity"
|
||||
"MixinServerPlayerEntity",
|
||||
"MixinMinecraftServer",
|
||||
"MixinServerPlayNetworkHandler"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue