Fixes, error handling

Sync events
Vanilla command error handling
(Older changes ^)
Fixed exception Coder pinging
This commit is contained in:
Norbi Peti 2019-06-30 22:05:33 +02:00
parent 0e24344efd
commit 4082c2abbf
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 39 additions and 26 deletions

View file

@ -19,7 +19,11 @@ public class DiscordConnectedPlayer extends DiscordFakePlayer implements IMCPlay
public DiscordConnectedPlayer(User user, MessageChannel channel, UUID uuid, String mcname, MinecraftChatModule module) { public DiscordConnectedPlayer(User user, MessageChannel channel, UUID uuid, String mcname, MinecraftChatModule module) {
super(user, channel, nextEntityId++, uuid, mcname, module); super(user, channel, nextEntityId++, uuid, mcname, module);
vanillaCmdListener = new VanillaCommandListener<>(this); try {
vanillaCmdListener = new VanillaCommandListener<>(this);
} catch (NoClassDefFoundError e) {
DPUtils.getLogger().warning("Vanilla commands won't be available from Discord due to a compatibility error.");
}
} }
} }

View file

@ -41,7 +41,11 @@ public class DiscordPlayerSender extends DiscordSenderBase implements IMCPlayer<
public DiscordPlayerSender(User user, MessageChannel channel, Player player) { public DiscordPlayerSender(User user, MessageChannel channel, Player player) {
super(user, channel); super(user, channel);
this.player = player; this.player = player;
vanillaCmdListener = new VanillaCommandListener<DiscordPlayerSender>(this); try {
vanillaCmdListener = new VanillaCommandListener<DiscordPlayerSender>(this);
} catch (NoClassDefFoundError e) {
DPUtils.getLogger().warning("Vanilla commands won't be available from Discord due to a compatibility error.");
}
} }
@Override @Override

View file

@ -50,28 +50,28 @@ public class ExceptionListenerModule extends Component<DiscordPlugin> implements
private static void SendException(Throwable e, String sourcemessage) { private static void SendException(Throwable e, String sourcemessage) {
if (instance == null) return; if (instance == null) return;
try { try {
Mono<MessageChannel> channel = getChannel(); getChannel().flatMap(channel -> {
assert channel != null; Mono<Role> coderRole;
Mono<Role> coderRole; if (channel instanceof GuildChannel)
if (channel instanceof GuildChannel) coderRole = instance.pingRole(((GuildChannel) channel).getGuild()).get();
coderRole = instance.pingRole(((GuildChannel) channel).getGuild()).get(); else
else coderRole = Mono.empty();
coderRole = Mono.empty(); return coderRole.map(role -> TBMCCoreAPI.IsTestServer() ? new StringBuilder()
coderRole.map(role -> TBMCCoreAPI.IsTestServer() ? new StringBuilder() : new StringBuilder(role.getMention()).append("\n"))
: new StringBuilder(role.getMention()).append("\n")) .defaultIfEmpty(new StringBuilder())
.defaultIfEmpty(new StringBuilder()) .flatMap(sb -> {
.flatMap(sb -> { sb.append(sourcemessage).append("\n");
sb.append(sourcemessage).append("\n"); sb.append("```").append("\n");
sb.append("```").append("\n"); String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n"))
String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n")) .filter(s -> !s.contains("\tat ") || s.contains("\tat buttondevteam."))
.filter(s -> !s.contains("\tat ") || s.contains("\tat buttondevteam.")) .collect(Collectors.joining("\n"));
.collect(Collectors.joining("\n")); if (sb.length() + stackTrace.length() >= 1980)
if (sb.length() + stackTrace.length() >= 1980) stackTrace = stackTrace.substring(0, 1980 - sb.length());
stackTrace = stackTrace.substring(0, 1980 - sb.length()); sb.append(stackTrace).append("\n");
sb.append(stackTrace).append("\n"); sb.append("```");
sb.append("```"); return channel.createMessage(sb.toString());
return channel.flatMap(ch -> ch.createMessage(sb.toString())); });
}).subscribe(); }).subscribe();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

View file

@ -321,7 +321,8 @@ public class MCChatListener implements Listener {
return; return;
} }
val ev = new TBMCCommandPreprocessEvent(dsender, dmessage); val ev = new TBMCCommandPreprocessEvent(dsender, dmessage);
Bukkit.getPluginManager().callEvent(ev); Bukkit.getScheduler().runTask(DiscordPlugin.plugin, () ->
Bukkit.getPluginManager().callEvent(ev));
if (ev.isCancelled()) if (ev.isCancelled())
return; return;
int spi = cmdlowercased.indexOf(' '); int spi = cmdlowercased.indexOf(' ');
@ -338,7 +339,11 @@ public class MCChatListener implements Listener {
if (clmd != null) { if (clmd != null) {
channel.set(clmd.mcchannel); //Hack to send command in the channel channel.set(clmd.mcchannel); //Hack to send command in the channel
} //TODO: Permcheck isn't implemented for commands } //TODO: Permcheck isn't implemented for commands
VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd); try {
VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd);
} catch (NoClassDefFoundError e) {
Bukkit.dispatchCommand(dsender, cmd);
}
Bukkit.getLogger().info(dsender.getName() + " issued command from Discord: /" + cmdlowercased); Bukkit.getLogger().info(dsender.getName() + " issued command from Discord: /" + cmdlowercased);
if (clmd != null) if (clmd != null)
channel.set(chtmp); channel.set(chtmp);