Skip to content
Merged
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Project metadata
group=xyz.earthcow.networkjoinmessages
version=3.1.0
version=3.2.0
description=A plugin handling join, leave and swap messages for proxy servers.

# Plugin.yml metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,13 @@ public PremiumVanish getVanishAPI() {
}

@Override
public void runTaskLater(Runnable task, int timeInSecondsLater) {
getProxy().getScheduler().schedule(this, task, timeInSecondsLater, TimeUnit.SECONDS);
public void cancelTask(int taskId) {
getProxy().getScheduler().cancel(taskId);
}

@Override
public int runTaskRepeatedly(Runnable task, int timeInSecondsLater) {
return getProxy().getScheduler().schedule(this, task, timeInSecondsLater, timeInSecondsLater, TimeUnit.SECONDS).getId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class BungeePlayer implements CorePlayer {
private final ProxiedPlayer bungeePlayer;
private CoreBackendServer lastKnownConnectedServer;
private final Audience audience;
private String cachedLeaveMessage;
private boolean disconnecting = false;

public BungeePlayer(ProxiedPlayer bungeePlayer) {
this.bungeePlayer = bungeePlayer;
Expand Down Expand Up @@ -77,4 +79,22 @@ public void setLastKnownConnectedServer(CoreBackendServer server) {
public boolean isInLimbo() {
return false;
}

@Override
public String getCachedLeaveMessage() {
return cachedLeaveMessage;
}
@Override
public void setCachedLeaveMessage(String cachedLeaveMessage) {
this.cachedLeaveMessage = cachedLeaveMessage;
}

@Override
public boolean isDisconnecting() {
return disconnecting;
}
@Override
public void setDisconnecting() {
this.disconnecting = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Core(CorePlugin plugin) {

this.coreImportCommand = new CoreImportCommand(corePlayerListener.getPlayerJoinTracker());
this.coreSpoofCommand = new CoreSpoofCommand(plugin, storage, messageHandler);
this.coreReloadCommand = new CoreReloadCommand(configManager, storage, discordIntegration, messageHandler);
this.coreReloadCommand = new CoreReloadCommand(plugin, configManager, storage, discordIntegration, messageHandler);
this.coreToggleJoinCommand = new CoreToggleJoinCommand(storage, messageHandler);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public final class MessageHandler {
private final Storage storage;
private final Formatter formatter;

private final Map<UUID, Integer> taskIds = new HashMap<>();

@Nullable
private final SayanVanishHook sayanVanishHook;

Expand All @@ -26,6 +28,36 @@ public MessageHandler(CorePlugin plugin, Storage storage, Formatter formatter, @
this.storage = storage;
this.formatter = formatter;
this.sayanVanishHook = sayanVanishHook;
initCacheTasks();
}

public void initCacheTasks() {
taskIds.values().forEach(plugin::cancelTask);
taskIds.clear();
if (storage.getLeaveCacheDuration() == 0) return;
for (CorePlayer player : plugin.getAllPlayers()) {
startLeaveCacheTaskForPlayer(player);
}
}

public void startLeaveCacheTaskForPlayer(CorePlayer player) {
if (storage.getLeaveCacheDuration() == 0) return;
taskIds.put(
player.getUniqueId(),
plugin.runTaskRepeatedly(() -> updateCachedLeaveMessage(player), storage.getLeaveCacheDuration())
);
}

public void stopLeaveCacheTaskForPlayer(CorePlayer player) {
if (taskIds.isEmpty()) return;
Integer taskId = taskIds.remove(player.getUniqueId());
if (taskId == null) return;
plugin.cancelTask(taskId);
}

public void updateCachedLeaveMessage(CorePlayer player) {
plugin.getCoreLogger().debug("Updating cached leave message for player " + player.getName());
formatter.parsePlaceholdersAndThen(formatLeaveMessage(player), player, player::setCachedLeaveMessage);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.*;

/**
* Singleton class for holding config values and user data that should persist after the user leaves the proxy
* Class for holding config values and user data that should persist after the user leaves the proxy
*/
public final class Storage {

Expand Down Expand Up @@ -85,6 +85,9 @@ public final class Storage {
@Getter
private String consoleSilentLeave;

@Getter
private int leaveCacheDuration;

/**
* The default silent state of a player joining with the networkjoinmessages.silent permission
* Default: true - Someone joining with the permission will be silent (not send a join message)
Expand Down Expand Up @@ -206,6 +209,8 @@ public void setUpDefaultValuesFromConfig() {

/// Settings

this.leaveCacheDuration = config.getInt("Settings.LeaveNetworkMessageCacheDuration");

this.silentJoinDefaultState = config.getBoolean("Settings.SilentJoinDefaultState");

this.swapServerMessageEnabled = config.getBoolean("Settings.SwapServerMessageEnabled");
Expand Down Expand Up @@ -266,6 +271,16 @@ public void setUpDefaultValuesFromConfig() {
);
this.swapServerMessageRequires = "ANY";
}

// Verify leave cache duration
if (leaveCacheDuration < 0) {
plugin.getCoreLogger()
.info(
"Setting error: Settings.LeaveNetworkMessageCacheDuration " +
"requires a non-negative value. Defaulting to 0 behavior."
);
this.leaveCacheDuration = 0;
}
}

public boolean getSilentMessageState(CorePlayer player) {
Expand Down Expand Up @@ -594,6 +609,8 @@ public String getLeaveNetworkMessage() {
public Collection<CustomChart> getCustomCharts() {
List<CustomChart> customCharts = new ArrayList<>();

customCharts.add(new SimplePie("leave_cache_duration", () -> String.valueOf(swapServerMessageEnabled)));

customCharts.add(new SimplePie("swap_enabled", () -> String.valueOf(swapServerMessageEnabled)));
customCharts.add(new SimplePie("first_join_enabled", () -> String.valueOf(firstJoinNetworkMessageEnabled)));
customCharts.add(new SimplePie("join_enabled", () -> String.valueOf(joinNetworkMessageEnabled)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public interface CorePlayer extends CoreCommandSender {
Audience getAudience();

boolean isInLimbo();

String getCachedLeaveMessage();
void setCachedLeaveMessage(String cachedLeaveMessage);

boolean isDisconnecting();
void setDisconnecting();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public interface CorePlugin {

PremiumVanish getVanishAPI();

void runTaskLater(Runnable task, int timeInSecondsLater);
void cancelTask(int taskId);
int runTaskRepeatedly(Runnable task, int timeInSecondsLater);
void runTaskAsync(Runnable task);

boolean isPluginLoaded(String pluginName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
import xyz.earthcow.networkjoinmessages.common.MessageHandler;
import xyz.earthcow.networkjoinmessages.common.Storage;
import xyz.earthcow.networkjoinmessages.common.abstraction.CoreCommandSender;
import xyz.earthcow.networkjoinmessages.common.abstraction.CorePlugin;
import xyz.earthcow.networkjoinmessages.common.modules.DiscordIntegration;

import java.util.List;

public class CoreReloadCommand implements Command {

private final CorePlugin plugin;
private final ConfigManager configManager;
private final Storage storage;
private final DiscordIntegration discordIntegration;
private final MessageHandler messageHandler;

public CoreReloadCommand(ConfigManager configManager, Storage storage, DiscordIntegration discordIntegration, MessageHandler messageHandler) {
public CoreReloadCommand(CorePlugin plugin, ConfigManager configManager, Storage storage, DiscordIntegration discordIntegration, MessageHandler messageHandler) {
this.plugin = plugin;
this.configManager = configManager;
this.storage = storage;
this.discordIntegration = discordIntegration;
Expand All @@ -28,10 +31,15 @@ public void execute(CoreCommandSender coreCommandSender, String[] args) {
configManager.reload();
storage.setUpDefaultValuesFromConfig();
discordIntegration.loadVariables();

messageHandler.sendMessage(coreCommandSender,
storage.getReloadConfirmation()
);
messageHandler.initCacheTasks();

// Update all player's cached leave message
plugin.runTaskAsync(() -> {
plugin.getAllPlayers().forEach(messageHandler::updateCachedLeaveMessage);
messageHandler.sendMessage(coreCommandSender,
storage.getReloadConfirmation()
);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public CorePlayerListener(CorePlugin plugin, Storage storage, MessageHandler mes
private boolean isSilentEvent(@NotNull CorePlayer player) {
// Event is silent if, the player has a silent message state OR
// premiumVanish is present, the treat vanished players as silent option is true, and the player is vanished
plugin.getCoreLogger().debug("Checking if the event for player " + player + " should been silent:");
plugin.getCoreLogger().debug("Checking if the event for player " + player.getName() + " should been silent:");
plugin.getCoreLogger().debug(String.format(
"silent message state: %s, SayanVanish hook is NOT null: %s, SVTreatVanishedPlayersAsSilent: %s, " +
"SayanVanish player is vanished: %s, PremiumVanish hook is NOT null: %s, " +
Expand Down Expand Up @@ -161,6 +161,9 @@ private void handlePlayerJoin(@NotNull CorePlayer player, @NotNull CoreBackendSe
storage.setConnected(player, true);
player.setLastKnownConnectedServer(server);

messageHandler.updateCachedLeaveMessage(player);
messageHandler.startLeaveCacheTaskForPlayer(player);

boolean firstJoin = !firstJoinTracker.hasJoined(player.getUniqueId());
MessageType msgType = firstJoin ? MessageType.FIRST_JOIN : MessageType.JOIN;

Expand Down Expand Up @@ -204,6 +207,8 @@ private void handlePlayerJoin(@NotNull CorePlayer player, @NotNull CoreBackendSe
private void handlePlayerSwap(@NotNull CorePlayer player, @NotNull CoreBackendServer server, boolean fromLimbo) {
player.setLastKnownConnectedServer(server);

messageHandler.updateCachedLeaveMessage(player);

String to = server.getName();
String from = storage.getFrom(player);

Expand Down Expand Up @@ -275,20 +280,27 @@ public void onServerConnected(@NotNull CorePlayer player, @NotNull CoreBackendSe
* @param player Trigger player
*/
public void onDisconnect(@NotNull CorePlayer player) {
if (player.isDisconnecting()) {
plugin.getCoreLogger().debug("Disconnect event ignored for player " + player.getName() +
", due to another disconnect event already processing them");
return;
}
player.setDisconnecting();

if (shouldNotBroadcast(player, MessageType.LEAVE)) {
plugin.getPlayerManager().removePlayer(player.getUniqueId());
storage.setConnected(player, false);
messageHandler.stopLeaveCacheTaskForPlayer(player);
return;
}

String message = messageHandler.formatLeaveMessage(player);
String message = player.getCachedLeaveMessage();

// Silent
boolean isSilent = isSilentEvent(player);

// Broadcast message
messageHandler.broadcastMessage(message, MessageType.LEAVE, player, isSilent);
// Broadcast message - NULL parseTarget skips parsing placeholders in sendMessage
messageHandler.broadcastMessage(message, MessageType.LEAVE, player.getCurrentServer().getName(), "", null, isSilent);

Component formattedMessage = Formatter.deserialize(message);
// Call the custom NetworkLeaveEvent
Expand All @@ -304,6 +316,7 @@ public void onDisconnect(@NotNull CorePlayer player) {

plugin.getPlayerManager().removePlayer(player.getUniqueId());
storage.setConnected(player, false);
messageHandler.stopLeaveCacheTaskForPlayer(player);
}

public H2PlayerJoinTracker getPlayerJoinTracker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.scheduler.ScheduledTask;
import lombok.Getter;
import org.bstats.charts.CustomChart;
import org.bstats.velocity.Metrics;
Expand All @@ -28,10 +29,12 @@

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Plugin(
Expand Down Expand Up @@ -69,6 +72,8 @@ public class VelocityMain implements CorePlugin {

private VelocityDiscordListener velocityDiscordListener = null;

private final List<ScheduledTask> tasks = new ArrayList<>();

@Inject
public VelocityMain(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory, Metrics.Factory metricsFactory) {
this.proxy = proxy;
Expand Down Expand Up @@ -169,8 +174,20 @@ public void unregisterDiscordListener() {
}

@Override
public void runTaskLater(Runnable task, int timeInSecondsLater) {
proxy.getScheduler().buildTask(this, task).delay(timeInSecondsLater, TimeUnit.SECONDS).schedule();
public void cancelTask(int taskId) {
try {
tasks.get(taskId).cancel();
} catch (IndexOutOfBoundsException ignored) {
}
}

@Override
public int runTaskRepeatedly(Runnable task, int timeInSecondsLater) {
tasks.add(
proxy.getScheduler().buildTask(this, task)
.delay(timeInSecondsLater, TimeUnit.SECONDS).repeat(timeInSecondsLater, TimeUnit.SECONDS).schedule()
);
return tasks.size() - 1;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class VelocityPlayer implements CorePlayer {
private final Player velocityPlayer;
private CoreBackendServer lastKnownConnectedServer;
private final Audience audience;
private String cachedLeaveMessage;
private boolean disconnecting = false;

public VelocityPlayer(Player velocityPlayer) {
this.velocityPlayer = velocityPlayer;
Expand Down Expand Up @@ -84,4 +86,23 @@ public boolean isInLimbo() {
//noinspection ConstantValue
return ((ConnectedPlayer) velocityPlayer).getConnection().getState().name() == null;
}

@Override
public String getCachedLeaveMessage() {
return cachedLeaveMessage;
}
@Override
public void setCachedLeaveMessage(String cachedLeaveMessage) {
this.cachedLeaveMessage = cachedLeaveMessage;
}

@Override
public boolean isDisconnecting() {
return disconnecting;
}

@Override
public void setDisconnecting() {
this.disconnecting = true;
}
}
Loading