Skip to content

Commit

Permalink
Mention Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Avalanche7CZ committed Feb 8, 2025
1 parent 6e53347 commit fcb41bc
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package eu.avalanche7.forgeannouncements.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import eu.avalanche7.forgeannouncements.utils.Lang;
import eu.avalanche7.forgeannouncements.utils.Mentions;
import eu.avalanche7.forgeannouncements.utils.PermissionsHandler;
import net.minecraft.Util;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.List;

@Mod.EventBusSubscriber(modid = "forgeannouncements")
public class MentionCommand {

@SubscribeEvent
public static void onServerStarting(RegisterCommandsEvent event) {
MentionCommand.register(event.getDispatcher());
}

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(Commands.literal("mention")
.then(Commands.argument("message", StringArgumentType.greedyString())
.executes(MentionCommand::executeMention)));
}

private static int executeMention(CommandContext<CommandSourceStack> context) {
CommandSourceStack source = context.getSource();
String message = StringArgumentType.getString(context, "message");
Level world = source.getLevel();
List<ServerPlayer> players = world.getServer().getPlayerList().getPlayers();
boolean isConsole = source.getEntity() == null;
ServerPlayer sender = isConsole ? null : (ServerPlayer) source.getEntity();

if (message.contains("@everyone")) {
if (!isConsole) {
if (!Mentions.canMentionEveryone(sender)) {
sender.sendMessage(Lang.translate("mention.too_frequent_mention_everyone"), Util.NIL_UUID);
return 0;
}

boolean hasPermission = PermissionsHandler.hasPermission(sender, PermissionsHandler.MENTION_EVERYONE_PERMISSION);
boolean hasPermissionLevel = sender.hasPermissions(PermissionsHandler.MENTION_EVERYONE_PERMISSION_LEVEL);
if (!hasPermission && !hasPermissionLevel) {
sender.sendMessage(Lang.translate("mention.no_permission_everyone"), Util.NIL_UUID);
return 0;
}
}
Mentions.notifyEveryone(players, sender, message, isConsole);
//source.sendSuccess(new TextComponent("Mentioned everyone successfully."), true);
return 1;
}
boolean mentioned = false;
for (ServerPlayer player : players) {
String mention = "@" + player.getName().getString();
if (message.contains(mention)) {
if (!isConsole) {
if (!Mentions.canMentionPlayer(sender, player)) {
sender.sendMessage(Lang.translate("mention.too_frequent_mention_player"), Util.NIL_UUID);
return 0;
}

boolean hasPermission = PermissionsHandler.hasPermission(sender, PermissionsHandler.MENTION_PLAYER_PERMISSION);
boolean hasPermissionLevel = sender.hasPermissions(PermissionsHandler.MENTION_PLAYER_PERMISSION_LEVEL);
if (!hasPermission && !hasPermissionLevel) {
sender.sendMessage(Lang.translate("mention.no_permission_player"), Util.NIL_UUID);
return 0;
}
}
Mentions.notifyPlayer(player, sender, message, isConsole);
mentioned = true;
}
}

if (mentioned) {
//source.sendSuccess(new TextComponent("Mentioned player(s) successfully."), true);
} else {
source.sendFailure(new TextComponent("No valid mentions found in the message."));
}

return 1;
}
}
34 changes: 18 additions & 16 deletions src/main/java/eu/avalanche7/forgeannouncements/utils/Mentions.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void onChatMessage(ServerChatEvent event) {
return;
}
DebugLogger.debugLog("Mention everyone detected");
notifyEveryone(players, sender, message);
notifyEveryone(players, sender, message, false);
event.setCanceled(true);
} else {
for (ServerPlayer player : players) {
Expand All @@ -73,15 +73,15 @@ public static void onChatMessage(ServerChatEvent event) {
return;
}
DebugLogger.debugLog("Mention player detected: " + player.getName().getString());
notifyPlayer(player, sender, message);
notifyPlayer(player, sender, message, false);
event.setCanceled(true);
}
}
}
}

private static boolean canMentionEveryone(ServerPlayer sender) {
if (sender.hasPermissions(2)) { // OP bypass
public static boolean canMentionEveryone(ServerPlayer sender) {
if (sender != null && sender.hasPermissions(2)) {
return true;
}
int rateLimit = MentionConfigHandler.EVERYONE_MENTION_RATE_LIMIT.get();
Expand All @@ -93,8 +93,8 @@ private static boolean canMentionEveryone(ServerPlayer sender) {
return true;
}

private static boolean canMentionPlayer(ServerPlayer sender, ServerPlayer player) {
if (sender.hasPermissions(2)) { // OP bypass
public static boolean canMentionPlayer(ServerPlayer sender, ServerPlayer player) {
if (sender != null && sender.hasPermissions(2)) {
return true;
}
int rateLimit = MentionConfigHandler.INDIVIDUAL_MENTION_RATE_LIMIT.get();
Expand All @@ -107,25 +107,27 @@ private static boolean canMentionPlayer(ServerPlayer sender, ServerPlayer player
return true;
}

private static void notifyEveryone(List<ServerPlayer> players, ServerPlayer sender, String message) {
String chatMessage = String.format(MentionConfigHandler.EVERYONE_MENTION_MESSAGE.get(), sender.getName().getString());
String titleMessage = String.format(MentionConfigHandler.EVERYONE_TITLE_MESSAGE.get(), sender.getName().getString());
public static void notifyEveryone(List<ServerPlayer> players, ServerPlayer sender, String message, boolean isConsole) {
String senderName = isConsole || sender == null ? "Console" : sender.getName().getString(); // Handle null sender
String chatMessage = String.format(MentionConfigHandler.EVERYONE_MENTION_MESSAGE.get(), senderName);
String titleMessage = String.format(MentionConfigHandler.EVERYONE_TITLE_MESSAGE.get(), senderName);
String subtitleMessage = message.replaceFirst("@everyone", "").trim();

for (ServerPlayer player : players) {
sendMentionNotification(player, chatMessage, titleMessage, subtitleMessage);
sendMentionNotification(player, player, chatMessage, titleMessage, subtitleMessage);
}
}

private static void notifyPlayer(ServerPlayer player, ServerPlayer sender, String message) {
String chatMessage = String.format(MentionConfigHandler.INDIVIDUAL_MENTION_MESSAGE.get(), sender.getName().getString());
String titleMessage = String.format(MentionConfigHandler.INDIVIDUAL_TITLE_MESSAGE.get(), sender.getName().getString());
public static void notifyPlayer(ServerPlayer player, ServerPlayer sender, String message, boolean isConsole) {
String senderName = isConsole || sender == null ? "Console" : sender.getName().getString();
String chatMessage = String.format(MentionConfigHandler.INDIVIDUAL_MENTION_MESSAGE.get(), senderName);
String titleMessage = String.format(MentionConfigHandler.INDIVIDUAL_TITLE_MESSAGE.get(), senderName);
String subtitleMessage = message.replaceFirst("@" + player.getName().getString(), "").trim();

sendMentionNotification(player, chatMessage, titleMessage, subtitleMessage);
sendMentionNotification(player, player, chatMessage, titleMessage, subtitleMessage);
}

private static void sendMentionNotification(ServerPlayer player, String chatMessage, String titleMessage, String subtitleMessage) {
private static void sendMentionNotification(ServerPlayer player, ServerPlayer senderForSound, String chatMessage, String titleMessage, String subtitleMessage) {
MutableComponent formattedChatMessage = MessageParser.parseMessage(chatMessage,null);
if (!subtitleMessage.isEmpty()) {
MutableComponent formattedSubtitleMessage = MessageParser.parseMessage("- " + subtitleMessage,null);
Expand All @@ -138,4 +140,4 @@ private static void sendMentionNotification(ServerPlayer player, String chatMess
}
player.playNotifySound(SoundEvents.PLAYER_LEVELUP, SoundSource.PLAYERS, 1.0F, 1.0F);
}
}
}

0 comments on commit fcb41bc

Please sign in to comment.