-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add separate events for local and global chat #4683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7bd890d
Add separate events for local and global chat
brawaru f739317
Merge branch '2.x' into feat/add-chat-type-events
JRoy 9e42f9e
Merge branch '2.x' into feat/add-chat-type-events
JRoy bbd9c1c
Merge branch '2.x' into feat/add-chat-type-events
mdcfe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
97 changes: 5 additions & 92 deletions
97
Essentials/src/main/java/net/ess3/api/events/LocalChatSpyEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Essentials/src/main/java/net/essentialsx/api/v2/ChatType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package net.essentialsx.api.v2; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| /** | ||
| * Represents chat type for a message | ||
| */ | ||
| public enum ChatType { | ||
| /** | ||
| * Message is being sent to global chat as a shout | ||
| */ | ||
| SHOUT, | ||
|
|
||
| /** | ||
| * Message is being sent to global chat as a question | ||
| */ | ||
| QUESTION, | ||
|
|
||
| /** | ||
| * Message is being sent locally | ||
| */ | ||
| LOCAL, | ||
|
|
||
| /** | ||
| * Message is being sent to spy channel | ||
| */ | ||
| SPY, | ||
|
|
||
| /** | ||
| * Chat type is not determined | ||
| * | ||
| * <p>This type used when local/global chat features are disabled | ||
| */ | ||
| UNKNOWN, | ||
| ; | ||
|
|
||
| private final String key; | ||
|
|
||
| ChatType() { | ||
| this.key = name().toLowerCase(Locale.ENGLISH); | ||
| } | ||
|
|
||
| /** | ||
| * @return Lowercase name of the chat type. | ||
| */ | ||
| public String key() { | ||
| return key; | ||
| } | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
Essentials/src/main/java/net/essentialsx/api/v2/events/chat/ChatEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package net.essentialsx.api.v2.events.chat; | ||
|
|
||
| import net.essentialsx.api.v2.ChatType; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.Cancellable; | ||
| import org.bukkit.event.Event; | ||
|
|
||
| import java.util.IllegalFormatException; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * This handles common boilerplate for other ChatEvents | ||
| */ | ||
| public abstract class ChatEvent extends Event implements Cancellable { | ||
| private final ChatType chatType; | ||
| private final Player player; | ||
| private final Set<Player> recipients; | ||
| private String message; | ||
| private String format; | ||
|
|
||
| private boolean cancelled = false; | ||
|
|
||
| public ChatEvent(final boolean async, final ChatType chatType, final Player player, | ||
| final String format, final String message, final Set<Player> recipients) { | ||
| super(async); | ||
|
|
||
| this.chatType = chatType; | ||
| this.player = player; | ||
| this.format = format; | ||
| this.message = message; | ||
| this.recipients = recipients; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the message that the player is attempting to send. This message will be used with | ||
| * {@link #getFormat()}. | ||
| * | ||
| * @return Message the player is attempting to send | ||
| */ | ||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the message that the player will send. This message will be used with | ||
| * {@link #getFormat()}. | ||
| * | ||
| * @param message New message that the player will send | ||
| */ | ||
| public void setMessage(final String message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the format to use to display this chat message. When this event finishes execution, the | ||
| * first format parameter is the {@link Player#getDisplayName()} and the second parameter is | ||
| * {@link #getMessage()} | ||
| * | ||
| * @return {@link String#format(String, Object...)} compatible format string | ||
| */ | ||
| public String getFormat() { | ||
| return format; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the format to use to display this chat message. When this event finishes execution, the | ||
| * first format parameter is the {@link Player#getDisplayName()} and the second parameter is | ||
| * {@link #getMessage()} | ||
| * | ||
| * @param format {@link String#format(String, Object...)} compatible format string | ||
| * @throws IllegalFormatException if the underlying API throws the exception | ||
| * @throws NullPointerException if format is null | ||
| * @see String#format(String, Object...) | ||
| */ | ||
| public void setFormat(final String format) throws IllegalFormatException, NullPointerException { | ||
| // Oh for a better way to do this! | ||
| try { | ||
| String.format(format, player, message); | ||
| } catch (final RuntimeException ex) { | ||
| ex.fillInStackTrace(); | ||
| throw ex; | ||
| } | ||
|
|
||
| this.format = format; | ||
| } | ||
|
|
||
| /** | ||
| * Gets a set of recipients that this chat message will be displayed to. | ||
| * | ||
| * @return All Players who will see this chat message | ||
| */ | ||
| public Set<Player> getRecipients() { | ||
| return recipients; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the player involved in this event | ||
| * | ||
| * @return Player who is involved in this event | ||
| */ | ||
| public final Player getPlayer() { | ||
| return player; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the type of chat this event is fired for | ||
| * | ||
| * @return Type of chat this event is fired for | ||
| */ | ||
| public ChatType getChatType() { | ||
| return chatType; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isCancelled() { | ||
| return cancelled; | ||
| } | ||
|
|
||
| @Override | ||
| public void setCancelled(final boolean cancel) { | ||
| this.cancelled = cancel; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
Essentials/src/main/java/net/essentialsx/api/v2/events/chat/GlobalChatEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package net.essentialsx.api.v2.events.chat; | ||
|
|
||
| import net.essentialsx.api.v2.ChatType; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.Cancellable; | ||
| import org.bukkit.event.HandlerList; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Fired when a player uses global chat | ||
| */ | ||
| public class GlobalChatEvent extends ChatEvent implements Cancellable { | ||
| private static final HandlerList handlers = new HandlerList(); | ||
|
|
||
| public GlobalChatEvent(final boolean async, final ChatType chatType, final Player player, final String format, final String message, final Set<Player> recipients) { | ||
| super(async, chatType, player, format, message, recipients); | ||
| } | ||
|
|
||
| public static HandlerList getHandlerList() { | ||
| return handlers; | ||
| } | ||
|
|
||
| @Override | ||
| public HandlerList getHandlers() { | ||
| return handlers; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
Essentials/src/main/java/net/essentialsx/api/v2/events/chat/LocalChatEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package net.essentialsx.api.v2.events.chat; | ||
|
|
||
| import net.essentialsx.api.v2.ChatType; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.Cancellable; | ||
| import org.bukkit.event.HandlerList; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Fired when a player uses local chat | ||
| */ | ||
| public class LocalChatEvent extends ChatEvent implements Cancellable { | ||
brawaru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static final HandlerList handlers = new HandlerList(); | ||
|
|
||
| private final long radius; | ||
|
|
||
| public LocalChatEvent(final boolean async, final Player player, final String format, final String message, final Set<Player> recipients, final long radius) { | ||
| super(async, ChatType.LOCAL, player, format, message, recipients); | ||
| this.radius = radius; | ||
| } | ||
|
|
||
| /** | ||
| * Returns local chat radius used to calculate recipients of this message. | ||
| * <p> | ||
| * <p>This is not a radius between players: for that use {@link ChatEvent#getRecipients()} and calculate distance | ||
| * to player who sent the message ({@link ChatEvent#getPlayer()}). | ||
| * @return Non-squared local chat radius. | ||
| */ | ||
| public long getRadius() { | ||
| return radius; | ||
| } | ||
|
|
||
| public static HandlerList getHandlerList() { | ||
| return handlers; | ||
| } | ||
|
|
||
| @Override | ||
| public HandlerList getHandlers() { | ||
| return handlers; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.