Merge pull request #36 from oskar3123/33-refactor-permissions-to-use-a-common-definition

Refactor permissions to common defintion
This commit is contained in:
2025-02-08 03:20:01 +01:00
committed by GitHub
10 changed files with 56 additions and 31 deletions
@@ -16,10 +16,6 @@ public class BungeeMain extends Plugin {
private static final int BSTATS_PLUGIN_ID = 836; private static final int BSTATS_PLUGIN_ID = 836;
public final String usePerm = "staffchat.use";
public final String seePerm = "staffchat.see";
public final String commandPerm = "staffchat.command";
public final String reloadPerm = "staffchat.reload";
public final BungeeChatListener chatListener = new BungeeChatListener(this); public final BungeeChatListener chatListener = new BungeeChatListener(this);
private Configuration config; private Configuration config;
@@ -1,6 +1,7 @@
package me.oskar3123.staffchat.bungee.command; package me.oskar3123.staffchat.bungee.command;
import me.oskar3123.staffchat.bungee.BungeeMain; import me.oskar3123.staffchat.bungee.BungeeMain;
import me.oskar3123.staffchat.util.Permissions;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
@@ -25,7 +26,7 @@ public class BungeeStaffChatCommand extends Command {
public void execute(@NotNull CommandSender sender, @NotNull String[] args) { public void execute(@NotNull CommandSender sender, @NotNull String[] args) {
BaseComponent[] noPerm = BaseComponent[] noPerm =
txt(config.getString("messages.prefix") + config.getString("messages.nopermission")); txt(config.getString("messages.prefix") + config.getString("messages.nopermission"));
if (!sender.hasPermission(plugin.commandPerm)) { if (!sender.hasPermission(Permissions.COMMAND.permission())) {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
return; return;
} }
@@ -34,14 +35,14 @@ public class BungeeStaffChatCommand extends Command {
return; return;
} }
if (args[0].equalsIgnoreCase("reload")) { if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission(plugin.reloadPerm)) { if (sender.hasPermission(Permissions.RELOAD.permission())) {
reload(sender); reload(sender);
} else { } else {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
} }
return; return;
} else if (args[0].equalsIgnoreCase("toggle")) { } else if (args[0].equalsIgnoreCase("toggle")) {
if (sender.hasPermission(plugin.usePerm)) { if (sender.hasPermission(Permissions.USE.permission())) {
toggle(sender); toggle(sender);
} else { } else {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
@@ -60,11 +61,11 @@ public class BungeeStaffChatCommand extends Command {
String prefix = config.getString("messages.prefix"); String prefix = config.getString("messages.prefix");
sender.sendMessage( sender.sendMessage(
txt(prefix + "Version " + plugin.getDescription().getVersion() + ", made by oskar3123")); txt(prefix + "Version " + plugin.getDescription().getVersion() + ", made by oskar3123"));
if (sender.hasPermission(plugin.usePerm)) { if (sender.hasPermission(Permissions.USE.permission())) {
sender.sendMessage(txt(prefix + "Message prefix: " + config.getString("settings.character"))); sender.sendMessage(txt(prefix + "Message prefix: " + config.getString("settings.character")));
sender.sendMessage(txt(prefix + "/" + label + " toggle - Toggles auto staffchat")); sender.sendMessage(txt(prefix + "/" + label + " toggle - Toggles auto staffchat"));
} }
if (sender.hasPermission(plugin.reloadPerm)) { if (sender.hasPermission(Permissions.RELOAD.permission())) {
sender.sendMessage(txt(prefix + "/" + label + " reload - Reloads the config file")); sender.sendMessage(txt(prefix + "/" + label + " reload - Reloads the config file"));
} }
} }
@@ -8,6 +8,7 @@ import java.util.UUID;
import me.oskar3123.staffchat.bungee.BungeeMain; import me.oskar3123.staffchat.bungee.BungeeMain;
import me.oskar3123.staffchat.bungee.event.BungeeStaffChatEvent; import me.oskar3123.staffchat.bungee.event.BungeeStaffChatEvent;
import me.oskar3123.staffchat.bungee.util.BungeeFormatUtils; import me.oskar3123.staffchat.bungee.util.BungeeFormatUtils;
import me.oskar3123.staffchat.util.Permissions;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
@@ -40,7 +41,7 @@ public class BungeeChatListener implements Listener {
} }
ProxiedPlayer player = (ProxiedPlayer) event.getSender(); ProxiedPlayer player = (ProxiedPlayer) event.getSender();
if (!player.hasPermission(plugin.usePerm)) { if (!player.hasPermission(Permissions.USE.permission())) {
return; return;
} }
@@ -76,7 +77,7 @@ public class BungeeChatListener implements Listener {
final BaseComponent[] messageComponents = txt(appliedFormat); final BaseComponent[] messageComponents = txt(appliedFormat);
plugin.getProxy().getPlayers().stream() plugin.getProxy().getPlayers().stream()
.filter(p -> p.hasPermission(plugin.seePerm)) .filter(p -> p.hasPermission(Permissions.SEE.permission()))
.forEach(p -> p.sendMessage(messageComponents)); .forEach(p -> p.sendMessage(messageComponents));
plugin.getLogger().info(ChatColor.stripColor(appliedFormat)); plugin.getLogger().info(ChatColor.stripColor(appliedFormat));
@@ -18,10 +18,6 @@ public class Main extends JavaPlugin {
private static final int BSTATS_PLUGIN_ID = 835; private static final int BSTATS_PLUGIN_ID = 835;
public final String usePerm = "staffchat.use";
public final String seePerm = "staffchat.see";
public final String commandPerm = "staffchat.command";
public final String reloadPerm = "staffchat.reload";
public final StaffChatHandler staffChatHandler = new StaffChatHandler(this); public final StaffChatHandler staffChatHandler = new StaffChatHandler(this);
public final ChatListener chatListener = new ChatListener(staffChatHandler); public final ChatListener chatListener = new ChatListener(staffChatHandler);
public final StaffChatPml staffChatPml = new StaffChatPml(staffChatHandler); public final StaffChatPml staffChatPml = new StaffChatPml(staffChatHandler);
@@ -2,6 +2,7 @@ package me.oskar3123.staffchat.spigot.command;
import java.util.Objects; import java.util.Objects;
import me.oskar3123.staffchat.spigot.Main; import me.oskar3123.staffchat.spigot.Main;
import me.oskar3123.staffchat.util.Permissions;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@@ -29,7 +30,7 @@ public class StaffChatCommand implements CommandExecutor {
@NotNull String[] args) { @NotNull String[] args) {
String noPerm = String noPerm =
clr(config.getString("messages.prefix") + config.getString("messages.nopermission")); clr(config.getString("messages.prefix") + config.getString("messages.nopermission"));
if (!sender.hasPermission(plugin.commandPerm)) { if (!sender.hasPermission(Permissions.COMMAND.permission())) {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
return true; return true;
} }
@@ -38,14 +39,14 @@ public class StaffChatCommand implements CommandExecutor {
return true; return true;
} }
if (args[0].equalsIgnoreCase("reload")) { if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission(plugin.reloadPerm)) { if (sender.hasPermission(Permissions.RELOAD.permission())) {
reload(sender); reload(sender);
} else { } else {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
} }
return true; return true;
} else if (args[0].equalsIgnoreCase("toggle")) { } else if (args[0].equalsIgnoreCase("toggle")) {
if (sender.hasPermission(plugin.usePerm)) { if (sender.hasPermission(Permissions.USE.permission())) {
toggle(sender); toggle(sender);
} else { } else {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
@@ -65,11 +66,11 @@ public class StaffChatCommand implements CommandExecutor {
String prefix = clr(Objects.requireNonNull(config.getString("messages.prefix", ""))); String prefix = clr(Objects.requireNonNull(config.getString("messages.prefix", "")));
sender.sendMessage( sender.sendMessage(
prefix + "Version " + plugin.getDescription().getVersion() + ", made by oskar3123"); prefix + "Version " + plugin.getDescription().getVersion() + ", made by oskar3123");
if (sender.hasPermission(plugin.usePerm)) { if (sender.hasPermission(Permissions.USE.permission())) {
sender.sendMessage(prefix + "Message prefix: " + config.getString("settings.character")); sender.sendMessage(prefix + "Message prefix: " + config.getString("settings.character"));
sender.sendMessage(prefix + "/" + label + " toggle - Toggles auto staffchat"); sender.sendMessage(prefix + "/" + label + " toggle - Toggles auto staffchat");
} }
if (sender.hasPermission(plugin.reloadPerm)) { if (sender.hasPermission(Permissions.RELOAD.permission())) {
sender.sendMessage(prefix + "/" + label + " reload - Reloads the config file"); sender.sendMessage(prefix + "/" + label + " reload - Reloads the config file");
} }
} }
@@ -19,6 +19,7 @@ import java.util.logging.Level;
import me.oskar3123.staffchat.spigot.Main; import me.oskar3123.staffchat.spigot.Main;
import me.oskar3123.staffchat.spigot.event.StaffChatEvent; import me.oskar3123.staffchat.spigot.event.StaffChatEvent;
import me.oskar3123.staffchat.util.FormatUtils; import me.oskar3123.staffchat.util.FormatUtils;
import me.oskar3123.staffchat.util.Permissions;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@@ -36,7 +37,7 @@ public class StaffChatHandler {
} }
public void onChatEvent(@NotNull AsyncPlayerChatEvent event) { public void onChatEvent(@NotNull AsyncPlayerChatEvent event) {
if (!event.getPlayer().hasPermission(plugin.usePerm)) { if (!event.getPlayer().hasPermission(Permissions.USE.permission())) {
return; return;
} }
@@ -133,7 +134,7 @@ public class StaffChatHandler {
private void sendStaffChatMessage(@NotNull String message) { private void sendStaffChatMessage(@NotNull String message) {
Bukkit.getOnlinePlayers().stream() Bukkit.getOnlinePlayers().stream()
.filter(p -> p.hasPermission(plugin.seePerm)) .filter(p -> p.hasPermission(Permissions.SEE.permission()))
.forEach(p -> p.sendMessage(message)); .forEach(p -> p.sendMessage(message));
plugin.getLogger().info(ChatColor.stripColor(message)); plugin.getLogger().info(ChatColor.stripColor(message));
} }
@@ -0,0 +1,21 @@
package me.oskar3123.staffchat.util;
import org.jetbrains.annotations.NotNull;
public enum Permissions {
USE("staffchat.use"),
SEE("staffchat.see"),
COMMAND("staffchat.command"),
RELOAD("staffchat.reload");
private final String permission;
Permissions(@NotNull String permission) {
this.permission = permission;
}
@NotNull
public String permission() {
return permission;
}
}
@@ -4,6 +4,7 @@ import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import java.io.IOException; import java.io.IOException;
import me.oskar3123.staffchat.util.Permissions;
import me.oskar3123.staffchat.velocity.VelocityStaffChat; import me.oskar3123.staffchat.velocity.VelocityStaffChat;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@@ -30,7 +31,7 @@ public class VelocityStaffChatCommand implements SimpleCommand {
txt( txt(
config.node("messages", "prefix").getString() config.node("messages", "prefix").getString()
+ config.node("messages", "nopermission").getString()); + config.node("messages", "nopermission").getString());
if (!sender.hasPermission("staffchat.command")) { if (!sender.hasPermission(Permissions.COMMAND.permission())) {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
return; return;
} }
@@ -39,14 +40,14 @@ public class VelocityStaffChatCommand implements SimpleCommand {
return; return;
} }
if (args[0].equalsIgnoreCase("reload")) { if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("staffchat.reload")) { if (sender.hasPermission(Permissions.RELOAD.permission())) {
reload(sender); reload(sender);
} else { } else {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
} }
return; return;
} else if (args[0].equalsIgnoreCase("toggle")) { } else if (args[0].equalsIgnoreCase("toggle")) {
if (sender.hasPermission("staffchat.use")) { if (sender.hasPermission(Permissions.USE.permission())) {
toggle(sender); toggle(sender);
} else { } else {
sender.sendMessage(noPerm); sender.sendMessage(noPerm);
@@ -58,7 +59,7 @@ public class VelocityStaffChatCommand implements SimpleCommand {
@Override @Override
public boolean hasPermission(Invocation invocation) { public boolean hasPermission(Invocation invocation) {
return invocation.source().hasPermission("staffchat.command"); return invocation.source().hasPermission(Permissions.COMMAND.permission());
} }
private void playerOnly(@NotNull CommandSource sender) { private void playerOnly(@NotNull CommandSource sender) {
@@ -72,12 +73,12 @@ public class VelocityStaffChatCommand implements SimpleCommand {
String prefix = config.node("messages", "prefix").getString(); String prefix = config.node("messages", "prefix").getString();
sender.sendMessage( sender.sendMessage(
txt(prefix + "Version " + VelocityStaffChat.PLUGIN_VERSION + ", made by oskar3123")); txt(prefix + "Version " + VelocityStaffChat.PLUGIN_VERSION + ", made by oskar3123"));
if (sender.hasPermission("staffchat.use")) { if (sender.hasPermission(Permissions.USE.permission())) {
sender.sendMessage( sender.sendMessage(
txt(prefix + "Message prefix: " + config.node("settings", "character").getString())); txt(prefix + "Message prefix: " + config.node("settings", "character").getString()));
sender.sendMessage(txt(prefix + "/" + label + " toggle - Toggles auto staffchat")); sender.sendMessage(txt(prefix + "/" + label + " toggle - Toggles auto staffchat"));
} }
if (sender.hasPermission("staffchat.reload")) { if (sender.hasPermission(Permissions.RELOAD.permission())) {
sender.sendMessage(txt(prefix + "/" + label + " reload - Reloads the config file")); sender.sendMessage(txt(prefix + "/" + label + " reload - Reloads the config file"));
} }
} }
@@ -6,6 +6,7 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Function; import java.util.function.Function;
import me.oskar3123.staffchat.util.FormatUtils; import me.oskar3123.staffchat.util.FormatUtils;
import me.oskar3123.staffchat.util.Permissions;
import me.oskar3123.staffchat.velocity.VelocityStaffChat; import me.oskar3123.staffchat.velocity.VelocityStaffChat;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@@ -48,7 +49,7 @@ public class VelocityStaffChatHandler {
TextComponent component = TextComponent component =
LegacyComponentSerializer.legacyAmpersand().deserialize(formattedMessage); LegacyComponentSerializer.legacyAmpersand().deserialize(formattedMessage);
plugin.getServer().getAllPlayers().stream() plugin.getServer().getAllPlayers().stream()
.filter(p -> p.hasPermission("staffchat.see")) .filter(p -> p.hasPermission(Permissions.SEE.permission()))
.forEach(p -> p.sendMessage(component)); .forEach(p -> p.sendMessage(component));
plugin.getServer().getConsoleCommandSource().sendMessage(component); plugin.getServer().getConsoleCommandSource().sendMessage(component);
} }
@@ -3,6 +3,7 @@ package me.oskar3123.staffchat.velocity.listener;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import me.oskar3123.staffchat.util.Permissions;
import me.oskar3123.staffchat.velocity.VelocityStaffChat; import me.oskar3123.staffchat.velocity.VelocityStaffChat;
public class VelocityStaffChatListener { public class VelocityStaffChatListener {
@@ -15,13 +16,18 @@ public class VelocityStaffChatListener {
@Subscribe @Subscribe
public void onPlayerChat(PlayerChatEvent event) { public void onPlayerChat(PlayerChatEvent event) {
String character = plugin.getConfig().node("settings", "character").getString("@");
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.USE.permission())) {
return;
}
String character = plugin.getConfig().node("settings", "character").getString("@");
String message = event.getMessage(); String message = event.getMessage();
boolean toggled = plugin.getStaffChatHandler().isToggled(player.getUniqueId()); boolean toggled = plugin.getStaffChatHandler().isToggled(player.getUniqueId());
// Check if player has staff chat toggled on or message starts with @ // Check if player has staff chat toggled on or message starts with @
if ((toggled || message.startsWith(character)) && player.hasPermission("staffchat.use")) { if (toggled || message.startsWith(character)) {
String staffMessage = toggled ? message : message.substring(character.length()).trim(); String staffMessage = toggled ? message : message.substring(character.length()).trim();
plugin.getStaffChatHandler().broadcastStaffMessage(player, staffMessage); plugin.getStaffChatHandler().broadcastStaffMessage(player, staffMessage);
event.setResult(PlayerChatEvent.ChatResult.denied()); event.setResult(PlayerChatEvent.ChatResult.denied());