Skip to content

Commit

Permalink
improve other modules in misc category
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 5, 2024
1 parent 044246e commit 55f6ace
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import java.util.HashSet;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;

public class FirstJoinMessages implements AnarchyExploitFixesModule, Listener {

private final AnarchyExploitFixes plugin;
private final Config config;
private final HashMap<Locale, RuleBasedNumberFormat> formats = new HashMap<>();
private final AtomicInteger uniquePlayerCount = new AtomicInteger(0);
private final boolean logFirstJoin;
private final HashSet<Locale> availableTranslations = new HashSet<>();
private final AtomicInteger totalPlayers = new AtomicInteger(0);

public FirstJoinMessages() {
shouldEnable();
this.config = AnarchyExploitFixes.getConfiguration();
config.addComment("misc.join-leave-messages.first-join-messages.enable", "Configure message in lang folder.");
config.addComment("misc.join-leave-messages.first-join-messages.enable",
"Configure messages in lang folder.");
this.plugin = AnarchyExploitFixes.getInstance();
// prevent lag by getting total player number once and then counting up manually, as always getting all players is resource intense
plugin.getServer().getGlobalRegionScheduler().run(plugin, getTotalPlayers -> totalPlayers.set(plugin.getServer().getOfflinePlayers().length));
plugin.getServer().getGlobalRegionScheduler().run(plugin,
getCount -> uniquePlayerCount.set(plugin.getServer().getOfflinePlayers().length));
this.logFirstJoin = config.getBoolean("misc.join-leave-messages.first-join-messages.show-in-console", true);
AnarchyExploitFixes.getLanguageCacheMap().keySet().forEach(localeString -> this.availableTranslations.add(Locale.forLanguageTag(localeString.replace("_", "-"))));
AnarchyExploitFixes.getLanguageCacheMap().keySet().stream()
.map(availableLang -> Locale.forLanguageTag(availableLang.replace("_", "-")))
.forEach(locale -> formats.put(locale, new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.ORDINAL)));
}

@Override
Expand Down Expand Up @@ -65,53 +69,56 @@ public void disable() {
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerJoinEvent(PlayerJoinEvent event) {
Player joiningPlayer = event.getPlayer();
private void onPlayerJoin(PlayerJoinEvent event) {
final Player joiningPlayer = event.getPlayer();
if (joiningPlayer.hasPlayedBefore()) return;
if (joiningPlayer.hasPermission("anarchyexploitfixes.silentJoin")) return;

int joiningPlayersNumber = totalPlayers.incrementAndGet();

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

if (showConnects) {
sendFirstJoinMessage(onlinePlayer, joiningPlayer, joiningPlayersNumber);
}
final int joiningPlayersNumber = uniquePlayerCount.incrementAndGet();

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

if (logFirstJoin) {
// Send to ConsoleSender so we can display colors
for (Component line : AnarchyExploitFixes.getLang(joiningPlayer.locale()).misc_firstJoinMessage) {
plugin.getServer().getConsoleSender().sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(joiningPlayer.name()).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%players_num%").replacement(formatToOrdinal(joiningPlayer.locale(), joiningPlayersNumber)).build())
.append(Component.text(" (" + joiningPlayer.locale() + ")"))
);
plugin.getComponentLogger().info(line
.replaceText(TextReplacementConfig.builder()
.matchLiteral("%player%")
.replacement(joiningPlayer.name())
.build())
.replaceText(TextReplacementConfig.builder()
.matchLiteral("%players_num%")
.replacement(formatToOrdinal(joiningPlayer.locale(), joiningPlayersNumber))
.build())
.append(Component.text(" (" + joiningPlayer.locale() + ")")));
}
}
}

private void sendFirstJoinMessage(Player receiver, Player joiningPlayer, int joiningPlayersNumber) {
for (Component line : AnarchyExploitFixes.getLang(receiver.locale()).misc_firstJoinMessage) {
receiver.sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(joiningPlayer.name()).build())
.replaceText(TextReplacementConfig.builder().matchLiteral("%players_num%").replacement(formatToOrdinal(receiver.locale(), joiningPlayersNumber)).build())
);
}
}

private String formatToOrdinal(Locale playerLocale, int number) {
if (config.auto_lang && availableTranslations.contains(playerLocale)) {
return new RuleBasedNumberFormat(playerLocale, RuleBasedNumberFormat.ORDINAL).format(number, "%digits-ordinal");
if (config.auto_lang && formats.containsKey(playerLocale)) {
return formats.get(playerLocale).format(number, "%digits-ordinal");
} else {
return new RuleBasedNumberFormat(config.default_lang, RuleBasedNumberFormat.ORDINAL).format(number, "%digits-ordinal");
return formats.get(config.default_lang).format(number, "%digits-ordinal");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class PreventMessageKick implements AnarchyExploitFixesModule, Listener {

private final HashSet<String> kickMessagesToListenTo = new HashSet<>();
private final HashSet<String> kickMessagesToListenTo;

public PreventMessageKick() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("misc.kicks.prevent-message-kick.enable", "Cancels the kick for specific kick messages.");
config.getList("misc.kicks.prevent-message-kick.kick-messages-to-listen-to",
List.of("Kicked for spamming", "Stop spamming!")
).forEach(configuredMsg -> this.kickMessagesToListenTo.add(configuredMsg.toLowerCase()));
this.kickMessagesToListenTo = config.getList("misc.kicks.prevent-message-kick.kick-messages-to-listen-to",
List.of("Kicked for spamming", "Stop spamming!"))
.stream()
.map(String::toLowerCase)
.collect(Collectors.toCollection(HashSet::new));
}

@Override
Expand Down Expand Up @@ -52,7 +55,7 @@ public void disable() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerKick(PlayerKickEvent event) {
if (kickMessagesToListenTo.contains(PlainTextComponentSerializer.plainText().serialize(event.reason()).toLowerCase())) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import java.util.HashSet;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;

public class FirstJoinMessages implements AnarchyExploitFixesModule, Listener {

private final AnarchyExploitFixes plugin;
private final Config config;
private final HashMap<String, RuleBasedNumberFormat> formats = new HashMap<>();
private final AtomicInteger totalPlayers;
private final boolean logFirstJoin;
private final HashSet<Locale> availableTranslations = new HashSet<>();
private final AtomicInteger totalPlayers = new AtomicInteger(0);

public FirstJoinMessages() {
shouldEnable();
this.plugin = AnarchyExploitFixes.getInstance();
this.config = AnarchyExploitFixes.getConfiguration();
config.addComment("misc.join-leave-messages.first-join-messages.enable", "Configure message in lang folder.");
this.plugin = AnarchyExploitFixes.getInstance();
// prevent lag by getting total player number once and then counting up manually, as always getting all players is resource intense
plugin.getServer().getScheduler().runTask(plugin, () -> totalPlayers.set(plugin.getServer().getOfflinePlayers().length));
this.totalPlayers = new AtomicInteger(plugin.getServer().getOfflinePlayers().length);
this.logFirstJoin = config.getBoolean("misc.join-leave-messages.first-join-messages.show-in-console", true);
AnarchyExploitFixes.getLanguageCacheMap().keySet().forEach(localeString -> this.availableTranslations.add(Locale.forLanguageTag(localeString.replace("_", "-"))));
for (String availableLang : AnarchyExploitFixes.getLanguageCacheMap().keySet()) {
formats.put(availableLang, new RuleBasedNumberFormat(Locale.forLanguageTag(availableLang.replace("_", "-")), RuleBasedNumberFormat.ORDINAL));
}
}

@Override
Expand All @@ -54,49 +56,39 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("misc.join-leave-messages.first-join-messages.enable", false);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerJoinEvent(PlayerJoinEvent event) {
Player joiningPlayer = event.getPlayer();
final Player joiningPlayer = event.getPlayer();
if (joiningPlayer.hasPlayedBefore()) return;
if (joiningPlayer.hasPermission("anarchyexploitfixes.silentJoin")) return;

int joiningPlayersNumber = totalPlayers.incrementAndGet();
final int joiningPlayersNumber = totalPlayers.incrementAndGet();

for (Player onlinePlayer : plugin.getServer().getOnlinePlayers()) {
if (
config.connectionMsgsAreOnByDefault && !AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())
|| !config.connectionMsgsAreOnByDefault && AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())
) {
sendFirstJoinMessage(onlinePlayer, joiningPlayer, joiningPlayersNumber);
for (final Player onlinePlayer : plugin.getServer().getOnlinePlayers()) {
if (config.connectionMsgsAreOnByDefault != AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())) {
for (String line : AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_firstJoinMessage) {
onlinePlayer.sendMessage(line
.replace("%player%", joiningPlayer.getName())
.replace("%players_num%", formatToOrdinal(onlinePlayer.getLocale(), joiningPlayersNumber)));
}
}
}

// Use Bukkit.getLogger() here to avoid using the plugin's prefix
// Use Bukkit.getLogger() here so we can log without prefix
if (logFirstJoin) {
for (String line : AnarchyExploitFixes.getLang(joiningPlayer.getLocale()).misc_firstJoinMessage) {
Bukkit.getLogger().info(line
.replace("%player%", joiningPlayer.getName())
.replace("%players_num%", formatToOrdinal(joiningPlayer.getLocale(), joiningPlayersNumber))
);
.replace("%players_num%", formatToOrdinal(joiningPlayer.getLocale(), joiningPlayersNumber)));
}
}
}

private void sendFirstJoinMessage(Player receiver, Player joiningPlayer, int joiningPlayersNumber) {
for (String line : AnarchyExploitFixes.getLang(receiver.getLocale()).misc_firstJoinMessage) {
receiver.sendMessage(line
.replace("%player%", joiningPlayer.getName())
.replace("%players_num%", formatToOrdinal(receiver.getLocale(), joiningPlayersNumber))
);
}
}

private String formatToOrdinal(String playerLanguage, int number) {
Locale playerLocale = Locale.forLanguageTag(playerLanguage.replace("-", "_"));
if (config.auto_lang && availableTranslations.contains(playerLocale)) {
return new RuleBasedNumberFormat(playerLocale, RuleBasedNumberFormat.ORDINAL).format(number, "%digits-ordinal");
private String formatToOrdinal(String playerLocale, int number) {
if (config.auto_lang && formats.containsKey(playerLocale)) {
return formats.get(playerLocale).format(number, "%digits-ordinal");
} else {
return new RuleBasedNumberFormat(config.default_lang, RuleBasedNumberFormat.ORDINAL).format(number, "%digits-ordinal");
return formats.get(config.default_lang.toString().toLowerCase()).format(number, "%digits-ordinal");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.Collectors;

public class PreventMessageKick implements AnarchyExploitFixesModule, Listener {

private final HashSet<String> kickMessagesToListenTo = new HashSet<>();
private final HashSet<String> kickMessagesToListenTo;

public PreventMessageKick() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("misc.kicks.prevent-message-kick.enable", "Cancels the kick for specific kick messages.");
config.getList("misc.kicks.prevent-message-kick.kick-messages-to-listen-to", Arrays.asList("Kicked for spamming", "Stop spamming!")).forEach(configuredMsg ->
this.kickMessagesToListenTo.add(configuredMsg.toLowerCase())
);
this.kickMessagesToListenTo = config.getList("misc.kicks.prevent-message-kick.kick-messages-to-listen-to",
Arrays.asList("Kicked for spamming", "Stop spamming!"))
.stream()
.map(String::toLowerCase)
.collect(Collectors.toCollection(HashSet::new));
}

@Override
Expand All @@ -45,7 +48,7 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("misc.kicks.prevent-message-kick.enable", false);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerKick(PlayerKickEvent event) {
if (kickMessagesToListenTo.contains(event.getReason().toLowerCase())) {
event.setCancelled(true);
Expand Down

0 comments on commit 55f6ace

Please sign in to comment.