diff --git a/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java b/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java index 78258c31..434e6eba 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java @@ -32,6 +32,7 @@ public class LibsDisguisesCommand implements CommandExecutor, TabCompleter { getCommands().add(new LDMods()); getCommands().add(new LDMetaInfo()); getCommands().add(new LDDebugPlayer()); + getCommands().add(new LDUploadLogs()); } protected ArrayList filterTabs(ArrayList list, String[] origArgs) { diff --git a/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUploadLogs.java b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUploadLogs.java new file mode 100644 index 00000000..64a8ceb7 --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUploadLogs.java @@ -0,0 +1,234 @@ +package me.libraryaddict.disguise.commands.libsdisguises; + +import lombok.Data; +import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.reflection.NmsVersion; +import me.libraryaddict.disguise.utilities.translations.LibsMsg; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.craftbukkit.libs.org.apache.commons.io.FileUtils; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import javax.net.ssl.HttpsURLConnection; +import java.io.*; +import java.net.URL; +import java.net.URLEncoder; +import java.util.AbstractMap.SimpleEntry; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +/** + * Created by libraryaddict on 18/06/2020. + */ +public class LDUploadLogs implements LDCommand { + /** + * Small modification of https://gist.github.com/jamezrin/12de49643d7be7150da362e86407113f + */ + @Data + public class GuestPaste { + private String name = null; + private final String text; + + public GuestPaste(String name, String text) { + this.name = name; + this.text = text; + } + + public URL paste() throws Exception { + URL url = new URL("https://pastebin.com/api/api_post.php"); + HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); + + con.setRequestMethod("POST"); + con.setRequestProperty("User-Agent", "Mozilla/5.0"); + + List> params = new LinkedList<>(); + // This doesn't give you access to my pastebin account ;) + // You need to enter another key for that, this key is for pastebin's tracking metrics. + // If you're using this code, please use your own pastebin dev key. + // Overuse will get it banned, and you'll have to ship a new version with your own key anyways. + // This is seperated into strings to prevent super easy scraping. + if (getClass().getName().contains("me.libraryaddict")) { + params.add(new SimpleEntry<>("api_dev_key", "62067f9d" + "cc1979a475105b529" + "eb453a5")); + } + params.add(new SimpleEntry<>("api_option", "paste")); + params.add(new SimpleEntry<>("api_paste_name", name)); + params.add(new SimpleEntry<>("api_paste_code", text)); + + params.add(new SimpleEntry<>("api_paste_format", "text")); + params.add(new SimpleEntry<>("api_paste_expire_date", "1M")); + params.add(new SimpleEntry<>("api_paste_private", "1")); + + StringBuilder output = new StringBuilder(); + for (SimpleEntry entry : params) { + if (output.length() > 0) + output.append('&'); + + output.append(URLEncoder.encode(entry.getKey(), "UTF-8")); + output.append('='); + output.append(URLEncoder.encode(entry.getValue(), "UTF-8")); + } + + con.setDoOutput(true); + try (DataOutputStream dos = new DataOutputStream(con.getOutputStream())) { + dos.writeBytes(output.toString()); + dos.flush(); + } + + int status = con.getResponseCode(); + + if (status >= 200 && status < 300) { + try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()))) { + String inputLine; + StringBuilder response = new StringBuilder(); + + while ((inputLine = br.readLine()) != null) { + response.append(inputLine); + } + + return new URL(response.toString()); + } + } else { + throw new IllegalStateException("Unexpected response code " + status); + } + } + } + + @Override + public List getTabComplete() { + return Collections.singletonList("uploadlogs"); + } + + @Override + public boolean hasPermission(CommandSender sender) { + return sender.isOp(); + } + + @Override + public String getPermission() { + return null; + } + + @Override + public void onCommand(CommandSender sender, String[] args) { + File latest = new File("logs/latest.log"); + File disguises = new File(LibsDisguises.getInstance().getDataFolder(), "disguises.yml"); + File config = new File(LibsDisguises.getInstance().getDataFolder(), "config.yml"); + + if (isTooBig(latest)) { + sender.sendMessage(ChatColor.RED + + "Your latest.log file is too big! It should be less than 512kb! Please restart and run this " + + "command again!"); + return; + } + + if (isTooBig(disguises)) { + sender.sendMessage(ChatColor.RED + + "Your disguises.yml is too big! You'll need to trim that file down before using this command! It " + + "should be less than 512kb!"); + return; + } + + if (isTooBig(config)) { + sender.sendMessage(ChatColor.RED + "Your config.yml is too big! It should be less than 512kb!"); + return; + } + + try { + String latestText = FileUtils.readFileToString(latest, "UTF-8"); + + boolean valid = false; + int lastFind = 0; + + for (int i = 0; i < 15; i++) { + int nextLine = latestText.indexOf("\n", lastFind); + + if (nextLine == -1) { + break; + } + + String str = latestText.substring(lastFind, nextLine); + + lastFind = nextLine + 2; + + if (!str.contains("Starting minecraft server version") && !str.contains("Loading properties") && + !str.contains("This server is running")) { + continue; + } + + valid = true; + break; + } + + if (!valid) { + sender.sendMessage( + ChatColor.RED + "Your latest.log is too old! Please restart the server and try again!"); + return; + } + + sender.sendMessage(ChatColor.GOLD + "Now creating pastebin links..."); + + new BukkitRunnable() { + @Override + public void run() { + try { + String disguiseText = FileUtils.readFileToString(disguises, "UTF-8"); + String configText = FileUtils.readFileToString(config, "UTF-8"); + + URL latestPaste = new GuestPaste("latest.log", latestText).paste(); + URL configPaste = new GuestPaste("LibsDisguises config.yml", configText).paste(); + URL disguisesPaste = new GuestPaste("LibsDisguises disguises.yml", disguiseText).paste(); + + new BukkitRunnable() { + @Override + public void run() { + sender.sendMessage(ChatColor.GOLD + "Upload successful!"); + + if (NmsVersion.v1_13.isSupported()) { + // Console can't click :( + if (sender instanceof Player) { + sender.sendMessage(ChatColor.GOLD + + "Click on the below message to have it appear in your chat input"); + } + + String text = "My log file: " + latestPaste + ", my config file: " + configPaste + + " and my disguises file: " + disguisesPaste; + + ComponentBuilder builder = new ComponentBuilder(""); + builder.appendLegacy(ChatColor.AQUA + ""); + builder.append(text); + builder.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, text)); + + sender.spigot().sendMessage(builder.create()); + } else { + sender.sendMessage( + ChatColor.GOLD + "Log: " + latestPaste + "\nConfig: " + configPaste + + "\nDisguises: " + disguisesPaste); + } + } + }.runTask(LibsDisguises.getInstance()); + } + catch (Exception e) { + e.printStackTrace(); + sender.sendMessage(ChatColor.RED + "Unexpected error! Upload failed! " + e.getMessage()); + } + } + }.runTaskAsynchronously(LibsDisguises.getInstance()); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + private boolean isTooBig(File file) { + return file.exists() && file.length() >= 512 * 1024; + } + + @Override + public LibsMsg getHelp() { + return LibsMsg.LD_COMMAND_UPLOAD_LOGS; + } +} diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java index 32d73f70..0ec6e55e 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -37,7 +37,6 @@ import org.bukkit.boss.BossBar; import org.bukkit.entity.*; import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; import java.lang.reflect.InvocationTargetException; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/listeners/ModdedListener.java b/src/main/java/me/libraryaddict/disguise/utilities/listeners/ModdedListener.java index cab6bdda..f0178206 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/listeners/ModdedListener.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/listeners/ModdedListener.java @@ -1,16 +1,10 @@ package me.libraryaddict.disguise.utilities.listeners; -import me.libraryaddict.disguise.LibsDisguises; -import me.libraryaddict.disguise.utilities.modded.ModdedEntity; import me.libraryaddict.disguise.utilities.modded.ModdedManager; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerLoginEvent; -import org.bukkit.metadata.FixedMetadataValue; - -import java.util.ArrayList; /** * Created by libraryaddict on 11/06/2020. diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java index ac00be45..023c3fad 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java @@ -339,6 +339,9 @@ public enum LibsMsg { "Reload's the plugin config and possibly blows disguises"), LD_COMMAND_DEBUG(ChatColor.BLUE + "/libsdisguises debug - " + ChatColor.AQUA + "Used to help debug scoreboard issues on a player disguise"), + LD_COMMAND_UPLOAD_LOGS(ChatColor.BLUE + "/libsdisguises uploadlogs - " + ChatColor.AQUA + + "Uploads latest.log, disguises.yml and config.yml and gives you the link to share. Used when seeking " + + "assistance."), SELF_DISGUISE_HIDDEN(ChatColor.GREEN + "Self disguise hidden as it's too tall.."); private final String string;