Merge pull request #36 from oskar3123/33-refactor-permissions-to-use-a-common-definition
Refactor permissions to common defintion
This commit is contained in:
@@ -16,10 +16,6 @@ public class BungeeMain extends Plugin {
|
||||
|
||||
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);
|
||||
private Configuration config;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.oskar3123.staffchat.bungee.command;
|
||||
|
||||
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.CommandSender;
|
||||
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) {
|
||||
BaseComponent[] noPerm =
|
||||
txt(config.getString("messages.prefix") + config.getString("messages.nopermission"));
|
||||
if (!sender.hasPermission(plugin.commandPerm)) {
|
||||
if (!sender.hasPermission(Permissions.COMMAND.permission())) {
|
||||
sender.sendMessage(noPerm);
|
||||
return;
|
||||
}
|
||||
@@ -34,14 +35,14 @@ public class BungeeStaffChatCommand extends Command {
|
||||
return;
|
||||
}
|
||||
if (args[0].equalsIgnoreCase("reload")) {
|
||||
if (sender.hasPermission(plugin.reloadPerm)) {
|
||||
if (sender.hasPermission(Permissions.RELOAD.permission())) {
|
||||
reload(sender);
|
||||
} else {
|
||||
sender.sendMessage(noPerm);
|
||||
}
|
||||
return;
|
||||
} else if (args[0].equalsIgnoreCase("toggle")) {
|
||||
if (sender.hasPermission(plugin.usePerm)) {
|
||||
if (sender.hasPermission(Permissions.USE.permission())) {
|
||||
toggle(sender);
|
||||
} else {
|
||||
sender.sendMessage(noPerm);
|
||||
@@ -60,11 +61,11 @@ public class BungeeStaffChatCommand extends Command {
|
||||
String prefix = config.getString("messages.prefix");
|
||||
sender.sendMessage(
|
||||
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 + "/" + 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"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.UUID;
|
||||
import me.oskar3123.staffchat.bungee.BungeeMain;
|
||||
import me.oskar3123.staffchat.bungee.event.BungeeStaffChatEvent;
|
||||
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.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
@@ -40,7 +41,7 @@ public class BungeeChatListener implements Listener {
|
||||
}
|
||||
|
||||
ProxiedPlayer player = (ProxiedPlayer) event.getSender();
|
||||
if (!player.hasPermission(plugin.usePerm)) {
|
||||
if (!player.hasPermission(Permissions.USE.permission())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -76,7 +77,7 @@ public class BungeeChatListener implements Listener {
|
||||
final BaseComponent[] messageComponents = txt(appliedFormat);
|
||||
|
||||
plugin.getProxy().getPlayers().stream()
|
||||
.filter(p -> p.hasPermission(plugin.seePerm))
|
||||
.filter(p -> p.hasPermission(Permissions.SEE.permission()))
|
||||
.forEach(p -> p.sendMessage(messageComponents));
|
||||
plugin.getLogger().info(ChatColor.stripColor(appliedFormat));
|
||||
|
||||
|
||||
@@ -18,10 +18,6 @@ public class Main extends JavaPlugin {
|
||||
|
||||
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 ChatListener chatListener = new ChatListener(staffChatHandler);
|
||||
public final StaffChatPml staffChatPml = new StaffChatPml(staffChatHandler);
|
||||
|
||||
@@ -2,6 +2,7 @@ package me.oskar3123.staffchat.spigot.command;
|
||||
|
||||
import java.util.Objects;
|
||||
import me.oskar3123.staffchat.spigot.Main;
|
||||
import me.oskar3123.staffchat.util.Permissions;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@@ -29,7 +30,7 @@ public class StaffChatCommand implements CommandExecutor {
|
||||
@NotNull String[] args) {
|
||||
String noPerm =
|
||||
clr(config.getString("messages.prefix") + config.getString("messages.nopermission"));
|
||||
if (!sender.hasPermission(plugin.commandPerm)) {
|
||||
if (!sender.hasPermission(Permissions.COMMAND.permission())) {
|
||||
sender.sendMessage(noPerm);
|
||||
return true;
|
||||
}
|
||||
@@ -38,14 +39,14 @@ public class StaffChatCommand implements CommandExecutor {
|
||||
return true;
|
||||
}
|
||||
if (args[0].equalsIgnoreCase("reload")) {
|
||||
if (sender.hasPermission(plugin.reloadPerm)) {
|
||||
if (sender.hasPermission(Permissions.RELOAD.permission())) {
|
||||
reload(sender);
|
||||
} else {
|
||||
sender.sendMessage(noPerm);
|
||||
}
|
||||
return true;
|
||||
} else if (args[0].equalsIgnoreCase("toggle")) {
|
||||
if (sender.hasPermission(plugin.usePerm)) {
|
||||
if (sender.hasPermission(Permissions.USE.permission())) {
|
||||
toggle(sender);
|
||||
} else {
|
||||
sender.sendMessage(noPerm);
|
||||
@@ -65,11 +66,11 @@ public class StaffChatCommand implements CommandExecutor {
|
||||
String prefix = clr(Objects.requireNonNull(config.getString("messages.prefix", "")));
|
||||
sender.sendMessage(
|
||||
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 + "/" + 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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.util.logging.Level;
|
||||
import me.oskar3123.staffchat.spigot.Main;
|
||||
import me.oskar3123.staffchat.spigot.event.StaffChatEvent;
|
||||
import me.oskar3123.staffchat.util.FormatUtils;
|
||||
import me.oskar3123.staffchat.util.Permissions;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@@ -36,7 +37,7 @@ public class StaffChatHandler {
|
||||
}
|
||||
|
||||
public void onChatEvent(@NotNull AsyncPlayerChatEvent event) {
|
||||
if (!event.getPlayer().hasPermission(plugin.usePerm)) {
|
||||
if (!event.getPlayer().hasPermission(Permissions.USE.permission())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -133,7 +134,7 @@ public class StaffChatHandler {
|
||||
|
||||
private void sendStaffChatMessage(@NotNull String message) {
|
||||
Bukkit.getOnlinePlayers().stream()
|
||||
.filter(p -> p.hasPermission(plugin.seePerm))
|
||||
.filter(p -> p.hasPermission(Permissions.SEE.permission()))
|
||||
.forEach(p -> p.sendMessage(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;
|
||||
}
|
||||
}
|
||||
+7
-6
@@ -4,6 +4,7 @@ import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.command.SimpleCommand;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import java.io.IOException;
|
||||
import me.oskar3123.staffchat.util.Permissions;
|
||||
import me.oskar3123.staffchat.velocity.VelocityStaffChat;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
@@ -30,7 +31,7 @@ public class VelocityStaffChatCommand implements SimpleCommand {
|
||||
txt(
|
||||
config.node("messages", "prefix").getString()
|
||||
+ config.node("messages", "nopermission").getString());
|
||||
if (!sender.hasPermission("staffchat.command")) {
|
||||
if (!sender.hasPermission(Permissions.COMMAND.permission())) {
|
||||
sender.sendMessage(noPerm);
|
||||
return;
|
||||
}
|
||||
@@ -39,14 +40,14 @@ public class VelocityStaffChatCommand implements SimpleCommand {
|
||||
return;
|
||||
}
|
||||
if (args[0].equalsIgnoreCase("reload")) {
|
||||
if (sender.hasPermission("staffchat.reload")) {
|
||||
if (sender.hasPermission(Permissions.RELOAD.permission())) {
|
||||
reload(sender);
|
||||
} else {
|
||||
sender.sendMessage(noPerm);
|
||||
}
|
||||
return;
|
||||
} else if (args[0].equalsIgnoreCase("toggle")) {
|
||||
if (sender.hasPermission("staffchat.use")) {
|
||||
if (sender.hasPermission(Permissions.USE.permission())) {
|
||||
toggle(sender);
|
||||
} else {
|
||||
sender.sendMessage(noPerm);
|
||||
@@ -58,7 +59,7 @@ public class VelocityStaffChatCommand implements SimpleCommand {
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(Invocation invocation) {
|
||||
return invocation.source().hasPermission("staffchat.command");
|
||||
return invocation.source().hasPermission(Permissions.COMMAND.permission());
|
||||
}
|
||||
|
||||
private void playerOnly(@NotNull CommandSource sender) {
|
||||
@@ -72,12 +73,12 @@ public class VelocityStaffChatCommand implements SimpleCommand {
|
||||
String prefix = config.node("messages", "prefix").getString();
|
||||
sender.sendMessage(
|
||||
txt(prefix + "Version " + VelocityStaffChat.PLUGIN_VERSION + ", made by oskar3123"));
|
||||
if (sender.hasPermission("staffchat.use")) {
|
||||
if (sender.hasPermission(Permissions.USE.permission())) {
|
||||
sender.sendMessage(
|
||||
txt(prefix + "Message prefix: " + config.node("settings", "character").getString()));
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import me.oskar3123.staffchat.util.FormatUtils;
|
||||
import me.oskar3123.staffchat.util.Permissions;
|
||||
import me.oskar3123.staffchat.velocity.VelocityStaffChat;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
@@ -48,7 +49,7 @@ public class VelocityStaffChatHandler {
|
||||
TextComponent component =
|
||||
LegacyComponentSerializer.legacyAmpersand().deserialize(formattedMessage);
|
||||
plugin.getServer().getAllPlayers().stream()
|
||||
.filter(p -> p.hasPermission("staffchat.see"))
|
||||
.filter(p -> p.hasPermission(Permissions.SEE.permission()))
|
||||
.forEach(p -> p.sendMessage(component));
|
||||
plugin.getServer().getConsoleCommandSource().sendMessage(component);
|
||||
}
|
||||
|
||||
+8
-2
@@ -3,6 +3,7 @@ package me.oskar3123.staffchat.velocity.listener;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.player.PlayerChatEvent;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import me.oskar3123.staffchat.util.Permissions;
|
||||
import me.oskar3123.staffchat.velocity.VelocityStaffChat;
|
||||
|
||||
public class VelocityStaffChatListener {
|
||||
@@ -15,13 +16,18 @@ public class VelocityStaffChatListener {
|
||||
|
||||
@Subscribe
|
||||
public void onPlayerChat(PlayerChatEvent event) {
|
||||
String character = plugin.getConfig().node("settings", "character").getString("@");
|
||||
Player player = event.getPlayer();
|
||||
|
||||
if (!player.hasPermission(Permissions.USE.permission())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String character = plugin.getConfig().node("settings", "character").getString("@");
|
||||
String message = event.getMessage();
|
||||
boolean toggled = plugin.getStaffChatHandler().isToggled(player.getUniqueId());
|
||||
|
||||
// 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();
|
||||
plugin.getStaffChatHandler().broadcastStaffMessage(player, staffMessage);
|
||||
event.setResult(PlayerChatEvent.ChatResult.denied());
|
||||
|
||||
Reference in New Issue
Block a user