Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to another Vault chat implementation if an invalid one is detected #6155

Merged
merged 1 commit into from
Sep 5, 2022
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
59 changes: 51 additions & 8 deletions src/com/palmergames/bukkit/towny/Towny.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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<Chat> chatProvider = findChatImplementation();
RegisteredServiceProvider<Permission> 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<Permission> 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());
}
}

Expand All @@ -626,6 +636,39 @@ private String returnPermissionsProviders() {
return output;
}

@Nullable
private RegisteredServiceProvider<Chat> findChatImplementation() {
Iterator<RegisteredServiceProvider<Chat>> iterator = Bukkit.getServicesManager().getRegistrations(Chat.class).iterator();

while (iterator.hasNext()) {
RegisteredServiceProvider<Chat> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 : "";
}

Expand Down