Component logging and toggle for vanilla cmds

Config option to enable or disable vanilla command support in mcchat
This commit is contained in:
Norbi Peti 2020-06-30 00:47:46 +02:00
parent ce71ff2dd6
commit 50500e87b9
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
9 changed files with 44 additions and 32 deletions

View file

@ -67,7 +67,7 @@ public abstract class DiscordConnectedPlayer extends DiscordSenderBase implement
this.module = module; this.module = module;
uniqueId = uuid; uniqueId = uuid;
displayName = mcname; displayName = mcname;
vanillaCmdListener = new VCMDWrapper(VCMDWrapper.createListener(this)); vanillaCmdListener = new VCMDWrapper(VCMDWrapper.createListener(this, module));
} }
/** /**

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.playerfaker.VCMDWrapper; import buttondevteam.discordplugin.playerfaker.VCMDWrapper;
import discord4j.core.object.entity.MessageChannel; import discord4j.core.object.entity.MessageChannel;
import discord4j.core.object.entity.User; import discord4j.core.object.entity.User;
@ -14,10 +15,10 @@ public abstract class DiscordPlayerSender extends DiscordSenderBase implements I
protected Player player; protected Player player;
private @Getter VCMDWrapper vanillaCmdListener; private @Getter VCMDWrapper vanillaCmdListener;
public DiscordPlayerSender(User user, MessageChannel channel, Player player) { public DiscordPlayerSender(User user, MessageChannel channel, Player player, MinecraftChatModule module) {
super(user, channel); super(user, channel);
this.player = player; this.player = player;
vanillaCmdListener = new VCMDWrapper(VCMDWrapper.createListener(this, player)); vanillaCmdListener = new VCMDWrapper(VCMDWrapper.createListener(this, player, module));
} }
@Override @Override
@ -32,11 +33,11 @@ public abstract class DiscordPlayerSender extends DiscordSenderBase implements I
super.sendMessage(messages); super.sendMessage(messages);
} }
public static DiscordPlayerSender create(User user, MessageChannel channel, Player player) { public static DiscordPlayerSender create(User user, MessageChannel channel, Player player, MinecraftChatModule module) {
return Mockito.mock(DiscordPlayerSender.class, Mockito.withSettings().stubOnly().defaultAnswer(invocation -> { return Mockito.mock(DiscordPlayerSender.class, Mockito.withSettings().stubOnly().defaultAnswer(invocation -> {
if (!Modifier.isAbstract(invocation.getMethod().getModifiers())) if (!Modifier.isAbstract(invocation.getMethod().getModifiers()))
return invocation.callRealMethod(); return invocation.callRealMethod();
return invocation.getMethod().invoke(((DiscordPlayerSender) invocation.getMock()).player, invocation.getArguments()); return invocation.getMethod().invoke(((DiscordPlayerSender) invocation.getMock()).player, invocation.getArguments());
}).useConstructor(user, channel, player)); }).useConstructor(user, channel, player, module));
} }
} }

View file

@ -1,6 +1,5 @@
package buttondevteam.discordplugin.broadcaster; package buttondevteam.discordplugin.broadcaster;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
@ -17,12 +16,12 @@ public class GeneralEventBroadcasterModule extends Component<DiscordPlugin> {
protected void enable() { protected void enable() {
try { try {
PlayerListWatcher.hookUpDown(true); PlayerListWatcher.hookUpDown(true);
DPUtils.getLogger().info("Finished hooking into the player list"); log("Finished hooking into the player list");
hooked = true; hooked = true;
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Error while hacking the player list! Disable this module if you're on an incompatible version.", e); TBMCCoreAPI.SendException("Error while hacking the player list! Disable this module if you're on an incompatible version.", e);
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
DPUtils.getLogger().warning("Error while hacking the player list! Disable this module if you're on an incompatible version."); logWarn("Error while hacking the player list! Disable this module if you're on an incompatible version.");
} }
} }
@ -32,9 +31,9 @@ public class GeneralEventBroadcasterModule extends Component<DiscordPlugin> {
try { try {
if (!hooked) return; if (!hooked) return;
if (PlayerListWatcher.hookUpDown(false)) if (PlayerListWatcher.hookUpDown(false))
DPUtils.getLogger().info("Finished unhooking the player list!"); log("Finished unhooking the player list!");
else else
DPUtils.getLogger().info("Didn't have the player list hooked."); log("Didn't have the player list hooked.");
hooked = false; hooked = false;
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Error while hacking the player list!", e); TBMCCoreAPI.SendException("Error while hacking the player list!", e);

View file

@ -38,7 +38,7 @@ import java.util.stream.Collectors;
public class MCChatListener implements Listener { public class MCChatListener implements Listener {
private BukkitTask sendtask; private BukkitTask sendtask;
private LinkedBlockingQueue<AbstractMap.SimpleEntry<TBMCChatEvent, Instant>> sendevents = new LinkedBlockingQueue<>(); private final LinkedBlockingQueue<AbstractMap.SimpleEntry<TBMCChatEvent, Instant>> sendevents = new LinkedBlockingQueue<>();
private Runnable sendrunnable; private Runnable sendrunnable;
private static Thread sendthread; private static Thread sendthread;
private final MinecraftChatModule module; private final MinecraftChatModule module;
@ -362,7 +362,7 @@ public class MCChatListener implements Listener {
+ "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`!");
return true; return true;
} }
Bukkit.getLogger().info(dsender.getName() + " issued command from Discord: /" + cmd); module.log(dsender.getName() + " ran from DC: /" + cmd);
val channel = clmd == null ? user.channel().get() : clmd.mcchannel; val channel = clmd == null ? user.channel().get() : clmd.mcchannel;
val ev = new TBMCCommandPreprocessEvent(dsender, channel, dmessage, clmd == null ? dsender : clmd.dcp); val ev = new TBMCCommandPreprocessEvent(dsender, channel, dmessage, clmd == null ? dsender : clmd.dcp);
Bukkit.getScheduler().runTask(DiscordPlugin.plugin, //Commands need to be run sync Bukkit.getScheduler().runTask(DiscordPlugin.plugin, //Commands need to be run sync
@ -372,7 +372,9 @@ public class MCChatListener implements Listener {
return; return;
try { try {
String mcpackage = Bukkit.getServer().getClass().getPackage().getName(); String mcpackage = Bukkit.getServer().getClass().getPackage().getName();
if (mcpackage.contains("1_12")) if (!module.enableVanillaCommands().get())
Bukkit.dispatchCommand(dsender, cmd);
else if (mcpackage.contains("1_12"))
VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd); VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd);
else if (mcpackage.contains("1_14")) else if (mcpackage.contains("1_14"))
VanillaCommandListener14.runBukkitOrVanillaCommand(dsender, cmd); VanillaCommandListener14.runBukkitOrVanillaCommand(dsender, cmd);

View file

@ -355,7 +355,8 @@ public class MCChatUtils {
} }
callEventExcludingSome(new PlayerJoinEvent(dcp, "")); callEventExcludingSome(new PlayerJoinEvent(dcp, ""));
dcp.setLoggedIn(true); dcp.setLoggedIn(true);
DPUtils.getLogger().info(dcp.getName() + " (" + dcp.getUniqueId() + ") logged in from Discord"); if (module != null)
module.log(dcp.getName() + " (" + dcp.getUniqueId() + ") logged in from Discord");
}); });
} }
@ -371,7 +372,8 @@ public class MCChatUtils {
if (needsSync) callEventSync(event); if (needsSync) callEventSync(event);
else callEventExcludingSome(event); else callEventExcludingSome(event);
dcp.setLoggedIn(false); dcp.setLoggedIn(false);
DPUtils.getLogger().info(dcp.getName() + " (" + dcp.getUniqueId() + ") logged out from Discord"); if (module != null)
module.log(dcp.getName() + " (" + dcp.getUniqueId() + ") logged out from Discord");
} }
static void callEventSync(Event event) { static void callEventSync(Event event) {

View file

@ -55,9 +55,9 @@ class MCListener implements Listener {
if (dp != null) { if (dp != null) {
DiscordPlugin.dc.getUserById(Snowflake.of(dp.getDiscordID())).flatMap(user -> user.getPrivateChannel().flatMap(chan -> module.chatChannelMono().flatMap(cc -> { DiscordPlugin.dc.getUserById(Snowflake.of(dp.getDiscordID())).flatMap(user -> user.getPrivateChannel().flatMap(chan -> module.chatChannelMono().flatMap(cc -> {
MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(), MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(),
DiscordPlayerSender.create(user, chan, p)); DiscordPlayerSender.create(user, chan, p, module));
MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(), MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(),
DiscordPlayerSender.create(user, cc, p)); //Stored per-channel DiscordPlayerSender.create(user, cc, p, module)); //Stored per-channel
return Mono.empty(); return Mono.empty();
}))).subscribe(); }))).subscribe();
} }
@ -130,7 +130,7 @@ class MCListener implements Listener {
user.removeRole(r.getId()); user.removeRole(r.getId());
val modlog = module.modlogChannel().get(); val modlog = module.modlogChannel().get();
String msg = (e.getValue() ? "M" : "Unm") + "uted user: " + user.getUsername() + "#" + user.getDiscriminator(); String msg = (e.getValue() ? "M" : "Unm") + "uted user: " + user.getUsername() + "#" + user.getDiscriminator();
DPUtils.getLogger().info(msg); module.log(msg);
if (modlog != null) if (modlog != null)
return modlog.flatMap(ch -> ch.createMessage(msg)); return modlog.flatMap(ch -> ch.createMessage(msg));
return Mono.empty(); return Mono.empty();

View file

@ -106,6 +106,13 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
return getConfig().getData("profileURL", ""); return getConfig().getData("profileURL", "");
} }
/**
* Enables support for running vanilla commands through Discord, if you ever need it.
*/
public ConfigData<Boolean> enableVanillaCommands() {
return getConfig().getData("enableVanillaCommands", true);
}
@Override @Override
protected void enable() { protected void enable() {
if (DPUtils.disableIfConfigErrorRes(this, chatChannel(), chatChannelMono())) if (DPUtils.disableIfConfigErrorRes(this, chatChannel(), chatChannelMono()))
@ -146,7 +153,7 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e); TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e);
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
getPlugin().getLogger().info("No LuckPerms, not injecting"); log("No LuckPerms, not injecting");
//e.printStackTrace(); //e.printStackTrace();
} }
} }

View file

@ -1,8 +1,8 @@
package buttondevteam.discordplugin.playerfaker; package buttondevteam.discordplugin.playerfaker;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordSenderBase; import buttondevteam.discordplugin.DiscordSenderBase;
import buttondevteam.discordplugin.IMCPlayer; import buttondevteam.discordplugin.IMCPlayer;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -19,8 +19,8 @@ public class VCMDWrapper {
* *
* @param player The Discord sender player (the wrapper) * @param player The Discord sender player (the wrapper)
*/ */
public static <T extends DiscordSenderBase & IMCPlayer<T>> Object createListener(T player) { public static <T extends DiscordSenderBase & IMCPlayer<T>> Object createListener(T player, MinecraftChatModule module) {
return createListener(player, null); return createListener(player, null, module);
} }
/** /**
@ -28,8 +28,9 @@ public class VCMDWrapper {
* *
* @param player The Discord sender player (the wrapper) * @param player The Discord sender player (the wrapper)
* @param bukkitplayer The Bukkit player to send the raw message to * @param bukkitplayer The Bukkit player to send the raw message to
* @param module The Minecraft chat module
*/ */
public static <T extends DiscordSenderBase & IMCPlayer<T>> Object createListener(T player, Player bukkitplayer) { public static <T extends DiscordSenderBase & IMCPlayer<T>> Object createListener(T player, Player bukkitplayer, MinecraftChatModule module) {
try { try {
Object ret; Object ret;
String mcpackage = Bukkit.getServer().getClass().getPackage().getName(); String mcpackage = Bukkit.getServer().getClass().getPackage().getName();
@ -42,16 +43,16 @@ public class VCMDWrapper {
else else
ret = null; ret = null;
if (ret == null) if (ret == null)
compatWarning(); compatWarning(module);
return ret; return ret;
} catch (NoClassDefFoundError | Exception e) { } catch (NoClassDefFoundError | Exception e) {
compatWarning(); compatWarning(module);
TBMCCoreAPI.SendException("Failed to create vanilla command listener", e); TBMCCoreAPI.SendException("Failed to create vanilla command listener", e);
return null; return null;
} }
} }
private static void compatWarning() { private static void compatWarning(MinecraftChatModule module) {
DPUtils.getLogger().warning("Vanilla commands won't be available from Discord due to a compatibility error."); module.logWarn("Vanilla commands won't be available from Discord due to a compatibility error. Disable vanilla command support to remove this message.");
} }
} }

View file

@ -77,7 +77,7 @@ public class GameRoleModule extends Component<DiscordPlugin> {
} else if (roleEvent instanceof RoleUpdateEvent) { } else if (roleEvent instanceof RoleUpdateEvent) {
val event = (RoleUpdateEvent) roleEvent; val event = (RoleUpdateEvent) roleEvent;
if (!event.getOld().isPresent()) { if (!event.getOld().isPresent()) {
DPUtils.getLogger().warning("Old role not stored, cannot update game role!"); grm.logWarn("Old role not stored, cannot update game role!");
return; return;
} }
Role or = event.getOld().get(); Role or = event.getOld().get();