Merge pull request #77 from TBMCPlugins/dev

Added escaping & fixed reset stuff
This commit is contained in:
Norbi Peti 2018-08-10 22:34:15 +02:00 committed by GitHub
commit e4d07562df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 75 additions and 55 deletions

View file

@ -9,6 +9,7 @@ import sx.blah.discord.util.RequestBuffer.IVoidRequest;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
public final class DPUtils { public final class DPUtils {
@ -16,17 +17,24 @@ public final class DPUtils {
return builder.withAuthorIcon("https://minotar.net/avatar/" + playername + "/32.png"); return builder.withAuthorIcon("https://minotar.net/avatar/" + playername + "/32.png");
} }
/** Removes §[char] colour codes from strings */ /**
* Removes §[char] colour codes from strings & escapes them for Discord <br>
* Ensure that this method only gets called once (escaping)
*/
public static String sanitizeString(String string) { public static String sanitizeString(String string) {
return escape(sanitizeStringNoEscape(string));
}
/**
* Removes §[char] colour codes from strings
*/
public static String sanitizeStringNoEscape(String string) {
String sanitizedString = ""; String sanitizedString = "";
boolean random = false; boolean random = false;
for (int i = 0; i < string.length(); i++) { for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '§') { if (string.charAt(i) == '§') {
i++;// Skips the data value, the 4 in "§4Alisolarflare" i++;// Skips the data value, the 4 in "§4Alisolarflare"
if (string.charAt(i) == 'k') random = string.charAt(i) == 'k';
random = true;
else
random = false;
} else { } else {
if (!random) // Skip random/obfuscated characters if (!random) // Skip random/obfuscated characters
sanitizedString += string.charAt(i); sanitizedString += string.charAt(i);
@ -84,4 +92,8 @@ public final class DPUtils {
RequestBuffer.request(action); RequestBuffer.request(action);
} }
public static String escape(String message) {
return message.replaceAll("([*_~])", Matcher.quoteReplacement("\\")+"$1");
}
} }

View file

@ -1,7 +1,10 @@
package buttondevteam.discordplugin; package buttondevteam.discordplugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase; import buttondevteam.discordplugin.commands.DiscordCommandBase;
import buttondevteam.discordplugin.listeners.*; import buttondevteam.discordplugin.listeners.CommandListener;
import buttondevteam.discordplugin.listeners.ExceptionListener;
import buttondevteam.discordplugin.listeners.MCChatListener;
import buttondevteam.discordplugin.listeners.MCListener;
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;
@ -188,9 +191,9 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
sent = true; sent = true;
if (TBMCCoreAPI.IsTestServer() && !dc.getOurUser().getName().toLowerCase().contains("test")) { if (TBMCCoreAPI.IsTestServer() && !dc.getOurUser().getName().toLowerCase().contains("test")) {
TBMCCoreAPI.SendException( TBMCCoreAPI.SendException(
"Won't load because we're in testing mode and not using the separate account.", "Won't load because we're in testing mode and not using a separate account.",
new Exception( new Exception(
"The plugin refuses to load until you change the token to the testing account.")); "The plugin refuses to load until you change the token to a testing account."));
Bukkit.getPluginManager().disablePlugin(this); Bukkit.getPluginManager().disablePlugin(this);
} }
TBMCCoreAPI.SendUnsentExceptions(); TBMCCoreAPI.SendUnsentExceptions();
@ -213,7 +216,6 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
MCChatListener mcchat = new MCChatListener(); MCChatListener mcchat = new MCChatListener();
dc.getDispatcher().registerListener(mcchat); dc.getDispatcher().registerListener(mcchat);
TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this); TBMCCoreAPI.RegisterEventsForExceptions(mcchat, this);
TBMCCoreAPI.RegisterEventsForExceptions(new AutoUpdaterListener(), 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);
@ -288,6 +290,16 @@ 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();

View file

@ -82,6 +82,25 @@ public class PlayerListWatcher extends DedicatedPlayerList {
} }
} }
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;
}
}
public void a(EntityHuman entityhuman, IChatBaseComponent ichatbasecomponent) { public void a(EntityHuman entityhuman, IChatBaseComponent ichatbasecomponent) {
plist.a(entityhuman, ichatbasecomponent); plist.a(entityhuman, ichatbasecomponent);
} }

View file

@ -1,24 +0,0 @@
package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.PluginUpdater;
import buttondevteam.lib.TBMCCoreAPI;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class AutoUpdaterListener implements Listener {
@EventHandler
public void handle(PluginUpdater.UpdatedEvent event) {
if (DiscordPlugin.SafeMode)
return;
try {
DPUtils.performNoWait(() -> DiscordPlugin.officechannel.getMessageHistory(10).stream()
.filter(m -> m.getWebhookLongID() == 239123781401051138L && m.getEmbeds().get(0).getTitle()
.contains(event.getData().get("repository").getAsJsonObject().get("name").getAsString()))
.findFirst().get().addReaction(DiscordPlugin.DELIVERED_REACTION));
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while reacting to plugin update!", e);
}
}
}

View file

@ -213,7 +213,7 @@ public class CommandListener {
private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, IMessage message) { private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, IMessage message) {
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
if (cmdwithargs.length() > mention.length() + 1) if (cmdwithargs.length() > mention.length() + 1)
cmdwithargs = cmdwithargs.delete(0, cmdwithargs.delete(0,
cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length()); cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length());
else else
cmdwithargs.replace(0, cmdwithargs.length(), "help"); cmdwithargs.replace(0, cmdwithargs.length(), "help");

View file

@ -76,9 +76,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
e = se.getKey(); e = se.getKey();
time = se.getValue(); time = se.getValue();
final String authorPlayer = "[" + DPUtils.sanitizeString(e.getChannel().DisplayName) + "] " // final String authorPlayer = "[" + DPUtils.sanitizeStringNoEscape(e.getChannel().DisplayName) + "] " //
+ (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") // + (e.getSender() instanceof DiscordSenderBase ? "[D]" : "") //
+ (DPUtils.sanitizeString(e.getSender() instanceof Player // + (DPUtils.sanitizeStringNoEscape(e.getSender() instanceof Player //
? ((Player) e.getSender()).getDisplayName() // ? ((Player) e.getSender()).getDisplayName() //
: e.getSender().getName())); : e.getSender().getName()));
final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer) final EmbedBuilder embed = new EmbedBuilder().withAuthorName(authorPlayer)

View file

@ -66,6 +66,7 @@ public class MCListener implements Listener {
if (!DiscordPlugin.hooked) if (!DiscordPlugin.hooked)
MCChatListener.sendSystemMessageToChat(message); MCChatListener.sendSystemMessageToChat(message);
MCChatListener.forAllowedCustomMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message), e.getPlayer()); MCChatListener.forAllowedCustomMCChat(ch -> DiscordPlugin.sendMessageToChannel(ch, message), e.getPlayer());
//System.out.println("Does this appear more than once?"); //No
MCChatListener.ListC = 0; MCChatListener.ListC = 0;
ChromaBot.getInstance().updatePlayerList(); ChromaBot.getInstance().updatePlayerList();
}); });
@ -120,7 +121,7 @@ public class MCListener implements Listener {
public void onPlayerAFK(AfkStatusChangeEvent e) { //TODO: Add AFK to custom chats? public void onPlayerAFK(AfkStatusChangeEvent e) { //TODO: Add AFK to custom chats?
if (e.isCancelled() || !e.getAffected().getBase().isOnline()) if (e.isCancelled() || !e.getAffected().getBase().isOnline())
return; return;
MCChatListener.sendSystemMessageToChat(DPUtils.sanitizeString(e.getAffected().getBase().getDisplayName()) MCChatListener.sendSystemMessageToChat(e.getAffected().getBase().getDisplayName()
+ " is " + (e.getValue() ? "now" : "no longer") + " AFK."); + " is " + (e.getValue() ? "now" : "no longer") + " AFK.");
} }