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;
uniqueId = uuid;
displayName = mcname;
vanillaCmdListener = new VCMDWrapper(VCMDWrapper.createListener(this));
vanillaCmdListener = new VCMDWrapper(VCMDWrapper.createListener(this, module));
}
/**

View file

@ -1,5 +1,6 @@
package buttondevteam.discordplugin;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.playerfaker.VCMDWrapper;
import discord4j.core.object.entity.MessageChannel;
import discord4j.core.object.entity.User;
@ -14,10 +15,10 @@ public abstract class DiscordPlayerSender extends DiscordSenderBase implements I
protected Player player;
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);
this.player = player;
vanillaCmdListener = new VCMDWrapper(VCMDWrapper.createListener(this, player));
vanillaCmdListener = new VCMDWrapper(VCMDWrapper.createListener(this, player, module));
}
@Override
@ -32,11 +33,11 @@ public abstract class DiscordPlayerSender extends DiscordSenderBase implements I
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 -> {
if (!Modifier.isAbstract(invocation.getMethod().getModifiers()))
return invocation.callRealMethod();
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;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component;
@ -17,12 +16,12 @@ public class GeneralEventBroadcasterModule extends Component<DiscordPlugin> {
protected void enable() {
try {
PlayerListWatcher.hookUpDown(true);
DPUtils.getLogger().info("Finished hooking into the player list");
log("Finished hooking into the player list");
hooked = true;
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while hacking the player list! Disable this module if you're on an incompatible version.", 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 {
if (!hooked) return;
if (PlayerListWatcher.hookUpDown(false))
DPUtils.getLogger().info("Finished unhooking the player list!");
log("Finished unhooking the player list!");
else
DPUtils.getLogger().info("Didn't have the player list hooked.");
log("Didn't have the player list hooked.");
hooked = false;
} catch (Exception 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 {
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 static Thread sendthread;
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`!");
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 ev = new TBMCCommandPreprocessEvent(dsender, channel, dmessage, clmd == null ? dsender : clmd.dcp);
Bukkit.getScheduler().runTask(DiscordPlugin.plugin, //Commands need to be run sync
@ -372,7 +372,9 @@ public class MCChatListener implements Listener {
return;
try {
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);
else if (mcpackage.contains("1_14"))
VanillaCommandListener14.runBukkitOrVanillaCommand(dsender, cmd);

View file

@ -109,12 +109,12 @@ public class MCChatUtils {
}
public static <T extends DiscordSenderBase> T addSender(HashMap<String, HashMap<Snowflake, T>> senders,
User user, T sender) {
User user, T sender) {
return addSender(senders, user.getId().asString(), sender);
}
public static <T extends DiscordSenderBase> T addSender(HashMap<String, HashMap<Snowflake, T>> senders,
String did, T sender) {
String did, T sender) {
var map = senders.get(did);
if (map == null)
map = new HashMap<>();
@ -124,7 +124,7 @@ public class MCChatUtils {
}
public static <T extends DiscordSenderBase> T getSender(HashMap<String, HashMap<Snowflake, T>> senders,
Snowflake channel, User user) {
Snowflake channel, User user) {
var map = senders.get(user.getId().asString());
if (map != null)
return map.get(channel);
@ -132,7 +132,7 @@ public class MCChatUtils {
}
public static <T extends DiscordSenderBase> T removeSender(HashMap<String, HashMap<Snowflake, T>> senders,
Snowflake channel, User user) {
Snowflake channel, User user) {
var map = senders.get(user.getId().asString());
if (map != null)
return map.remove(channel);
@ -355,7 +355,8 @@ public class MCChatUtils {
}
callEventExcludingSome(new PlayerJoinEvent(dcp, ""));
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);
else callEventExcludingSome(event);
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) {

View file

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

View file

@ -106,6 +106,13 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
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
protected void enable() {
if (DPUtils.disableIfConfigErrorRes(this, chatChannel(), chatChannelMono()))
@ -146,7 +153,7 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e);
} catch (NoClassDefFoundError e) {
getPlugin().getLogger().info("No LuckPerms, not injecting");
log("No LuckPerms, not injecting");
//e.printStackTrace();
}
}

View file

@ -1,8 +1,8 @@
package buttondevteam.discordplugin.playerfaker;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordSenderBase;
import buttondevteam.discordplugin.IMCPlayer;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.lib.TBMCCoreAPI;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@ -19,8 +19,8 @@ public class VCMDWrapper {
*
* @param player The Discord sender player (the wrapper)
*/
public static <T extends DiscordSenderBase & IMCPlayer<T>> Object createListener(T player) {
return createListener(player, null);
public static <T extends DiscordSenderBase & IMCPlayer<T>> Object createListener(T player, MinecraftChatModule module) {
return createListener(player, null, module);
}
/**
@ -28,8 +28,9 @@ public class VCMDWrapper {
*
* @param player The Discord sender player (the wrapper)
* @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 {
Object ret;
String mcpackage = Bukkit.getServer().getClass().getPackage().getName();
@ -42,16 +43,16 @@ public class VCMDWrapper {
else
ret = null;
if (ret == null)
compatWarning();
compatWarning(module);
return ret;
} catch (NoClassDefFoundError | Exception e) {
compatWarning();
compatWarning(module);
TBMCCoreAPI.SendException("Failed to create vanilla command listener", e);
return null;
}
}
private static void compatWarning() {
DPUtils.getLogger().warning("Vanilla commands won't be available from Discord due to a compatibility error.");
private static void compatWarning(MinecraftChatModule module) {
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) {
val event = (RoleUpdateEvent) roleEvent;
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;
}
Role or = event.getOld().get();