commit
c5192f5873
7 changed files with 632 additions and 572 deletions
1
lombok.config
Normal file
1
lombok.config
Normal file
|
@ -0,0 +1 @@
|
|||
lombok.var.flagUsage = ALLOW
|
|
@ -47,7 +47,7 @@ public class ChromaBot {
|
|||
|
||||
/**
|
||||
* Send a message to the chat channels and private chats.
|
||||
*
|
||||
*
|
||||
* @param message
|
||||
* The message to send, duh
|
||||
* @param embed
|
||||
|
@ -57,6 +57,16 @@ public class ChromaBot {
|
|||
MCChatListener.forAllMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message, embed));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the chat channels, private chats and custom chats.
|
||||
*
|
||||
* @param message The message to send, duh
|
||||
* @param embed Custom fancy stuff, use {@link EmbedBuilder} to create one
|
||||
*/
|
||||
public void sendMessageCustomAsWell(String message, EmbedObject embed) {
|
||||
MCChatListener.forCustomAndAllMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message, embed));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to an arbitrary channel. This will not send it to the private chats.
|
||||
*
|
||||
|
|
|
@ -158,17 +158,17 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
|
||||
DiscordCommandBase.registerCommands();
|
||||
if (ResetMCCommand.resetting)
|
||||
ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.CYAN)
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.CYAN)
|
||||
.withTitle("Discord plugin restarted - chat connected.").build()); //Really important to note the chat, hmm
|
||||
else if (getConfig().getBoolean("serverup", false)) {
|
||||
ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.YELLOW)
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.YELLOW)
|
||||
.withTitle("Server recovered from a crash - chat connected.").build());
|
||||
val thr = new Throwable(
|
||||
"The server shut down unexpectedly. See the log of the previous run for more details.");
|
||||
thr.setStackTrace(new StackTraceElement[0]);
|
||||
TBMCCoreAPI.SendException("The server crashed!", thr);
|
||||
} else
|
||||
ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.GREEN)
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell("", new EmbedBuilder().withColor(Color.GREEN)
|
||||
.withTitle("Server started - chat connected.").build());
|
||||
|
||||
ResetMCCommand.resetting = false; //This is the last event handling this flag
|
||||
|
@ -242,7 +242,8 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
public void onDisable() {
|
||||
stop = true;
|
||||
for (val entry : MCChatListener.ConnectedSenders.entrySet())
|
||||
MCListener.callEventExcludingSome(new PlayerQuitEvent(entry.getValue(), ""));
|
||||
for (val valueEntry : entry.getValue().entrySet())
|
||||
MCListener.callEventExcludingSome(new PlayerQuitEvent(valueEntry.getValue(), ""));
|
||||
MCChatListener.ConnectedSenders.clear();
|
||||
getConfig().set("lastannouncementtime", lastannouncementtime);
|
||||
getConfig().set("lastseentime", lastseentime);
|
||||
|
@ -261,7 +262,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
|||
}
|
||||
|
||||
saveConfig();
|
||||
MCChatListener.forAllMCChat(ch -> {
|
||||
MCChatListener.forCustomAndAllMCChat(ch -> {
|
||||
try {
|
||||
if (ResetMCCommand.resetting)
|
||||
DiscordPlugin.sendMessageToChannelWait(ch, "",
|
||||
|
|
|
@ -2,10 +2,7 @@ package buttondevteam.discordplugin.listeners;
|
|||
|
||||
import buttondevteam.discordplugin.*;
|
||||
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
|
||||
import buttondevteam.lib.TBMCChatEvent;
|
||||
import buttondevteam.lib.TBMCChatPreprocessEvent;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||
import buttondevteam.lib.*;
|
||||
import buttondevteam.lib.chat.Channel;
|
||||
import buttondevteam.lib.chat.ChatMessage;
|
||||
import buttondevteam.lib.chat.ChatRoom;
|
||||
|
@ -15,8 +12,10 @@ import com.vdurmont.emoji.EmojiParser;
|
|||
import io.netty.util.collection.LongObjectHashMap;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.experimental.var;
|
||||
import lombok.val;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
@ -227,11 +226,11 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
val op = Bukkit.getOfflinePlayer(mcp.getUUID());
|
||||
if (start) {
|
||||
val sender = new DiscordConnectedPlayer(user, channel, mcp.getUUID(), op.getName());
|
||||
ConnectedSenders.put(user.getStringID(), sender);
|
||||
addSender(ConnectedSenders, user, sender);
|
||||
if (p == null)// Player is offline - If the player is online, that takes precedence
|
||||
MCListener.callEventExcludingSome(new PlayerJoinEvent(sender, ""));
|
||||
} else {
|
||||
val sender = ConnectedSenders.remove(user.getStringID());
|
||||
val sender = removeSender(ConnectedSenders, channel, user);
|
||||
if (p == null)// Player is offline - If the player is online, that takes precedence
|
||||
MCListener.callEventExcludingSome(new PlayerQuitEvent(sender, ""));
|
||||
}
|
||||
|
@ -243,6 +242,37 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
: lastmsgPerUser.removeIf(lmd -> lmd.channel.getLongID() == channel.getLongID());
|
||||
}
|
||||
|
||||
public static <T extends DiscordSenderBase> T addSender(HashMap<String, HashMap<IChannel, T>> senders,
|
||||
IUser user, T sender) {
|
||||
return addSender(senders, user.getStringID(), sender);
|
||||
}
|
||||
|
||||
public static <T extends DiscordSenderBase> T addSender(HashMap<String, HashMap<IChannel, T>> senders,
|
||||
String did, T sender) {
|
||||
var map = senders.get(did);
|
||||
if (map == null)
|
||||
map = new HashMap<>();
|
||||
map.put(sender.getChannel(), sender);
|
||||
senders.put(did, map);
|
||||
return sender;
|
||||
}
|
||||
|
||||
public static <T extends DiscordSenderBase> T getSender(HashMap<String, HashMap<IChannel, T>> senders,
|
||||
IChannel channel, IUser user) {
|
||||
var map = senders.get(user.getStringID());
|
||||
if (map != null)
|
||||
return map.get(channel);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T extends DiscordSenderBase> T removeSender(HashMap<String, HashMap<IChannel, T>> senders,
|
||||
IChannel channel, IUser user) {
|
||||
var map = senders.get(user.getStringID());
|
||||
if (map != null)
|
||||
return map.remove(channel);
|
||||
return null;
|
||||
}
|
||||
|
||||
// ......................DiscordSender....DiscordConnectedPlayer.DiscordPlayerSender
|
||||
// Offline public chat......x............................................
|
||||
// Online public chat.......x...........................................x
|
||||
|
@ -255,8 +285,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
// The maps may not contain the senders for UnconnectedSenders
|
||||
|
||||
public static boolean isMinecraftChatEnabled(DiscordPlayer dp) {
|
||||
return lastmsgPerUser.stream().anyMatch(
|
||||
lmd -> ((IPrivateChannel) lmd.channel).getRecipient().getStringID().equals(dp.getDiscordID()));
|
||||
return isMinecraftChatEnabled(dp.getDiscordID());
|
||||
}
|
||||
|
||||
public static boolean isMinecraftChatEnabled(String did) { // Don't load the player data just for this
|
||||
|
@ -289,12 +318,12 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
/**
|
||||
* May contain P<DiscordID> as key for public chat
|
||||
*/
|
||||
public static final HashMap<String, DiscordSender> UnconnectedSenders = new HashMap<>();
|
||||
public static final HashMap<String, DiscordConnectedPlayer> ConnectedSenders = new HashMap<>();
|
||||
public static final HashMap<String, HashMap<IChannel, DiscordSender>> UnconnectedSenders = new HashMap<>();
|
||||
public static final HashMap<String, HashMap<IChannel, DiscordConnectedPlayer>> ConnectedSenders = new HashMap<>();
|
||||
/**
|
||||
* May contain P<DiscordID> as key for public chat
|
||||
*/
|
||||
public static final HashMap<String, DiscordPlayerSender> OnlineSenders = new HashMap<>();
|
||||
public static final HashMap<String, HashMap<IChannel, DiscordPlayerSender>> OnlineSenders = new HashMap<>();
|
||||
public static short ListC = 0;
|
||||
|
||||
public static void resetLastMessage() {
|
||||
|
@ -330,9 +359,22 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
action.accept(DiscordPlugin.chatchannel);
|
||||
for (LastMsgData data : lastmsgPerUser)
|
||||
action.accept(data.channel);
|
||||
// lastmsgCustom.forEach(cc -> action.accept(cc.channel)); - Only send relevant messages to custom chat
|
||||
}
|
||||
|
||||
public static void forCustomAndAllMCChat(Consumer<IChannel> action) {
|
||||
forAllMCChat(action);
|
||||
lastmsgCustom.forEach(cc -> action.accept(cc.channel));
|
||||
}
|
||||
|
||||
public static void forAllowedCustomMCChat(Consumer<IChannel> action, CommandSender sender) {
|
||||
lastmsgCustom.stream().filter(clmd -> {
|
||||
//new TBMCChannelConnectFakeEvent(sender, clmd.mcchannel).shouldSendTo(clmd.dcp) - Thought it was this simple hehe - Wait, it *should* be this simple
|
||||
val e = new TBMCChannelConnectFakeEvent(sender, clmd.mcchannel);
|
||||
return clmd.groupID.equals(e.getGroupID(sender));
|
||||
}).forEach(cc -> action.accept(cc.channel)); //TODO: Use getScore and getGroupID in fake event constructor - This should also send error messages on channel connect
|
||||
}
|
||||
|
||||
private static void forAllowedMCChat(Consumer<IChannel> action, TBMCSystemChatEvent event) {
|
||||
if (Channel.GlobalChat.ID.equals(event.getChannel().ID))
|
||||
action.accept(DiscordPlugin.chatchannel);
|
||||
|
@ -596,14 +638,13 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
|||
* This method will find the best sender to use: if the player is online, use that, if not but connected then use that etc.
|
||||
*/
|
||||
private static DiscordSenderBase getSender(IChannel channel, final IUser author) {
|
||||
val key = (channel.isPrivate() ? "" : "P") + author.getStringID();
|
||||
val key = author.getStringID();
|
||||
return Stream.<Supplier<Optional<DiscordSenderBase>>>of( // https://stackoverflow.com/a/28833677/2703239
|
||||
() -> Optional.ofNullable(OnlineSenders.get(key)), // Find first non-null
|
||||
() -> Optional.ofNullable(ConnectedSenders.get(key)), // This doesn't support the public chat, but it'll always return null for it
|
||||
() -> Optional.ofNullable(UnconnectedSenders.get(key)), () -> {
|
||||
val dsender = new DiscordSender(author, channel);
|
||||
UnconnectedSenders.put(key, dsender);
|
||||
return Optional.of(dsender);
|
||||
() -> Optional.ofNullable(getSender(OnlineSenders, channel, author)), // Find first non-null
|
||||
() -> Optional.ofNullable(getSender(ConnectedSenders, channel, author)), // This doesn't support the public chat, but it'll always return null for it
|
||||
() -> Optional.ofNullable(getSender(OnlineSenders, channel, author)), () -> {
|
||||
return Optional.of(addSender(UnconnectedSenders, author,
|
||||
new DiscordSender(author, channel)));
|
||||
}).map(Supplier::get).filter(Optional::isPresent).map(Optional::get).findFirst().get();
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ public class MCListener implements Listener {
|
|||
public void onPlayerLogin(PlayerLoginEvent e) {
|
||||
if (e.getResult() != Result.ALLOWED)
|
||||
return;
|
||||
MCChatListener.ConnectedSenders.values().stream()
|
||||
MCChatListener.ConnectedSenders.values().stream().flatMap(v -> v.values().stream()) //Only private mcchat should be in ConnectedSenders
|
||||
.filter(s -> s.getUniqueId().equals(e.getPlayer().getUniqueId())).findAny()
|
||||
.ifPresent(dcp -> callEventExcludingSome(new PlayerQuitEvent(dcp, "")));
|
||||
}
|
||||
|
@ -50,10 +50,10 @@ public class MCListener implements Listener {
|
|||
DiscordPlayer dp = e.GetPlayer().getAs(DiscordPlayer.class);
|
||||
if (dp != null) {
|
||||
val user = DiscordPlugin.dc.getUserByID(Long.parseLong(dp.getDiscordID()));
|
||||
MCChatListener.OnlineSenders.put(dp.getDiscordID(),
|
||||
MCChatListener.addSender(MCChatListener.OnlineSenders, dp.getDiscordID(),
|
||||
new DiscordPlayerSender(user, user.getOrCreatePMChannel(), p));
|
||||
MCChatListener.OnlineSenders.put("P" + dp.getDiscordID(),
|
||||
new DiscordPlayerSender(user, DiscordPlugin.chatchannel, p));
|
||||
MCChatListener.addSender(MCChatListener.OnlineSenders, dp.getDiscordID(),
|
||||
new DiscordPlayerSender(user, DiscordPlugin.chatchannel, p)); //Stored per-channel
|
||||
}
|
||||
if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().PlayerName().get())) {
|
||||
IUser user = DiscordPlugin.dc
|
||||
|
@ -62,8 +62,10 @@ public class MCListener implements Listener {
|
|||
+ " do /discord accept");
|
||||
p.sendMessage("§bIf it wasn't you, do /discord decline");
|
||||
}
|
||||
final String message = e.GetPlayer().PlayerName().get() + " joined the game";
|
||||
if (!DiscordPlugin.hooked)
|
||||
MCChatListener.sendSystemMessageToChat(e.GetPlayer().PlayerName().get() + " joined the game");
|
||||
MCChatListener.sendSystemMessageToChat(message);
|
||||
MCChatListener.forAllowedCustomMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message), e.getPlayer());
|
||||
MCChatListener.ListC = 0;
|
||||
ChromaBot.getInstance().updatePlayerList();
|
||||
});
|
||||
|
@ -74,13 +76,17 @@ public class MCListener implements Listener {
|
|||
if (e.getPlayer() instanceof DiscordConnectedPlayer)
|
||||
return; // Only care about real users
|
||||
MCChatListener.OnlineSenders.entrySet()
|
||||
.removeIf(entry -> entry.getValue().getUniqueId().equals(e.getPlayer().getUniqueId()));
|
||||
.removeIf(entry -> entry.getValue().entrySet().stream().anyMatch(p -> p.getValue().getUniqueId().equals(e.getPlayer().getUniqueId())));
|
||||
Bukkit.getScheduler().runTask(DiscordPlugin.plugin,
|
||||
() -> MCChatListener.ConnectedSenders.values().stream()
|
||||
() -> MCChatListener.ConnectedSenders.values().stream().flatMap(v -> v.values().stream())
|
||||
.filter(s -> s.getUniqueId().equals(e.getPlayer().getUniqueId())).findAny()
|
||||
.ifPresent(dcp -> callEventExcludingSome(new PlayerJoinEvent(dcp, ""))));
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin,
|
||||
ChromaBot.getInstance()::updatePlayerList, 5);
|
||||
final String message = e.GetPlayer().PlayerName().get() + " left the game";
|
||||
if (!DiscordPlugin.hooked)
|
||||
MCChatListener.sendSystemMessageToChat(message); //TODO: Probably double sends if kicked and unhooked
|
||||
MCChatListener.forAllowedCustomMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message), e.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
|
@ -111,7 +117,7 @@ public class MCListener implements Listener {
|
|||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerAFK(AfkStatusChangeEvent e) {
|
||||
public void onPlayerAFK(AfkStatusChangeEvent e) { //TODO: Add AFK to custom chats?
|
||||
if (e.isCancelled() || !e.getAffected().getBase().isOnline())
|
||||
return;
|
||||
MCChatListener.sendSystemMessageToChat(DPUtils.sanitizeString(e.getAffected().getBase().getDisplayName())
|
||||
|
|
|
@ -35,7 +35,7 @@ public class AcceptMCCommand extends DiscordMCCommandBase {
|
|||
dp.save();
|
||||
mcp.save();
|
||||
ConnectCommand.WaitingToConnect.remove(player.getName());
|
||||
MCChatListener.UnconnectedSenders.remove(did);
|
||||
MCChatListener.UnconnectedSenders.remove(did); //Remove all unconnected, will be recreated where needed
|
||||
player.sendMessage("§bAccounts connected.");
|
||||
return true;
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue