From 89d88ef952ca60900a101eb2b5d24d0f53575814 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 7 Aug 2017 17:51:04 +0200 Subject: [PATCH] Added Chat Rooms, lombok, prep. for ann. proc. --- .../javax.annotation.processing.Processor | 1 - pom.xml | 14 +++- .../java/buttondevteam/core/MainPlugin.java | 9 +-- .../buttondevteam/core/PlayerListener.java | 10 +++ .../lib/AnnotationProcessor.java | 33 --------- .../java/buttondevteam/lib/TBMCChatEvent.java | 70 +++---------------- .../buttondevteam/lib/TBMCChatEventBase.java | 40 +++++++++++ .../buttondevteam/lib/TBMCExceptionEvent.java | 46 +++--------- .../lib/TBMCSystemChatEvent.java | 37 ++++++++++ .../java/buttondevteam/lib/chat/Channel.java | 40 +++++++++-- .../java/buttondevteam/lib/chat/ChatRoom.java | 24 +++++++ .../buttondevteam/lib/chat/TBMCChatAPI.java | 37 ++++++++-- .../lib/player/ChromaGamerBase.java | 6 +- 13 files changed, 214 insertions(+), 153 deletions(-) delete mode 100644 META-INF/services/javax.annotation.processing.Processor delete mode 100644 src/main/java/buttondevteam/lib/AnnotationProcessor.java create mode 100644 src/main/java/buttondevteam/lib/TBMCChatEventBase.java create mode 100644 src/main/java/buttondevteam/lib/TBMCSystemChatEvent.java create mode 100644 src/main/java/buttondevteam/lib/chat/ChatRoom.java diff --git a/META-INF/services/javax.annotation.processing.Processor b/META-INF/services/javax.annotation.processing.Processor deleted file mode 100644 index c52a464..0000000 --- a/META-INF/services/javax.annotation.processing.Processor +++ /dev/null @@ -1 +0,0 @@ -buttondevteam.lib.AnnotationProcessor \ No newline at end of file diff --git a/pom.xml b/pom.xml index ba1b5fd..fab3d94 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,6 @@ 1.8 1.8 - -proc:none @@ -138,6 +137,19 @@ mockito-core 2.7.20 + + + org.projectlombok + lombok + 1.16.16 + provided + + + com.github.TBMCPlugins + ButtonProcessor + master-SNAPSHOT + compile + TBMCPlugins diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index ac4a4ad..2762a48 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -7,6 +7,7 @@ import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.Channel; +import buttondevteam.lib.chat.ChatRoom; import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.player.TBMCPlayerBase; @@ -35,11 +36,11 @@ public class MainPlugin extends JavaPlugin { 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", Channel.filteranderrormsg(null))); + Channel.AdminChat = new Channel("§cADMIN§f", Color.Red, "a", Channel.inGroupFilter(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"))); + Channel.ModChat = new Channel("§9MOD§f", Color.Blue, "mod", Channel.inGroupFilter("mod"))); + TBMCChatAPI.RegisterChatChannel(new Channel("§6DEV§", Color.Gold, "dev", Channel.inGroupFilter("developer"))); + TBMCChatAPI.RegisterChatChannel(new ChatRoom("§cRED", Color.DarkRed, "red")); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ") Test: " + Test + "."); } diff --git a/src/main/java/buttondevteam/core/PlayerListener.java b/src/main/java/buttondevteam/core/PlayerListener.java index 0a2ecb3..f2b4518 100644 --- a/src/main/java/buttondevteam/core/PlayerListener.java +++ b/src/main/java/buttondevteam/core/PlayerListener.java @@ -1,11 +1,13 @@ package buttondevteam.core; +import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +import buttondevteam.lib.TBMCSystemChatEvent; import buttondevteam.lib.player.TBMCPlayerBase; public class PlayerListener implements Listener { @@ -19,4 +21,12 @@ public class PlayerListener implements Listener { public void OnPlayerLeave(PlayerQuitEvent event) { TBMCPlayerBase.quitPlayer(event.getPlayer()); } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onSystemChat(TBMCSystemChatEvent event) { + if (event.isHandled()) + return; // Only handle here if ButtonChat couldn't + Bukkit.getOnlinePlayers().stream().filter(p -> event.shouldSendTo(p)) + .forEach(p -> p.sendMessage(event.getChannel().DisplayName.substring(0, 2) + event.getMessage())); + } } diff --git a/src/main/java/buttondevteam/lib/AnnotationProcessor.java b/src/main/java/buttondevteam/lib/AnnotationProcessor.java deleted file mode 100644 index 8e763ec..0000000 --- a/src/main/java/buttondevteam/lib/AnnotationProcessor.java +++ /dev/null @@ -1,33 +0,0 @@ -package buttondevteam.lib; - -import java.util.List; -import java.util.Set; -import javax.annotation.processing.AbstractProcessor; -import javax.annotation.processing.RoundEnvironment; -import javax.annotation.processing.SupportedSourceVersion; -import javax.lang.model.SourceVersion; -import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.Element; -import javax.lang.model.element.TypeElement; - -import buttondevteam.lib.player.ChromaGamerEnforcer; - -/** * A simple session bean type annotation processor. The implementation * is based on the standard annotation processing API in Java 6. */ -@SupportedSourceVersion(SourceVersion.RELEASE_8) -public class AnnotationProcessor extends AbstractProcessor { - /** * Check if both @Stateful and @Stateless are present in an * session bean. If so, emits a warning message. */ - @Override - public boolean process(Set typeElements, RoundEnvironment roundEnv) { // TODO: SEparate JAR - Set elements = roundEnv.getElementsAnnotatedWith(ChromaGamerEnforcer.class); - for (Element element : elements) { - System.out.println("Processing " + element); - List annotationMirrors = element.getAnnotationMirrors(); - System.out.println("Annotations: " + annotationMirrors); - for (AnnotationMirror annotation : annotationMirrors) { - String type = annotation.getAnnotationType().toString(); - System.out.println("Type: " + type); - } - } - return true; // claim the annotations - } -} diff --git a/src/main/java/buttondevteam/lib/TBMCChatEvent.java b/src/main/java/buttondevteam/lib/TBMCChatEvent.java index 192b7c1..9b292df 100644 --- a/src/main/java/buttondevteam/lib/TBMCChatEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCChatEvent.java @@ -1,12 +1,10 @@ package buttondevteam.lib; import org.bukkit.command.CommandSender; -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import buttondevteam.lib.chat.Channel; -import buttondevteam.lib.chat.Channel.RecipientTestResult; +import lombok.Getter; /** * Make sure to only send the message to users who {@link #shouldSendTo(CommandSender)} returns true. @@ -14,37 +12,17 @@ import buttondevteam.lib.chat.Channel.RecipientTestResult; * @author NorbiPeti * */ -public class TBMCChatEvent extends Event implements Cancellable { +@Getter +public class TBMCChatEvent extends TBMCChatEventBase { + public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score) { + super(channel, message, score); + this.sender = sender; + } + private static final HandlerList handlers = new HandlerList(); - private Channel channel; private CommandSender sender; - private String message; - private boolean cancelled; - private int score; - - public TBMCChatEvent(CommandSender sender, Channel channel, String message, int score) { - this.sender = sender; - this.channel = channel; - this.message = message; // TODO: Message object with data? - this.score = score; - } - - /* - * public TBMCPlayer getPlayer() { return TBMCPlayer.getPlayer(sender); // TODO: Get Chroma user } - */ - - public Channel getChannel() { - return channel; - } - - public CommandSender getSender() { - return sender; - } - - public String getMessage() { - return message; - } + // TODO: Message object with data? @Override public HandlerList getHandlers() { @@ -54,34 +32,4 @@ public class TBMCChatEvent extends Event implements Cancellable { public static HandlerList getHandlerList() { return handlers; } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean cancelled) { - this.cancelled = cancelled; - } - - /** - * Note: Errors are sent to the sender automatically - */ - public boolean shouldSendTo(CommandSender sender) { - if (channel.filteranderrormsg == null) - return true; - RecipientTestResult result = channel.filteranderrormsg.apply(sender); - return result.errormessage == null && score == result.score; - } - - /** - * Note: Errors are sent to the sender automatically - */ - public int getMCScore(CommandSender sender) { - if (channel.filteranderrormsg == null) - return 0; - RecipientTestResult result = channel.filteranderrormsg.apply(sender); - return result.errormessage == null ? result.score : -1; - } } diff --git a/src/main/java/buttondevteam/lib/TBMCChatEventBase.java b/src/main/java/buttondevteam/lib/TBMCChatEventBase.java new file mode 100644 index 0000000..627f7c6 --- /dev/null +++ b/src/main/java/buttondevteam/lib/TBMCChatEventBase.java @@ -0,0 +1,40 @@ +package buttondevteam.lib; + +import org.bukkit.command.CommandSender; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import buttondevteam.lib.chat.Channel; +import buttondevteam.lib.chat.Channel.RecipientTestResult; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +@Getter +@RequiredArgsConstructor +public abstract class TBMCChatEventBase extends Event implements Cancellable { + private final Channel channel; + private @NonNull String message; + private @Setter boolean cancelled; + private final int score; + + /** + * Note: Errors are sent to the sender automatically + */ + public boolean shouldSendTo(CommandSender sender) { + if (channel.filteranderrormsg == null) + return true; + RecipientTestResult result = channel.filteranderrormsg.apply(sender); + return result.errormessage == null && score == result.score; + } + + /** + * Note: Errors are sent to the sender automatically + */ + public int getMCScore(CommandSender sender) { + if (channel.filteranderrormsg == null) + return 0; + RecipientTestResult result = channel.filteranderrormsg.apply(sender); + return result.errormessage == null ? result.score : -1; + } +} diff --git a/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java b/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java index 8526248..7bb0e05 100644 --- a/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java +++ b/src/main/java/buttondevteam/lib/TBMCExceptionEvent.java @@ -3,6 +3,9 @@ package buttondevteam.lib; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + /** *

* This event gets called (ideally) each time an exception occurs in a TBMC plugin. To call it, use {@link TBMCCoreAPI#SendException(String, Exception)}. @@ -11,50 +14,17 @@ import org.bukkit.event.HandlerList; * @author Norbi * */ +@Getter +@RequiredArgsConstructor public class TBMCExceptionEvent extends Event { private static final HandlerList handlers = new HandlerList(); - private String sourcemsg; - private Throwable exception; + private final String sourceMessage; + private final Throwable exception; private boolean handled; - TBMCExceptionEvent(String sourcemsg, Throwable exception) { - this.sourcemsg = sourcemsg; - this.exception = exception; - } - - /** - * Gets the source message (where did this exception occur, etc.) - * - * @return The message - */ - public String getSourceMessage() { - return sourcemsg; - } - - /** - * Gets the exception - * - * @return The exception - */ - public Throwable getException() { - return exception; - } - - /** - * Gets if this event was handled - * - * @return True if it was handled - */ - public boolean isHandled() { - return handled; - } - - /** - * Flags the event as handled - */ public void setHandled() { - this.handled = true; + handled = true; } @Override diff --git a/src/main/java/buttondevteam/lib/TBMCSystemChatEvent.java b/src/main/java/buttondevteam/lib/TBMCSystemChatEvent.java new file mode 100644 index 0000000..c0fce47 --- /dev/null +++ b/src/main/java/buttondevteam/lib/TBMCSystemChatEvent.java @@ -0,0 +1,37 @@ +package buttondevteam.lib; + +import org.bukkit.command.CommandSender; +import org.bukkit.event.HandlerList; + +import buttondevteam.lib.chat.Channel; +import lombok.Getter; + +/** + * Make sure to only send the message to users who {@link #shouldSendTo(CommandSender)} returns true. + * + * @author NorbiPeti + * + */ +@Getter +public class TBMCSystemChatEvent extends TBMCChatEventBase { + private boolean handled; + + public void setHandled() { + handled = true; + } + + public TBMCSystemChatEvent(Channel channel, String message, int score) { // TODO: RIch message + super(channel, message, score); + } + + private static final HandlerList handlers = new HandlerList(); + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/buttondevteam/lib/chat/Channel.java b/src/main/java/buttondevteam/lib/chat/Channel.java index a8d84dd..43e00ea 100644 --- a/src/main/java/buttondevteam/lib/chat/Channel.java +++ b/src/main/java/buttondevteam/lib/chat/Channel.java @@ -2,7 +2,10 @@ package buttondevteam.lib.chat; import java.util.ArrayList; import java.util.List; +import java.util.function.BiFunction; +import java.util.function.BiPredicate; import java.util.function.Function; +import java.util.function.Predicate; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -25,7 +28,7 @@ public class Channel { * Creates a channel. * * @param displayname - * The name that should appear at the start of the message + * The name that should appear at the start of the message. A chat color is expected at the beginning (§9). * @param color * The default color of the messages sent in the channel * @param command @@ -42,6 +45,20 @@ public class Channel { this.filteranderrormsg = filteranderrormsg; } + /** + * Must be only called from a subclass - otherwise it'll throw an exception. + * + * @see Channel#Channel(String, Color, String, Function) + */ + @SuppressWarnings("unchecked") + protected Channel(String displayname, Color color, String command, + BiFunction filteranderrormsg) { + DisplayName = displayname; + this.color = color; + ID = command; + this.filteranderrormsg = s -> filteranderrormsg.apply((T) this, s); + } + public static List getChannels() { return channels; } @@ -54,12 +71,21 @@ public class Channel { * 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 Function inGroupFilter(String permgroup) { + return noScoreResult( + s -> s.isOp() || (permgroup != null + ? s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, permgroup) : false), + "You need to be a(n) " + (permgroup != null ? permgroup : "OP") + " to use this channel."); + } + + public static Function noScoreResult(Predicate filter, + String errormsg) { + return s -> filter.test(s) ? new RecipientTestResult(0) : new RecipientTestResult(errormsg); + } + + public static BiFunction noScoreResult( + BiPredicate filter, String errormsg) { + return (this_, s) -> filter.test(this_, s) ? new RecipientTestResult(0) : new RecipientTestResult(errormsg); } public static Channel GlobalChat; diff --git a/src/main/java/buttondevteam/lib/chat/ChatRoom.java b/src/main/java/buttondevteam/lib/chat/ChatRoom.java new file mode 100644 index 0000000..6a735e7 --- /dev/null +++ b/src/main/java/buttondevteam/lib/chat/ChatRoom.java @@ -0,0 +1,24 @@ +package buttondevteam.lib.chat; + +import java.util.ArrayList; +import java.util.List; +import org.bukkit.command.CommandSender; + +public class ChatRoom extends Channel { + private List usersInRoom = new ArrayList<>(); + + public ChatRoom(String displayname, Color color, String command) { + super(displayname, color, command, noScoreResult((this_, s) -> this_.usersInRoom.contains(s), + "Not implemented yet. Please report it to the devs along with which platform you're trying to talk from.")); + } + + public void joinRoom(CommandSender sender) { + usersInRoom.add(sender); + TBMCChatAPI.SendSystemMessage(this, 0, sender.getName() + " joined the room"); + } + + public void leaveRoom(CommandSender sender) { + usersInRoom.remove(sender); + TBMCChatAPI.SendSystemMessage(this, 0, sender.getName() + " left the room"); + } +} diff --git a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index 359e7e7..c537b63 100644 --- a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -22,6 +22,7 @@ import buttondevteam.core.MainPlugin; import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.TBMCChatPreprocessEvent; import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.TBMCSystemChatEvent; import buttondevteam.lib.chat.Channel.RecipientTestResult; public class TBMCChatAPI { @@ -223,20 +224,46 @@ public class TBMCChatAPI { Bukkit.getPluginManager().callEvent(eventPre); if (eventPre.isCancelled()) return true; + int score = getScoreOrSendError(channel, sender); + if (score == -1) + return true; + TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score); + Bukkit.getPluginManager().callEvent(event); + return event.isCancelled(); + } + + /** + * Sends a regular message to Minecraft. Make sure that the channel is registered with {@link #RegisterChatChannel(Channel)}. + * + * @param channel + * The channel to send to + * @param score + * The score to use to find the group - use 0 if the channel doesn't have scores + * @param message + * The message to send + * @return The event cancelled state + */ + public static boolean SendSystemMessage(Channel channel, int score, String message) { + if (!Channel.getChannels().contains(channel)) + throw new RuntimeException("Channel " + channel.DisplayName + " not registered!"); + TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, score); + Bukkit.getPluginManager().callEvent(event); + return event.isCancelled(); + } + + private static int getScoreOrSendError(Channel channel, CommandSender sender) { int score; if (channel.filteranderrormsg == null) - score = -1; + score = 0; else { RecipientTestResult result = channel.filteranderrormsg.apply(sender); if (result.errormessage != null) { sender.sendMessage("§c" + result.errormessage); - return true; + return -1; } score = result.score; } - TBMCChatEvent event = new TBMCChatEvent(sender, channel, eventPre.getMessage(), score); - Bukkit.getPluginManager().callEvent(event); - return event.isCancelled(); + return score; } /** diff --git a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java index 0424c41..c533fa8 100644 --- a/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java +++ b/src/main/java/buttondevteam/lib/player/ChromaGamerBase.java @@ -2,7 +2,6 @@ package buttondevteam.lib.player; import java.io.File; import java.util.HashMap; -import java.util.Map.Entry; import java.util.function.Consumer; import org.bukkit.Bukkit; @@ -11,6 +10,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import com.google.common.collect.HashBiMap; import buttondevteam.lib.TBMCCoreAPI; +import lombok.val; @ChromaGamerEnforcer public abstract class ChromaGamerBase implements AutoCloseable { @@ -131,7 +131,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { Consumer sync = sourcedata -> { final String sourcefolder = sourcedata == plugindata ? ownFolder : userFolder; final String id = sourcedata.getString(sourcefolder + "_id"); - for (Entry, String> entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones + for (val entry : playerTypes.entrySet()) { // Set our ID in all files we can find, both from our connections and the new ones if (entry.getKey() == getClass() || entry.getKey() == user.getClass()) continue; final String otherid = sourcedata.getString(entry.getValue() + "_id"); @@ -139,7 +139,7 @@ public abstract class ChromaGamerBase implements AutoCloseable { continue; try (ChromaGamerBase cg = getUser(otherid, entry.getKey())) { cg.plugindata.set(sourcefolder + "_id", id); // Set new IDs - for (Entry, String> item : playerTypes.entrySet()) + for (val item : playerTypes.entrySet()) if (sourcedata.contains(item.getValue() + "_id")) cg.plugindata.set(item.getValue() + "_id", sourcedata.getString(item.getValue() + "_id")); // Set all existing IDs } catch (Exception e) {