Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
implementation("com.velocitypowered:velocity-api:3.1.2-SNAPSHOT")
implementation("cloud.commandframework:cloud-velocity:1.8.2")
implementation("cloud.commandframework:cloud-minecraft-extras:1.8.2")
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
compileOnly("net.luckperms:api:5.4")
compileOnly("io.github.miniplaceholders:miniplaceholders-api:2.0.0")
}
Expand All @@ -40,7 +41,7 @@ fun runCommand(command: String): String {
val release = System.getenv("GRADLE_RELEASE").equals("true", ignoreCase = true)
val gitHash = runCommand("git rev-parse --short HEAD")
group = "com.oskarsmc"
version = "1.4.0"
version = "1.5.0"

if (!release) {
version = "$version-$gitHash-SNAPSHOT"
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/oskarsmc/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.oskarsmc.message.command.MessageCommand;
import com.oskarsmc.message.command.MessageToggleCommand;
import com.oskarsmc.message.command.ReplyCommand;
import com.oskarsmc.message.command.SocialSpyCommand;
import com.oskarsmc.message.configuration.MessageSettings;
import com.oskarsmc.message.configuration.UserData;
import com.oskarsmc.message.locale.CommandExceptionHandler;
import com.oskarsmc.message.locale.TranslationManager;
import com.oskarsmc.message.logic.MessageMetrics;
Expand Down Expand Up @@ -50,6 +52,7 @@ public final class Message {
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
MessageSettings messageSettings = new MessageSettings(dataFolder, logger);
UserData userData = new UserData(dataFolder, logger);

injector = injector.createChildInjector(
new CloudInjectionModule<>(
Expand Down Expand Up @@ -81,7 +84,9 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
messageSettings.miniPlaceholdersIntegration(false);
}
}

if (messageSettings.isLoggingEnabled()){
com.oskarsmc.message.util.Logger.setLogContext(dataFolder, logger);
}
// Register custom exception handlers
injector.getInstance(CommandExceptionHandler.class);

Expand All @@ -91,6 +96,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {

// Commands
injector.getInstance(MessageCommand.class);
injector.getInstance(MessageToggleCommand.class);
injector.getInstance(SocialSpyCommand.class);
injector.getInstance(ReplyCommand.class);

Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/oskarsmc/message/command/MessageCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import cloud.commandframework.velocity.arguments.PlayerArgument;
import com.google.inject.Inject;
import com.oskarsmc.message.configuration.MessageSettings;
import com.oskarsmc.message.configuration.UserData;
import com.oskarsmc.message.event.MessageEvent;
import com.oskarsmc.message.logic.MessageHandler;
import com.oskarsmc.message.util.DefaultPermission;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -25,9 +27,10 @@ public final class MessageCommand {
* @param commandManager Command Manager
* @param proxyServer Proxy Server
* @param messageHandler Message Handler
* @param userData User Data
*/
@Inject
public MessageCommand(@NotNull MessageSettings messageSettings, @NotNull VelocityCommandManager<CommandSource> commandManager, ProxyServer proxyServer, MessageHandler messageHandler) {
public MessageCommand(@NotNull MessageSettings messageSettings, @NotNull VelocityCommandManager<CommandSource> commandManager, ProxyServer proxyServer, MessageHandler messageHandler, UserData userData) {
Command.Builder<CommandSource> builder = commandManager.commandBuilder("message", messageSettings.messageAliases().toArray(new String[0]));

commandManager.command(builder
Expand All @@ -36,12 +39,15 @@ public MessageCommand(@NotNull MessageSettings messageSettings, @NotNull Velocit
.permission(new DefaultPermission("osmc.message.send"))
.handler(context -> {
Player receiver = context.get("player");

proxyServer.getEventManager().fire(new MessageEvent(
context.getSender(),
receiver,
context.get("message")
)).thenAccept(messageHandler::handleMessageEvent);
if (!userData.getUserMessageState(receiver.getUniqueId()) && !context.getSender().hasPermission("osmc.message.bypass")) {
context.getSender().sendMessage(Component.translatable("oskarsmc.messages.disabled"));
} else {
proxyServer.getEventManager().fire(new MessageEvent(
context.getSender(),
receiver,
context.get("message")
)).thenAccept(messageHandler::handleMessageEvent);
}
})
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.oskarsmc.message.command;

import cloud.commandframework.Command;
import cloud.commandframework.velocity.VelocityCommandManager;
import com.google.inject.Inject;
import com.oskarsmc.message.configuration.MessageSettings;
import com.oskarsmc.message.configuration.UserData;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

/**
* Message Toggle command
*/
public final class MessageToggleCommand {

/**
* Construct the message toggle command.
*
* @param messageSettings Message Settings
* @param commandManager Command Manager
* @param userData User Data
*/
@Inject
public MessageToggleCommand(@NotNull MessageSettings messageSettings, @NotNull VelocityCommandManager<CommandSource> commandManager, UserData userData) {
Command.Builder<CommandSource> builder = commandManager.commandBuilder("msgtoggle",messageSettings.msgToggleAliases().toArray(new String[0])).permission("osmc.message.toggle");

commandManager.command(builder
.senderType(Player.class)
.literal("on")
.handler(context -> {
Player player = (Player) context.getSender();
UUID playerUUID = player.getUniqueId();
userData.saveUserMessageState(playerUUID, true);
player.sendMessage(Component.translatable("oskarsmc.message.command.msgtoggle.on"));
})
);

commandManager.command(builder
.senderType(Player.class)
.literal("off")
.handler(context -> {
Player player = (Player) context.getSender();
UUID playerUUID = player.getUniqueId();
userData.saveUserMessageState(playerUUID, false);
player.sendMessage(Component.translatable("oskarsmc.message.command.msgtoggle.off"));
})
);

commandManager.command(builder
.senderType(Player.class)
.handler(context -> {
Player player = (Player) context.getSender();
UUID playerId = player.getUniqueId();
boolean canBeMessaged = userData.getUserMessageState(playerId);
userData.saveUserMessageState(playerId, !canBeMessaged);
player.sendMessage(Component.translatable("oskarsmc.message.command.msgtoggle." + (canBeMessaged ? "off" : "on")));
})
);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ public final class MessageSettings {
private String messageSentMiniMessage;
private String messageReceivedMiniMessage;
private String messageSocialSpyMiniMessage;
private String messageLogFormat;

private HashMap<Class<? extends Exception>, String> customErrorHandlers;

private List<String> messageAlias;
private List<String> replyAlias;
private List<String> socialSpyAlias;
private List<String> msgToggleAlias;

private boolean luckpermsIntegration;
private boolean miniPlaceholdersIntegration;
private boolean selfMessageSending;
private boolean loggingEnabled;

private final double configVersion;
private boolean enabled;
Expand Down Expand Up @@ -74,16 +77,18 @@ public MessageSettings(@DataDirectory @NotNull Path dataFolder, Logger logger) {
this.luckpermsIntegration = toml.getBoolean("plugin.luckperms-integration");
this.selfMessageSending = toml.getBoolean("plugin.allow-self-message-sending");
this.miniPlaceholdersIntegration = toml.getBoolean("plugin.miniplaceholders-integration");
this.loggingEnabled = toml.getBoolean("plugin.enable-logging");

// Messages - Message
this.messageSentMiniMessage = toml.getString("messages.message-sent");
this.messageReceivedMiniMessage = toml.getString("messages.message-received");
this.messageSocialSpyMiniMessage = toml.getString("messages.message-socialspy");

this.messageLogFormat = toml.getString("messages.message-log-format");
// Aliases
this.messageAlias = toml.getList("aliases.message");
this.replyAlias = toml.getList("aliases.reply");
this.socialSpyAlias = toml.getList("aliases.socialspy");
this.msgToggleAlias = toml.getList("aliases.toggle");

// Exceptions
this.customErrorHandlers = new HashMap<>();
Expand Down Expand Up @@ -227,6 +232,16 @@ public boolean selfMessageSending() {
return selfMessageSending;
}

/**
* Get if the config enables the logging of messages.
*
* @return If the config enables the logging of messages.
*/
@Pure
public boolean isLoggingEnabled() {
return loggingEnabled;
}

/**
* Get the configuration version.
*
Expand All @@ -247,6 +262,16 @@ public String messageSocialSpyMiniMessage() {
return messageSocialSpyMiniMessage;
}

/**
* Get the MiniMessage markup of the log entry.
*
* @return The MiniMessage markup of the log entry.
*/
@Pure
public String messageLogFormat() {
return messageLogFormat;
}

/**
* Get the aliases of the message command.
*
Expand All @@ -267,6 +292,16 @@ public List<String> socialSpyAliases() {
return socialSpyAlias;
}

/**
* Get the aliases of the msgtoggle command.
*
* @return The aliases of the msgtoggle command.
*/
@Pure
public List<String> msgToggleAliases() {
return msgToggleAlias;
}

/**
* Get the aliases of the reply command.
*
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/com/oskarsmc/message/configuration/UserData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.oskarsmc.message.configuration;

import com.google.inject.Inject;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;

/**
* User Data class
*/
public final class UserData {
private final Path dataFolder;
private final Path file;

/**
* Construct userdata.
*
* @param dataFolder Data Folder
* @param logger Logger
*/
@Inject
public UserData(@DataDirectory @NotNull Path dataFolder, Logger logger) {
this.dataFolder = dataFolder;
this.file = this.dataFolder.resolve("userdata.yml");

createUserDataFile();
}

/**
* Save the user message state.
*
* @param playerUUID Player UUID
* @param canBeMessaged Can be messaged
*/
public void saveUserMessageState(UUID playerUUID, boolean canBeMessaged) {
final ConfigurationNode yaml = loadConfig();
yaml.getNode("users", playerUUID.toString(), "canBeMessaged").setValue(canBeMessaged);
saveUserData(yaml);
}

/**
* Get the user message state.
*
* @param playerUUID Player UUID
* @return Can be messaged
*/
public boolean getUserMessageState(UUID playerUUID) {
final ConfigurationNode yaml = loadConfig();
return yaml.getNode("users", playerUUID.toString(), "canBeMessaged").getBoolean(true);
}

private void createUserDataFile() {
if (!Files.exists(dataFolder)) {
try {
Files.createDirectory(dataFolder);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

if (!Files.exists(file)) {
try (InputStream in = MessageSettings.class.getResourceAsStream("/userdata.yml")) {
assert in != null;
Files.copy(in, file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

private @NotNull Path userDataFile() {
return this.file;
}

private ConfigurationNode loadConfig() {
try {
return YAMLConfigurationLoader.builder().setPath(this.file).build().load();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void saveUserData(ConfigurationNode yaml) {
try {
YAMLConfigurationLoader.builder().setPath(this.file).build().save(yaml);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/oskarsmc/message/logic/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.oskarsmc.message.configuration.MessageSettings;
import com.oskarsmc.message.event.MessageEvent;
import com.oskarsmc.message.util.Logger;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import io.github.miniplaceholders.api.MiniPlaceholders;
Expand All @@ -28,6 +29,7 @@ public final class MessageHandler {
private final MessageSettings messageSettings;
private final MiniMessage miniMessage = MiniMessage.miniMessage();
private final ConcurrentHashMap<Player, Player> conversations = new ConcurrentHashMap<>();

/**
* Conversation Watchers
*/
Expand Down Expand Up @@ -113,6 +115,10 @@ public void handleMessageEvent(@NotNull MessageEvent event) {
for (CommandSource watcher : conversationWatchers) {
watcher.sendMessage(socialSpyComponent);
}

if (messageSettings.isLoggingEnabled()){
Logger.addLog(miniMessage.deserialize(messageSettings.messageLogFormat(), placeholders));
}
}

private @NotNull TagResolver craftLuckpermsPlaceholders(String role, CachedMetaData cachedMetaData) {
Expand Down
Loading