Added mcchat (#12), needs testing

This commit is contained in:
Norbi Peti 2017-05-24 13:37:12 +02:00
parent adb0144f91
commit 914cd60694
4 changed files with 55 additions and 8 deletions

View file

@ -1,6 +1,7 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.PlayerData;
import buttondevteam.lib.player.UserClass; import buttondevteam.lib.player.UserClass;
@UserClass(foldername = "discord") @UserClass(foldername = "discord")
@ -15,4 +16,8 @@ public class DiscordPlayer extends ChromaGamerBase {
did = plugindata.getString(getFolder() + "_id"); did = plugindata.getString(getFolder() + "_id");
return did; return did;
} }
public PlayerData<Boolean> minecraftChat() {
return data();
}
} }

View file

@ -4,7 +4,10 @@ import java.awt.Color;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Random;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -135,6 +138,16 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
setupProviders(); setupProviders();
TBMCCoreAPI.SendUnsentExceptions(); TBMCCoreAPI.SendUnsentExceptions();
TBMCCoreAPI.SendUnsentDebugMessages(); TBMCCoreAPI.SendUnsentDebugMessages();
final Calendar currentCal = Calendar.getInstance();
final Calendar newCal = Calendar.getInstance();
currentCal.set(currentCal.get(Calendar.YEAR), currentCal.get(Calendar.MONTH),
currentCal.get(Calendar.DAY_OF_MONTH), 4, 10);
if (currentCal.get(Calendar.DAY_OF_MONTH) % 9 == 0 && currentCal.before(newCal)) {
Random rand = new Random();
sendMessageToChannel(dc.getChannels().get(rand.nextInt(dc.getChannels().size())),
"You could make a religion out of this");
}
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while enabling DiscordPlugin!", e); TBMCCoreAPI.SendException("An error occured while enabling DiscordPlugin!", e);
} }

View file

@ -0,0 +1,34 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.lib.player.PlayerData;
import sx.blah.discord.handle.obj.IMessage;
public class MCChatCommand extends DiscordCommandBase {
@Override
public String getCommandName() {
return "mcchat";
}
@Override
public void run(IMessage message, String args) {
if (!message.getChannel().isPrivate()) {
message.reply("This command can only be issued while DMing the bot.");
return;
}
PlayerData<Boolean> mcchat = DiscordPlayer.getUser(message.getAuthor().getStringID(), DiscordPlayer.class)
.minecraftChat();
mcchat.set(!mcchat.get());
message.reply("Minecraft chat " + (mcchat.get() ? "enabled." : "disabled."));
}
@Override
public String[] getHelpText() {
return new String[] { //
"mcchat enables or disables the Minecraft chat in private messages.", //
"It can be useful if you don't want your messages to be visible, for example when talking a private channel." //
}; // TODO: Pin channel switching to indicate the current channel
}
}

View file

@ -59,21 +59,15 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
@EventHandler @EventHandler
public void onChatPreprocess(TBMCChatPreprocessEvent event) { public void onChatPreprocess(TBMCChatPreprocessEvent event) {
int start = -1; int start = -1;
// System.out.println("A");
while ((start = event.getMessage().indexOf('@', start + 1)) != -1) { while ((start = event.getMessage().indexOf('@', start + 1)) != -1) {
// System.out.println("Start: " + start);
int mid = event.getMessage().indexOf('#', start + 1); int mid = event.getMessage().indexOf('#', start + 1);
// System.out.println("Mid: " + mid);
if (mid == -1) if (mid == -1)
return; return;
int end_ = event.getMessage().indexOf(' ', mid + 1); int end_ = event.getMessage().indexOf(' ', mid + 1);
// System.out.println("End: " + end_);
if (end_ == -1) if (end_ == -1)
end_ = event.getMessage().length(); end_ = event.getMessage().length();
final int end = end_; final int end = end_;
final int startF = start; final int startF = start;
// System.out.println("Name: " + event.getMessage().substring(start, mid));
// System.out.println("Disc: " + event.getMessage().substring(mid, end));
DiscordPlugin.dc.getUsersByName(event.getMessage().substring(start + 1, mid)).stream() DiscordPlugin.dc.getUsersByName(event.getMessage().substring(start + 1, mid)).stream()
.filter(u -> u.getDiscriminator().equals(event.getMessage().substring(mid + 1, end))).findAny() .filter(u -> u.getDiscriminator().equals(event.getMessage().substring(mid + 1, end))).findAny()
.ifPresent(user -> event.setMessage(event.getMessage().substring(0, startF) + "@" + user.getName() .ifPresent(user -> event.setMessage(event.getMessage().substring(0, startF) + "@" + user.getName()
@ -104,7 +98,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
public void handle(sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent event) { public void handle(sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent event) {
final IUser author = event.getMessage().getAuthor(); final IUser author = event.getMessage().getAuthor();
if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID()) if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID())
/* && !(event.getMessage().getChannel().isPrivate() && privatechat) */) && !(event.getMessage().getChannel().isPrivate()
&& DiscordPlayer.getUser(author.getStringID(), DiscordPlayer.class).minecraftChat().get()))
return; return;
lastmessage = null; lastmessage = null;
lastlist++; lastlist++;
@ -117,7 +112,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
try { try {
DiscordPlayer dp = ChromaGamerBase.getUser(author.getStringID(), DiscordPlayer.class); DiscordPlayer dp = ChromaGamerBase.getUser(author.getStringID(), DiscordPlayer.class);
final DiscordSenderBase dsender; final DiscordSenderBase dsender;
Player mcp = null; // Offline players can't really run commands, or can they? Player mcp = null; // Offline players can't really run commands, or can they? No, they can't, really.
final String cid; final String cid;
if ((cid = dp.getConnectedID(TBMCPlayer.class)) != null // Connected? if ((cid = dp.getConnectedID(TBMCPlayer.class)) != null // Connected?
&& (mcp = Bukkit.getPlayer(UUID.fromString(cid))) != null) { // Execute as ingame player && (mcp = Bukkit.getPlayer(UUID.fromString(cid))) != null) { // Execute as ingame player