Skip to content

Commit

Permalink
improve dirty reload compatibility
Browse files Browse the repository at this point in the history
improve join/leave messages
  • Loading branch information
xGinko committed Mar 5, 2024
1 parent 2fe94af commit aae2b13
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class AnarchyExploitFixes extends JavaPlugin {
private static Config config;
private static TPSCache tpsCache;
private static Logger logger;
private static Metrics metrics;
private static boolean isServerFolia, foundProtocolLib;

@Override
Expand Down Expand Up @@ -87,11 +88,27 @@ public void onEnable() {
AnarchyExploitFixesCommand.registerCommands();

logger.info("Loading metrics");
new Metrics(this, 8700);
metrics = new Metrics(this, 8700);

logger.info("Done.");
}

@Override
public void onDisable() {
AnarchyExploitFixesModule.modules.forEach(AnarchyExploitFixesModule::disable);
AnarchyExploitFixesModule.modules.clear();
if (metrics != null) {
metrics.shutdown();
metrics = null;
}
instance = null;
config = null;
languageCacheMap = null;
tpsCache = null;
logger = null;
isServerFolia = foundProtocolLib = false;
}

public static AnarchyExploitFixes getInstance() {
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public void onConMsgToggle(final CommandSender sender) {
final boolean msgsWereVisible;
PersistentDataContainer dataContainer = player.getPersistentDataContainer();

if (!dataContainer.has(NamespacedKeys.SHOW_CONNECTION_MSGS.key(), PersistentDataType.BOOLEAN)) {
if (!dataContainer.has(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN)) {
msgsWereVisible = AnarchyExploitFixes.getConfiguration().connectionMsgsAreOnByDefault;
} else {
msgsWereVisible = dataContainer.get(NamespacedKeys.SHOW_CONNECTION_MSGS.key(), PersistentDataType.BOOLEAN);
msgsWereVisible = dataContainer.get(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN);
}

dataContainer.set(NamespacedKeys.SHOW_CONNECTION_MSGS.key(), PersistentDataType.BOOLEAN, !msgsWereVisible);
dataContainer.set(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN, !msgsWereVisible);

if (msgsWereVisible) {
player.sendMessage(AnarchyExploitFixes.getLang(player.locale()).misc_disabledConnectionMsgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public enum NamespacedKeys {

SHOW_CONNECTION_MSGS(AnarchyExploitFixes.getKey("show-connection-msgs"));
CONNECT_MSG_TOGGLE(AnarchyExploitFixes.getKey("show-connection-msgs"));

private final NamespacedKey key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ private void onPlayerJoinEvent(PlayerJoinEvent event) {
for (Player onlinePlayer : plugin.getServer().getOnlinePlayers()) {
final boolean showConnects;
PersistentDataContainer dataContainer = onlinePlayer.getPersistentDataContainer();
if (!dataContainer.has(NamespacedKeys.SHOW_CONNECTION_MSGS.key())) {
if (!dataContainer.has(NamespacedKeys.CONNECT_MSG_TOGGLE.key())) {
showConnects = config.connectionMsgsAreOnByDefault;
} else {
showConnects = dataContainer.get(NamespacedKeys.SHOW_CONNECTION_MSGS.key(), PersistentDataType.BOOLEAN);
showConnects = dataContainer.get(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN);
}

if (showConnects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public JoinLeaveMessages() {
shouldEnable();
this.plugin = AnarchyExploitFixes.getInstance();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("misc.join-leave-messages.enable", "If you want to hide yourself or someone else when logging into the game,\nuse these permissions: anarchyexploitfixes.silentJoin, anarchyexploitfixes.silentLeave");
config.addComment("misc.join-leave-messages.enable",
"If you want to hide yourself or someone else when logging into the game,\n" +
"use these permissions: anarchyexploitfixes.silentJoin, anarchyexploitfixes.silentLeave");
this.connectionMsgsOnByDefault = config.connectionMsgsAreOnByDefault;
this.showInConsole = config.getBoolean("misc.join-leave-messages.show-in-console", false);
this.firstJoinEnabled = config.getBoolean("misc.join-leave-messages.first-join-messages.enable", false);
Expand Down Expand Up @@ -59,73 +61,54 @@ public void disable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerJoinEvent(PlayerJoinEvent event) {
event.joinMessage(null);
Player joiningPlayer = event.getPlayer();
if (firstJoinEnabled && !joiningPlayer.hasPlayedBefore()) return;
final Player joiningPlayer = event.getPlayer();
if (joiningPlayer.hasPermission("anarchyexploitfixes.silentJoin")) return;
if (firstJoinEnabled && !joiningPlayer.hasPlayedBefore()) return;

for (Player onlinePlayer : plugin.getServer().getOnlinePlayers()) {
final boolean showJoin;
PersistentDataContainer dataContainer = onlinePlayer.getPersistentDataContainer();
if (!dataContainer.has(NamespacedKeys.SHOW_CONNECTION_MSGS.key())) {
showJoin = connectionMsgsOnByDefault;
} else {
showJoin = dataContainer.get(NamespacedKeys.SHOW_CONNECTION_MSGS.key(), PersistentDataType.BOOLEAN);
}

if (showJoin) {
sendJoinMessage(onlinePlayer, joiningPlayer);
}
for (final Player onlinePlayer : plugin.getServer().getOnlinePlayers()) {
onlinePlayer.getScheduler().execute(plugin, () -> {
final PersistentDataContainer dataContainer = onlinePlayer.getPersistentDataContainer();
if (
!dataContainer.has(NamespacedKeys.CONNECT_MSG_TOGGLE.key()) ? connectionMsgsOnByDefault :
dataContainer.get(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN)
) {
onlinePlayer.sendMessage(AnarchyExploitFixes.getLang(onlinePlayer.locale()).misc_joinMessage
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(joiningPlayer.name()).build()));
}
}, null, 1L);
}

if (showInConsole) {
// Send to ConsoleSender so we can display colors
plugin.getServer().getConsoleSender().sendMessage(AnarchyExploitFixes.getLang(joiningPlayer.locale()).misc_joinMessage
plugin.getComponentLogger().info(AnarchyExploitFixes.getLang(joiningPlayer.locale()).misc_joinMessage
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(joiningPlayer.name()).build())
.append(Component.text(" (" + joiningPlayer.locale() + ")"))
);
.append(Component.text(" (" + joiningPlayer.locale() + ")")));
}
}

private void sendJoinMessage(Player receiver, Player joiningPlayer) {
receiver.sendMessage(AnarchyExploitFixes.getLang(receiver.locale()).misc_joinMessage
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(joiningPlayer.name()).build())
);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerLeaveEvent(PlayerQuitEvent event) {
event.quitMessage(null);
Player leavingPlayer = event.getPlayer();
final Player leavingPlayer = event.getPlayer();
if (leavingPlayer.hasPermission("anarchyexploitfixes.silentLeave")) return;

for (Player onlinePlayer : plugin.getServer().getOnlinePlayers()) {
if (onlinePlayer.equals(leavingPlayer)) continue; // No need to send it to the leaver

final boolean showLeave;
PersistentDataContainer dataContainer = onlinePlayer.getPersistentDataContainer();
if (!dataContainer.has(NamespacedKeys.SHOW_CONNECTION_MSGS.key())) {
showLeave = connectionMsgsOnByDefault;
} else {
showLeave = dataContainer.get(NamespacedKeys.SHOW_CONNECTION_MSGS.key(), PersistentDataType.BOOLEAN);
}

if (showLeave) {
sendLeaveMessage(onlinePlayer, leavingPlayer);
}
for (final Player onlinePlayer : plugin.getServer().getOnlinePlayers()) {
onlinePlayer.getScheduler().execute(plugin, () -> {
if (onlinePlayer.getUniqueId().equals(leavingPlayer.getUniqueId())) return;
final PersistentDataContainer dataContainer = onlinePlayer.getPersistentDataContainer();
if (
!dataContainer.has(NamespacedKeys.CONNECT_MSG_TOGGLE.key()) ? connectionMsgsOnByDefault :
dataContainer.get(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN)
) {
onlinePlayer.sendMessage(AnarchyExploitFixes.getLang(onlinePlayer.locale()).misc_leaveMessage
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(leavingPlayer.name()).build()));
}
}, null, 1L);
}

if (showInConsole) {
// Send to ConsoleSender so we can display colors
plugin.getServer().getConsoleSender().sendMessage(AnarchyExploitFixes.getLang(leavingPlayer.locale()).misc_leaveMessage
plugin.getComponentLogger().info(AnarchyExploitFixes.getLang(leavingPlayer.locale()).misc_leaveMessage
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(leavingPlayer.name()).build())
.append(Component.text(" (" + leavingPlayer.locale() + ")"))
);
.append(Component.text(" (" + leavingPlayer.locale() + ")")));
}
}

private void sendLeaveMessage(Player onlinePlayer, Player leavingPlayer) {
onlinePlayer.sendMessage(AnarchyExploitFixes.getLang(onlinePlayer.locale()).misc_leaveMessage
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(leavingPlayer.name()).build())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bstats.bukkit.Metrics;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
Expand All @@ -28,11 +29,11 @@ public class AnarchyExploitFixes extends JavaPlugin {
private static HashMap<String, LanguageCache> languageCacheMap;
private static Config config;
private static Logger logger;
private static int minorMCVersion;
private static Metrics metrics;
private static boolean foundProtocolLib;

public TPSCache tpsCache;
public final Set<UUID> CONNECTION_MSG_TOGGLE = new HashSet<>();
public static final Set<UUID> CONNECT_MSG_TOGGLE = new HashSet<>();

@Override
public void onEnable() {
Expand Down Expand Up @@ -63,12 +64,11 @@ public void onEnable() {
return;
}

minorMCVersion = PaperLib.getMinecraftVersion();
logger.info("Detected Version 1." + minorMCVersion + "." + PaperLib.getMinecraftPatchVersion());
logger.info("Detected Version 1." + PaperLib.getMinecraftVersion() + "." + PaperLib.getMinecraftPatchVersion());
foundProtocolLib = getServer().getPluginManager().isPluginEnabled("ProtocolLib");
logger.info(foundProtocolLib ? "Found ProtocolLib" : "Could not find ProtocolLib");

if (minorMCVersion < 12) {
if (PaperLib.getMinecraftVersion() < 12) {
logger.warning("##########################################################");
logger.warning("# #");
logger.warning("# WARNING #");
Expand All @@ -90,11 +90,29 @@ public void onEnable() {
AnarchyExploitFixesCommand.registerCommands();

logger.info("Loading metrics");
new Metrics(this, 8700);
metrics = new Metrics(this, 8700);

logger.info("Done.");
}

@Override
public void onDisable() {
getServer().getScheduler().cancelTasks(this);
HandlerList.unregisterAll(this);
AnarchyExploitFixesModule.unregisterPacketListeners(this);
AnarchyExploitFixesModule.modules.clear();
if (metrics != null) {
metrics.shutdown();
metrics = null;
}
instance = null;
config = null;
languageCacheMap = null;
tpsCache = null;
logger = null;
foundProtocolLib = false;
}

public static AnarchyExploitFixes getInstance() {
return instance;
}
Expand All @@ -118,7 +136,7 @@ public static LanguageCache getLang(String lang) {
return languageCacheMap.getOrDefault(lang.replace("-", "_"), languageCacheMap.get(config.default_lang.toString().toLowerCase()));
}
public static int getMCVersion() {
return minorMCVersion;
return PaperLib.getMinecraftVersion();
}
public static boolean isProtocolLibInstalled() {
return foundProtocolLib;
Expand All @@ -132,8 +150,8 @@ public void reloadPlugin() {
private void reloadConfiguration() {
try {
config = new Config();
tpsCache = TPSCache.create(config.max_tps_check_interval_millis);
AnarchyExploitFixesModule.reloadModules();
this.tpsCache = TPSCache.create(config.max_tps_check_interval_millis);
config.saveConfig();
} catch (Exception e) {
logger.severe("Failed to load config file! - " + e.getLocalizedMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ public void onConMsgToggle(final CommandSender sender) {
return;
}

AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
Player player = (Player) sender;
final Player player = (Player) sender;

if (plugin.CONNECTION_MSG_TOGGLE.contains(player.getUniqueId())) {
plugin.CONNECTION_MSG_TOGGLE.remove(player.getUniqueId());
if (AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(player.getUniqueId())) {
AnarchyExploitFixes.CONNECT_MSG_TOGGLE.remove(player.getUniqueId());

if (AnarchyExploitFixes.getConfiguration().connectionMsgsAreOnByDefault) {
sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_enabledConnectionMsgs);
} else {
sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_disabledConnectionMsgs);
}
} else {
plugin.CONNECTION_MSG_TOGGLE.add(player.getUniqueId());
AnarchyExploitFixes.CONNECT_MSG_TOGGLE.add(player.getUniqueId());

if (AnarchyExploitFixes.getConfiguration().connectionMsgsAreOnByDefault) {
sender.sendMessage(AnarchyExploitFixes.getLang(player.getLocale()).misc_disabledConnectionMsgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private void onPlayerJoinEvent(PlayerJoinEvent event) {

for (Player onlinePlayer : plugin.getServer().getOnlinePlayers()) {
if (
config.connectionMsgsAreOnByDefault && !plugin.CONNECTION_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())
|| !config.connectionMsgsAreOnByDefault && plugin.CONNECTION_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())
config.connectionMsgsAreOnByDefault && !AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())
|| !config.connectionMsgsAreOnByDefault && AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())
) {
sendFirstJoinMessage(onlinePlayer, joiningPlayer, joiningPlayersNumber);
}
Expand Down
Loading

0 comments on commit aae2b13

Please sign in to comment.