Skip to content

Commit

Permalink
1.1.5 尝试添加私信功能
Browse files Browse the repository at this point in the history
  • Loading branch information
handy-git committed Nov 6, 2024
1 parent ab7c91b commit b18177a
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.handyplus.chat</groupId>
<artifactId>PlayerChat</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
<description>一款有点好用的聊天插件</description>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.handyplus.chat.command.player;
package cn.handyplus.chat.command;

import cn.handyplus.chat.PlayerChat;
import cn.handyplus.chat.core.HornUtil;
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/cn/handyplus/chat/command/player/TellCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cn.handyplus.chat.command.player;

import cn.handyplus.chat.constants.ChatConstants;
import cn.handyplus.chat.listener.AsyncPlayerChatEventListener;
import cn.handyplus.chat.util.ConfigUtil;
import cn.handyplus.lib.command.IHandyCommandEvent;
import cn.handyplus.lib.util.AssertUtil;
import cn.handyplus.lib.util.BaseUtil;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
* 私信
*
* @author handy
*/
public class TellCommand implements IHandyCommandEvent {

@Override
public String command() {
return "tell";
}

@Override
public String permission() {
return "playerChat.tell";
}

@Override
public boolean isAsync() {
return true;
}

@Override
public void onCommand(CommandSender sender, Command cmd, String label, String[] args) {
// 参数是否正常
AssertUtil.notTrue(args.length < 2, sender, ConfigUtil.LANG_CONFIG.getString("paramFailureMsg"));
// 是否为玩家
Player player = AssertUtil.notPlayer(sender, BaseUtil.getMsgNotColor("noPlayerFailureMsg"));
// 接收人
String playerName = args[1];
// 获取消息
StringBuilder message = new StringBuilder();
for (int i = 2; i < args.length; i++) {
message.append(args[i]).append(" ");
}
// 发送消息
AsyncPlayerChatEventListener.sendMsg(player, message.toString(), ChatConstants.TELL, playerName);
}

}
5 changes: 5 additions & 0 deletions src/main/java/cn/handyplus/chat/constants/ChatConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public abstract class ChatConstants {
*/
public final static String DEFAULT = "default";

/**
* 私信频道
*/
public static final String TELL = "tell";

/**
* 消息类型
*/
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/handyplus/chat/core/ChatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ private synchronized static void sendTextMsg(BcUtil.BcMessageParam param, boolea
}
// 根据频道发送消息
for (Player onlinePlayer : ChannelUtil.getChannelPlayer(channel)) {
// 判断是否开启私信
if (StrUtil.isNotEmpty(chatParam.getTellPlayerName()) && !onlinePlayer.getName().equals(chatParam.getTellPlayerName())) {
continue;
}
MessageUtil.sendMessage(onlinePlayer, textComponent);
// 如果开启艾特,发送消息
if (ChatConstants.CHAT_TYPE.equals(param.getType()) && ConfigUtil.CHAT_CONFIG.getBoolean("at.enable") && chatParam.getMessage().contains(onlinePlayer.getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,28 @@ public void onChat(AsyncPlayerChatEvent event) {
}
// 取消事件
event.setCancelled(true);
// 发送消息
sendMsg(player, event.getMessage(), channel);
}

public static void sendMsg(Player player, String message, String channel) {
sendMsg(player, message, channel, null);
}

/**
* 发送消息
*
* @param player 玩家
* @param message 消息
* @param channel 渠道
* @param tellPlayerName 接收人
* @since 1.1.5
*/
public static void sendMsg(Player player, String message, String channel, String tellPlayerName) {
// 聊天频率处理
if (!this.chatTimeCheck(player)) {
if (chatTimeCheck(player)) {
return;
}

// 参数构建
BcUtil.BcMessageParam param = new BcUtil.BcMessageParam();
param.setPluginName(PlayerChat.getInstance().getName());
Expand All @@ -74,11 +90,14 @@ public void onChat(AsyncPlayerChatEvent event) {
if (chatParam == null) {
return;
}

// 添加接收人 1.1.5
chatParam.setTellPlayerName(tellPlayerName);
// 内容黑名单处理
String message = this.blackListCheck(event);
message = blackListCheck(message);
// @处理
message = ChatUtil.at(message);
// 消息内容
chatParam.setMessage(ChatUtil.at(message));
chatParam.setMessage(message);
// 有权限进行颜色代码处理
chatParam.setHasColor(player.hasPermission("playerChat.color"));
chatParam.setChannel(channel);
Expand All @@ -91,11 +110,10 @@ public void onChat(AsyncPlayerChatEvent event) {
/**
* 替换黑名单词语为*
*
* @param event 事件
* @param message 消息
* @return 健康消息
*/
private String blackListCheck(AsyncPlayerChatEvent event) {
String message = event.getMessage();
private static String blackListCheck(String message) {
List<String> blacklist = ConfigUtil.CONFIG.getStringList("blacklist");
if (CollUtil.isNotEmpty(blacklist)) {
for (String blackMsg : blacklist) {
Expand All @@ -113,18 +131,18 @@ private String blackListCheck(AsyncPlayerChatEvent event) {
* @param player 玩家
* @return true 可
*/
private boolean chatTimeCheck(Player player) {
private static boolean chatTimeCheck(Player player) {
int chatTime = HandyPermissionUtil.getReverseIntNumber(player, ConfigUtil.CONFIG, "chatTime");
if (ChatConstants.PLAYER_CHAT_TIME.containsKey(player.getUniqueId())) {
long keepAlive = (System.currentTimeMillis() - ChatConstants.PLAYER_CHAT_TIME.get(player.getUniqueId())) / 1000L;
if (keepAlive < chatTime) {
String waitTimeMsg = BaseUtil.getLangMsg("chatTime").replace("${chatTime}", (chatTime - keepAlive) + "");
MessageUtil.sendMessage(player, waitTimeMsg);
return false;
return true;
}
}
ChatConstants.PLAYER_CHAT_TIME.put(player.getUniqueId(), System.currentTimeMillis());
return true;
return false;
}

/**
Expand All @@ -149,7 +167,7 @@ public void onItemChat(AsyncPlayerChatEvent event) {
event.setCancelled(true);
Player player = event.getPlayer();
// 聊天频率处理
if (!this.chatTimeCheck(player)) {
if (chatTimeCheck(player)) {
return;
}
// 获取物品参数
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void unregister() {
* @param message 消息
*/
@Override
public void onPluginMessageReceived(@NotNull String channel,@NotNull Player player, byte[] message) {
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte[] message) {
// 自定义消息处理
String server = ConfigUtil.CONFIG.getString("server");
MessageUtil.sendConsoleDebugMessage("子服:" + server + "收到消息");
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/cn/handyplus/chat/param/ChatParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ public class ChatParam {
*/
private Integer itemId;

/**
* 接收人
*
* @since 1.1.5
*/
private String tellPlayerName;

}
31 changes: 31 additions & 0 deletions src/main/resources/chat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,37 @@ chat:
- '&7时间: %server_time_h:mm:ss a%'
# 点击后执行的命令
click: ''
# 私信频道(严禁删除本频道)
tell:
# 是否开启
enable: true
# 频道名称
name: "&7[&a私信频道&7]"
# 聊天格式 支持变量
format:
# 聊天前缀
prefix:
text: '${channel}%playerTitle_use%'
# 1.16+可用hover
hover:
- '&8▪ &6所在聊天频道: ${channel}'
- '&8▪ &6点击可执行命令/spawn'
# 点击后执行的命令
click: '/spawn'
# 聊天玩家名
player:
text: '&f${player}&7: '
hover:
- '快点击tpa我啦~'
# 点击后执行的命令
click: '/tpa ${player}'
# 聊天内容
msg:
text: '&f'
hover:
- '&7时间: %server_time_h:mm:ss a%'
# 点击后执行的命令
click: ''
# VIP频道
vip:
# 是否开启
Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: PlayerChat
main: cn.handyplus.chat.PlayerChat
version: 1.1.4
version: 1.1.5
author: handy
api-version: 1.13
softdepend: [ PlaceholderAPI ]
Expand All @@ -26,6 +26,7 @@ permissions:
playerChat.color: true
playerChat.channel: true
playerChat.item: true
playerChat.tell: true
playerChat.reload:
description: 重新加载配置
default: op
Expand Down Expand Up @@ -55,4 +56,13 @@ permissions:
default: true
playerChat.use.default:
description: 默认频道使用权限
default: true
playerChat.tell:
description: 私聊权限
default: op
playerChat.chat.tell:
description: 私信频道查看权限
default: true
playerChat.use.tell:
description: 私信频道使用权限
default: true

0 comments on commit b18177a

Please sign in to comment.