diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 8731693b..f2bff2dd 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -74,9 +74,9 @@ public class AuthMe extends JavaPlugin { private static final int CLEANUP_INTERVAL = 5 * TICKS_PER_MINUTE; // Version and build number values - private static String pluginVersion = "5.6.0-Fork"; + private static String pluginVersion = "5.7.0-Fork"; private static final String pluginBuild = "b"; - private static String pluginBuildNumber = "50"; + private static String pluginBuildNumber = "51"; // Private instances private EmailService emailService; private CommandHandler commandHandler; diff --git a/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java b/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java index fd487d66..27743d5d 100644 --- a/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java +++ b/src/main/java/fr/xephi/authme/api/v3/AuthMeApi.java @@ -15,6 +15,8 @@ import fr.xephi.authme.util.PlayerUtils; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; import javax.inject.Inject; import java.time.Instant; @@ -24,6 +26,8 @@ import java.util.List; import java.util.Locale; import java.util.Optional; +import static fr.xephi.authme.listener.PlayerListener.PENDING_INVENTORIES; + /** * The current API of AuthMe. * @@ -32,6 +36,7 @@ import java.util.Optional; * AuthMeApi authmeApi = AuthMeApi.getInstance(); * */ +@SuppressWarnings("unused") public class AuthMeApi { private static AuthMeApi singleton; @@ -256,6 +261,18 @@ public class AuthMeApi { return dataSource.saveAuth(auth); } + /** + * Open an inventory for the given player at any time. + * + * @param player The player to open the inventory for + * @param inventory The inventory to open + * @return The inventory view + */ + public InventoryView openInventory(Player player, Inventory inventory) { + PENDING_INVENTORIES.add(inventory); + return player.openInventory(inventory); + } + /** * Force a player to login, i.e. the player is logged in without needing his password. * diff --git a/src/main/java/fr/xephi/authme/listener/PlayerListener.java b/src/main/java/fr/xephi/authme/listener/PlayerListener.java index e435b9c2..b24da9da 100644 --- a/src/main/java/fr/xephi/authme/listener/PlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/PlayerListener.java @@ -16,9 +16,11 @@ import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.HooksSettings; +import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.util.TeleportUtils; +import fr.xephi.authme.util.message.I18NUtils; import fr.xephi.authme.util.message.MiniMessageUtils; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -50,9 +52,12 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerShearEntityEvent; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryView; import javax.inject.Inject; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.Set; @@ -93,6 +98,7 @@ public class PlayerListener implements Listener { @Inject private QuickCommandsProtectionManager quickCommandsProtectionManager; + public static List PENDING_INVENTORIES = new ArrayList<>(); // Lowest priority to apply fast protection checks @EventHandler(priority = EventPriority.LOWEST) @@ -248,6 +254,11 @@ public class PlayerListener implements Listener { } } + // Remove data from locale map when player quit + if (settings.getProperty(PluginSettings.I18N_MESSAGES)) { + I18NUtils.removeLocale(player.getUniqueId()); + } + if (antiBotService.wasPlayerKicked(player.getName())) { return; } @@ -489,6 +500,17 @@ public class PlayerListener implements Listener { } } + private boolean isInventoryOpenedByApi(Inventory inventory) { + if (inventory == null) { + return false; + } + if (PENDING_INVENTORIES.contains(inventory)) { + PENDING_INVENTORIES.remove(inventory); + return true; + } else { + return false; + } + } @SuppressWarnings("all") private boolean isInventoryWhitelisted(InventoryView inventory) { if (inventory == null) { @@ -515,7 +537,8 @@ public class PlayerListener implements Listener { public void onPlayerInventoryOpen(InventoryOpenEvent event) { final HumanEntity player = event.getPlayer(); if (listenerService.shouldCancelEvent(player) - && !isInventoryWhitelisted(event.getView())) { + && !isInventoryWhitelisted(event.getView()) + && !isInventoryOpenedByApi(event.getInventory())) { event.setCancelled(true); /* diff --git a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java index dbfd9df0..424c4989 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java @@ -29,8 +29,6 @@ import fr.xephi.authme.util.PlayerUtils; import org.bukkit.GameMode; import org.bukkit.Server; import org.bukkit.entity.Player; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; import javax.inject.Inject; import java.util.Locale; @@ -208,7 +206,7 @@ public class AsynchronousJoin implements AsynchronousProcess { int blindTimeOut = (registrationTimeout <= 0) ? 99999 : registrationTimeout; // AuthMeReReloaded - Fix potion apply on Folia - bukkitService.runTaskIfFolia(player,() -> player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, blindTimeOut, 2))); + bukkitService.runTaskIfFolia(player, () -> player.addPotionEffect(bukkitService.createBlindnessEffect(blindTimeOut))); } commandManager.runCommandsOnJoin(player); }); diff --git a/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java b/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java index 6aeed332..96865e21 100644 --- a/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java +++ b/src/main/java/fr/xephi/authme/process/logout/ProcessSyncPlayerLogout.java @@ -14,8 +14,6 @@ import fr.xephi.authme.settings.commandconfig.CommandManager; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import org.bukkit.entity.Player; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; import javax.inject.Inject; @@ -75,7 +73,7 @@ public class ProcessSyncPlayerLogout implements SynchronousProcess { // Apply Blindness effect if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) { int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND; - player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2)); + player.addPotionEffect(bukkitService.createBlindnessEffect(timeout)); } // Set player's data to unauthenticated diff --git a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java index b69b9930..caecd295 100644 --- a/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java +++ b/src/main/java/fr/xephi/authme/process/unregister/AsynchronousUnregister.java @@ -23,8 +23,6 @@ import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; import javax.inject.Inject; @@ -150,7 +148,7 @@ public class AsynchronousUnregister implements AsynchronousProcess { private void applyBlindEffect(Player player) { if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) { int timeout = service.getProperty(RestrictionSettings.TIMEOUT) * TICKS_PER_SECOND; - player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, timeout, 2)); + bukkitService.runTaskIfFolia(player, () -> player.addPotionEffect(bukkitService.createBlindnessEffect(timeout))); } } diff --git a/src/main/java/fr/xephi/authme/service/BukkitService.java b/src/main/java/fr/xephi/authme/service/BukkitService.java index 45008a38..8a899795 100644 --- a/src/main/java/fr/xephi/authme/service/BukkitService.java +++ b/src/main/java/fr/xephi/authme/service/BukkitService.java @@ -18,6 +18,8 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.Event; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; import javax.inject.Inject; import java.util.Collection; @@ -327,6 +329,16 @@ public class BukkitService implements SettingsDependent { return event; } + /** + * Creates a PotionEffect with blindness for the given duration in ticks. + * + * @param timeoutInTicks duration of the effect in ticks + * @return blindness potion effect + */ + public PotionEffect createBlindnessEffect(int timeoutInTicks) { + return new PotionEffect(PotionEffectType.BLINDNESS, timeoutInTicks, 2); + } + /** * Gets the world with the given name. * @@ -380,7 +392,11 @@ public class BukkitService implements SettingsDependent { * @param bytes the message */ public void sendVelocityMessage(Player player, byte[] bytes) { - player.sendPluginMessage(authMe, "authmevelocity:main", bytes); + if (player != null) { + player.sendPluginMessage(authMe, "authmevelocity:main", bytes); + } else { + Bukkit.getServer().sendPluginMessage(authMe, "authmevelocity:main", bytes); + } } diff --git a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandManager.java b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandManager.java index 1018da0a..86f06aa8 100644 --- a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandManager.java +++ b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandManager.java @@ -129,9 +129,9 @@ public class CommandManager implements Reloadable { if (predicate.test(cmd)) { long delay = cmd.getDelay(); if (delay > 0) { - bukkitService.scheduleSyncDelayedTask(() -> dispatchCommand(player, cmd), delay); + bukkitService.runTaskLater(player, () -> dispatchCommand(player, cmd), delay); } else { - dispatchCommand(player, cmd); + bukkitService.runTaskIfFolia(player, () -> dispatchCommand(player, cmd)); } } } diff --git a/src/main/java/fr/xephi/authme/util/message/I18NUtils.java b/src/main/java/fr/xephi/authme/util/message/I18NUtils.java index ff68b5ab..da1d6769 100644 --- a/src/main/java/fr/xephi/authme/util/message/I18NUtils.java +++ b/src/main/java/fr/xephi/authme/util/message/I18NUtils.java @@ -60,6 +60,10 @@ public class I18NUtils { PLAYER_LOCALE.put(uuid, locale); } + public static void removeLocale(UUID uuid) { + PLAYER_LOCALE.remove(uuid); + } + /** * Returns the AuthMe messages file language code, by given locale and settings. * Dreeam - Hard mapping, based on mc1.20.6, 5/29/2024 diff --git a/src/main/java/fr/xephi/authme/util/message/MiniMessageUtils.java b/src/main/java/fr/xephi/authme/util/message/MiniMessageUtils.java index 5d7e6216..36756c62 100644 --- a/src/main/java/fr/xephi/authme/util/message/MiniMessageUtils.java +++ b/src/main/java/fr/xephi/authme/util/message/MiniMessageUtils.java @@ -2,9 +2,7 @@ package fr.xephi.authme.util.message; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; -import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import net.md_5.bungee.api.chat.BaseComponent; public class MiniMessageUtils { private static final MiniMessage miniMessage = MiniMessage.miniMessage(); @@ -19,18 +17,6 @@ public class MiniMessageUtils { Component component = miniMessage.deserialize(message); return LegacyComponentSerializer.legacyAmpersand().serialize(component); } - - /** - * Parse a MiniMessage string into a BaseComponent. - * - * @param message The message to parse. - * @return The parsed message. - */ - public static BaseComponent[] parseMiniMessageToBaseComponent(String message) { - Component component = miniMessage.deserialize(message); - return BungeeComponentSerializer.legacy().serialize(component); - } - private MiniMessageUtils() { } }