Skip to content

Commit

Permalink
now runs asynchronously to prevent seconds-long freezing
Browse files Browse the repository at this point in the history
  • Loading branch information
Grovre committed Mar 14, 2022
1 parent d611c51 commit 21402a5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
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>me.grovre</groupId>
<artifactId>ConDump</artifactId>
<version>1.2</version>
<version>1.3</version>
<packaging>jar</packaging>

<name>ConDump</name>
Expand Down
42 changes: 25 additions & 17 deletions src/main/java/me/grovre/condump/commands/ConDumpCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.grovre.condump.commands;

import com.google.common.io.Files;
import me.grovre.condump.ConDump;
import me.grovre.condump.Ghub;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -32,25 +33,32 @@ public boolean onCommand(@NonNull CommandSender sender,
player.sendMessage(ChatColor.RED + "You don't have permission to dump the most recent console log.");
return true;
}
// Tries to get the latest logs as a string, line breaks and all
try {
Ghub.commitToLogRepo(getLatestLogString());
} catch (IOException e) {
if(player != null) {
player.sendMessage(Ghub.errorMessage);

Bukkit.getScheduler().runTaskAsynchronously(ConDump.getPlugin(), () -> {
boolean failFlag = false;

// Tries to get the latest logs as a string, line breaks and all
try {
Ghub.commitToLogRepo(getLatestLogString());
} catch (IOException e) {
if (player != null) {
player.sendMessage(Ghub.errorMessage);
}
System.out.println("Failed to commit to repo.");
e.printStackTrace();
System.out.println(Ghub.errorMessage);
failFlag = true;
}
System.out.println("Failed to commit to repo.");
e.printStackTrace();
System.out.println(Ghub.errorMessage);
return true;
}
if(failFlag) return;

// Messages the player who executed /condump and the console about the new commit with the link for viewing
// Success message
if (player != null) {
player.sendMessage("Commit is at: " + Ghub.lastCreatedCommitUrl);
}
System.out.println("Commit is at: " + Ghub.lastCreatedCommitUrl);
});

// Messages the player who executed /condump and the console about the new commit with the link for viewing
// Success message
if(player != null) {
player.sendMessage("Commit is at: " + Ghub.lastCreatedCommitUrl);
}
System.out.println("Commit is at: " + Ghub.lastCreatedCommitUrl);
return true;
}

Expand Down
50 changes: 30 additions & 20 deletions src/main/java/me/grovre/condump/commands/ConWipeCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package me.grovre.condump.commands;

import me.grovre.condump.ConDump;
import me.grovre.condump.Ghub;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand All @@ -24,29 +27,36 @@ public boolean onCommand(@NonNull CommandSender commandSender,

// Makes sure the player has permission
if(player != null && !player.hasPermission("condump.wipe")) {
player.sendMessage(ChatColor.RED + "You don't have permission to wipe.");
return true;
}

long timeTaken;
try {
long start = System.currentTimeMillis();
Ghub.clearLogRepo();
long end = System.currentTimeMillis();
timeTaken = end - start;
System.out.println("Repo successfully cleared in " + timeTaken / 1000.0 + " seconds " +
"(" + timeTaken + "ms)");

} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to wipe repo.");
System.out.println(Ghub.errorMessage);
return true;
}

if(player != null) {
player.sendMessage("Repo successfully cleared in " + timeTaken / 1000.0 + " seconds " +
"(" + timeTaken + "ms)");
}
Bukkit.getScheduler().runTaskAsynchronously(ConDump.getPlugin(), () -> {
boolean failFlag = false;

long timeTaken = 0;
try {
long start = System.currentTimeMillis();
Ghub.clearLogRepo();
long end = System.currentTimeMillis();
timeTaken = end - start;
System.out.println("Repo successfully cleared in " + timeTaken / 1000D + " seconds " +
"(" + timeTaken + "ms)");

} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to wipe repo.");
System.out.println(Ghub.errorMessage);
failFlag = true;
}
if(failFlag) return;

if (player != null) {
player.sendMessage("Repo successfully cleared in " + timeTaken / 1000D + " seconds " +
"(" + timeTaken + "ms)");
}
});

return true;
}
}
Binary file removed target/ConDump-1.1.jar
Binary file not shown.

0 comments on commit 21402a5

Please sign in to comment.