Skip to content

Commit

Permalink
Fixed bug with calling YamlConfiguration#getLocation which is not a…
Browse files Browse the repository at this point in the history
… thing in legacy versions

Added /kitpvpplus dump to dump debug information to pastebin
Added dump method to api
  • Loading branch information
Nuckerr committed Sep 28, 2021
1 parent 6f28b2a commit f4af341
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ A customizable KitPvP Core for 1.8 - 1.7.
- PlaceholderAPI support
- PlaceholderAPI placeholders
- Leaderboards
- Playervaultws
- Playervaults
- Signs
- Full developer api (allowing you to add custom abilities & integrate statistics
and leaderboards easily into your plugin)
Expand Down
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>wtf.nucker</groupId>
<artifactId>KitPvPPlus-API</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<packaging>jar</packaging>

<name>api</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ protected void setInstance(KitPvPPlusAPI instance) {
* @return that player's playerdata instance
*/
public abstract PlayerData getPlayerData(OfflinePlayer player);

/**
* @return the dump debug information (Same as what you get when you run /kitpvpplus dump)
*/
public abstract String getDumpInformation();
}
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>KitPvPCore</artifactId>
<groupId>wtf.nucker</groupId>
<version>1.2.1</version>
<version>1.2.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/wtf/nucker/kitpvpplus/KitPvPPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ public final class KitPvPPlus extends JavaPlugin {
private PlayerVaultManager pvManager;
private LeaderBoardManager leaderBoardManager;
private VersionManager verManager;
private Debugger debugger;

@Override
public void onLoad() {
KitPvPPlus.instance = this;
this.debugger = new Debugger();

if(this.getSubVersion() > 12) {
if(getServer().getPluginManager().getPlugin("WorldGuard") != null) {
Expand Down Expand Up @@ -424,6 +426,10 @@ public PlayerVaultManager getPvManager() {
return pvManager;
}

public Debugger getDebugger() {
return debugger;
}

public boolean isWGEnabled() {
return this.worldGuardManager != null;
}
Expand Down Expand Up @@ -457,6 +463,7 @@ public void reloadDatabase() {
e.printStackTrace();
}
this.dataManager = new DataManager(this);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.*;
import jodd.http.HttpBrowser;
import jodd.http.HttpRequest;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
Expand All @@ -11,12 +13,20 @@
import wtf.nucker.kitpvpplus.managers.PlayerBank;
import wtf.nucker.kitpvpplus.objects.Ability;
import wtf.nucker.kitpvpplus.utils.ChatUtils;
import wtf.nucker.kitpvpplus.utils.Debugger;
import wtf.nucker.kitpvpplus.utils.ItemUtils;
import wtf.nucker.kitpvpplus.utils.Language;
import wtf.nucker.kitpvpplus.utils.menuUtils.menuBuilders.AbilityMenus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* @author Nucker
Expand Down Expand Up @@ -82,6 +92,16 @@ public void onAbility(Player player, @Optional Ability ability) {
player.getInventory().addItem(ItemUtils.copyItem(ability.getItem()));
}

@Subcommand("dump")
@Description("Dumps all debug information into a pastebin")
@CommandPermission("kitpvpplus.admin")
public void onDump(CommandSender player) {
player.sendMessage(ChatColor.GOLD + "WARNING: Hastebin currently has a bug, as such you wont be able to follow the pastebin link.");
player.sendMessage(ChatColor.GOLD + "An issue has been posted: https://github.com/toptal/haste-server/issues/390");
String link = KitPvPPlus.getInstance().getDebugger().dumpToPasteServer();
player.sendMessage(ChatUtils.translate("&bDump link: &e" + link));
}

@Subcommand("debug")
@Private
public void onDebugCommand(Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,9 @@ public PlayerData getPlayerData(OfflinePlayer player) {
return APIConversion.fromInstanceData(plugin.getDataManager().getPlayerData(player));
}

@Override
public String getDumpInformation() {
return plugin.getDebugger().dumpContents();
}

}
36 changes: 33 additions & 3 deletions core/src/main/java/wtf/nucker/kitpvpplus/managers/SignManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package wtf.nucker.kitpvpplus.managers;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.metadata.FixedMetadataValue;
import wtf.nucker.kitpvpplus.KitPvPPlus;
Expand All @@ -22,7 +24,7 @@ public SignManager() {
this.config = this.configInstance.getConfig();
if(config.getConfigurationSection("signs") == null) return;
for (String key : config.getConfigurationSection("signs").getKeys(false)) {
Block block = config.getLocation("signs."+key+".block").getBlock();
Block block = this.deserialize("signs."+key+".block").getBlock();
if(block.getType().name().contains("SIGN")) {
block.setMetadata("kpvp", new FixedMetadataValue(KitPvPPlus.getInstance(), config.getString("signs."+key+".type")));
}else {
Expand All @@ -36,15 +38,15 @@ public void addSign(Block block, String type) {
int i;
if(config.getConfigurationSection("signs") == null) i = 0; else i = config.getConfigurationSection("signs").getKeys(false).size() +1;
config.set("signs."+i+".type", type);
config.set("signs."+i+".block", block.getLocation());
this.serialize(block.getLocation(), "signs."+i+".block");

configInstance.save();
}

public void removeSign(Block block) {
if(config.getConfigurationSection("signs") == null) return;
for (String key : config.getConfigurationSection("signs").getKeys(false)) {
if(config.getLocation("signs."+key+".block").getBlock().equals(block)) {
if(this.deserialize("signs." + key + ".block").getBlock().equals(block)) {
config.set("signs."+key, null);
}
}
Expand All @@ -60,4 +62,32 @@ public Config getConfigInstance() {
public YamlConfiguration getConfig() {
return config;
}

private Location deserialize(String path) {
if(this.config == null) return new Location(Bukkit.getWorld("world"), 0, 65, 0);
if(Locations.getConfig().getConfig().getConfigurationSection(path) == null) Locations.getConfig().getConfig().set(path, "");
ConfigurationSection section = Locations.getConfig().getConfig().getConfigurationSection(path);
if(section == null) throw new NullPointerException("Unable to load " + section.getCurrentPath());
return new Location(
Bukkit.getWorld(section.getString("world")),
section.getDouble("x"),
section.getDouble("y"),
section.getDouble("z"),
Float.parseFloat(section.getString("yaw")),
Float.parseFloat(section.getString("pitch"))
);
}

private void serialize(Location location, String path) {
if(this.config == null) return;
ConfigurationSection section = this.configInstance.getConfig().getConfigurationSection(path);
if(section == null) throw new NullPointerException("Unable to load " + section.getCurrentPath());
section.set("world", location.getWorld().getName());
section.set("x", location.getX());
section.set("y", location.getY());
section.set("z", location.getZ());
section.set("yaw", location.getYaw());
section.set("pitch", location.getPitch());
this.configInstance.save();
}
}
9 changes: 9 additions & 0 deletions core/src/main/java/wtf/nucker/kitpvpplus/utils/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Config(String name) {
e.printStackTrace();
}
yaml.options().copyDefaults(true);

}

/**
Expand Down Expand Up @@ -90,4 +91,12 @@ public void save() {
public YamlConfiguration getConfig() {
return yaml;
}

public String getName() {
return name;
}

public File getFile() {
return file;
}
}
109 changes: 109 additions & 0 deletions core/src/main/java/wtf/nucker/kitpvpplus/utils/Debugger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package wtf.nucker.kitpvpplus.utils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import org.bukkit.Bukkit;
import wtf.nucker.kitpvpplus.KitPvPPlus;
import wtf.nucker.kitpvpplus.managers.*;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Nucker
* @project KitPvPPlus
* @date 28/09/2021
*/
public class Debugger {

private static final String LINE = "===================================================";

private static final String HASTE_URL = "https://paste.nucker.me";

public String dumpContents() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String res = LINE + "\n DUMP CONTENTS - " + dtf.format(now) + "\n" + LINE + "\n";

res = res + this.dumpConfig(KitPvPPlus.getInstance().getMessagesConfig());
res = res + this.dumpConfig(KitPvPPlus.getInstance().getDataManager().getDataConfig());
res = res + this.dumpConfig(Locations.getConfig());
res = res + this.dumpConfig(KitManager.getConfigInstance());
res = res + this.dumpConfig(KitPvPPlus.getInstance().getSignManager().getConfigInstance());

res = res + "PLUGIN DEBUG DATA: \n" + LINE + "\n";

res = res + "Version: " + KitPvPPlus.getInstance().getDescription().getVersion() + "\n";
res = res + "Sending metrics: " + KitPvPPlus.getInstance().getMetrics().isEnabled() + "\n";
res = res + "World guard integration: " + KitPvPPlus.getInstance().isWGEnabled() + "\n";
res = res + "Placeholder API integration: " + (Bukkit.getServer().getPluginManager().getPlugin("PlaceHolderAPI") != null) + "\n";
res = res + "Storage system: " + KitPvPPlus.getInstance().getDataManager().getStorageType() + "\n";
res = res + "Bank Storage type: " + PlayerBank.getStorageType() + "\n";
res = res + "Providing vault: " + PlayerBank.providingVault() + "\n";
res = res + "Debug mode: " + KitPvPPlus.DEBUG + "\n";
res = res + "Legacy version: " + KitPvPPlus.getInstance().getVerManager().needsUpdating() + "\n";

res = res + LINE + "\n";

res = res + "SERVER DEBUG DATA: \n" + LINE + "\n";

res = res + "Full version: " + Bukkit.getVersion() + "\n";
res = res + "Bukkit version: " + Bukkit.getBukkitVersion() + "\n";
res = res + "Sub version: " + KitPvPPlus.getInstance().getSubVersion() + "\n";

res = res + LINE + "\n";

res = res + "Please send this in #kitpvpplus-help";

return res;
}

public String dumpToPasteServer() {
HttpRequest req = HttpRequest.post(HASTE_URL + "/documents").header("data", this.dumpContents());
HttpResponse response = req.send();

JsonReader reader = new JsonReader(new StringReader(response.bodyText()));
reader.setLenient(true);
JsonObject obj = new JsonParser().parse(reader).getAsJsonObject();
return HASTE_URL + "/" + obj.get("key").getAsString();
}

private String dumpConfig(Config config) {
StringBuilder res = new StringBuilder(LINE + "\n" + config.getName() + "\n" + LINE + "\n");

try {
for (String line : this.getLines(config.getFile())) {
res.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}

res.append(LINE).append("\n");
return res.toString();
}


private List<String> getLines(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;

List<String> res = new ArrayList<>();
while((line = reader.readLine()) != null){
res.add(line);
}

return res;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>wtf.nucker</groupId>
<artifactId>KitPvPCore</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<url>https://wiki.nucker.me</url>
<description>A customizable KitPvP core for 1.8 - 1.17</description>
<name>KitPvPPlus</name>
Expand Down

0 comments on commit f4af341

Please sign in to comment.