Skip to content

Commit

Permalink
Add /ess dump command to generate a debug dump output (#4361)
Browse files Browse the repository at this point in the history
Co-authored-by: MD <1917406+mdcfe@users.noreply.github.com>

Command usage: /essentials dump [config] [discord] [kits] [log]

Either of the optional args can be used to add the given data to the dump.

Related: EssentialsX/Website#51
  • Loading branch information
JRoy authored Aug 19, 2021
1 parent 1179caa commit 3692740
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.event.EventPriority;
import org.spongepowered.configurate.CommentedConfigurationNode;

import java.io.File;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;
Expand All @@ -17,6 +18,8 @@
import java.util.regex.Pattern;

public interface ISettings extends IConf {
File getConfigFile();

boolean areSignsDisabled();

IText getAnnounceNewPlayerFormat();
Expand Down
4 changes: 4 additions & 0 deletions Essentials/src/main/java/com/earth2me/essentials/Kits.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public void reloadConfig() {
kits = _getKits();
}

public File getFile() {
return config.getFile();
}

private CommentedConfigurationNode _getKits() {
final CommentedConfigurationNode section = config.getSection("kits");
if (section != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public Settings(final IEssentials ess) {
reloadConfig();
}

@Override
public File getConfigFile() {
return config.getFile();
}

@Override
public boolean getRespawnAtHome() {
return config.getBoolean("respawn-at-home", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.DateUtil;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.earth2me.essentials.utils.PasteUtil;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
Expand All @@ -17,25 +13,16 @@
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;

import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;

import static com.earth2me.essentials.I18n.tl;

public class Commandcreatekit extends EssentialsCommand {
private static final String PASTE_URL = "https://paste.gg/";
private static final String PASTE_UPLOAD_URL = "https://api.paste.gg/v1/pastes";
private static final Gson GSON = new Gson();

private final ExecutorService executorService = Executors.newSingleThreadExecutor();

public Commandcreatekit() {
super("createkit");
}
Expand Down Expand Up @@ -81,7 +68,7 @@ public void run(final Server server, final User user, final String commandLabel,
}

private void uploadPaste(final CommandSource sender, final String kitName, final long delay, final List<String> list) {
executorService.submit(() -> {
ess.runTaskAsynchronously(() -> {
try {
final StringWriter sw = new StringWriter();
final YamlConfigurationLoader loader = YamlConfigurationLoader.builder().sink(() -> new BufferedWriter(sw)).indent(2).nodeStyle(NodeStyle.BLOCK).build();
Expand All @@ -95,52 +82,27 @@ private void uploadPaste(final CommandSource sender, final String kitName, final

final String fileContents = sw.toString();

final HttpURLConnection connection = (HttpURLConnection) new URL(PASTE_UPLOAD_URL).openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent", "EssentialsX plugin");
connection.setRequestProperty("Content-Type", "application/json");
final JsonObject body = new JsonObject();
final JsonArray files = new JsonArray();
final JsonObject file = new JsonObject();
final JsonObject content = new JsonObject();
content.addProperty("format", "text");
content.addProperty("value", fileContents);
file.add("content", content);
files.add(file);
body.add("files", files);

try (final OutputStream os = connection.getOutputStream()) {
os.write(body.toString().getBytes(Charsets.UTF_8));
}
// Error
if (connection.getResponseCode() >= 400) {
final CompletableFuture<PasteUtil.PasteResult> future = PasteUtil.createPaste(Collections.singletonList(new PasteUtil.PasteFile("kit_" + kitName + ".yml", fileContents)));
future.thenAccept(result -> {
if (result != null) {
final String separator = tl("createKitSeparator");
final String delayFormat = delay <= 0 ? "0" : DateUtil.formatDateDiff(System.currentTimeMillis() + (delay * 1000));
sender.sendMessage(separator);
sender.sendMessage(tl("createKitSuccess", kitName, delayFormat, result.getPasteUrl()));
sender.sendMessage(separator);
if (ess.getSettings().isDebug()) {
ess.getLogger().info(sender.getSender().getName() + " created a kit: " + result.getPasteUrl());
}
}
});
future.exceptionally(throwable -> {
sender.sendMessage(tl("createKitFailed", kitName));
final String message = CharStreams.toString(new InputStreamReader(connection.getErrorStream(), Charsets.UTF_8));
ess.getLogger().severe("Error creating kit: " + message);
return;
}

// Read URL
final JsonObject object = GSON.fromJson(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8), JsonObject.class);
final String pasteUrl = PASTE_URL + object.get("result").getAsJsonObject().get("id").getAsString();
connection.disconnect();

final String separator = tl("createKitSeparator");
String delayFormat = "0";
if (delay > 0) {
delayFormat = DateUtil.formatDateDiff(System.currentTimeMillis() + (delay * 1000));
}
sender.sendMessage(separator);
sender.sendMessage(tl("createKitSuccess", kitName, delayFormat, pasteUrl));
sender.sendMessage(separator);
if (ess.getSettings().isDebug()) {
ess.getLogger().info(sender.getSender().getName() + " created a kit: " + pasteUrl);
}
} catch (final Exception e) {
ess.getLogger().log(Level.SEVERE, "Error creating kit: ", throwable);
return null;
});
} catch (Exception e) {
sender.sendMessage(tl("createKitFailed", kitName));
e.printStackTrace();
ess.getLogger().log(Level.SEVERE, "Error creating kit: ", e);
}
});
}
Expand Down
Loading

0 comments on commit 3692740

Please sign in to comment.