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