Reorganized lots of stuff (components) #86

Merged
NorbiPeti merged 12 commits from dev into master 2019-01-03 20:39:53 +00:00
8 changed files with 107 additions and 77 deletions
Showing only changes of commit 38b45299c0 - Show all commits

View file

@ -1,13 +1,16 @@
package buttondevteam.discordplugin;
import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule;
import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.discordplugin.listeners.CommandListener;
import buttondevteam.discordplugin.listeners.CommonListeners;
import buttondevteam.discordplugin.listeners.ExceptionListener;
import buttondevteam.discordplugin.listeners.MCChatListener;
import buttondevteam.discordplugin.listeners.MCListener;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.mccommands.DiscordMCCommandBase;
import buttondevteam.discordplugin.mccommands.ResetMCCommand;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.player.ChromaGamerBase;
@ -51,22 +54,12 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
public static DiscordPlugin plugin;
public static boolean SafeMode = true;
public static List<String> GameRoles;
public static boolean hooked = false;
@SuppressWarnings("unchecked")
@Override
public void onEnable() {
stop = false; //If not the first time
try {
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;
lastannouncementtime = getConfig().getLong("lastannouncementtime");
lastseentime = getConfig().getLong("lastseentime");
@ -212,11 +205,10 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
}*/
}
}, 0, 10);
for (IListener<?> listener : CommandListener.getListeners())
for (IListener<?> listener : CommonListeners.getListeners())
dc.getDispatcher().registerListener(listener);
MCChatListener mcchat = new MCChatListener();
dc.getDispatcher().registerListener(mcchat);
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
Component.registerComponent(this, new GeneralEventBroadcasterModule());
Component.registerComponent(this, new MinecraftChatModule());
Bukkit.getPluginManager().registerEvents(new ExceptionListener(), this);
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(), this);
TBMCChatAPI.AddCommands(this, DiscordMCCommandBase.class);
@ -299,16 +291,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
try {
SafeMode = true; // Stop interacting with Discord
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();
dc.changePresence(StatusType.IDLE, ActivityType.PLAYING, "Chromacraft"); //No longer using the same account for testing
dc.logout();

View file

@ -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);
}
}
}

View file

@ -1,4 +1,4 @@
package buttondevteam.discordplugin;
package buttondevteam.discordplugin.broadcaster;
import buttondevteam.discordplugin.listeners.MCChatListener;
import buttondevteam.lib.TBMCCoreAPI;
@ -53,52 +53,43 @@ public class PlayerListWatcher extends DedicatedPlayerList {
}
@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) {
sendMessage(component, true);
}
}
public static void hookUp() {
try {
Field conf = CraftServer.class.getDeclaredField("console");
conf.setAccessible(true);
val server = (MinecraftServer) conf.get(Bukkit.getServer());
val plw = new ObjenesisStd().newInstance(PlayerListWatcher.class); // Cannot call super constructor
plw.plist = (DedicatedPlayerList) server.getPlayerList();
plw.maxPlayers = plw.plist.getMaxPlayers();
Field plf = plw.getClass().getField("players");
plf.setAccessible(true);
Field modf = plf.getClass().getDeclaredField("modifiers");
modf.setAccessible(true);
modf.set(plf, plf.getModifiers() & ~Modifier.FINAL);
plf.set(plw, plw.plist.players);
server.a(plw);
Field pllf = CraftServer.class.getDeclaredField("playerList");
pllf.setAccessible(true);
pllf.set(Bukkit.getServer(), plw);
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while hacking the player list!", e);
}
static void hookUp() throws Exception {
Field conf = CraftServer.class.getDeclaredField("console");
conf.setAccessible(true);
val server = (MinecraftServer) conf.get(Bukkit.getServer());
val plw = new ObjenesisStd().newInstance(PlayerListWatcher.class); // Cannot call super constructor
plw.plist = (DedicatedPlayerList) server.getPlayerList();
plw.maxPlayers = plw.plist.getMaxPlayers();
Field plf = plw.getClass().getField("players");
plf.setAccessible(true);
Field modf = plf.getClass().getDeclaredField("modifiers");
modf.setAccessible(true);
modf.set(plf, plf.getModifiers() & ~Modifier.FINAL);
plf.set(plw, plw.plist.players);
server.a(plw);
Field pllf = CraftServer.class.getDeclaredField("playerList");
pllf.setAccessible(true);
pllf.set(Bukkit.getServer(), plw);
}
public static boolean hookDown() {
try {
Field conf = CraftServer.class.getDeclaredField("console");
conf.setAccessible(true);
val server = (MinecraftServer) conf.get(Bukkit.getServer());
val plist = (DedicatedPlayerList) server.getPlayerList();
if (!(plist instanceof PlayerListWatcher))
return false;
server.a(((PlayerListWatcher) plist).plist);
Field pllf = CraftServer.class.getDeclaredField("playerList");
pllf.setAccessible(true);
pllf.set(Bukkit.getServer(), ((PlayerListWatcher) plist).plist);
return true;
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while hacking the player list!", e);
return true;
}
static boolean hookDown() throws Exception {
Field conf = CraftServer.class.getDeclaredField("console");
conf.setAccessible(true);
val server = (MinecraftServer) conf.get(Bukkit.getServer());
val plist = (DedicatedPlayerList) server.getPlayerList();
if (!(plist instanceof PlayerListWatcher))
return false;
server.a(((PlayerListWatcher) plist).plist);
Field pllf = CraftServer.class.getDeclaredField("playerList");
pllf.setAccessible(true);
pllf.set(Bukkit.getServer(), ((PlayerListWatcher) plist).plist);
return true;
}
public void a(EntityHuman entityhuman, IChatBaseComponent ichatbasecomponent) {

View file

@ -1,7 +1,7 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.listeners.CommandListener;
import buttondevteam.discordplugin.listeners.CommonListeners;
import sx.blah.discord.handle.obj.IMessage;
public class DebugCommand extends DiscordCommandBase {
@ -13,7 +13,7 @@ public class DebugCommand extends DiscordCommandBase {
@Override
public boolean run(IMessage message, String args) {
if (message.getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L)))
message.reply("Debug " + (CommandListener.debug() ? "enabled" : "disabled"));
message.reply("Debug " + (CommonListeners.debug() ? "enabled" : "disabled"));
else
message.reply("You need to be a moderator to use this command.");
return true;

View file

@ -8,7 +8,7 @@ import java.util.Arrays;
import java.util.HashMap;
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 String getCommandName();

View file

@ -23,7 +23,7 @@ import java.util.Calendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class CommandListener {
public class CommonListeners {
private static final String[] serverReadyStrings = new String[]{"In one week from now", // Ali
"Between now and the heat-death of the universe.", // Ghostise
@ -200,7 +200,7 @@ public class CommandListener {
private static boolean debug = false;
public static void debug(String debug) {
if (CommandListener.debug) //Debug
if (CommonListeners.debug) //Debug
System.out.println(debug);
}

View file

@ -1,6 +1,9 @@
package buttondevteam.discordplugin.listeners;
import buttondevteam.core.ComponentManager;
import buttondevteam.discordplugin.*;
import buttondevteam.discordplugin.broadcaster.GeneralEventBroadcasterModule;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
import buttondevteam.lib.TBMCChatEvent;
import buttondevteam.lib.TBMCChatPreprocessEvent;
@ -58,7 +61,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
@EventHandler // Minecraft
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;
sendevents.add(new AbstractMap.SimpleEntry<>(ev, Instant.now()));
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
*/
public static void forCustomAndAllMCChat(Consumer<IChannel> action, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
if (!DiscordPlugin.hooked || !hookmsg)
if (!GeneralEventBroadcasterModule.isHooked() || !hookmsg)
forAllMCChat(action);
final Consumer<CustomLMD> customLMDConsumer = cc -> action.accept(cc.channel);
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
*/
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);
forAllowedCustomMCChat(action, sender, toggle);
}
@ -463,9 +466,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
private Runnable recrun;
private static Thread recthread;
@Override // Discord
@Override // Discord - TODO: Call from the common listener
public void handle(MessageReceivedEvent ev) {
if (DiscordPlugin.SafeMode)
if (!ComponentManager.isEnabled(MinecraftChatModule.class))
return;
val author = ev.getMessage().getAuthor();
if (author.isBot())
@ -476,8 +479,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
&& !hasCustomChat)
return;
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)
if (CommandListener.runCommand(ev.getMessage(), true))
return; // Race condition: If it gets here after it enabled mcchat it says it - I might as well allow disabling with this (CommonListeners)
if (CommonListeners.runCommand(ev.getMessage(), true))
return;
resetLastMessage(ev.getChannel());
lastlist++;

View file

@ -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
}