From 3a21fc01db71f99fa652a9a4e8502bb20e56cfe8 Mon Sep 17 00:00:00 2001 From: Warrior <50800980+Warriorrrr@users.noreply.github.com> Date: Mon, 5 Sep 2022 17:14:10 +0200 Subject: [PATCH] Fallback to another Vault chat if an invalid one is detected --- src/com/palmergames/bukkit/towny/Towny.java | 59 ++++++++++++++++--- .../towny/permissions/VaultPermSource.java | 13 ++-- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/com/palmergames/bukkit/towny/Towny.java b/src/com/palmergames/bukkit/towny/Towny.java index 73e612d8d0..dcd31c9a1e 100644 --- a/src/com/palmergames/bukkit/towny/Towny.java +++ b/src/com/palmergames/bukkit/towny/Towny.java @@ -60,6 +60,7 @@ import com.palmergames.util.StringMgmt; import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.permission.Permission; import org.bstats.bukkit.Metrics; @@ -76,6 +77,7 @@ import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; @@ -85,6 +87,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; @@ -567,7 +570,10 @@ private void checkPlugins() { * Output discovered plugins and warnings. */ plugin.getLogger().info("Plugins found: "); - plugin.getLogger().info(permissions); + + for (String string : permissions.split("\n")) + plugin.getLogger().info(string); + if (!economy.isEmpty()) plugin.getLogger().info(economy); if (!addons.isEmpty()) @@ -602,19 +608,23 @@ private String returnPermissionsProviders() { // Try Vault test = getServer().getPluginManager().getPlugin("Vault"); if (test != null) { - net.milkbowl.vault.chat.Chat chat = getServer().getServicesManager().load(net.milkbowl.vault.chat.Chat.class); - if (chat == null) { + RegisteredServiceProvider chatProvider = findChatImplementation(); + RegisteredServiceProvider permissionProvider = getServer().getServicesManager().getRegistration(Permission.class); + + if (chatProvider == null) { // No Chat implementation test = null; // Fall back to BukkitPermissions below } else { - TownyUniverse.getInstance().setPermissionSource(new VaultPermSource(this, chat)); - RegisteredServiceProvider vaultPermProvider = plugin.getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class); - if (vaultPermProvider != null) { - output += vaultPermProvider.getPlugin().getName() + " " + vaultPermProvider.getPlugin().getDescription().getVersion() + " via Vault"; + TownyUniverse.getInstance().setPermissionSource(new VaultPermSource(this, chatProvider.getProvider())); + + if (permissionProvider != null) { + output += permissionProvider.getPlugin().getName() + " v" + permissionProvider.getPlugin().getDescription().getVersion() + " via Vault"; } else { - output += String.format("%s v%s", "Vault", test.getDescription().getVersion()); + output += String.format("Vault v%s", test.getDescription().getVersion()); } + + output += String.format("\n Chat: %s v%s via Vault", chatProvider.getPlugin().getName(), chatProvider.getPlugin().getDescription().getVersion()); } } @@ -626,6 +636,39 @@ private String returnPermissionsProviders() { return output; } + @Nullable + private RegisteredServiceProvider findChatImplementation() { + Iterator> iterator = Bukkit.getServicesManager().getRegistrations(Chat.class).iterator(); + + while (iterator.hasNext()) { + RegisteredServiceProvider chatProvider = iterator.next(); + + if (chatProvider == null) + continue; + + try { + // If the 'perms' field in the chat implementation is null, log some warning messages. + // The perms field being null causes issues with plot claiming, and is caused by a faulty chat implementation. + Field field = Chat.class.getDeclaredField("perms"); + field.setAccessible(true); + + if (field.get(chatProvider.getProvider()) == null) { + getLogger().warning(String.format("WARNING: Plugin %s v%s has an improper Chat implementation, please inform the authors about the following:", chatProvider.getPlugin().getName(), chatProvider.getPlugin().getDescription().getVersion())); + getLogger().warning(String.format("Class '%s' has a null Permission field, which is not supported.", chatProvider.getProvider().getClass().getName())); + + if (!iterator.hasNext()) + return chatProvider; + else + continue; + } + } catch (Exception ignored) {} + + return chatProvider; + } + + return null; + } + private void cycleTimers() { toggleTimersOff(); diff --git a/src/com/palmergames/bukkit/towny/permissions/VaultPermSource.java b/src/com/palmergames/bukkit/towny/permissions/VaultPermSource.java index 6e05f54933..d6409af917 100644 --- a/src/com/palmergames/bukkit/towny/permissions/VaultPermSource.java +++ b/src/com/palmergames/bukkit/towny/permissions/VaultPermSource.java @@ -8,7 +8,6 @@ import net.milkbowl.vault.chat.Chat; import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; public class VaultPermSource extends TownyPermissionSource { @@ -102,13 +101,10 @@ public int getPlayerPermissionIntNode(String playerName, String node) { int iReturn = -1; - OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(BukkitTools.getPlayerExact(playerName).getUniqueId()); - Player player = BukkitTools.getPlayer(playerName); + Player player = Bukkit.getPlayerExact(playerName); - if (player != null) { - - iReturn = chat.getPlayerInfoInteger(player.getWorld().getName(), offlinePlayer, node, -1); - } + if (player != null) + iReturn = chat.getPlayerInfoInteger(player.getWorld().getName(), player, node, -1); if (iReturn == -1) @@ -119,8 +115,7 @@ public int getPlayerPermissionIntNode(String playerName, String node) { @Override public String getPlayerGroup(Player player) { - OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUniqueId()); - String result = chat.getPrimaryGroup(player.getWorld().getName(), offlinePlayer); + String result = chat.getPrimaryGroup(player.getWorld().getName(), player); return result != null ? result : ""; }