Added broadcast toggles

TBMCPlugins/DiscordPlugin#89
This commit is contained in:
Norbi Peti 2019-02-12 22:25:50 +01:00
parent e3df62af7e
commit 6b99bc22d7
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 46 additions and 6 deletions

View file

@ -1,22 +1,26 @@
package buttondevteam.core.component.channel;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.architecture.Component;
import org.bukkit.plugin.java.JavaPlugin;
public class ChannelComponent extends Component {
static TBMCSystemChatEvent.BroadcastTarget roomJoinLeave;
@Override
protected void register(JavaPlugin plugin) {
super.register(plugin);
roomJoinLeave = TBMCSystemChatEvent.BroadcastTarget.add("roomJoinLeave"); //Even if it's disabled, global channels continue to work
}
@Override
protected void unregister(JavaPlugin plugin) {
super.unregister(plugin);
TBMCSystemChatEvent.BroadcastTarget.remove(roomJoinLeave);
roomJoinLeave = null;
}
@Override
protected void enable() {
}
@Override

View file

@ -17,11 +17,11 @@ public class ChatRoom extends Channel {
public void joinRoom(CommandSender sender) {
usersInRoom.add(sender);
TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " joined the room");
TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " joined the room", ChannelComponent.roomJoinLeave);
}
public void leaveRoom(CommandSender sender) {
usersInRoom.remove(sender);
TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " left the room");
TBMCChatAPI.SendSystemMessage(this, RecipientTestResult.ALL, sender.getName() + " left the room", ChannelComponent.roomJoinLeave);
}
}

View file

@ -1,10 +1,18 @@
package buttondevteam.lib;
import buttondevteam.core.component.channel.Channel;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Objects;
import java.util.stream.Stream;
/**
* Make sure to only send the message to users who {@link #shouldSendTo(CommandSender)} returns true.
*
@ -14,15 +22,17 @@ import org.bukkit.event.HandlerList;
@Getter
public class TBMCSystemChatEvent extends TBMCChatEventBase {
private final String[] exceptions;
private final BroadcastTarget target;
private boolean handled;
public void setHandled() {
handled = true;
}
public TBMCSystemChatEvent(Channel channel, String message, int score, String groupid, String[] exceptions) { // TODO: Rich message
public TBMCSystemChatEvent(Channel channel, String message, int score, String groupid, String[] exceptions, BroadcastTarget target) { // TODO: Rich message
super(channel, message, score, groupid);
this.exceptions = exceptions;
this.target = target;
}
private static final HandlerList handlers = new HandlerList();
@ -35,4 +45,30 @@ public class TBMCSystemChatEvent extends TBMCChatEventBase {
public static HandlerList getHandlerList() {
return handlers;
}
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static class BroadcastTarget {
private final @Getter String name;
private static final HashSet<BroadcastTarget> targets = new HashSet<>();
public static final BroadcastTarget ALL = new BroadcastTarget("ALL");
public static BroadcastTarget add(String name) {
val bt = new BroadcastTarget(Objects.requireNonNull(name));
targets.add(bt);
return bt;
}
public static void remove(BroadcastTarget target) {
targets.remove(target);
}
@Nullable
public static BroadcastTarget get(String name) {
return targets.stream().filter(bt -> bt.name.equals(name)).findAny().orElse(null);
}
public static Stream<BroadcastTarget> stream() {
return targets.stream();
}
}
}

View file

@ -311,12 +311,12 @@ public class TBMCChatAPI {
* @param exceptions Platforms where this message shouldn't be sent (same as {@link ChatMessage#getOrigin()}
* @return The event cancelled state
*/
public static boolean SendSystemMessage(Channel channel, RecipientTestResult rtr, String message, String... exceptions) {
public static boolean SendSystemMessage(Channel channel, RecipientTestResult rtr, String message, TBMCSystemChatEvent.BroadcastTarget target, String... exceptions) {
if (!Channel.getChannelList().contains(channel))
throw new RuntimeException("Channel " + channel.DisplayName().get() + " not registered!");
if (!channel.Enabled().get())
return true; //Cancel sending
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID, exceptions);
TBMCSystemChatEvent event = new TBMCSystemChatEvent(channel, message, rtr.score, rtr.groupID, exceptions, target);
Bukkit.getPluginManager().callEvent(event);
return event.isCancelled();
}