Skip to content

Commit

Permalink
now with async building
Browse files Browse the repository at this point in the history
  • Loading branch information
Nopro263 committed Apr 6, 2024
1 parent 78ea945 commit f0fa656
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
40 changes: 30 additions & 10 deletions src/main/java/dev/nopro263/buildmcfromgit/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.scheduler.BukkitRunnable;
import org.yaml.snakeyaml.reader.StreamReader;

import java.io.*;
Expand Down Expand Up @@ -209,18 +210,37 @@ public void build(CommandSender sender) throws InvalidPluginException, InvalidDe
throw new RuntimeException(ex);
}

String name = loader.getPluginDescription(f).getName();

for(org.bukkit.plugin.Plugin p:Bukkit.getPluginManager().getPlugins()) {
if(p.getName().equals(name)) {
Bukkit.getPluginManager().disablePlugin(p);
PluginUtils.unload(p);
File finalF = f;
new BukkitRunnable() {
@Override
public void run() {
String name = null;
try {
name = loader.getPluginDescription(finalF).getName();
} catch (InvalidDescriptionException e) {
throw new RuntimeException(e);
}

for(org.bukkit.plugin.Plugin p:Bukkit.getPluginManager().getPlugins()) {
if(p.getName().equals(name)) {
Bukkit.getPluginManager().disablePlugin(p);
PluginUtils.unload(p);
}
}

org.bukkit.plugin.Plugin plugin = null;
try {
plugin = Bukkit.getPluginManager().loadPlugin(finalF);
} catch (InvalidPluginException e) {
throw new RuntimeException(e);
} catch (InvalidDescriptionException e) {
throw new RuntimeException(e);
}
plugin.onLoad();
Bukkit.getPluginManager().enablePlugin(plugin);
}
}
}.runTask(Main.getInstance());

org.bukkit.plugin.Plugin plugin = Bukkit.getPluginManager().loadPlugin(f);
plugin.onLoad();
Bukkit.getPluginManager().enablePlugin(plugin);
}

public boolean canBuild(String player, boolean isOp) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/dev/nopro263/buildmcfromgit/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public final class Main extends JavaPlugin {
private static Config config;
private static Main instance;

private void fillDefaultFile(File file) throws IOException {
OutputStream stream = Files.newOutputStream(file.toPath());
Expand Down Expand Up @@ -49,7 +50,12 @@ private void loadConfig() throws IOException {
public static Config getPluginConfig() {
return config;
}
public static Main getInstance() {return instance;}

@Override
public void onLoad() {
instance = this;
}
@Override
public void onEnable() {
try {
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/dev/nopro263/buildmcfromgit/command/GitBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.scheduler.BukkitRunnable;

public class GitBuild implements CommandExecutor {
@Override
Expand All @@ -27,14 +28,20 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
}

if(canBuild) {
try {
plugin.build(commandSender);
commandSender.sendMessage(ChatColor.GREEN + "Built '" + strings[0] + "' successfully");
} catch (InvalidPluginException | InvalidDescriptionException | RuntimeException e) {
commandSender.sendMessage(ChatColor.RED + "Building '" + strings[0] + "' failed:");
commandSender.sendMessage(e.getMessage());
e.printStackTrace();
}
new BukkitRunnable() {
@Override
public void run() {
try {
commandSender.sendMessage(ChatColor.GRAY + "Building '" + strings[0] + "'");
plugin.build(commandSender);
commandSender.sendMessage(ChatColor.GREEN + "Built '" + strings[0] + "' successfully");
} catch (InvalidPluginException | InvalidDescriptionException | RuntimeException e) {
commandSender.sendMessage(ChatColor.RED + "Building '" + strings[0] + "' failed:");
commandSender.sendMessage(e.getMessage());
e.printStackTrace();
}
}
}.runTaskAsynchronously(Main.getInstance());
} else {
commandSender.sendMessage(ChatColor.RED + "You are not allowed to build this");
}
Expand Down

0 comments on commit f0fa656

Please sign in to comment.