Continued work on mcchat

This commit is contained in:
Norbi Peti 2017-07-09 21:30:37 +02:00
parent d2467210dd
commit a85b7f9a2d
3 changed files with 29 additions and 6 deletions

View file

@ -16,13 +16,13 @@ public class MCChatCommand extends DiscordCommandBase {
@Override @Override
public void run(IMessage message, String args) { public void run(IMessage message, String args) {
if (!message.getChannel().isPrivate()) { if (!message.getChannel().isPrivate()) {
message.reply("This command can only be issued while DMing the bot."); message.reply("This command can only be issued in a direct message with the bot.");
return; return;
} }
try (final DiscordPlayer user = DiscordPlayer.getUser(message.getAuthor().getStringID(), DiscordPlayer.class)) { try (final DiscordPlayer user = DiscordPlayer.getUser(message.getAuthor().getStringID(), DiscordPlayer.class)) {
PlayerData<Boolean> mcchat = user.minecraftChat(); PlayerData<Boolean> mcchat = user.minecraftChat();
mcchat.set(!mcchat.get()); mcchat.set(!mcchat.get());
MCChatListener.privateMCChat(message.getChannel(), mcchat.get()); MCChatListener.privateMCChat(message.getChannel(), mcchat.get(), message.getAuthor(), user);
message.reply("Minecraft chat " + (mcchat.get() // message.reply("Minecraft chat " + (mcchat.get() //
? "enabled. Use '" + message.getClient().getOurUser().mention() ? "enabled. Use '" + message.getClient().getOurUser().mention()
+ " mcchat' (with the mention) to disable." // + " mcchat' (with the mention) to disable." //

View file

@ -13,6 +13,8 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import buttondevteam.discordplugin.*; import buttondevteam.discordplugin.*;
import buttondevteam.lib.*; import buttondevteam.lib.*;
@ -123,8 +125,20 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
*/ */
private static ArrayList<LastMsgData> lastmsgPerUser = new ArrayList<LastMsgData>(); private static ArrayList<LastMsgData> lastmsgPerUser = new ArrayList<LastMsgData>();
public static boolean privateMCChat(IChannel channel, boolean start) { public static boolean privateMCChat(IChannel channel, boolean start, IUser user, DiscordPlayer dp) {
return start ? lastmsgPerUser.add(new LastMsgData(channel)) if (start) {
val sender = new DiscordConnectedPlayer(user, channel,
UUID.fromString(dp.getConnectedID(TBMCPlayer.class)));
ConnectedSenders.put(user.getStringID(), sender);
if (!OnlineSenders.containsKey(user.getStringID()))// If the player is online, that takes precedence
Bukkit.getPluginManager().callEvent(new PlayerJoinEvent(sender, ""));
} else {
val sender = ConnectedSenders.remove(user.getStringID());
if (!OnlineSenders.containsKey(user.getStringID()))// If the player is online, that takes precedence
Bukkit.getPluginManager().callEvent(new PlayerQuitEvent(sender, ""));
}
return start //
? lastmsgPerUser.add(new LastMsgData(channel)) //
: lastmsgPerUser.removeIf(lmd -> lmd.channel.getLongID() == channel.getLongID()); : lastmsgPerUser.removeIf(lmd -> lmd.channel.getLongID() == channel.getLongID());
} }

View file

@ -1,6 +1,5 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -10,7 +9,9 @@ import org.bukkit.event.server.ServerCommandEvent;
import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.CommandSource;
import buttondevteam.discordplugin.DiscordConnectedPlayer;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlayerSender;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.ConnectCommand; import buttondevteam.discordplugin.commands.ConnectCommand;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
@ -24,7 +25,12 @@ import sx.blah.discord.util.MissingPermissionsException;
public class MCListener implements Listener { public class MCListener implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(TBMCPlayerJoinEvent e) { public void onPlayerJoin(TBMCPlayerJoinEvent e) {
final Player p = Bukkit.getPlayer(e.GetPlayer().getUUID()); if (e.getPlayer() instanceof DiscordConnectedPlayer)
return; // Don't show the joined message for the fake player
final Player p = e.getPlayer();
DiscordPlayer dp = e.GetPlayer().getAs(DiscordPlayer.class);
// if(dp!=null) //TODO
// MCChatListener.OnlineSenders.put(dp.getDiscordID(), new DiscordPlayerSender(DiscordPlugin.dc.getUserByID(Long.parseLong(dp.getDiscordID()), channel, player))
if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().PlayerName().get())) { if (ConnectCommand.WaitingToConnect.containsKey(e.GetPlayer().PlayerName().get())) {
IUser user = DiscordPlugin.dc IUser user = DiscordPlugin.dc
.getUserByID(Long.parseLong(ConnectCommand.WaitingToConnect.get(e.GetPlayer().PlayerName().get()))); .getUserByID(Long.parseLong(ConnectCommand.WaitingToConnect.get(e.GetPlayer().PlayerName().get())));
@ -39,6 +45,9 @@ public class MCListener implements Listener {
@EventHandler @EventHandler
public void onPlayerLeave(TBMCPlayerQuitEvent e) { public void onPlayerLeave(TBMCPlayerQuitEvent e) {
if (MCChatListener.OnlineSenders.entrySet()
.removeIf(entry -> entry.getValue().getUniqueId().equals(e.getPlayer().getUniqueId())))
; // TODO
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel,
e.GetPlayer().PlayerName().get() + " left the game"); e.GetPlayer().PlayerName().get() + " left the game");
} }