Skip to content

Commit

Permalink
Merge pull request #187 from beanbeanjuice/179-send-last-messages-dur…
Browse files Browse the repository at this point in the history
…ing-switch

Send Last Few Messages During Switch
  • Loading branch information
beanbeanjuice committed Jun 19, 2024
2 parents 8d957a9 + f6699ad commit 03e2502
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 11 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@
# True if you will be using Discord. The reload command does not work with this.
use-discord: false

# Discord Bot Token (IGNORE IF use_discord = false). The reload command does not work with this.
# Discord Bot Token (IGNORE IF use_discord = false).
BOT-TOKEN: "TOKEN_HERE"

# Channel to send Discord messages to (IGNORE IF use_discord = false). The reload command does not work with this.
# Channel to send Discord messages to (IGNORE IF use_discord = false).
CHANNEL-ID: "GLOBAL_CHANNEL_ID"

bot-activity:
Expand Down Expand Up @@ -122,6 +122,7 @@ aliases:
# simpleproxychat.ban - Ban a player from the proxy. ➕
# simpleproxychat.unban - Unban a player from the proxy. ➕
# simpleproxychat.whisper - Whisper to another player on the proxy. ➕
# simpleproxychat.broadcast - Broadcast a message to everyone on the server. ➕
use-permissions: false

# Only messages that start with this character will be sent through the plugin.
Expand Down Expand Up @@ -156,6 +157,13 @@ update-notifications: true
# A FULL PROXY RESTART IS REQUIRED TO USE THIS.
use-simple-proxy-chat-banning-system: false

# This will store and re-send the last few chat messages when a player switches servers.
# This is here because sometimes Velocity/Bungee does not keep the previous messages when switching.
# This WILL retain old formatting if you change the formatting prior to reloading.
send-previous-messages-on-switch:
enabled: false
amount: 15

# These require a restart in order to take place.
commands:
whisper-aliases:
Expand All @@ -164,7 +172,7 @@ commands:
- "spc-r"

# DO NOT TOUCH THIS
file-version: 12
file-version: 13
```

**messages.yml**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.beanbeanjuice.simpleproxychat.utility.config.ConfigDataKey;
import com.beanbeanjuice.simpleproxychat.utility.config.Permission;
import com.beanbeanjuice.simpleproxychat.utility.epoch.EpochHelper;
import com.beanbeanjuice.simpleproxychat.utility.helper.LastMessagesHelper;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class ChatHandler {
private final Config config;
private final EpochHelper epochHelper;
private final Bot discordBot;
private final LastMessagesHelper lastMessagesHelper;

private final Consumer<String> globalLogger;
private final Consumer<String> pluginLogger;
Expand All @@ -47,6 +49,7 @@ public ChatHandler(Config config, EpochHelper epochHelper, Bot discordBot,
this.config = config;
this.epochHelper = epochHelper;
this.discordBot = discordBot;
this.lastMessagesHelper = new LastMessagesHelper(config);

this.globalLogger = globalLogger;
this.pluginLogger = pluginLogger;
Expand Down Expand Up @@ -89,7 +92,10 @@ public void chat(ChatMessageData chatMessageData, String minecraftMessage, Strin
}

// Log to Minecraft
if (config.getAsBoolean(ConfigDataKey.MINECRAFT_CHAT_ENABLED)) chatMessageData.chatSendToAllOtherPlayers(minecraftMessage);
if (config.getAsBoolean(ConfigDataKey.MINECRAFT_CHAT_ENABLED)) {
chatMessageData.chatSendToAllOtherPlayers(minecraftMessage);
lastMessagesHelper.addMessage(minecraftMessage);
}

}

Expand Down Expand Up @@ -226,7 +232,7 @@ public void runProxyJoinMessage(String playerName, UUID playerUUID, String serve
}

public void runProxySwitchMessage(String from, String to, String playerName, UUID playerUUID,
Consumer<String> minecraftLogger) {
Consumer<String> minecraftLogger, Consumer<String> playerLogger) {
String consoleConfigString = config.getAsString(ConfigDataKey.MINECRAFT_SWITCH_DEFAULT);
String discordConfigString = config.getAsString(ConfigDataKey.DISCORD_SWITCH_MESSAGE);
String minecraftConfigString = config.getAsString(ConfigDataKey.MINECRAFT_SWITCH_SHORT);
Expand Down Expand Up @@ -267,8 +273,10 @@ public void runProxySwitchMessage(String from, String to, String playerName, UUI
}

// Log to Minecraft
if (config.getAsBoolean(ConfigDataKey.MINECRAFT_SWITCH_ENABLED))
if (config.getAsBoolean(ConfigDataKey.MINECRAFT_SWITCH_ENABLED)) {
minecraftLogger.accept(minecraftMessage);
lastMessagesHelper.getBoundedArrayList().forEach(playerLogger);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public Bot(final Config config, final Consumer<String> errorLogger) {
bot = null;
return;
}

config.addReloadListener(this::updateActivity);
config.addReloadListener(this::updateStatus);
}

public void sendMessage(final String messageToSend) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ public class Config {
private YamlDocument yamlMessages;
private final File configFolder;
private final HashMap<ConfigDataKey, Object> config;
private final ArrayList<Runnable> reloadFunctions;

private boolean initialSetup = true;
@Getter private final ServerChatLockHelper serverChatLockHelper;

public Config(File configFolder) {
this.configFolder = configFolder;
config = new HashMap<>();
reloadFunctions = new ArrayList<>();
serverChatLockHelper = new ServerChatLockHelper();
}

Expand All @@ -48,11 +50,16 @@ public void initialize() {
} catch (IOException ignored) { }
}

public void addReloadListener(Runnable runnable) {
reloadFunctions.add(runnable);
}

public void reload() {
try {
yamlConfig.reload();
yamlMessages.reload();
readConfig();
reloadFunctions.forEach(Runnable::run);
} catch (IOException ignored) { }
}

Expand Down Expand Up @@ -115,6 +122,8 @@ private void readConfig() throws IOException {
config.put(ConfigDataKey.USE_HELPER, yamlConfig.getBoolean("use-helper"));
config.put(ConfigDataKey.UPDATE_NOTIFICATIONS, yamlConfig.getBoolean("update-notifications"));
config.put(ConfigDataKey.USE_SIMPLE_PROXY_CHAT_BANNING_SYSTEM, yamlConfig.getBoolean("use-simple-proxy-chat-banning-system"));
config.put(ConfigDataKey.SEND_PREVIOUS_MESSAGES_ON_SWITCH_ENABLED, yamlConfig.getBoolean("send-previous-messages-on-switch.enabled"));
config.put(ConfigDataKey.SEND_PREVIOUS_MESSAGES_ON_SWITCH_AMOUNT, yamlConfig.getInt("send-previous-messages-on-switch.amount"));

ArrayList<String> whisperAliases = (ArrayList<String>) yamlConfig.getStringList("commands.whisper-aliases");
config.put(ConfigDataKey.WHISPER_ALIASES, whisperAliases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public enum ConfigDataKey {
USE_HELPER,
UPDATE_NOTIFICATIONS,
USE_SIMPLE_PROXY_CHAT_BANNING_SYSTEM,
SEND_PREVIOUS_MESSAGES_ON_SWITCH_ENABLED,
SEND_PREVIOUS_MESSAGES_ON_SWITCH_AMOUNT,
WHISPER_ALIASES,
REPLY_ALIASES,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.beanbeanjuice.simpleproxychat.utility.datastructures;

import java.util.ArrayList;

public class BoundedArrayList<T> extends ArrayList<T> {

private final int maximumSize;

public BoundedArrayList(final int maximumSize) {
this.maximumSize = maximumSize;
}

@Override
public boolean add(T t) {
if (super.size() >= maximumSize) super.remove(0);
return super.add(t);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.beanbeanjuice.simpleproxychat.utility.helper;

import com.beanbeanjuice.simpleproxychat.utility.config.Config;
import com.beanbeanjuice.simpleproxychat.utility.config.ConfigDataKey;
import com.beanbeanjuice.simpleproxychat.utility.datastructures.BoundedArrayList;

public class LastMessagesHelper {

private final Config config;
private BoundedArrayList<String> boundedArrayList;

public LastMessagesHelper(final Config config) {
this.config = config;
boundedArrayList = new BoundedArrayList<>(config.getAsInteger(ConfigDataKey.SEND_PREVIOUS_MESSAGES_ON_SWITCH_AMOUNT));
config.addReloadListener(this::reset);
}

private void reset() {
BoundedArrayList<String> old = boundedArrayList;
boundedArrayList = new BoundedArrayList<>(config.getAsInteger(ConfigDataKey.SEND_PREVIOUS_MESSAGES_ON_SWITCH_AMOUNT));
boundedArrayList.addAll(old);
}

public void addMessage(final String message) {
boundedArrayList.add(message);
}

public BoundedArrayList<String> getBoundedArrayList() {
if (!config.getAsBoolean(ConfigDataKey.SEND_PREVIOUS_MESSAGES_ON_SWITCH_ENABLED)) return new BoundedArrayList<>(0);

return boundedArrayList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public void onPlayerServerSwitch(ServerSwitchEvent event) {
return streamPlayer.hasPermission(Permission.READ_SWITCH_MESSAGE.getPermissionNode());
return true;
})
.forEach((streamPlayer) -> streamPlayer.sendMessage(ChatMessageType.CHAT, Helper.convertToBungee(message)))
.forEach((streamPlayer) -> streamPlayer.sendMessage(ChatMessageType.CHAT, Helper.convertToBungee(message))),
(message) -> player.sendMessage(ChatMessageType.CHAT, Helper.convertToBungee(message))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ public void onServerConnected(ServerConnectedEvent event) {
return true;
})
.forEach((streamPlayer) -> streamPlayer.sendMessage(component));
}
},
(message) -> event.getPlayer().sendMessage(Helper.stringToComponent(message))
);
}

Expand Down
13 changes: 10 additions & 3 deletions projects/main-app/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
# True if you will be using Discord. The reload command does not work with this.
use-discord: false

# Discord Bot Token (IGNORE IF use_discord = false). The reload command does not work with this.
# Discord Bot Token (IGNORE IF use_discord = false).
BOT-TOKEN: "TOKEN_HERE"

# Channel to send Discord messages to (IGNORE IF use_discord = false). The reload command does not work with this.
# Channel to send Discord messages to (IGNORE IF use_discord = false).
CHANNEL-ID: "GLOBAL_CHANNEL_ID"

bot-activity:
Expand Down Expand Up @@ -82,6 +82,13 @@ update-notifications: true
# A FULL PROXY RESTART IS REQUIRED TO USE THIS.
use-simple-proxy-chat-banning-system: false

# This will store and re-send the last few chat messages when a player switches servers.
# This is here because sometimes Velocity/Bungee does not keep the previous messages when switching.
# This WILL retain old formatting if you change the formatting prior to reloading.
send-previous-messages-on-switch:
enabled: false
amount: 15

# These require a restart in order to take place.
commands:
whisper-aliases:
Expand All @@ -90,4 +97,4 @@ commands:
- "spc-r"

# DO NOT TOUCH THIS
file-version: 12
file-version: 13

0 comments on commit 03e2502

Please sign in to comment.