Made the chat also support players

This commit is contained in:
Norbi Peti 2016-12-04 21:41:17 +01:00
parent c336de27ea
commit faf3b8857f

View file

@ -47,50 +47,44 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.chatchannel.getID())
/* && !(event.getMessage().getChannel().isPrivate() && privatechat) */)
return;
if (event.getMessage().getContent().startsWith("/")) {
final String cmd = event.getMessage().getContent().substring(1);
try {
Optional<? extends Player> player = Bukkit.getOnlinePlayers().stream().filter(p -> { // TODO: Support offline players
DiscordPlayer dp = TBMCPlayer.getPlayerAs(p, DiscordPlayer.class); // Online player, already loaded
return author.getID().equals(dp.getDiscordID());
}).findAny();
try {
final CommandSender dsender;
if (player.isPresent()) // Connected?
{
{ // Execute as ingame player
if (!ConnectedSenders.containsKey(author.getID()))
ConnectedSenders.put(author.getID(), new DiscordPlayerSender(author, player.get()));
final DiscordPlayerSender dsender = ConnectedSenders.get(author.getID());
dsender.setChannel(event.getMessage().getChannel());
// Execute as ingame player
Bukkit.dispatchCommand(dsender, cmd);
dsender = ConnectedSenders.get(author.getID());
((DiscordPlayerSender) dsender).setChannel(event.getMessage().getChannel());
} else {
if (!UnconnectedSenders.containsKey(author.getID()))
UnconnectedSenders.put(author.getID(), new DiscordSender(author));
final DiscordSender dsender = UnconnectedSenders.get(author.getID());
dsender.setChannel(event.getMessage().getChannel());
if (!Arrays.stream(UnconnectedCmds).anyMatch(s -> cmd.startsWith(s))) {
dsender = UnconnectedSenders.get(author.getID());
((DiscordSender) dsender).setChannel(event.getMessage().getChannel());
}
if (event.getMessage().getContent().startsWith("/")) {
final String cmd = event.getMessage().getContent().substring(1);
if (!player.isPresent()) {
// Command not whitelisted
DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), // TODO
"Sorry, you need to be online on the server and have your accounts connected, you can only access these commands:\n"
+ Arrays.toString(UnconnectedCmds));
} else
Bukkit.dispatchCommand(dsender, cmd);
}
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while executing command " + cmd + "!", e);
return;
}
} else {
CommandSender dsender = UnconnectedSenders.get(author.getID());
if (dsender == null)
dsender = ConnectedSenders.get(author.getID());
if (dsender == null) {
UnconnectedSenders.put(author.getID(), new DiscordSender(author));
dsender = UnconnectedSenders.get(author.getID());
}
Bukkit.dispatchCommand(dsender, cmd);
} else
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender,
event.getMessage().getContent()
+ (event.getMessage().getAttachments().size() > 0 ? event.getMessage().getAttachments()
.stream().map(a -> a.getUrl()).collect(Collectors.joining("\n")) : ""));
} catch (
Exception e) {
TBMCCoreAPI.SendException("An error occured while handling " + event.getMessage().getContent() + "!", e);
return;
}
}
}