Many fixes and debug messages

Mainly game roles
Mostly debug messages
This commit is contained in:
Norbi Peti 2018-06-06 23:22:16 +02:00
parent 0497d6c8ac
commit b60c4f5090
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 107 additions and 73 deletions

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin;
import org.bukkit.Bukkit;
import sx.blah.discord.util.EmbedBuilder;
import sx.blah.discord.util.RequestBuffer;
import sx.blah.discord.util.RequestBuffer.IRequest;
@ -34,10 +35,13 @@ public final class DPUtils {
* Performs Discord actions, retrying when ratelimited. May return null if action fails too many times or in safe mode.
*/
public static <T> T perform(IRequest<T> action) {
System.out.println("performA");
if (DiscordPlugin.SafeMode)
return null;
// if (Thread.currentThread() == DiscordPlugin.mainThread) - TODO: Ignore shutdown message <--
System.out.println("performB");
if (Thread.currentThread() == DiscordPlugin.mainThread) // TODO: Ignore shutdown message <--
// throw new RuntimeException("Tried to wait for a Discord request on the main thread. This could cause lag.");
Bukkit.getLogger().warning("Waiting for a Discord request on the main thread!");
return RequestBuffer.request(action).get(); // Let the pros handle this
}

View file

@ -132,7 +132,11 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
if (!sent) {
new ChromaBot(this).updatePlayerList();
//Get all roles with the default color
GameRoles = mainServer.getRoles().stream().filter(r -> r.getColor().getAlpha() == 0).map(IRole::getName).collect(Collectors.toList());
/*GameRoles = mainServer.getRoles().stream().filter(r -> {
System.out.println(r.getName()+" - "+r.getColor().toString()+" "+r.getColor().getAlpha());
return r.getColor().getAlpha() == 0;
}).map(IRole::getName).collect(Collectors.toList()); //r=149,g=165,b=166*/
GameRoles = mainServer.getRoles().stream().filter(this::isGameRole).map(IRole::getName).collect(Collectors.toList());
DiscordCommandBase.registerCommands();
if (getConfig().getBoolean("serverup", false)) {
ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.YELLOW)
@ -204,6 +208,12 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
}
}
public boolean isGameRole(IRole r) {
val rc = new Color(149, 165, 166, 0);
return r.getColor().equals(rc)
&& r.getPosition() < mainServer.getRoleByID(234343495735836672L).getPosition(); //Below the ChromaBot role
}
/**
* Always true, except when running "stop" from console
*/
@ -212,12 +222,16 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
@Override
public void onDisable() {
stop = true;
System.out.println("X");
for (val entry : MCChatListener.ConnectedSenders.entrySet())
MCListener.callEventExcludingSome(new PlayerQuitEvent(entry.getValue(), ""));
System.out.println("Y");
getConfig().set("lastannouncementtime", lastannouncementtime);
getConfig().set("lastseentime", lastseentime);
getConfig().set("serverup", false);
System.out.println("Z");
saveConfig();
System.out.println("XX");
MCChatListener.forAllMCChat(ch -> DiscordPlugin.sendMessageToChannelWait(ch, "",
new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED)
.withTitle(Restart ? "Server restarting" : "Server stopping")
@ -230,13 +244,19 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
+ "asked *politely* to leave the server for a bit.")
: "")
.build()));
System.out.println("XY");
ChromaBot.getInstance().updatePlayerList();
try {
System.out.println("XZ");
SafeMode = true; // Stop interacting with Discord
MCChatListener.stop();
System.out.println("YX");
ChromaBot.delete();
System.out.println("YY");
dc.changePresence(StatusType.IDLE, ActivityType.PLAYING, "Chromacraft"); //No longer using the same account for testing
System.out.println("YZ");
dc.logout();
System.out.println("ZX");
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while disabling DiscordPlugin!", e);
}
@ -325,6 +345,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
}
public static IMessage sendMessageToChannelWait(IChannel channel, String message, EmbedObject embed) {
System.out.println("lol");
return sendMessageToChannel(channel, message, embed, true);
}
@ -335,13 +356,16 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
.warning("Message was too long to send to discord and got truncated. In " + channel.getName());
}
try {
System.out.println("sendA");
if (channel == chatchannel)
MCChatListener.resetLastMessage(); // If this is a chat message, it'll be set again
else if (channel.isPrivate())
MCChatListener.resetLastMessage(channel);
System.out.println("sendB");
final String content = message;
RequestBuffer.IRequest<IMessage> r = () -> embed == null ? channel.sendMessage(content)
: channel.sendMessage(content, embed, false);
System.out.println("sendC");
if (wait)
return DPUtils.perform(r);
else {

View file

@ -11,81 +11,83 @@ import java.util.stream.Collectors;
public class RoleCommand extends DiscordCommandBase {
@Override
public String getCommandName() {
return "role";
}
@Override
public String getCommandName() {
return "role";
}
@Override
@Override
public boolean run(IMessage message, String args) {
if (args.length() == 0)
return false;
String[] argsa = splitargs(args);
if (argsa[0].equalsIgnoreCase("add")) {
final IRole role = checkAndGetRole(message, argsa, "This command adds a game role to your account.");
if (role == null)
if (argsa[0].equalsIgnoreCase("add")) {
final IRole role = checkAndGetRole(message, argsa, "This command adds a game role to your account.");
if (role == null)
return true;
try {
DPUtils.perform(() -> message.getAuthor().addRole(role));
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Added game role.");
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while adding role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while adding the role.");
}
} else if (argsa[0].equalsIgnoreCase("remove")) {
final IRole role = checkAndGetRole(message, argsa, "This command removes a game role from your account.");
if (role == null)
try {
DPUtils.perform(() -> message.getAuthor().addRole(role));
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Added game role.");
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while adding role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while adding the role.");
}
} else if (argsa[0].equalsIgnoreCase("remove")) {
final IRole role = checkAndGetRole(message, argsa, "This command removes a game role from your account.");
if (role == null)
return true;
try {
DPUtils.perform(() -> message.getAuthor().removeRole(role));
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Removed game role.");
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while removing role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while removing the role.");
}
} else if (argsa[0].equalsIgnoreCase("list")) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"List of game roles:\n" + DiscordPlugin.GameRoles.stream().sorted().collect(Collectors.joining("\n")));
try {
DPUtils.perform(() -> message.getAuthor().removeRole(role));
DiscordPlugin.sendMessageToChannel(message.getChannel(), "Removed game role.");
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while removing role!", e);
DiscordPlugin.sendMessageToChannel(message.getChannel(), "An error occured while removing the role.");
}
} else if (argsa[0].equalsIgnoreCase("list")) {
listRoles(message);
} else return false;
return true;
}
}
private IRole checkAndGetRole(IMessage message, String[] argsa, String usage) {
if (argsa.length < 2) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " <rolename>");
return null;
}
String rolename = argsa[1];
for (int i = 2; i < argsa.length; i++)
rolename += " " + argsa[i];
if (!DiscordPlugin.GameRoles.contains(rolename)) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"That game role cannot be found.\nList of game roles:\n"
+ DiscordPlugin.GameRoles.stream().collect(Collectors.joining("\n")));
return null;
}
final List<IRole> roles = (TBMCCoreAPI.IsTestServer() ? DiscordPlugin.devServer : DiscordPlugin.mainServer)
.getRolesByName(rolename);
if (roles.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The specified role cannot be found on Discord! Removing from the list.");
DiscordPlugin.GameRoles.remove(rolename);
return null;
}
if (roles.size() > 1) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"There are more roles with this name. Why are there more roles with this name?");
return null;
}
return roles.get(0);
}
private void listRoles(IMessage message) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"List of game roles:\n" + DiscordPlugin.GameRoles.stream().sorted().collect(Collectors.joining("\n")));
}
@Override
public String[] getHelpText() {
return new String[] { //
"Add or remove game roles from yourself.", //
"Usage: role add|remove <name> or role list", //
private IRole checkAndGetRole(IMessage message, String[] argsa, String usage) {
if (argsa.length < 2) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), usage + "\nUsage: " + argsa[0] + " <rolename>");
return null;
}
String rolename = argsa[1];
for (int i = 2; i < argsa.length; i++)
rolename += " " + argsa[i];
if (!DiscordPlugin.GameRoles.contains(rolename)) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), "That game role cannot be found.");
listRoles(message);
return null;
}
final List<IRole> roles = DiscordPlugin.mainServer.getRolesByName(rolename);
if (roles.size() == 0) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"The specified role cannot be found on Discord! Removing from the list.");
DiscordPlugin.GameRoles.remove(rolename);
return null;
}
if (roles.size() > 1) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"There are more roles with this name. Why are there more roles with this name?");
return null;
}
return roles.get(0);
}
@Override
public String[] getHelpText() {
return new String[]{ //
"Add or remove game roles from yourself.", //
"Usage: role add|remove <name> or role list", //
};
}
}
}

View file

@ -127,7 +127,7 @@ public class CommandListener {
}
}, (IListener<RoleCreateEvent>) event -> {
Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, () -> {
if (event.getRole().isDeleted() || event.getRole().getColor().getAlpha() != 0)
if (event.getRole().isDeleted() || DiscordPlugin.plugin.isGameRole(event.getRole()))
return; //Deleted or not a game role
DiscordPlugin.GameRoles.add(event.getRole().getName());
DiscordPlugin.sendMessageToChannel(DiscordPlugin.modlogchannel, "Added " + event.getRole().getName() + " as game role. If you don't want this, change the role's color from the default.");
@ -136,10 +136,10 @@ public class CommandListener {
if (DiscordPlugin.GameRoles.remove(event.getRole().getName()))
DiscordPlugin.sendMessageToChannel(DiscordPlugin.modlogchannel, "Removed " + event.getRole().getName() + " as a game role.");
}, (IListener<RoleUpdateEvent>) event -> { //Role update event
if (event.getNewRole().getColor().getAlpha() != 0)
if (!DiscordPlugin.plugin.isGameRole(event.getNewRole())) {
if (DiscordPlugin.GameRoles.remove(event.getOldRole().getName()))
DiscordPlugin.sendMessageToChannel(DiscordPlugin.modlogchannel, "Removed " + event.getOldRole().getName() + " as a game role because it's color changed.");
else {
DiscordPlugin.sendMessageToChannel(DiscordPlugin.modlogchannel, "Removed " + event.getOldRole().getName() + " as a game role because it's color changed.");
} else {
boolean removed = DiscordPlugin.GameRoles.remove(event.getOldRole().getName()); //Regardless of whether it was a game role
DiscordPlugin.GameRoles.add(event.getNewRole().getName()); //Add it because it has no color
if (removed)

View file

@ -275,9 +275,12 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
}
public static void forAllMCChat(Consumer<IChannel> action) {
System.out.println("XA");
action.accept(DiscordPlugin.chatchannel);
System.out.println("XB");
for (LastMsgData data : lastmsgPerUser)
action.accept(data.channel);
System.out.println("XC");
}
private static void forAllowedMCChat(Consumer<IChannel> action, TBMCSystemChatEvent event) {
@ -390,14 +393,15 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
int spi = cmd.indexOf(' ');
final String topcmd = spi == -1 ? cmd : cmd.substring(0, spi);
Optional<Channel> ch = Channel.getChannels().stream()
.filter(c -> c.ID.equalsIgnoreCase(topcmd)).findAny();
.filter(c -> c.ID.equalsIgnoreCase(topcmd)
|| (c.IDs != null && c.IDs.length > 0
&& Arrays.stream(c.IDs).noneMatch(id -> id.equalsIgnoreCase(topcmd)))).findAny();
if (!ch.isPresent())
Bukkit.getScheduler().runTask(DiscordPlugin.plugin,
() -> VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd));
else {
Channel chc = ch.get();
if (!chc.ID.equals(Channel.GlobalChat.ID)
&& !event.getMessage().getChannel().isPrivate())
if (!chc.ID.equals(Channel.GlobalChat.ID) && !event.getMessage().getChannel().isPrivate())
dsender.sendMessage(
"You can only talk in global in the public chat. DM `mcchat` to enable private chat to talk in the other channels.");
else {