mirror of
https://github.com/jie65535/gc-openchat-plugin.git
synced 2025-12-05 18:01:38 +08:00
Initial commit
This commit is contained in:
@@ -1,2 +1,13 @@
|
||||
package com.github.jie65535.openchat;public class EventListeners {
|
||||
package com.github.jie65535.openchat;
|
||||
|
||||
import emu.grasscutter.server.event.player.PlayerJoinEvent;
|
||||
|
||||
public final class EventListeners {
|
||||
private static final OpenChatConfig config = OpenChatPlugin.getInstance().getConfig();
|
||||
|
||||
public static void onJoin(PlayerJoinEvent event) {
|
||||
if (!config.sendJoinMessage || config.joinMessage.isEmpty()) return;
|
||||
|
||||
event.getPlayer().dropMessage(config.joinMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,49 @@
|
||||
package com.github.jie65535.openchat;public class OpenChatConfig {
|
||||
/*
|
||||
* gc-openchat
|
||||
* Copyright (C) 2022 jie65535
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.github.jie65535.openchat;
|
||||
|
||||
public class OpenChatConfig {
|
||||
|
||||
/**
|
||||
* 服务器聊天开关
|
||||
*/
|
||||
public boolean serverChatEnabled = true;
|
||||
|
||||
/**
|
||||
* 服务器聊天消息格式
|
||||
* {nickName} 为玩家昵称
|
||||
* {uid} 为玩家UID
|
||||
* {message} 为消息内容
|
||||
*/
|
||||
public String serverChatFormat = "<color=#99CC99>{nickName}({uid})</color>: {message}";
|
||||
|
||||
/**
|
||||
* 每分钟发言消息数限制
|
||||
*/
|
||||
public int messageFreLimitPerMinute = 20;
|
||||
|
||||
/**
|
||||
* 是否发送玩家加入消息
|
||||
*/
|
||||
public boolean sendJoinMessage = true;
|
||||
|
||||
/**
|
||||
* 玩家加入消息
|
||||
*/
|
||||
public String joinMessage = "本服已启用聊天,/chat on 开启(默认),/chat off 屏蔽";
|
||||
}
|
||||
|
||||
@@ -17,23 +17,25 @@
|
||||
*/
|
||||
package com.github.jie65535.openchat;
|
||||
|
||||
public class OpenChatConfig {
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.ints.IntSet;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class OpenChatData {
|
||||
|
||||
/**
|
||||
* 服务器聊天开关
|
||||
* 禁言列表
|
||||
* Key: Uid
|
||||
* Value: End time
|
||||
*/
|
||||
public boolean serverChatEnabled = true;
|
||||
public Int2ObjectMap<Date> banList = new Int2ObjectOpenHashMap<>();
|
||||
|
||||
/**
|
||||
* 服务器聊天消息格式
|
||||
* {nickName} 为玩家昵称
|
||||
* {uid} 为玩家UID
|
||||
* {message} 为消息内容
|
||||
* 关闭聊天的玩家集合
|
||||
* Key: Uid
|
||||
*/
|
||||
public String serverChatFormat = "<color=#99CC99>{nickName}({uid})</color>: {message}";
|
||||
|
||||
/**
|
||||
* 每分钟发言消息数限制
|
||||
*/
|
||||
public int messageFreLimitPerMinute = 20;
|
||||
public IntSet offChatPlayers = new IntOpenHashSet();
|
||||
}
|
||||
|
||||
@@ -1,2 +1,136 @@
|
||||
package com.github.jie65535.openchat;public class OpenChatPlugin {
|
||||
/*
|
||||
* gc-openchat
|
||||
* Copyright (C) 2022 jie65535
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.github.jie65535.openchat;
|
||||
|
||||
import com.github.jie65535.openchat.commands.ChatPlayerCommands;
|
||||
import com.github.jie65535.openchat.commands.ChatServerCommands;
|
||||
import emu.grasscutter.plugin.Plugin;
|
||||
import emu.grasscutter.server.event.EventHandler;
|
||||
import emu.grasscutter.server.event.HandlerPriority;
|
||||
import emu.grasscutter.server.event.player.PlayerJoinEvent;
|
||||
import emu.grasscutter.utils.JsonUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Date;
|
||||
|
||||
public final class OpenChatPlugin extends Plugin {
|
||||
private static OpenChatPlugin instance;
|
||||
public static OpenChatPlugin getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private OpenChatConfig config;
|
||||
public OpenChatConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
private void loadConfig() {
|
||||
var configFile = new File(getDataFolder(), "config.json");
|
||||
if (!configFile.exists()) {
|
||||
config = new OpenChatConfig();
|
||||
try (var file = new FileWriter(configFile)) {
|
||||
file.write(JsonUtils.encode(config));
|
||||
} catch (IOException e) {
|
||||
getLogger().error("[OpenChat] Unable to write to config file.");
|
||||
} catch (Exception e) {
|
||||
getLogger().error("[OpenChat] Unable to save config file.");
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
config = JsonUtils.decode(Files.readString(configFile.toPath(), StandardCharsets.UTF_8), OpenChatConfig.class);
|
||||
} catch (Exception exception) {
|
||||
config = new OpenChatConfig();
|
||||
getLogger().error("[OpenChat] There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private OpenChatData data;
|
||||
public OpenChatData getData() {
|
||||
return data;
|
||||
}
|
||||
private void loadData() {
|
||||
data = new OpenChatData();
|
||||
var dataFile = new File(getDataFolder(), "data.json");
|
||||
if (!dataFile.exists()) {
|
||||
data = new OpenChatData();
|
||||
saveData();
|
||||
} else {
|
||||
try {
|
||||
data = JsonUtils.decode(Files.readString(dataFile.toPath(), StandardCharsets.UTF_8), OpenChatData.class);
|
||||
} catch (Exception exception) {
|
||||
data = new OpenChatData();
|
||||
getLogger().error("[OpenChat] There was an error while trying to load the data from data.json. Please make sure that there are no syntax errors. If you want to start with a default data, delete your existing data.json.");
|
||||
}
|
||||
}
|
||||
}
|
||||
public void saveData() {
|
||||
try (var file = new FileWriter(new File(getDataFolder(), "data.json"))) {
|
||||
file.write(JsonUtils.encode(data));
|
||||
} catch (IOException e) {
|
||||
getLogger().error("[OpenChat] Unable to write to data file.");
|
||||
} catch (Exception e) {
|
||||
getLogger().error("[OpenChat] Unable to save data file.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
instance = this;
|
||||
loadConfig();
|
||||
loadData();
|
||||
getLogger().info("[OpenChat] Loaded.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Register event listeners.
|
||||
new EventHandler<>(PlayerJoinEvent.class)
|
||||
.priority(HandlerPriority.LOW)
|
||||
.listener(EventListeners::onJoin)
|
||||
.register(this);
|
||||
|
||||
// Register commands.
|
||||
getHandle().registerCommand(new ChatServerCommands());
|
||||
getHandle().registerCommand(new ChatPlayerCommands());
|
||||
|
||||
getServer().setChatSystem(new OpenChatSystem(getServer(), this));
|
||||
|
||||
// Log a plugin status message.
|
||||
getLogger().info("[OpenChat] Enabled.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
saveData();
|
||||
getLogger().info("[OpenChat] Disabled.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新禁言列表,清理所有解除禁言用户
|
||||
*/
|
||||
public void updateBanList() {
|
||||
if (getData().banList.isEmpty())
|
||||
return;
|
||||
var now = new Date();
|
||||
getData().banList.int2ObjectEntrySet().removeIf(entry -> entry.getValue().before(now));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,83 @@
|
||||
package com.github.jie65535.openchat;public class OpenChatSystem {
|
||||
package com.github.jie65535.openchat;
|
||||
|
||||
import emu.grasscutter.GameConstants;
|
||||
import emu.grasscutter.game.chat.ChatSystem;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
import emu.grasscutter.server.game.GameServer;
|
||||
|
||||
public class OpenChatSystem extends ChatSystem {
|
||||
private final OpenChatPlugin plugin;
|
||||
public OpenChatSystem(GameServer server, OpenChatPlugin plugin) {
|
||||
super(server);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPrivateMessage(Player player, int targetUid, String message) {
|
||||
// Sanity checks.
|
||||
if (message == null || message.length() == 0) {
|
||||
return;
|
||||
}
|
||||
super.sendPrivateMessage(player, targetUid, message);
|
||||
|
||||
if (targetUid != GameConstants.SERVER_CONSOLE_UID || message.charAt(0) == '/' || message.charAt(0) == '!') {
|
||||
return;
|
||||
}
|
||||
|
||||
handlePlayerMessage(player, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理玩家消息
|
||||
* @param player 玩家对象
|
||||
* @param message 消息内容
|
||||
*/
|
||||
private void handlePlayerMessage(Player player, String message) {
|
||||
if (!plugin.getConfig().serverChatEnabled) {
|
||||
return;
|
||||
}
|
||||
// 刷新列表
|
||||
plugin.updateBanList();
|
||||
// 检测是否正在禁言中
|
||||
if (plugin.getData().banList.containsKey(player.getUid())) {
|
||||
return;
|
||||
}
|
||||
// 处理发言频率限制与发言内容审查
|
||||
if (!checkMessageFre(player) || !checkMessageModeration(message)) {
|
||||
// 可提示也可忽略,忽略可让玩家以为自己发送成功,其实别人看不到
|
||||
return;
|
||||
}
|
||||
// 格式化消息
|
||||
message = OpenChatPlugin.getInstance().getConfig().serverChatFormat
|
||||
.replace("{nickName}", player.getNickname())
|
||||
.replace("{uid}", String.valueOf(player.getUid()))
|
||||
.replace("{message}", message);
|
||||
// 转发给其它玩家
|
||||
for (Player p : getServer().getPlayers().values()) {
|
||||
// 将消息发送给除了自己以外所有未关闭聊天的玩家
|
||||
if (p != player && plugin.getData().offChatPlayers.contains(p.getUid())) {
|
||||
p.dropMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息内容审查
|
||||
* @param message 消息
|
||||
* @return 是否合法合规
|
||||
*/
|
||||
private boolean checkMessageModeration(String message) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息频率检查
|
||||
* @param player 玩家对象
|
||||
* @return 是否在约定阈值内
|
||||
*/
|
||||
private boolean checkMessageFre(Player player) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.github.jie65535.openchat;
|
||||
package com.github.jie65535.openchat.commands;
|
||||
|
||||
import com.github.jie65535.openchat.OpenChatPlugin;
|
||||
import emu.grasscutter.command.Command;
|
||||
import emu.grasscutter.command.CommandHandler;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
@@ -19,9 +20,10 @@ public class ChatPlayerCommands implements CommandHandler {
|
||||
var set = OpenChatPlugin.getInstance().getData().offChatPlayers;
|
||||
if (subCommand.equals("on")) {
|
||||
set.remove(targetPlayer.getUid());
|
||||
CommandHandler.sendMessage(sender);
|
||||
CommandHandler.sendMessage(sender, "OK");
|
||||
} else if (subCommand.equals("off")) {
|
||||
set.add(targetPlayer.getUid());
|
||||
CommandHandler.sendMessage(sender, "OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,54 @@
|
||||
package com.github.jie65535.openchat;
|
||||
package com.github.jie65535.openchat.commands;
|
||||
|
||||
import com.github.jie65535.openchat.OpenChatPlugin;
|
||||
import emu.grasscutter.command.Command;
|
||||
import emu.grasscutter.command.CommandHandler;
|
||||
import emu.grasscutter.game.player.Player;
|
||||
|
||||
@Command(label = "chat",
|
||||
aliases = { "openchat" },
|
||||
usage = { "on/off", "unban <uid>", "ban <uid> <time(Minutes)>"},
|
||||
permission = "player.chat",
|
||||
permissionTargeted = "server.chat")
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Command(label = "serverchat",
|
||||
aliases = { "sc" },
|
||||
usage = { "on/off", "unban|unmute @uid", "ban|mute @uid [time(Minutes)]"},
|
||||
permission = "server.chat",
|
||||
permissionTargeted = "server.chat.others")
|
||||
public class ChatServerCommands implements CommandHandler {
|
||||
@Override
|
||||
public void execute(Player sender, Player targetPlayer, List<String> args) {
|
||||
if (args.size() < 1) {
|
||||
sendUsageMessage(sender);
|
||||
return;
|
||||
}
|
||||
|
||||
var plugin = OpenChatPlugin.getInstance();
|
||||
var subCommand = args.get(0);
|
||||
switch (subCommand) {
|
||||
case "on" -> {
|
||||
plugin.getConfig().serverChatEnabled = true;
|
||||
CommandHandler.sendMessage(sender, "OK");
|
||||
}
|
||||
case "off" -> {
|
||||
plugin.getConfig().serverChatEnabled = false;
|
||||
CommandHandler.sendMessage(sender, "OK");
|
||||
}
|
||||
case "unban", "unmute" -> {
|
||||
plugin.getData().banList.remove(targetPlayer.getUid());
|
||||
CommandHandler.sendMessage(sender, "OK");
|
||||
}
|
||||
case "ban", "mute" -> {
|
||||
var time = new Date(2051190000);
|
||||
if (args.size() == 2) {
|
||||
try {
|
||||
time = new Date(System.currentTimeMillis() / 1000 + Integer.parseInt(args.get(0)) * 60L);
|
||||
} catch (NumberFormatException ignored) {
|
||||
CommandHandler.sendTranslatedMessage(sender, "commands.ban.invalid_time");
|
||||
return;
|
||||
}
|
||||
}
|
||||
plugin.getData().banList.put(targetPlayer.getUid(), time);
|
||||
CommandHandler.sendMessage(sender, "OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "opencommand-plugin",
|
||||
"description": "Open command interface for third-party clients",
|
||||
"version": "dev-1.5.0",
|
||||
"mainClass": "com.github.jie65535.opencommand.OpenCommandPlugin",
|
||||
"authors": ["jie65535", "方块君"]
|
||||
"name": "openchat-plugin",
|
||||
"description": "Chat with players in the server console",
|
||||
"version": "0.1.0",
|
||||
"mainClass": "com.github.jie65535.openchat.OpenChatPlugin",
|
||||
"authors": ["jie65535"]
|
||||
}
|
||||
Reference in New Issue
Block a user