Channel IDs shown always + more

* Server restart/stop now sends to PMs
* Special handling of players kicked because of shutdown/restart
* Enter safe mode on disable so actions won't be attempted to be
performed anymore
* Channel ID relocation in PMs also has the effect of showing the actual
message in notifications
This commit is contained in:
Norbi Peti 2017-12-07 22:01:05 +01:00
parent faa1c96a4b
commit 6027315d09
5 changed files with 33 additions and 15 deletions

View file

@ -36,8 +36,8 @@ public final class DPUtils {
public static <T> T perform(IRequest<T> action) { public static <T> T perform(IRequest<T> action) {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)
return null; return null;
if (Thread.currentThread() == DiscordPlugin.mainThread) // 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."); // throw new RuntimeException("Tried to wait for a Discord request on the main thread. This could cause lag.");
return RequestBuffer.request(action).get(); // Let the pros handle this return RequestBuffer.request(action).get(); // Let the pros handle this
} }

View file

@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.stream.Collectors;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
@ -125,15 +126,16 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
if (task != null) if (task != null)
task.cancel(); task.cancel();
if (!sent) { if (!sent) {
new ChromaBot(this).updatePlayerList();
if (getConfig().getBoolean("serverup", false)) { if (getConfig().getBoolean("serverup", false)) {
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Color.YELLOW) ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.YELLOW)
.withTitle("Server recovered from a crash - chat connected.").build()); .withTitle("Server recovered from a crash - chat connected.").build());
val thr = new Throwable( val thr = new Throwable(
"The server shut down unexpectedly. See the log of the previous run for more details."); "The server shut down unexpectedly. See the log of the previous run for more details.");
thr.setStackTrace(new StackTraceElement[0]); thr.setStackTrace(new StackTraceElement[0]);
TBMCCoreAPI.SendException("The server crashed!", thr); TBMCCoreAPI.SendException("The server crashed!", thr);
} else } else
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Color.GREEN) ChromaBot.getInstance().sendMessage("", new EmbedBuilder().withColor(Color.GREEN)
.withTitle("Server started - chat connected.").build()); .withTitle("Server started - chat connected.").build());
getConfig().set("serverup", true); getConfig().set("serverup", true);
saveConfig(); saveConfig();
@ -168,7 +170,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
"You could make a religion out of this"); "You could make a religion out of this");
} }
} }
new ChromaBot(this).updatePlayerList();
} }
}, 0, 10); }, 0, 10);
for (IListener<?> listener : CommandListener.getListeners()) for (IListener<?> listener : CommandListener.getListeners())
@ -211,9 +212,20 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
getConfig().set("gameroles", GameRoles); getConfig().set("gameroles", GameRoles);
getConfig().set("serverup", false); getConfig().set("serverup", false);
saveConfig(); saveConfig();
sendMessageToChannel(chatchannel, "", new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED) MCChatListener.forAllMCChat(ch -> DiscordPlugin.sendMessageToChannelWait(ch, "",
.withTitle(Restart ? "Server restarting" : "Server stopping").build()); new EmbedBuilder().withColor(Restart ? Color.ORANGE : Color.RED)
.withTitle(Restart ? "Server restarting" : "Server stopping")
.withDescription(
Bukkit.getOnlinePlayers().size() > 0
? (DPUtils
.sanitizeString(Bukkit.getOnlinePlayers().stream()
.map(p -> p.getDisplayName()).collect(Collectors.joining(", ")))
+ (Bukkit.getOnlinePlayers().size() == 1 ? " was " : " were ")
+ "asked *politely* to leave the server for a bit.")
: "")
.build()));
try { try {
SafeMode = true; // Stop interacting with Discord
ChromaBot.delete(); ChromaBot.delete();
dc.online("on TBMC"); dc.online("on TBMC");
dc.logout(); dc.logout();

View file

@ -54,17 +54,17 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
+ ((DiscordSenderBase) e.getSender()).getUser().getStringID()); // TODO: Constant/method to get URLs like this + ((DiscordSenderBase) e.getSender()).getUser().getStringID()); // TODO: Constant/method to get URLs like this
if (e.getSender() instanceof DiscordSenderBase) if (e.getSender() instanceof DiscordSenderBase)
embed.withAuthorName("[D]" + authorPlayer); embed.withAuthorName("[D]" + authorPlayer);
// embed.withFooterText(e.getChannel().DisplayName);
final long nanoTime = System.nanoTime(); final long nanoTime = System.nanoTime();
Consumer<LastMsgData> doit = lastmsgdata -> { Consumer<LastMsgData> doit = lastmsgdata -> {
final EmbedObject embedObject = embed.build(); final EmbedObject embedObject = embed.build();
final String dmsg = lastmsgdata.channel.isPrivate() embedObject.author.name = "[" + DPUtils.sanitizeString(e.getChannel().DisplayName) + "] "
? DPUtils.sanitizeString(e.getChannel().DisplayName) + embedObject.author.name;
: "";
if (lastmsgdata.message == null || lastmsgdata.message.isDeleted() if (lastmsgdata.message == null || lastmsgdata.message.isDeleted()
|| !authorPlayer.equals(lastmsgdata.message.getEmbeds().get(0).getAuthor().getName()) || !authorPlayer.equals(lastmsgdata.message.getEmbeds().get(0).getAuthor().getName())
|| lastmsgdata.time / 1000000000f < nanoTime / 1000000000f - 120 || lastmsgdata.time / 1000000000f < nanoTime / 1000000000f - 120
|| !lastmsgdata.mcchannel.ID.equals(e.getChannel().ID)) { || !lastmsgdata.mcchannel.ID.equals(e.getChannel().ID)) {
lastmsgdata.message = DiscordPlugin.sendMessageToChannelWait(lastmsgdata.channel, dmsg, lastmsgdata.message = DiscordPlugin.sendMessageToChannelWait(lastmsgdata.channel, "",
embedObject); // TODO Use ChromaBot API embedObject); // TODO Use ChromaBot API
lastmsgdata.time = nanoTime; lastmsgdata.time = nanoTime;
lastmsgdata.mcchannel = e.getChannel(); lastmsgdata.mcchannel = e.getChannel();
@ -74,7 +74,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
lastmsgdata.content = embedObject.description = lastmsgdata.content + "\n" lastmsgdata.content = embedObject.description = lastmsgdata.content + "\n"
+ embedObject.description;// The message object doesn't get updated + embedObject.description;// The message object doesn't get updated
final LastMsgData _lastmsgdata = lastmsgdata; final LastMsgData _lastmsgdata = lastmsgdata;
DPUtils.perform(() -> _lastmsgdata.message.edit(dmsg, embedObject)); DPUtils.perform(() -> _lastmsgdata.message.edit("", embedObject));
} catch (MissingPermissionsException | DiscordException e1) { } catch (MissingPermissionsException | DiscordException e1) {
TBMCCoreAPI.SendException("An error occured while editing chat message!", e1); TBMCCoreAPI.SendException("An error occured while editing chat message!", e1);
} }

View file

@ -12,6 +12,7 @@ import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result; import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
@ -86,12 +87,17 @@ public class MCListener implements Listener {
() -> MCChatListener.ConnectedSenders.values().stream() () -> MCChatListener.ConnectedSenders.values().stream()
.filter(s -> s.getUniqueId().equals(e.getPlayer().getUniqueId())).findAny() .filter(s -> s.getUniqueId().equals(e.getPlayer().getUniqueId())).findAny()
.ifPresent(dcp -> callEventExcludingSome(new PlayerJoinEvent(dcp, "")))); .ifPresent(dcp -> callEventExcludingSome(new PlayerJoinEvent(dcp, ""))));
if (!DiscordPlugin.hooked)
MCChatListener.sendSystemMessageToChat(e.GetPlayer().PlayerName().get() + " left the game");
Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin,
ChromaBot.getInstance()::updatePlayerList, 5); ChromaBot.getInstance()::updatePlayerList, 5);
} }
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerKick(PlayerKickEvent e) {
if (!DiscordPlugin.hooked && !e.getReason().equals("The server is restarting")
&& !e.getReason().equals("Server closed")) // The leave messages errored with the previous setup, I could make it wait since I moved it here, but instead I have a special
MCChatListener.sendSystemMessageToChat(e.getPlayer().getName() + " left the game"); // message for this - Oh wait this doesn't even send normally because of the hook
}
@EventHandler @EventHandler
public void onGetInfo(TBMCPlayerGetInfoEvent e) { public void onGetInfo(TBMCPlayerGetInfoEvent e) {
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)

View file

@ -67,7 +67,7 @@ public abstract class DiscordLivingEntity extends DiscordEntity implements Livin
} }
@Override @Override
public AttributeInstance getAttribute(Attribute attribute) { // We don't support any attribute public AttributeInstance getAttribute(Attribute attribute) { // We don't support any attributes
return null; return null;
} }