Skip to content

Commit

Permalink
Logging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pugzy committed Nov 6, 2024
1 parent 5616aa7 commit a3c9e3f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/channels/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import tc.oc.pgm.api.event.ChannelMessageEvent;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.api.setting.SettingValue;
import tc.oc.pgm.util.Audience;
import tc.oc.pgm.util.Players;

/**
Expand Down Expand Up @@ -63,6 +64,10 @@ default SettingValue getSetting() {
return null;
}

default String getLoggerFormat(T target) {
return "<%s>: %s";
}

/**
* If the channel supports message redirection, where messages may be forwarded to another channel
* or location e.g. team messages might be redirected to global chat post match end.
Expand Down Expand Up @@ -206,5 +211,6 @@ default void broadcastMessage(Component component, T target, Predicate<MatchPlay
Collection<MatchPlayer> viewers = getBroadcastViewers(target);
Component finalMessage = formatMessage(target, null, component);
viewers.stream().filter(filter).forEach(player -> player.sendMessage(finalMessage));
Audience.console().sendMessage(finalMessage);
}
}
5 changes: 5 additions & 0 deletions core/src/main/java/tc/oc/pgm/channels/AdminChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public SettingValue getSetting() {
return SettingValue.CHAT_ADMIN;
}

@Override
public String getLoggerFormat(Void target) {
return "[A] %s: %s";
}

@Override
public boolean canSendMessage(MatchPlayer sender) {
return sender.getBukkit().hasPermission(Permissions.ADMINCHAT);
Expand Down
67 changes: 52 additions & 15 deletions core/src/main/java/tc/oc/pgm/channels/ChannelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -28,6 +30,7 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.key.CloudKey;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.channels.Channel;
import tc.oc.pgm.api.event.ChannelMessageEvent;
Expand All @@ -38,12 +41,16 @@
import tc.oc.pgm.api.setting.SettingValue;
import tc.oc.pgm.api.setting.Settings;
import tc.oc.pgm.ffa.Tribute;
import tc.oc.pgm.util.Audience;
import tc.oc.pgm.util.Players;
import tc.oc.pgm.util.bukkit.OnlinePlayerUUIDMapAdapter;
import tc.oc.pgm.util.text.TextException;

public class ChannelManager implements Listener {

CloudKey<AsyncPlayerChatEvent> ORIGINAL_EVENT_KEY =
CloudKey.of("event", AsyncPlayerChatEvent.class);

private static final Cache<AsyncPlayerChatEvent, Boolean> CHAT_EVENT_CACHE =
CacheBuilder.newBuilder()
.weakKeys()
Expand Down Expand Up @@ -85,30 +92,39 @@ public void registerCommands(CommandManager<CommandSender> manager) {
}
}

public void processChat(MatchPlayer sender, String message) {
if (message.isEmpty()) return;
public boolean processChat(MatchPlayer sender, AsyncPlayerChatEvent event) {
final String message = event.getMessage().trim();
if (message.isEmpty()) return false;

CommandContext<CommandSender> context = new CommandContext<>(sender.getBukkit(), manager);
context.store(ORIGINAL_EVENT_KEY, event);

Channel<?> channel = shortcuts.get(message.charAt(0));

if (channel != null && channel.canSendMessage(sender)) {
channel.processChatShortcut(sender, message, context);
context.optional(Channel.MESSAGE_KEY).ifPresent(event::setMessage);
}

if (channel == null) {
channel = getSelectedChannel(sender);
channel.processChatMessage(sender, message, context);
}

if (context.contains(Channel.MESSAGE_KEY)) process(channel, sender, context);
if (context.contains(Channel.MESSAGE_KEY)) {
return process(channel, sender, context);
}

return false;
}

public void process(
public boolean process(
Channel<?> channel, MatchPlayer sender, CommandContext<CommandSender> context) {
processChannelMessage(calculateChannelRedirect(channel, sender, context), sender, context);
return processChannelMessage(
calculateChannelRedirect(channel, sender, context), sender, context);
}

private <T> void processChannelMessage(
private <T> boolean processChannelMessage(
Channel<T> channel, MatchPlayer sender, CommandContext<CommandSender> context) {
if (!channel.canSendMessage(sender)) throw noPermission();
throwMuted(sender);
Expand All @@ -124,7 +140,7 @@ private <T> void processChannelMessage(

CHAT_EVENT_CACHE.put(asyncEvent, true);
sender.getMatch().callEvent(asyncEvent);
if (asyncEvent.isCancelled()) return;
if (asyncEvent.isCancelled()) return false;

final ChannelMessageEvent<T> event =
new ChannelMessageEvent<>(channel, sender, target, viewers, asyncEvent.getMessage());
Expand All @@ -135,7 +151,7 @@ private <T> void processChannelMessage(
if (event.getSender() != null && event.getCancellationReason() != null) {
event.getSender().sendWarning(event.getCancellationReason());
}
return;
return false;
}

Component finalMessage = event
Expand All @@ -144,6 +160,15 @@ private <T> void processChannelMessage(
event.getViewers().forEach(player -> player.sendMessage(finalMessage));

channel.messageSent(event);

String logFormat = "[CHAT] " + channel.getLoggerFormat(target);
context.optional(ORIGINAL_EVENT_KEY).ifPresentOrElse(e -> e.setFormat(logFormat), () -> {
String message = String.format(
logFormat, sender.getBukkit().getDisplayName(), context.get(Channel.MESSAGE_KEY));
Audience.console().sendMessage(text(message));
});

return true;
}

private Channel<?> calculateChannelRedirect(
Expand Down Expand Up @@ -208,17 +233,29 @@ public void onChat(AsyncPlayerChatEvent event) {
return;
}

event.setCancelled(true);
event.getRecipients().clear();

final MatchPlayer player = PGM.get().getMatchManager().getPlayer(event.getPlayer());
if (player == null) return;

final String message = event.getMessage().trim();
try {
processChat(player, message);
} catch (TextException e) {
// Allow sub-handlers to throw command exceptions just fine
player.sendWarning(e);
Runnable completion = () -> {
try {
boolean sent = processChat(player, event);
if (!sent) event.setCancelled(true);
} catch (TextException e) {
// Allow sub-handlers to throw command exceptions just fine
event.setCancelled(true);
player.sendWarning(e);
}
};

if (Bukkit.isPrimaryThread()) completion.run();
else {
try {
PGM.get().getExecutor().submit(completion).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public Character getShortcut() {
return '@';
}

@Override
public String getLoggerFormat(MatchPlayer target) {
return "(DM) %s -> " + target.getBukkit().getDisplayName() + ": %s";
}

@Override
public MatchPlayer getTarget(MatchPlayer sender, CommandContext<CommandSender> arguments) {
MatchPlayer target = arguments.get(TARGET_KEY);
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/tc/oc/pgm/channels/TeamChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public SettingValue getSetting() {
return SettingValue.CHAT_TEAM;
}

@Override
public String getLoggerFormat(Party target) {
return "(" + target.getNameLegacy() + ") %s: %s";
}

@Override
public boolean supportsRedirect() {
return true;
Expand Down

0 comments on commit a3c9e3f

Please sign in to comment.