From 1a975e0d8e7182509464691fc0047368e6d1fed9 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 5 Aug 2017 00:21:57 +0200 Subject: [PATCH] Added Channel.filteranderrormsg() TBMCPlugins/ButtonChat#61 --- .../java/buttondevteam/core/MainPlugin.java | 16 ++++++---------- .../java/buttondevteam/lib/TBMCCoreAPI.java | 2 +- .../java/buttondevteam/lib/chat/Channel.java | 19 +++++++++++++++++++ .../java/buttondevteam/lib/chat/Color.java | 4 ++-- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index 966505b..ac4a4ad 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -2,7 +2,6 @@ package buttondevteam.core; import java.util.logging.Logger; -import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; @@ -10,7 +9,6 @@ import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.TBMCChatAPI; -import buttondevteam.lib.chat.Channel.RecipientTestResult; import buttondevteam.lib.player.TBMCPlayerBase; import net.milkbowl.vault.permission.Permission; @@ -36,14 +34,12 @@ public class MainPlugin extends JavaPlugin { TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class); TBMCChatAPI.RegisterChatChannel(Channel.GlobalChat = new Channel("§fg§f", Color.White, "g", null)); - TBMCChatAPI.RegisterChatChannel(Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a", - s -> s.isOp() // - ? new RecipientTestResult(0) - : new RecipientTestResult("You need to be an admin to use this channel."))); - TBMCChatAPI.RegisterChatChannel(Channel.ModChat = new Channel("§9MOD§f", Color.Blue, "mod", - s -> s.isOp() || (s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, "mod")) - ? new RecipientTestResult(0) // - : new RecipientTestResult("You need to be a mod to use this channel."))); + TBMCChatAPI.RegisterChatChannel( + Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a", Channel.filteranderrormsg(null))); + TBMCChatAPI.RegisterChatChannel( + Channel.ModChat = new Channel("§9MOD§f", Color.Blue, "mod", Channel.filteranderrormsg("mod"))); + TBMCChatAPI + .RegisterChatChannel(new Channel("§6DEV§", Color.Gold, "dev", Channel.filteranderrormsg("developer"))); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ") Test: " + Test + "."); } diff --git a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java index f1c0441..2e0c9ae 100644 --- a/src/main/java/buttondevteam/lib/TBMCCoreAPI.java +++ b/src/main/java/buttondevteam/lib/TBMCCoreAPI.java @@ -127,7 +127,7 @@ public class TBMCCoreAPI { } /** - * Registers Bukkit events, handling the exceptions occuring in those events + * Registers Bukkit events, handling the exceptions occurring in those events * * @param listener * The class that handles the events diff --git a/src/main/java/buttondevteam/lib/chat/Channel.java b/src/main/java/buttondevteam/lib/chat/Channel.java index cfb86a7..a8d84dd 100644 --- a/src/main/java/buttondevteam/lib/chat/Channel.java +++ b/src/main/java/buttondevteam/lib/chat/Channel.java @@ -6,6 +6,9 @@ import java.util.function.Function; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import buttondevteam.core.MainPlugin; public class Channel { public final String DisplayName; @@ -43,6 +46,22 @@ public class Channel { return channels; } + /** + * Convenience method for the function parameter of {@link #Channel(String, Color, String, Function)}. It checks if the sender is OP or optionally has the specified group. The error message is + * generated automatically. + * + * @param permgroup + * The group that can access the channel or null to only allow OPs. + * @return + */ + public static Function filteranderrormsg(String permgroup) { + return s -> s.isOp() || (permgroup != null + ? s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, permgroup) : false) + ? new RecipientTestResult(0) // + : new RecipientTestResult("You need to be a(n) " + (permgroup != null ? permgroup : "OP") + + " to use this channel."); + } + public static Channel GlobalChat; public static Channel AdminChat; public static Channel ModChat; diff --git a/src/main/java/buttondevteam/lib/chat/Color.java b/src/main/java/buttondevteam/lib/chat/Color.java index 864ba9e..db646bb 100644 --- a/src/main/java/buttondevteam/lib/chat/Color.java +++ b/src/main/java/buttondevteam/lib/chat/Color.java @@ -5,8 +5,8 @@ public enum Color implements TellrawSerializableEnum { 0, 170, 170), DarkRed("dark_red", 170, 0, 0), DarkPurple("dark_purple", 0, 170, 0), Gold("gold", 255, 170, 0), Gray("gray", 170, 170, 170), DarkGray("dark_gray", 85, 85, 85), Blue("blue", 85, 85, 255), Green("green", 85, 255, 85), Aqua("aqua", 85, 255, 255), Red("red", 255, 85, - 85), LightPurple("light_purple", 255, 85, 255), Yellow("yellow", 255, 255, - 85), White("white", 255, 255, 255), RPC("rpc", 0, 0, 0); + 85), LightPurple("light_purple", 255, 85, + 255), Yellow("yellow", 255, 255, 85), White("white", 255, 255, 255); private final String name; private final int red;