Skip to content

Commit

Permalink
Merge pull request #6155 from TownyAdvanced/feature/vault-chat-fallback
Browse files Browse the repository at this point in the history
Fallback to another Vault chat implementation if an invalid one is detected
  • Loading branch information
LlmDl authored Sep 5, 2022
2 parents be1e2bf + 3a21fc0 commit afa0968
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
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

0 comments on commit afa0968

Please sign in to comment.