parent
9527be99d1
commit
38b45299c0
8 changed files with 107 additions and 77 deletions
|
@ -1,13 +1,16 @@
|
||||||
package buttondevteam.discordplugin;
|
package buttondevteam.discordplugin;
|
||||||
|
|
||||||
|
import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule;
|
||||||
import buttondevteam.discordplugin.commands.DiscordCommandBase;
|
import buttondevteam.discordplugin.commands.DiscordCommandBase;
|
||||||
import buttondevteam.discordplugin.listeners.CommandListener;
|
import buttondevteam.discordplugin.listeners.CommonListeners;
|
||||||
import buttondevteam.discordplugin.listeners.ExceptionListener;
|
import buttondevteam.discordplugin.listeners.ExceptionListener;
|
||||||
import buttondevteam.discordplugin.listeners.MCChatListener;
|
import buttondevteam.discordplugin.listeners.MCChatListener;
|
||||||
import buttondevteam.discordplugin.listeners.MCListener;
|
import buttondevteam.discordplugin.listeners.MCListener;
|
||||||
|
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
|
||||||
import buttondevteam.discordplugin.mccommands.DiscordMCCommandBase;
|
import buttondevteam.discordplugin.mccommands.DiscordMCCommandBase;
|
||||||
import buttondevteam.discordplugin.mccommands.ResetMCCommand;
|
import buttondevteam.discordplugin.mccommands.ResetMCCommand;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
import buttondevteam.lib.architecture.Component;
|
||||||
import buttondevteam.lib.chat.Channel;
|
import buttondevteam.lib.chat.Channel;
|
||||||
import buttondevteam.lib.chat.TBMCChatAPI;
|
import buttondevteam.lib.chat.TBMCChatAPI;
|
||||||
import buttondevteam.lib.player.ChromaGamerBase;
|
import buttondevteam.lib.player.ChromaGamerBase;
|
||||||
|
@ -51,22 +54,12 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
public static DiscordPlugin plugin;
|
public static DiscordPlugin plugin;
|
||||||
public static boolean SafeMode = true;
|
public static boolean SafeMode = true;
|
||||||
public static List<String> GameRoles;
|
public static List<String> GameRoles;
|
||||||
public static boolean hooked = false;
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
stop = false; //If not the first time
|
stop = false; //If not the first time
|
||||||
try {
|
try {
|
||||||
Bukkit.getLogger().info("Initializing DiscordPlugin...");
|
Bukkit.getLogger().info("Initializing DiscordPlugin...");
|
||||||
try {
|
|
||||||
PlayerListWatcher.hookUp();
|
|
||||||
hooked = true;
|
|
||||||
Bukkit.getLogger().info("Finished hooking into the player list");
|
|
||||||
} catch (Throwable e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Bukkit.getLogger().warning("Couldn't hook into the player list!");
|
|
||||||
}
|
|
||||||
plugin = this;
|
plugin = this;
|
||||||
lastannouncementtime = getConfig().getLong("lastannouncementtime");
|
lastannouncementtime = getConfig().getLong("lastannouncementtime");
|
||||||
lastseentime = getConfig().getLong("lastseentime");
|
lastseentime = getConfig().getLong("lastseentime");
|
||||||
|
@ -212,11 +205,10 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
}, 0, 10);
|
}, 0, 10);
|
||||||
for (IListener<?> listener : CommandListener.getListeners())
|
for (IListener<?> listener : CommonListeners.getListeners())
|
||||||
dc.getDispatcher().registerListener(listener);
|
dc.getDispatcher().registerListener(listener);
|
||||||
MCChatListener mcchat = new MCChatListener();
|
Component.registerComponent(this, new GeneralEventBroadcasterModule());
|
||||||
dc.getDispatcher().registerListener(mcchat);
|
Component.registerComponent(this, new MinecraftChatModule());
|
||||||
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
|
|
||||||
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
|
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
|
||||||
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
|
||||||
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
|
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
|
||||||
|
@ -299,16 +291,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
|
||||||
try {
|
try {
|
||||||
SafeMode = true; // Stop interacting with Discord
|
SafeMode = true; // Stop interacting with Discord
|
||||||
MCChatListener.stop(true);
|
MCChatListener.stop(true);
|
||||||
try {
|
|
||||||
if (PlayerListWatcher.hookDown())
|
|
||||||
System.out.println("Finished unhooking the player list!");
|
|
||||||
else
|
|
||||||
System.out.println("Didn't have the player list hooked.");
|
|
||||||
hooked = false;
|
|
||||||
} catch (Throwable e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Bukkit.getLogger().warning("Couldn't unhook the player list!");
|
|
||||||
}
|
|
||||||
ChromaBot.delete();
|
ChromaBot.delete();
|
||||||
dc.changePresence(StatusType.IDLE, ActivityType.PLAYING, "Chromacraft"); //No longer using the same account for testing
|
dc.changePresence(StatusType.IDLE, ActivityType.PLAYING, "Chromacraft"); //No longer using the same account for testing
|
||||||
dc.logout();
|
dc.logout();
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
package buttondevteam.discordplugin.broadcaster;
|
||||||
|
|
||||||
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
import buttondevteam.lib.architecture.Component;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
|
public class GeneralEventBroadcasterModule extends Component {
|
||||||
|
private static @Getter boolean hooked = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void enable() {
|
||||||
|
try {
|
||||||
|
PlayerListWatcher.hookUp();
|
||||||
|
Bukkit.getLogger().info("Finished hooking into the player list");
|
||||||
|
hooked = true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
TBMCCoreAPI.SendException("Error while hacking the player list!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void disable() {
|
||||||
|
try {
|
||||||
|
if (PlayerListWatcher.hookDown())
|
||||||
|
System.out.println("Finished unhooking the player list!");
|
||||||
|
else
|
||||||
|
System.out.println("Didn't have the player list hooked.");
|
||||||
|
hooked = false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
TBMCCoreAPI.SendException("Error while hacking the player list!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package buttondevteam.discordplugin;
|
package buttondevteam.discordplugin.broadcaster;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.listeners.MCChatListener;
|
import buttondevteam.discordplugin.listeners.MCChatListener;
|
||||||
import buttondevteam.lib.TBMCCoreAPI;
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
@ -53,52 +53,43 @@ public class PlayerListWatcher extends DedicatedPlayerList {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(IChatBaseComponent[] iChatBaseComponents) { // Needed so it calls the overriden method
|
public void sendMessage(IChatBaseComponent[] iChatBaseComponents) { // Needed so it calls the overridden method
|
||||||
for (IChatBaseComponent component : iChatBaseComponents) {
|
for (IChatBaseComponent component : iChatBaseComponents) {
|
||||||
sendMessage(component, true);
|
sendMessage(component, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void hookUp() {
|
static void hookUp() throws Exception {
|
||||||
try {
|
Field conf = CraftServer.class.getDeclaredField("console");
|
||||||
Field conf = CraftServer.class.getDeclaredField("console");
|
conf.setAccessible(true);
|
||||||
conf.setAccessible(true);
|
val server = (MinecraftServer) conf.get(Bukkit.getServer());
|
||||||
val server = (MinecraftServer) conf.get(Bukkit.getServer());
|
val plw = new ObjenesisStd().newInstance(PlayerListWatcher.class); // Cannot call super constructor
|
||||||
val plw = new ObjenesisStd().newInstance(PlayerListWatcher.class); // Cannot call super constructor
|
plw.plist = (DedicatedPlayerList) server.getPlayerList();
|
||||||
plw.plist = (DedicatedPlayerList) server.getPlayerList();
|
plw.maxPlayers = plw.plist.getMaxPlayers();
|
||||||
plw.maxPlayers = plw.plist.getMaxPlayers();
|
Field plf = plw.getClass().getField("players");
|
||||||
Field plf = plw.getClass().getField("players");
|
plf.setAccessible(true);
|
||||||
plf.setAccessible(true);
|
Field modf = plf.getClass().getDeclaredField("modifiers");
|
||||||
Field modf = plf.getClass().getDeclaredField("modifiers");
|
modf.setAccessible(true);
|
||||||
modf.setAccessible(true);
|
modf.set(plf, plf.getModifiers() & ~Modifier.FINAL);
|
||||||
modf.set(plf, plf.getModifiers() & ~Modifier.FINAL);
|
plf.set(plw, plw.plist.players);
|
||||||
plf.set(plw, plw.plist.players);
|
server.a(plw);
|
||||||
server.a(plw);
|
Field pllf = CraftServer.class.getDeclaredField("playerList");
|
||||||
Field pllf = CraftServer.class.getDeclaredField("playerList");
|
pllf.setAccessible(true);
|
||||||
pllf.setAccessible(true);
|
pllf.set(Bukkit.getServer(), plw);
|
||||||
pllf.set(Bukkit.getServer(), plw);
|
|
||||||
} catch (Exception e) {
|
|
||||||
TBMCCoreAPI.SendException("Error while hacking the player list!", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean hookDown() {
|
static boolean hookDown() throws Exception {
|
||||||
try {
|
Field conf = CraftServer.class.getDeclaredField("console");
|
||||||
Field conf = CraftServer.class.getDeclaredField("console");
|
conf.setAccessible(true);
|
||||||
conf.setAccessible(true);
|
val server = (MinecraftServer) conf.get(Bukkit.getServer());
|
||||||
val server = (MinecraftServer) conf.get(Bukkit.getServer());
|
val plist = (DedicatedPlayerList) server.getPlayerList();
|
||||||
val plist = (DedicatedPlayerList) server.getPlayerList();
|
if (!(plist instanceof PlayerListWatcher))
|
||||||
if (!(plist instanceof PlayerListWatcher))
|
return false;
|
||||||
return false;
|
server.a(((PlayerListWatcher) plist).plist);
|
||||||
server.a(((PlayerListWatcher) plist).plist);
|
Field pllf = CraftServer.class.getDeclaredField("playerList");
|
||||||
Field pllf = CraftServer.class.getDeclaredField("playerList");
|
pllf.setAccessible(true);
|
||||||
pllf.setAccessible(true);
|
pllf.set(Bukkit.getServer(), ((PlayerListWatcher) plist).plist);
|
||||||
pllf.set(Bukkit.getServer(), ((PlayerListWatcher) plist).plist);
|
return true;
|
||||||
return true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
TBMCCoreAPI.SendException("Error while hacking the player list!", e);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void a(EntityHuman entityhuman, IChatBaseComponent ichatbasecomponent) {
|
public void a(EntityHuman entityhuman, IChatBaseComponent ichatbasecomponent) {
|
|
@ -1,7 +1,7 @@
|
||||||
package buttondevteam.discordplugin.commands;
|
package buttondevteam.discordplugin.commands;
|
||||||
|
|
||||||
import buttondevteam.discordplugin.DiscordPlugin;
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
import buttondevteam.discordplugin.listeners.CommandListener;
|
import buttondevteam.discordplugin.listeners.CommonListeners;
|
||||||
import sx.blah.discord.handle.obj.IMessage;
|
import sx.blah.discord.handle.obj.IMessage;
|
||||||
|
|
||||||
public class DebugCommand extends DiscordCommandBase {
|
public class DebugCommand extends DiscordCommandBase {
|
||||||
|
@ -13,7 +13,7 @@ public class DebugCommand extends DiscordCommandBase {
|
||||||
@Override
|
@Override
|
||||||
public boolean run(IMessage message, String args) {
|
public boolean run(IMessage message, String args) {
|
||||||
if (message.getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L)))
|
if (message.getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L)))
|
||||||
message.reply("Debug " + (CommandListener.debug() ? "enabled" : "disabled"));
|
message.reply("Debug " + (CommonListeners.debug() ? "enabled" : "disabled"));
|
||||||
else
|
else
|
||||||
message.reply("You need to be a moderator to use this command.");
|
message.reply("You need to be a moderator to use this command.");
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static buttondevteam.discordplugin.listeners.CommandListener.debug;
|
import static buttondevteam.discordplugin.listeners.CommonListeners.debug;
|
||||||
|
|
||||||
public abstract class DiscordCommandBase {
|
public abstract class DiscordCommandBase {
|
||||||
public abstract String getCommandName();
|
public abstract String getCommandName();
|
||||||
|
|
|
@ -23,7 +23,7 @@ import java.util.Calendar;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class CommandListener {
|
public class CommonListeners {
|
||||||
|
|
||||||
private static final String[] serverReadyStrings = new String[]{"In one week from now", // Ali
|
private static final String[] serverReadyStrings = new String[]{"In one week from now", // Ali
|
||||||
"Between now and the heat-death of the universe.", // Ghostise
|
"Between now and the heat-death of the universe.", // Ghostise
|
||||||
|
@ -200,7 +200,7 @@ public class CommandListener {
|
||||||
private static boolean debug = false;
|
private static boolean debug = false;
|
||||||
|
|
||||||
public static void debug(String debug) {
|
public static void debug(String debug) {
|
||||||
if (CommandListener.debug) //Debug
|
if (CommonListeners.debug) //Debug
|
||||||
System.out.println(debug);
|
System.out.println(debug);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package buttondevteam.discordplugin.listeners;
|
package buttondevteam.discordplugin.listeners;
|
||||||
|
|
||||||
|
import buttondevteam.core.ComponentManager;
|
||||||
import buttondevteam.discordplugin.*;
|
import buttondevteam.discordplugin.*;
|
||||||
|
import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule;
|
||||||
|
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
|
||||||
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
|
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
|
||||||
import buttondevteam.lib.TBMCChatEvent;
|
import buttondevteam.lib.TBMCChatEvent;
|
||||||
import buttondevteam.lib.TBMCChatPreprocessEvent;
|
import buttondevteam.lib.TBMCChatPreprocessEvent;
|
||||||
|
@ -58,7 +61,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
|
|
||||||
@EventHandler // Minecraft
|
@EventHandler // Minecraft
|
||||||
public void onMCChat(TBMCChatEvent ev) {
|
public void onMCChat(TBMCChatEvent ev) {
|
||||||
if (DiscordPlugin.SafeMode || ev.isCancelled()) //SafeMode: Needed so it doesn't restart after server shutdown
|
if (!ComponentManager.isEnabled(MinecraftChatModule.class) || ev.isCancelled()) //SafeMode: Needed so it doesn't restart after server shutdown
|
||||||
return;
|
return;
|
||||||
sendevents.add(new AbstractMap.SimpleEntry<>(ev, Instant.now()));
|
sendevents.add(new AbstractMap.SimpleEntry<>(ev, Instant.now()));
|
||||||
if (sendtask != null)
|
if (sendtask != null)
|
||||||
|
@ -368,7 +371,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
* @param hookmsg Whether the message is also sent from the hook
|
* @param hookmsg Whether the message is also sent from the hook
|
||||||
*/
|
*/
|
||||||
public static void forCustomAndAllMCChat(Consumer<IChannel> action, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
|
public static void forCustomAndAllMCChat(Consumer<IChannel> action, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
|
||||||
if (!DiscordPlugin.hooked || !hookmsg)
|
if (!GeneralEventBroadcasterModule.isHooked() || !hookmsg)
|
||||||
forAllMCChat(action);
|
forAllMCChat(action);
|
||||||
final Consumer<CustomLMD> customLMDConsumer = cc -> action.accept(cc.channel);
|
final Consumer<CustomLMD> customLMDConsumer = cc -> action.accept(cc.channel);
|
||||||
if (toggle == null)
|
if (toggle == null)
|
||||||
|
@ -404,7 +407,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
* @param hookmsg Whether the message is also sent from the hook
|
* @param hookmsg Whether the message is also sent from the hook
|
||||||
*/
|
*/
|
||||||
public static void forAllowedCustomAndAllMCChat(Consumer<IChannel> action, @Nullable CommandSender sender, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
|
public static void forAllowedCustomAndAllMCChat(Consumer<IChannel> action, @Nullable CommandSender sender, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
|
||||||
if (!DiscordPlugin.hooked || !hookmsg)
|
if (!GeneralEventBroadcasterModule.isHooked() || !hookmsg)
|
||||||
forAllMCChat(action);
|
forAllMCChat(action);
|
||||||
forAllowedCustomMCChat(action, sender, toggle);
|
forAllowedCustomMCChat(action, sender, toggle);
|
||||||
}
|
}
|
||||||
|
@ -463,9 +466,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
private Runnable recrun;
|
private Runnable recrun;
|
||||||
private static Thread recthread;
|
private static Thread recthread;
|
||||||
|
|
||||||
@Override // Discord
|
@Override // Discord - TODO: Call from the common listener
|
||||||
public void handle(MessageReceivedEvent ev) {
|
public void handle(MessageReceivedEvent ev) {
|
||||||
if (DiscordPlugin.SafeMode)
|
if (!ComponentManager.isEnabled(MinecraftChatModule.class))
|
||||||
return;
|
return;
|
||||||
val author = ev.getMessage().getAuthor();
|
val author = ev.getMessage().getAuthor();
|
||||||
if (author.isBot())
|
if (author.isBot())
|
||||||
|
@ -476,8 +479,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|
||||||
&& !hasCustomChat)
|
&& !hasCustomChat)
|
||||||
return;
|
return;
|
||||||
if (ev.getMessage().getContent().equalsIgnoreCase("mcchat"))
|
if (ev.getMessage().getContent().equalsIgnoreCase("mcchat"))
|
||||||
return; // Race condition: If it gets here after it enabled mcchat it says it - I might as well allow disabling with this (CommandListener)
|
return; // Race condition: If it gets here after it enabled mcchat it says it - I might as well allow disabling with this (CommonListeners)
|
||||||
if (CommandListener.runCommand(ev.getMessage(), true))
|
if (CommonListeners.runCommand(ev.getMessage(), true))
|
||||||
return;
|
return;
|
||||||
resetLastMessage(ev.getChannel());
|
resetLastMessage(ev.getChannel());
|
||||||
lastlist++;
|
lastlist++;
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package buttondevteam.discordplugin.mcchat;
|
||||||
|
|
||||||
|
import buttondevteam.discordplugin.DiscordPlugin;
|
||||||
|
import buttondevteam.discordplugin.listeners.MCChatListener;
|
||||||
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
|
import buttondevteam.lib.architecture.Component;
|
||||||
|
|
||||||
|
public class MinecraftChatModule extends Component {
|
||||||
|
@Override
|
||||||
|
protected void enable() {
|
||||||
|
MCChatListener mcchat = new MCChatListener();
|
||||||
|
DiscordPlugin.dc.getDispatcher().registerListener(mcchat);
|
||||||
|
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, getPlugin());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void disable() {
|
||||||
|
//These get undone if restarting/resetting - it will ignore events if disabled
|
||||||
|
} //TODO: Use ComponentManager.isEnabled() at other places too, instead of SafeMode
|
||||||
|
}
|
Loading…
Reference in a new issue