Skip to content

Commit

Permalink
Remove duplicated code as a result of the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Sep 7, 2024
1 parent 42e54b7 commit 4b5ef0d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.scoreboard.Scoreboard;
import com.velocitypowered.api.scoreboard.ScoreboardManager;
import com.velocitypowered.proxy.scoreboard.downstream.DownstreamScoreboard;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

/**
* Plugin's main command
*/
public class VSACommand implements SimpleCommand {

@NotNull
private final ProxyServer server;

public VSACommand(ProxyServer server) {
/**
* Constructs new instance with given parameter.
*
* @param server
* Proxy server instance
*/
public VSACommand(@NotNull ProxyServer server) {
this.server = server;
}

Expand All @@ -52,7 +61,9 @@ public void execute(@NotNull Invocation invocation) {
if (player != null) {
sender.sendMessage(Component.text("Dumping scoreboard into console"));
DownstreamScoreboard scoreboard = ((DownstreamScoreboard) ScoreboardManager.getInstance().getBackendScoreboard(player));
scoreboard.dump();
for (String line : scoreboard.dump()) {
System.out.println(line);
}
sender.sendMessage(Component.text("Uploading the result ..."));
try {
scoreboard.upload(sender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,23 @@ public void removeScore(@NotNull String holder) {
scores.remove(holder);
}

public void dump() {
System.out.println(" " + objectiveName + ":");
System.out.println(" Title: " + title);
System.out.println(" HealthDisplay: " + healthDisplay);
System.out.println(" NumberFormat: " + numberFormat);
System.out.println(" DisplaySlot: " + displaySlot);
System.out.println(" Scores (" + scores.size() + "):");
for (DownstreamScore score : scores.values()) {
score.dump();
}
}

public ArrayList<String> getDump() {
ArrayList<String> content = new ArrayList<>();
/**
* Creates a dump of this objective into a list of lines.
*
* @return dump of this objective
*/
@NotNull
public List<String> dump() {
List<String> content = new ArrayList<>();
content.add(" " + objectiveName + ":");
content.add(" Title: " + title);
content.add(" HealthDisplay: " + healthDisplay);
content.add(" NumberFormat: " + numberFormat);
content.add(" DisplaySlot: " + displaySlot);
content.add(" Scores (" + scores.size() + "):");
for (DownstreamScore score : scores.values()) content.addAll(score.getDump());
for (DownstreamScore score : scores.values()) {
content.addAll(score.dump());
}
return content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
* A score that comes from a backend scoreboard.
Expand Down Expand Up @@ -103,15 +104,14 @@ public NumberFormat getNumberFormat() {
return numberFormat;
}

public void dump() {
System.out.println(" " + holder + ":");
System.out.println(" Score: " + score);
System.out.println(" DisplayName: " + displayName);
System.out.println(" NumberFormat: " + numberFormat);
}

public ArrayList<String> getDump() {
ArrayList<String> content = new ArrayList<>();
/**
* Creates a dump of this score into a list of lines.
*
* @return dump of this score
*/
@NotNull
public List<String> dump() {
List<String> content = new ArrayList<>();
content.add(" " + holder + ":");
content.add(" Score: " + score);
content.add(" DisplayName: " + displayName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -331,28 +332,24 @@ public void clear() {
teams.clear();
}

public void dump() {
System.out.println("--- DownstreamScoreboard of player " + viewer.getUsername() + " ---");
System.out.println("Teams (" + teams.size() + "):");
for (DownstreamTeam team : teams.values()) {
team.dump();
}
System.out.println("Objectives (" + objectives.size() + "):");
for (DownstreamObjective objective : objectives.values()) {
objective.dump();
}
/**
* Creates a dump of this scoreboard into a list of lines.
*
* @return dump of this scoreboard
*/
@NotNull
public List<String> dump() {
ArrayList<String> dump = new ArrayList<>();
dump.add("--- DownstreamScoreboard of player " + viewer.getUsername() + " ---");
dump.add("Teams (" + teams.size() + "):");
teams.values().forEach(team -> dump.addAll(team.dump()));
dump.add("Objectives (" + objectives.size() + "):");
objectives.values().forEach(objective -> dump.addAll(objective.dump()));
return dump;
}

public void upload(CommandSource sender) throws Exception {
ArrayList<String> content = new ArrayList<>();
content.add("--- DownstreamScoreboard of player " + viewer.getUsername() + " ---");
content.add("Teams (" + teams.size() + "):");
teams.values().forEach(team -> content.addAll(team.getDump()));
content.add("Objectives (" + objectives.size() + "):");
objectives.values().forEach(objective -> content.addAll(objective.getDump()));

StringBuilder contentBuilder = new StringBuilder();
content.forEach(line -> contentBuilder.append(line).append("\n"));
public void upload(@NotNull CommandSource sender) throws Exception {
String contentString = String.join("\n", dump()) + "\n";

URL url = new URL("https://api.pastes.dev/post");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Expand All @@ -361,7 +358,7 @@ public void upload(CommandSource sender) throws Exception {
connection.setRequestProperty("Content-Type", "text/log; charset=UTF-8");

try (OutputStream os = connection.getOutputStream()) {
os.write(contentBuilder.toString().getBytes("UTF-8"));
os.write(contentString.getBytes(StandardCharsets.UTF_8));
}

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Expand All @@ -375,7 +372,7 @@ public void upload(CommandSource sender) throws Exception {
String id = responseString.substring(responseString.indexOf("\"key\":\"") + 7, responseString.indexOf("\"", responseString.indexOf("\"key\":\"") + 7));

TextComponent message = Component.text("Click here to open the result.");
message = message.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL,"https://pastes.dev/" + id));
message = message.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.OPEN_URL, "https://pastes.dev/" + id));
sender.sendMessage(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,14 @@ public void removeEntriesIfPresent(@NotNull StringCollection entries) {
this.entries.removeAll(entries);
}

public void dump() {
System.out.println(" " + name + ":");
System.out.println(" DisplayName: " + properties.getDisplayName());
System.out.println(" Prefix: " + properties.getPrefix());
System.out.println(" Suffix: " + properties.getSuffix());
System.out.println(" Entries: " + entries);
}

public ArrayList<String> getDump() {
ArrayList<String> content = new ArrayList<>();
/**
* Creates a dump of this team into a list of lines.
*
* @return dump of this team
*/
@NotNull
public List<String> dump() {
List<String> content = new ArrayList<>();
content.add(" " + name + ":");
content.add(" DisplayName: " + properties.getDisplayName());
content.add(" Prefix: " + properties.getPrefix());
Expand Down

0 comments on commit 4b5ef0d

Please sign in to comment.