Fixes and improvements (config)

Fixed occasional ConcurrentModificationException with the custom chat loading
Waiting much less on MC command responses (0.2s instead of 0.5s)
Made the #bot channel configurable
Linking the #bot channel everywhere it's mentioned
#51
This commit is contained in:
Norbi Peti 2019-01-10 01:45:45 +01:00
parent f13c8321cd
commit 93af5f66d2
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
11 changed files with 710 additions and 664 deletions

19
.editorconfig Normal file
View file

@ -0,0 +1,19 @@
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = false
indent_style = space
indent_size = 4
[*.json]
indent_style = space
indent_size = 2
[*.java]
indent_style = tab
tab_width = 4
[{*.yml, *.yaml}]
indent_style = space
indent_size = 2

View file

@ -1,6 +1,10 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.architecture.IHaveConfig;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IIDLinkedObject;
import sx.blah.discord.util.EmbedBuilder; import sx.blah.discord.util.EmbedBuilder;
import sx.blah.discord.util.RequestBuffer; import sx.blah.discord.util.RequestBuffer;
import sx.blah.discord.util.RequestBuffer.IRequest; import sx.blah.discord.util.RequestBuffer.IRequest;
@ -103,4 +107,18 @@ public final class DPUtils {
return DiscordPlugin.plugin.getLogger(); return DiscordPlugin.plugin.getLogger();
} }
public static ConfigData<IChannel> channelData(IHaveConfig config, String key, long defID) {
return config.getDataPrimDef(key, defID, id -> DiscordPlugin.dc.getChannelByID((long) id), IIDLinkedObject::getLongID); //We can afford to search for the channel in the cache once (instead of using mainServer)
}
/**
* Mentions the <b>bot channel</b>. Useful for help texts.
*
* @return The string for mentioning the channel
*/
public static String botmention() {
if (DiscordPlugin.plugin == null) return "#bot";
return DiscordPlugin.plugin.CommandChannel().get().mention();
}
} }

View file

@ -55,7 +55,7 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
public static List<String> GameRoles; public static List<String> GameRoles;
public ConfigData<Character> Prefix() { public ConfigData<Character> Prefix() {
return getData("prefix", '/'); return getIConfig().getData("prefix", '/', str -> ((String) str).charAt(0), Object::toString);
} }
public static char getPrefix() { public static char getPrefix() {
@ -63,6 +63,14 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
return plugin.Prefix().get(); return plugin.Prefix().get();
} }
public ConfigData<IGuild> MainServer() {
return getIConfig().getDataPrimDef("mainServer", 219529124321034241L, id -> dc.getGuildByID((long) id), IIDLinkedObject::getLongID);
}
public ConfigData<IChannel> CommandChannel() {
return DPUtils.channelData(getIConfig(), "commandChannel", 239519012529111040L);
}
@Override @Override
public void pluginEnable() { public void pluginEnable() {
stop = false; //If not the first time stop = false; //If not the first time
@ -81,7 +89,7 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
} }
} }
public static IChannel botchannel; public static IChannel botchannel; //Can be removed
public static IChannel annchannel; public static IChannel annchannel;
public static IChannel genchannel; public static IChannel genchannel;
public static IChannel chatchannel; public static IChannel chatchannel;
@ -152,12 +160,14 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
val ch = dc.getChannelByID(chcon.getLong("chid")); val ch = dc.getChannelByID(chcon.getLong("chid"));
val did = chcon.getLong("did"); val did = chcon.getLong("did");
val user = dc.fetchUser(did); val user = dc.fetchUser(did);
val dcp = new DiscordConnectedPlayer(user, ch, UUID.fromString(chcon.getString("mcuid")), chcon.getString("mcname"));
val groupid = chcon.getString("groupid"); val groupid = chcon.getString("groupid");
val toggles = chcon.getInt("toggles"); val toggles = chcon.getInt("toggles");
if (!mcch.isPresent() || ch == null || user == null || groupid == null) if (!mcch.isPresent() || ch == null || user == null || groupid == null)
continue; continue;
Bukkit.getScheduler().runTask(this, () -> { //<-- Needed because of occasional ConcurrentModificationExceptions when creating the player (PermissibleBase)
val dcp = new DiscordConnectedPlayer(user, ch, UUID.fromString(chcon.getString("mcuid")), chcon.getString("mcname"));
MCChatCustom.addCustomChat(ch, groupid, mcch.get(), user, dcp, toggles); MCChatCustom.addCustomChat(ch, groupid, mcch.get(), user, dcp, toggles);
});
} }
} }
@ -195,7 +205,7 @@ public class DiscordPlugin extends ButtonPlugin implements IListener<ReadyEvent>
TBMCCoreAPI.SendException( TBMCCoreAPI.SendException(
"Won't load because we're in testing mode and not using a separate account.", "Won't load because we're in testing mode and not using a separate account.",
new Exception( new Exception(
"The plugin refuses to load until you change the token to a testing account.")); "The plugin refuses to load until you change the token to a testing account. (The account needs to have \"test\" in it's name.)"));
Bukkit.getPluginManager().disablePlugin(this); Bukkit.getPluginManager().disablePlugin(this);
} }
TBMCCoreAPI.SendUnsentExceptions(); TBMCCoreAPI.SendUnsentExceptions();

View file

@ -62,7 +62,7 @@ public abstract class DiscordSenderBase implements CommandSender {
(!broadcast && user != null ? user.mention() + "\n" : "") + msgtosend.trim()); (!broadcast && user != null ? user.mention() + "\n" : "") + msgtosend.trim());
sendtask = null; sendtask = null;
msgtosend = ""; msgtosend = "";
}, 10); // Waits a half second to gather all/most of the different messages }, 4); // Waits a 0.2 second to gather all/most of the different messages
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while sending message to DiscordSender", e); TBMCCoreAPI.SendException("An error occured while sending message to DiscordSender", e);
} }

View file

@ -1,9 +1,6 @@
package buttondevteam.discordplugin.commands; package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.ChannelconBroadcast; import buttondevteam.discordplugin.*;
import buttondevteam.discordplugin.DiscordConnectedPlayer;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.mcchat.MCChatCustom; import buttondevteam.discordplugin.mcchat.MCChatCustom;
import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
@ -75,7 +72,7 @@ public class ChannelconCommand extends DiscordCommandBase {
val dp = DiscordPlayer.getUser(message.getAuthor().getStringID(), DiscordPlayer.class); val dp = DiscordPlayer.getUser(message.getAuthor().getStringID(), DiscordPlayer.class);
val chp = dp.getAs(TBMCPlayer.class); val chp = dp.getAs(TBMCPlayer.class);
if (chp == null) { if (chp == null) {
message.reply("you need to connect your Minecraft account. On our server in #bot do /connect <MCname>"); message.reply("you need to connect your Minecraft account. On our server in " + DPUtils.botmention() + " do " + DiscordPlugin.getPrefix() + "connect <MCname>");
return true; return true;
} }
DiscordConnectedPlayer dcp = new DiscordConnectedPlayer(message.getAuthor(), message.getChannel(), chp.getUUID(), Bukkit.getOfflinePlayer(chp.getUUID()).getName()); DiscordConnectedPlayer dcp = new DiscordConnectedPlayer(message.getAuthor(), message.getChannel(), chp.getUUID(), Bukkit.getOfflinePlayer(chp.getUUID()).getName());
@ -100,12 +97,12 @@ public class ChannelconCommand extends DiscordCommandBase {
"---- Channel connect ---", // "---- Channel connect ---", //
"This command allows you to connect a Minecraft channel to a Discord channel (just like how the global chat is connected to #minecraft-chat).", // "This command allows you to connect a Minecraft channel to a Discord channel (just like how the global chat is connected to #minecraft-chat).", //
"You need to have access to the MC channel and have manage permissions on the Discord channel.", // "You need to have access to the MC channel and have manage permissions on the Discord channel.", //
"You also need to have your Minecraft account connected. In #bot use " + DiscordPlugin.getPrefix() + "connect <mcname>.", // "You also need to have your Minecraft account connected. In " + DPUtils.botmention() + " use " + DiscordPlugin.getPrefix() + "connect <mcname>.", //
"Call this command from the channel you want to use.", // "Call this command from the channel you want to use.", //
"Usage: @" + DiscordPlugin.dc.getOurUser().getName() + " channelcon <mcchannel>", // "Usage: @" + DiscordPlugin.dc.getOurUser().getName() + " channelcon <mcchannel>", //
"Use the ID (command) of the channel, for example `g` for the global chat.", // "Use the ID (command) of the channel, for example `g` for the global chat.", //
"To remove a connection use @ChromaBot channelcon remove in the channel.", // "To remove a connection use @ChromaBot channelcon remove in the channel.", //
"Mentioning the bot is needed in this case because the " + DiscordPlugin.getPrefix() + " prefix only works in #bot.", // "Mentioning the bot is needed in this case because the " + DiscordPlugin.getPrefix() + " prefix only works in " + DPUtils.botmention() + ".", //
"Invite link: <https://discordapp.com/oauth2/authorize?client_id=226443037893591041&scope=bot&permissions=268509264>" // "Invite link: <https://discordapp.com/oauth2/authorize?client_id=226443037893591041&scope=bot&permissions=268509264>" //
}; };
} }

View file

@ -32,11 +32,11 @@ public class FunModule extends Component {
}; };
private ConfigData<Boolean> serverReady() { private ConfigData<Boolean> serverReady() {
return getData("serverReady", true); return getConfig().getData("serverReady", true);
} }
private ConfigData<List<String>> serverReadyAnswers() { private ConfigData<List<String>> serverReadyAnswers() {
return getData("serverReadyAnswers", Arrays.asList(serverReadyStrings), return getConfig().getData("serverReadyAnswers", Arrays.asList(serverReadyStrings),
data -> (List<String>) data, data -> data); //TODO: Test data -> (List<String>) data, data -> data); //TODO: Test
} }

View file

@ -21,7 +21,7 @@ public class CommandListener {
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
if (!message.getChannel().isPrivate() if (!message.getChannel().isPrivate()
&& !(message.getContent().charAt(0) == DiscordPlugin.getPrefix() && !(message.getContent().charAt(0) == DiscordPlugin.getPrefix()
&& channel.getStringID().equals(DiscordPlugin.botchannel.getStringID()))) // && channel.getStringID().equals(DiscordPlugin.plugin.CommandChannel().get().getStringID()))) //
return false; return false;
message.getChannel().setTypingStatus(true); // Fun message.getChannel().setTypingStatus(true); // Fun
} }

View file

@ -56,7 +56,7 @@ public class CommonListeners {
if (event.getMessage().getAuthor().isBot()) if (event.getMessage().getAuthor().isBot())
return; return;
boolean handled = false; boolean handled = false;
if (event.getChannel().getLongID() == DiscordPlugin.botchannel.getLongID() //If mentioned, that's higher than chat if (event.getChannel().getLongID() == DiscordPlugin.plugin.CommandChannel().get().getLongID() //If mentioned, that's higher than chat
|| event.getMessage().getContent().contains("channelcon")) //Only 'channelcon' is allowed in other channels || event.getMessage().getContent().contains("channelcon")) //Only 'channelcon' is allowed in other channels
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here
if (handled) return; if (handled) return;

View file

@ -313,7 +313,7 @@ public class MCChatListener implements Listener {
.collect(Collectors.joining(", ")) .collect(Collectors.joining(", "))
+ (user.getConnectedID(TBMCPlayer.class) == null + (user.getConnectedID(TBMCPlayer.class) == null
? "\nTo access your commands, first please connect your accounts, using /connect in " ? "\nTo access your commands, first please connect your accounts, using /connect in "
+ DiscordPlugin.botchannel.mention() + DPUtils.botmention()
+ "\nThen y" + "\nThen y"
: "\nY") : "\nY")
+ "ou can access all of your regular commands (even offline) in private chat: DM me `mcchat`!"); + "ou can access all of your regular commands (even offline) in private chat: DM me `mcchat`!");

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin.mccommands; package buttondevteam.discordplugin.mccommands;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlayer; import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.commands.ConnectCommand; import buttondevteam.discordplugin.commands.ConnectCommand;
import buttondevteam.discordplugin.mcchat.MCChatUtils; import buttondevteam.discordplugin.mcchat.MCChatUtils;
@ -17,7 +18,7 @@ public class AcceptMCCommand extends DiscordMCCommandBase {
return new String[] { // return new String[] { //
"§6---- Accept Discord connection ----", // "§6---- Accept Discord connection ----", //
"Accept a pending connection between your Discord and Minecraft account.", // "Accept a pending connection between your Discord and Minecraft account.", //
"To start the connection process, do §b/connect <MCname>§r in the #bot channel on Discord", // "To start the connection process, do §b/connect <MCname>§r in the " + DPUtils.botmention() + " channel on Discord", //
"Usage: /" + alias + " accept" // "Usage: /" + alias + " accept" //
}; };
} }

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin.mccommands; package buttondevteam.discordplugin.mccommands;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.commands.ConnectCommand; import buttondevteam.discordplugin.commands.ConnectCommand;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -12,7 +13,7 @@ public class DeclineMCCommand extends DiscordMCCommandBase {
return new String[] { // return new String[] { //
"§6---- Decline Discord connection ----", // "§6---- Decline Discord connection ----", //
"Decline a pending connection between your Discord and Minecraft account.", // "Decline a pending connection between your Discord and Minecraft account.", //
"To start the connection process, do §b/connect <MCname>§r in the #bot channel on Discord", // "To start the connection process, do §b/connect <MCname>§r in the " + DPUtils.botmention() + " channel on Discord", //
"Usage: /" + alias + " decline" // "Usage: /" + alias + " decline" //
}; };
} }