make vault optional and automatically enable if a server is using vault

This commit is contained in:
Taah 2022-05-10 22:48:47 -07:00
parent bb8c34e0cd
commit b78f71c238
12 changed files with 151 additions and 47 deletions

View File

@ -0,0 +1,32 @@
package dev.plex.api.permission;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface IPermissionHandler
{
default boolean hasPermission(@NotNull Player player, @Nullable String permission)
{
if (permission == null)
{
return true;
}
return player.hasPermission(permission);
}
default boolean hasPermission(@NotNull OfflinePlayer player, @Nullable String permission)
{
if (permission == null)
{
return true;
}
if (player.isOnline() && Bukkit.getPlayer(player.getUniqueId()) != null)
{
return Bukkit.getPlayer(player.getUniqueId()).hasPermission(permission);
}
throw new UnsupportedOperationException("Not implemented yet");
}
}

View File

@ -1,6 +1,7 @@
package dev.plex.api.plugin; package dev.plex.api.plugin;
import dev.plex.api.chat.IChatHandler; import dev.plex.api.chat.IChatHandler;
import dev.plex.api.permission.IPermissionHandler;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -14,6 +15,7 @@ public abstract class PlexPlugin extends JavaPlugin
private static PlexPlugin plugin; private static PlexPlugin plugin;
private IChatHandler chatHandler; private IChatHandler chatHandler;
private IPermissionHandler permissionHandler;
@Override @Override
public void onLoad() public void onLoad()

View File

@ -2,14 +2,18 @@ package dev.plex;
import dev.plex.admin.Admin; import dev.plex.admin.Admin;
import dev.plex.admin.AdminList; import dev.plex.admin.AdminList;
import dev.plex.api.permission.IPermissionHandler;
import dev.plex.api.plugin.PlexPlugin; import dev.plex.api.plugin.PlexPlugin;
import dev.plex.cache.DataUtils; import dev.plex.cache.DataUtils;
import dev.plex.cache.PlayerCache; import dev.plex.cache.PlayerCache;
import dev.plex.config.Config; import dev.plex.config.Config;
import dev.plex.handlers.CommandHandler; import dev.plex.handlers.CommandHandler;
import dev.plex.handlers.ListenerHandler; import dev.plex.handlers.ListenerHandler;
import dev.plex.hook.VaultHook;
import dev.plex.listener.impl.ChatListener; import dev.plex.listener.impl.ChatListener;
import dev.plex.module.ModuleManager; import dev.plex.module.ModuleManager;
import dev.plex.permission.handler.NativePermissionHandler;
import dev.plex.permission.handler.VaultPermissionHandler;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.punishment.PunishmentManager; import dev.plex.punishment.PunishmentManager;
import dev.plex.rank.RankManager; import dev.plex.rank.RankManager;
@ -74,8 +78,6 @@ public class Plex extends PlexPlugin
private UpdateChecker updateChecker; private UpdateChecker updateChecker;
private String system; private String system;
public Permission permissions;
public Chat chat;
public static Plex get() public static Plex get()
{ {
@ -134,13 +136,22 @@ public class Plex extends PlexPlugin
e.printStackTrace(); e.printStackTrace();
} }
if (system.equalsIgnoreCase("permissions") && !getServer().getPluginManager().isPluginEnabled("Vault")) boolean permissions = false;
if (getServer().getPluginManager().isPluginEnabled("Vault"))
{ {
throw new RuntimeException("Vault is required to run on the server if you use permissions!"); VaultPermissionHandler handler = new VaultPermissionHandler();
if (VaultHook.getPermission() != null)
{
this.setPermissionHandler(handler);
permissions = true;
PlexLog.debug("Enabling Vault support for permissions with a permission plugin: " + VaultHook.getPermission().getName());
}
} }
permissions = setupPermissions(); if (!permissions)
chat = setupChat(); {
this.setPermissionHandler(new NativePermissionHandler());
}
updateChecker = new UpdateChecker(); updateChecker = new UpdateChecker();
PlexLog.log("Update checking enabled"); PlexLog.log("Update checking enabled");
@ -261,28 +272,4 @@ public class Plex extends PlexPlugin
} }
}); });
} }
public Permission setupPermissions()
{
RegisteredServiceProvider<Permission> rsp = Bukkit.getServicesManager().getRegistration(Permission.class);
permissions = rsp.getProvider();
return permissions;
}
public Chat setupChat()
{
RegisteredServiceProvider<Chat> rsp = Bukkit.getServicesManager().getRegistration(Chat.class);
chat = rsp.getProvider();
return chat;
}
public Permission getVaultPermissions()
{
return permissions;
}
public Chat getVaultChat()
{
return chat;
}
} }

View File

@ -151,7 +151,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
if (!perms.permission().isEmpty() && !player.hasPermission(perms.permission())) if (!perms.permission().isEmpty() && !plugin.getPermissionHandler().hasPermission(player, perms.permission()))
{ {
send(sender, messageComponent("noPermissionNode", perms.permission())); send(sender, messageComponent("noPermissionNode", perms.permission()));
return true; return true;
@ -187,7 +187,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
if (!perms.permission().isEmpty() && !plugin.getPermissions().playerHas(null, Bukkit.getOfflinePlayer(plexPlayer.getUuid()), perms.permission())) if (!perms.permission().isEmpty() && !plugin.getPermissionHandler().hasPermission(Bukkit.getOfflinePlayer(plexPlayer.getName()), perms.permission()))
{ {
send(sender, messageComponent("noPermissionNode", perms.permission())); send(sender, messageComponent("noPermissionNode", perms.permission()));
return true; return true;
@ -306,7 +306,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
if (!perms.permission().isEmpty() && !plugin.getPermissions().playerHas(null, Bukkit.getOfflinePlayer(plexPlayer.getUuid()), permission)) if (!perms.permission().isEmpty() && !plugin.getPermissionHandler().hasPermission(Bukkit.getOfflinePlayer(plexPlayer.getName()), perms.permission()))
{ {
throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission)); throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission));
} }
@ -344,7 +344,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
if (!perms.permission().isEmpty() && !player.hasPermission(permission)) if (!perms.permission().isEmpty() && !plugin.getPermissionHandler().hasPermission(player, permission))
{ {
throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission)); throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission));
} }
@ -365,7 +365,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
return !perms.permission().isEmpty() && player.hasPermission(permission); return !perms.permission().isEmpty() && plugin.getPermissionHandler().hasPermission(player, permission);
} }
return false; return false;
} }
@ -406,7 +406,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
return !perms.permission().isEmpty() && player.hasPermission(permission); return !perms.permission().isEmpty() && plugin.getPermissionHandler().hasPermission(player, permission);
} }
return true; return true;
} }

View File

@ -46,7 +46,7 @@ public class AdminChatCMD extends PlexCommand
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
if (player.hasPermission("plex.adminchat")) if (plugin.getPermissionHandler().hasPermission(player, "plex.adminchat"))
{ {
player.sendMessage(PlexUtils.messageComponent("adminChatFormat", sender.getName(), message)); player.sendMessage(PlexUtils.messageComponent("adminChatFormat", sender.getName(), message));
} }

View File

@ -0,0 +1,43 @@
package dev.plex.hook;
import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
public class VaultHook
{
private static Chat CHAT;
private static Permission PERMISSIONS;
static {
CHAT = setupChat();
PERMISSIONS = setupPermissions();
}
private static Chat setupChat()
{
if (!Bukkit.getPluginManager().isPluginEnabled("Vault")) return null;
RegisteredServiceProvider<Chat> rsp = Bukkit.getServicesManager().getRegistration(Chat.class);
CHAT = rsp.getProvider();
return CHAT;
}
private static Permission setupPermissions()
{
if (!Bukkit.getPluginManager().isPluginEnabled("Vault")) return null;
RegisteredServiceProvider<Permission> rsp = Bukkit.getServicesManager().getRegistration(Permission.class);
PERMISSIONS = rsp.getProvider();
return PERMISSIONS;
}
public static Permission getPermission()
{
return PERMISSIONS;
}
public static Chat getChat()
{
return CHAT;
}
}

View File

@ -17,10 +17,13 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags; import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
@Toggleable("chat.enabled") @Toggleable("chat.enabled")
public class ChatListener extends PlexListener public class ChatListener extends PlexListener
{ {
@ -61,6 +64,7 @@ public class ChatListener extends PlexListener
{ {
public boolean hasPrefix; public boolean hasPrefix;
public Component prefix; public Component prefix;
public Supplier<Component> before = null;
@Override @Override
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer) public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer)
@ -69,12 +73,15 @@ public class ChatListener extends PlexListener
Component component = Component.empty(); Component component = Component.empty();
if (before != null)
{
component = component.append(before.get());
}
if (hasPrefix) if (hasPrefix)
{ {
component = component.append(prefix); component = component.append(prefix).append(Component.space());
} }
return component.append(PlexUtils.mmDeserialize(plugin.config.getString("chat.name-color", "<white>") + MiniMessage.builder().tags(TagResolver.resolver(StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition())).build().serialize(sourceDisplayName))).append(Component.space()).append(Component.text("»").color(NamedTextColor.GRAY)).append(Component.space()).append(SafeMiniMessage.mmDeserializeWithoutEvents(text)).replaceText(URL_REPLACEMENT_CONFIG);
return component.append(Component.space()).append(PlexUtils.mmDeserialize(plugin.config.getString("chat.name-color", "<white>") + MiniMessage.builder().tags(TagResolver.resolver(StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition())).build().serialize(sourceDisplayName))).append(Component.space()).append(Component.text("»").color(NamedTextColor.GRAY)).append(Component.space()).append(SafeMiniMessage.mmDeserializeWithoutEvents(text)).replaceText(URL_REPLACEMENT_CONFIG);
} }
} }
} }

View File

@ -105,7 +105,7 @@ public class WorldListener extends PlexListener
{ {
e.setCancelled(true); e.setCancelled(true);
} }
else if (plugin.getSystem().equals("permissions") && !e.getPlayer().hasPermission("plex.adminworld.enter")) else if (plugin.getSystem().equals("permissions") && !plugin.getPermissionHandler().hasPermission(e.getPlayer(), "plex.adminworld.enter"))
{ {
e.setCancelled(true); e.setCancelled(true);
} }
@ -164,7 +164,7 @@ public class WorldListener extends PlexListener
{ {
return true; return true;
} }
if (player.hasPermission(permission)) if (plugin.getPermissionHandler().hasPermission(player, permission))
{ {
return true; return true;
} }

View File

@ -0,0 +1,7 @@
package dev.plex.permission.handler;
import dev.plex.api.permission.IPermissionHandler;
public class NativePermissionHandler implements IPermissionHandler
{
}

View File

@ -0,0 +1,25 @@
package dev.plex.permission.handler;
import dev.plex.api.permission.IPermissionHandler;
import dev.plex.hook.VaultHook;
import lombok.Getter;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@Getter
public class VaultPermissionHandler implements IPermissionHandler
{
@Override
public boolean hasPermission(@NotNull OfflinePlayer player, @Nullable String permission)
{
if (!Bukkit.getPluginManager().isPluginEnabled("Vault"))
{
return IPermissionHandler.super.hasPermission(player, permission);
}
return VaultHook.getPermission().playerHas(null, player, permission);
}
}

View File

@ -1,6 +1,7 @@
package dev.plex.rank; package dev.plex.rank;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.hook.VaultHook;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.rank.enums.Title; import dev.plex.rank.enums.Title;
@ -96,7 +97,7 @@ public class RankManager
public Component getPrefix(PlexPlayer player) public Component getPrefix(PlexPlayer player)
{ {
if (!player.getPrefix().equals("")) if (player.getPrefix() != null && !player.getPrefix().isEmpty())
{ {
return SafeMiniMessage.mmDeserializeWithoutEvents(player.getPrefix()); return SafeMiniMessage.mmDeserializeWithoutEvents(player.getPrefix());
} }
@ -116,11 +117,11 @@ public class RankManager
{ {
return player.getRankFromString().getPrefix(); return player.getRankFromString().getPrefix();
} }
if (Plex.get().getSystem().equalsIgnoreCase("permissions")) if (Bukkit.getServer().getPluginManager().isPluginEnabled("Vault") && Plex.get().getSystem().equalsIgnoreCase("permissions"))
{ {
Player bukkitPlayer = Bukkit.getPlayer(player.getUuid()); Player bukkitPlayer = Bukkit.getPlayer(player.getUuid());
String group = Plex.get().getVaultPermissions().getPrimaryGroup(bukkitPlayer); String group = VaultHook.getPermission().getPrimaryGroup(bukkitPlayer);
String vaultPrefix = Plex.get().getVaultChat().getGroupPrefix(bukkitPlayer.getWorld(), group); String vaultPrefix = VaultHook.getChat().getGroupPrefix(bukkitPlayer.getWorld(), group);
return LegacyComponentSerializer.legacyAmpersand().deserialize(vaultPrefix); return LegacyComponentSerializer.legacyAmpersand().deserialize(vaultPrefix);
} }
return null; return null;

View File

@ -31,7 +31,7 @@ public class PlexUtils implements PlexBase
public static List<String> DEVELOPERS = public static List<String> DEVELOPERS =
Arrays.asList("78408086-1991-4c33-a571-d8fa325465b2", // Telesphoreo Arrays.asList("78408086-1991-4c33-a571-d8fa325465b2", // Telesphoreo
"f5cd54c4-3a24-4213-9a56-c06c49594dff", // Taahh /*"f5cd54c4-3a24-4213-9a56-c06c49594dff",*/ // Taahh
"53b1512e-3481-4702-9f4f-63cb9c8be6a1", // supernt "53b1512e-3481-4702-9f4f-63cb9c8be6a1", // supernt
"ca83b658-c03b-4106-9edc-72f70a80656d", // ayunami2000 "ca83b658-c03b-4106-9edc-72f70a80656d", // ayunami2000
"2e06e049-24c8-42e4-8bcf-d35372af31e6", // Fleek "2e06e049-24c8-42e4-8bcf-d35372af31e6", // Fleek