Make addons prevent channel switching

This commit is contained in:
Draycia committed 2020-07-27 22:17:18 -07:00
1 parent 126df2c3e2
commit 947b3f9317
7 files changed
+71 -6

No files matched your search

@@ -3,13 +3,15 @@ package net.draycia.simplechat.events;
import net.draycia.simplechat.channels.ChatChannel;
import net.draycia.simplechat.storage.ChatUser;
import org.bukkit.Bukkit;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class ChannelSwitchEvent extends Event {
public class ChannelSwitchEvent extends Event implements Cancellable {
/** Bukkit event stuff **/
private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
@Override
public HandlerList getHandlers() {
@@ -20,15 +22,27 @@ public class ChannelSwitchEvent extends Event {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
/** Relevant stuff **/
private ChatChannel channel;
private ChatUser user;
private String failureMessage;
public ChannelSwitchEvent(ChatChannel channel, ChatUser user) {
public ChannelSwitchEvent(ChatChannel channel, ChatUser user, String failureMessage) {
super(!Bukkit.isPrimaryThread());
this.channel = channel;
this.user = user;
this.failureMessage = failureMessage;
}
public ChatUser getUser() {
@@ -39,4 +53,11 @@ public class ChannelSwitchEvent extends Event {
return channel;
}
public String getFailureMessage() {
return failureMessage;
}
public void setFailureMessage(String failureMessage) {
this.failureMessage = failureMessage;
}
}
@@ -75,10 +75,20 @@ public class SimpleChatUser implements ChatUser, ForwardingAudience {
@Override
public void setSelectedChannel(ChatChannel chatChannel) {
String failureMessage = simpleChat.getConfig().getString("language.channel-switch-failure");
ChannelSwitchEvent event = new ChannelSwitchEvent(chatChannel, this, failureMessage);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
sendMessage(simpleChat.getAdventureManager().processMessage(event.getFailureMessage(),
"channel", chatChannel.getName()));
return;
}
this.selectedChannel = chatChannel.getKey();
syncToRedis();
Bukkit.getPluginManager().callEvent(new ChannelSwitchEvent(chatChannel, this));
}
@Override
@@ -74,6 +74,7 @@ language:
spy-whispers: '<yellow>Spy [<gray><sender> <gold>-> <gray><target><yellow>] <message>'
no-reply-target: '<red>You have no one to reply to!'
empty-channel: 'NONE' #<channel>, set to "NONE" to disable
channel-switch-failure: '<red>You cannot use channel <channel>!'
# The following messages are compatible with PlaceholderAPI and MiniMessage/MineDown
me: '<italic><dark_purple>*%player_displayname% <pre><message></pre>*</dark_purple></italic>'