Skip to content

Commit

Permalink
Use a RunResult when a script file is not found instead of throwing a…
Browse files Browse the repository at this point in the history
… FileNotFoundException
  • Loading branch information
magicmq committed Jun 9, 2024
1 parent a9ff117 commit 2745a20
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
import dev.magicmq.pyspigot.manager.redis.client.ScriptRedisClient;
import dev.magicmq.pyspigot.manager.script.Script;
import dev.magicmq.pyspigot.manager.script.ScriptManager;
import dev.magicmq.pyspigot.manager.script.ScriptOptions;
import dev.magicmq.pyspigot.manager.task.Task;
import dev.magicmq.pyspigot.manager.task.TaskManager;
import dev.magicmq.pyspigot.util.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

import java.io.FileNotFoundException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -121,14 +121,13 @@ public boolean onCommand(CommandSender sender, String[] args) {

sender.sendMessage(builder.toString());
} else {
try {
ScriptOptions options = ScriptManager.get().getScriptOptions(args[0]);
if (options != null) {
builder.append(ChatColor.GOLD + "Uptime: " + ChatColor.RESET + "Currently not loaded" + "\n");

builder.append(ChatColor.GOLD + "Script options: " + ChatColor.RESET + ScriptManager.get().getScriptOptions(args[0]).toString());
builder.append(ChatColor.GOLD + "Script options: " + ChatColor.RESET + options.toString());

sender.sendMessage(builder.toString());
} catch (FileNotFoundException e) {
sender.sendMessage(ChatColor.RED + "No script found in the scripts folder with the name '" + args[0] + "'.");
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -51,8 +50,8 @@ else if (result == RunResult.FAIL_DISABLED)
sender.sendMessage(ChatColor.RED + "Script '" + args[0] + "' was not run because it is disabled as per its options in script_options.yml.");
else if (result == RunResult.FAIL_ERROR)
sender.sendMessage(ChatColor.RED + "There was an error when running script '" + args[0] + "'. See console for details.");
} catch (FileNotFoundException e) {
sender.sendMessage(ChatColor.RED + "No script found in the scripts folder with the name '" + args[0] + "'.");
else if (result == RunResult.FAIL_SCRIPT_NOT_FOUND)
sender.sendMessage(ChatColor.RED + "No script found in the scripts folder with the name '" + args[0] + "'.");
} catch (IOException e) {
e.printStackTrace();
sender.sendMessage(ChatColor.RED + "There was an error when loading script '" + args[0] + "'. See console for details.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -58,8 +57,8 @@ else if (result == RunResult.FAIL_DISABLED)
sender.sendMessage(ChatColor.RED + "Script '" + args[0] + "' was not reloaded because it is disabled as per its options in script_options.yml.");
else if (result == RunResult.FAIL_ERROR)
sender.sendMessage(ChatColor.RED + "There was an error when reloading script '" + args[0] + "'. See console for details.");
} catch (FileNotFoundException e) {
sender.sendMessage(ChatColor.RED + "No script found in the scripts folder with the name '" + args[0] + "'.");
else if (result == RunResult.FAIL_SCRIPT_NOT_FOUND)
sender.sendMessage(ChatColor.RED + "No script found in the scripts folder with the name '" + args[0] + "'.");
} catch (IOException e) {
e.printStackTrace();
sender.sendMessage(ChatColor.RED + "There was an error when reloading script '" + args[0] + "'. See console for details.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public enum RunResult {
/**
* Returned if a script is already loaded with the same name.
*/
FAIL_DUPLICATE
FAIL_DUPLICATE,

/**
* Returned if a script was not found with the given name.
*/
FAIL_SCRIPT_NOT_FOUND
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,20 @@ public void loadScripts() {
* Get the {@link ScriptOptions} for a particular script.
* @param name The name of the script to fetch options for. Name should contain the file extension (.py)
* @return A ScriptOptions object representing the options beloinging to the specified script
* @throws FileNotFoundException If a script file was not found in the scripts folder with the given name
*/
public ScriptOptions getScriptOptions(String name) throws FileNotFoundException {
public ScriptOptions getScriptOptions(String name) {
File scriptFile = new File(scriptsFolder, name);
if (scriptFile.exists()) {
return new ScriptOptions(PySpigot.get().getScriptOptionsConfig().getConfigurationSection(scriptFile.getName()));
} else
throw new FileNotFoundException("Script file not found in the scripts folder with the name '" + name + "'");

return null;
}

/**
* Load a script with the given name.
* @param name The file name of the script to load. Name should contain the file extension (.py)
* @return A {@link RunResult} describing the outcome of the load operation
* @throws FileNotFoundException If a script file was not found in the scripts folder with the given name
* @throws IOException If there was another IOException related to loading the script file
* @throws IOException If there was an IOException related to loading the script file
*/
public RunResult loadScript(String name) throws IOException {
File scriptFile = new File(scriptsFolder, name);
Expand All @@ -157,7 +154,7 @@ public RunResult loadScript(String name) throws IOException {

return loadScript(script);
} else
throw new FileNotFoundException("Script file not found in the scripts folder with the name '" + name + "'");
return RunResult.FAIL_SCRIPT_NOT_FOUND;
}

/**
Expand Down

0 comments on commit 2745a20

Please sign in to comment.