Changes resource loading
Some checks failed
KnarCraft/Minecraft-Server-Launcher/master There was a failure building this commit

Loads configuration and image files from resources folder
Removes duplicate resources
This commit is contained in:
Kristian Knarvik 2020-02-12 23:00:51 +01:00
parent f841d73e2d
commit c59cbcefbb
9 changed files with 71 additions and 46 deletions

View File

@ -20,9 +20,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static net.knarcraft.minecraftserverlauncher.Shared.downloadFile; import static net.knarcraft.minecraftserverlauncher.Shared.*;
import static net.knarcraft.minecraftserverlauncher.Shared.readFile;
import static net.knarcraft.minecraftserverlauncher.Shared.stringBetween;
//Java 8 required. //Java 8 required.
/** /**
@ -186,17 +184,17 @@ public class Main {
/** /**
* Checks if a newer version is available * Checks if a newer version is available
* *
* @throws IOException * @throws IOException <p>If the update check fails.</p>
*/ */
private static void checkForUpdate() throws IOException { private static void checkForUpdate() throws IOException {
Scanner file; Scanner file = getResourceAsScanner("currentversion.csv");
try { if (!file.hasNextLine()) {
file = new Scanner(new File("config/currentversion.csv")); throw new FileNotFoundException("File currentversion.csv is invalid");
} catch (FileNotFoundException e) {
file = new Scanner(Main.class.getResourceAsStream("/config/currentversion.csv"));
} }
String oldType = file.nextLine(); String oldType = file.nextLine();
if (!file.hasNextLine()) {
throw new FileNotFoundException("File currentversion.csv is invalid");
}
String oldVer = file.nextLine(); String oldVer = file.nextLine();
file.close(); file.close();

View File

@ -1,5 +1,6 @@
package net.knarcraft.minecraftserverlauncher; package net.knarcraft.minecraftserverlauncher;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
@ -16,6 +17,30 @@ import java.util.Scanner;
* @since 1.0.0 * @since 1.0.0
*/ */
public class Shared { public class Shared {
/**
* Gets a resource as an InputStream
* @param resourceName <p>The name of the resource you want to read.</p>
* @return <p>An input stream which can be used to access the resource.</p>
*/
public static InputStream getResourceAsStream(String resourceName) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
return classloader.getResourceAsStream(resourceName);
}
/**
* Gets a resource as a Scanner
* @param resourceName <p>The name of the resource you want to read.</p>
* @return <p>A scanner which can be used to read contents of the resource.</p>
* @throws FileNotFoundException <p>If the resource is not found.</p>
*/
public static Scanner getResourceAsScanner(String resourceName) throws FileNotFoundException {
InputStream is = getResourceAsStream(resourceName);
if (is == null) {
throw new FileNotFoundException("The resource was not found.");
}
return new Scanner(is);
}
/** /**
* Finds a substring between two substrings in a string. * Finds a substring between two substrings in a string.
* *

View File

@ -16,6 +16,7 @@ import java.io.*;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -193,7 +194,7 @@ public class Profile {
if (name == null) { //If a user cancels or crosses out window if (name == null) { //If a user cancels or crosses out window
return; return;
} }
if (name.equals("") && !name.contains("!") && !name.contains("?") && !name.contains(";")) { if (name.equals("") && !name.matches("^[!?;]+$")) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
null, null,
"Profile name can't be blank.", "Profile name can't be blank.",
@ -228,11 +229,7 @@ public class Profile {
public static void removeProfile(String name) { public static void removeProfile(String name) {
if (profiles.size() > 1) { if (profiles.size() > 1) {
for (int i = 0; i < profiles.size(); i++) { profiles.removeIf(profile -> profile.name.equals(name));
if (profiles.get(i).name.equals((name))) {
profiles.remove(i);
}
}
} }
} }
@ -376,7 +373,7 @@ public class Profile {
throw new FileNotFoundException("Unable to save to the profiles file."); throw new FileNotFoundException("Unable to save to the profiles file.");
} }
} }
} catch (IOException e) { } catch (IOException | NullPointerException e) {
if (gui != null) { if (gui != null) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
null, null,
@ -434,18 +431,31 @@ public class Profile {
JOptionPane.ERROR_MESSAGE JOptionPane.ERROR_MESSAGE
); );
System.exit(1); System.exit(1);
} catch (IOException e) {
e.printStackTrace();
} }
if (profiles.size() == 0) { if (profiles.size() == 0) {
addProfile("Default"); addProfile("Default");
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException | NoSuchElementException e) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
null, null,
"A profiles file was not found. Default profile was created.", "A profiles file was not found. Default profile was created.",
"Info", "Info",
JOptionPane.INFORMATION_MESSAGE JOptionPane.INFORMATION_MESSAGE
); );
gui = new GUI(); try {
gui = new GUI();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(
null,
"Failed to load GUI messages. The GUI can't be shown.",
"Info",
JOptionPane.INFORMATION_MESSAGE
);
} catch (IOException ex) {
ex.printStackTrace();
}
addProfile("Default"); addProfile("Default");
} }
gui.update(); gui.update();
@ -549,7 +559,7 @@ public class Profile {
String url = Objects.requireNonNull(type).getDownloadURL(), name = type.getName(), newestVersion; String url = Objects.requireNonNull(type).getDownloadURL(), name = type.getName(), newestVersion;
AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null; AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null;
for (String version : type.getVersions()) { for (String version : type.getVersions()) {
Boolean success; boolean success;
printToGui("Downloading: " + name + version + ".jar"); printToGui("Downloading: " + name + version + ".jar");
File file = new File(jarDir + type.getName() + version + ".jar"); File file = new File(jarDir + type.getName() + version + ".jar");
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar"); Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");

View File

@ -6,6 +6,8 @@ import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
import static net.knarcraft.minecraftserverlauncher.Shared.getResourceAsScanner;
/** /**
* Contains the bare minimum to be a functional server type. * Contains the bare minimum to be a functional server type.
* *
@ -80,11 +82,10 @@ public class ServerType {
if (serverTypes.isEmpty()) { if (serverTypes.isEmpty()) {
Scanner file; Scanner file;
try { try {
file = new Scanner(new File("config/servertypes.csv")); file = getResourceAsScanner("servertypes.csv");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
file = new Scanner(ServerType.class.getResourceAsStream("/config/servertypes.csv")); throw new ConfigurationException("Server type configuration file is missing.");
} }
while (file.hasNextLine()) { while (file.hasNextLine()) {
String[] str = file.nextLine().split(";", -1); String[] str = file.nextLine().split(";", -1);
int len = str.length; int len = str.length;

View File

@ -19,6 +19,8 @@ import java.util.concurrent.Executors;
import static java.awt.Frame.NORMAL; import static java.awt.Frame.NORMAL;
import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE; import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;
import static net.knarcraft.minecraftserverlauncher.Shared.getResourceAsScanner;
import static net.knarcraft.minecraftserverlauncher.Shared.getResourceAsStream;
/** /**
* Generates a GUI. * Generates a GUI.
@ -60,7 +62,7 @@ public class GUI implements ActionListener {
/** /**
* Create the application window. * Create the application window.
*/ */
public GUI() { public GUI() throws IOException {
initialize(440, 170); initialize(440, 170);
loadMessages(); loadMessages();
this.globalPlayers = new ArrayList<>(); this.globalPlayers = new ArrayList<>();
@ -72,7 +74,7 @@ public class GUI implements ActionListener {
* @param width The preferred width * @param width The preferred width
* @param height The preferred height * @param height The preferred height
*/ */
public GUI(int width, int height) { public GUI(int width, int height) throws IOException {
initialize(width, height); initialize(width, height);
loadMessages(); loadMessages();
this.globalPlayers = new ArrayList<>(); this.globalPlayers = new ArrayList<>();
@ -117,11 +119,7 @@ public class GUI implements ActionListener {
* @param name The name of the player to remove. * @param name The name of the player to remove.
*/ */
public void removePlayer(String name) { public void removePlayer(String name) {
for (int i = 0; i < this.globalPlayers.size(); i++) { globalPlayers.removeIf(playerName -> playerName.equals(name));
if (this.globalPlayers.get(i).equals(name)) {
this.globalPlayers.remove(i);
}
}
this.updatePlayers(); this.updatePlayers();
} }
@ -165,7 +163,7 @@ public class GUI implements ActionListener {
/** /**
* Creates the GUI, * Creates the GUI,
*/ */
private void initialize(int width, int height) { private void initialize(int width, int height) throws IOException {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | } catch (ClassNotFoundException |
@ -179,12 +177,9 @@ public class GUI implements ActionListener {
frame = new JFrame("Minecraft server launcher"); frame = new JFrame("Minecraft server launcher");
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(width, height)); frame.getContentPane().setPreferredSize(new Dimension(width, height));
ImageIcon img;
try { ImageIcon img = new ImageIcon(ImageIO.read(getResourceAsStream("GUIIcon.png")));
img = new ImageIcon(ImageIO.read(GUI.class.getResourceAsStream("/files/GUIIcon.png")));
} catch (IOException | IllegalArgumentException e) {
img = new ImageIcon("files/GUIIcon.png");
}
frame.setIconImage(img.getImage()); frame.setIconImage(img.getImage());
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
@ -839,15 +834,11 @@ public class GUI implements ActionListener {
/** /**
* Loads popup messages from a text file. * Loads popup messages from a text file.
*/ */
private void loadMessages() { private void loadMessages() throws FileNotFoundException {
Scanner file; Scanner file = getResourceAsScanner("menumsg.csv");
try {
file = new Scanner(new File("config/menumsg.csv"));
} catch (FileNotFoundException e) {
file = new Scanner(GUI.class.getResourceAsStream("/config/menumsg.csv"));
}
while (file.hasNextLine()) { while (file.hasNextLine()) {
String[] line = file.nextLine().split("="); String nextLine = file.nextLine();
String[] line = nextLine.split("=");
String content = line[1].replaceAll("_BREAK_", System.getProperty("line.separator")); String content = line[1].replaceAll("_BREAK_", System.getProperty("line.separator"));
switch (line[0]) { switch (line[0]) {
case "setup": case "setup":
@ -895,7 +886,7 @@ public class GUI implements ActionListener {
return; return;
} }
} }
String files[] = src.list(); String[] files = src.list();
if (files != null) { if (files != null) {
for (String file : files) { for (String file : files) {
File srcFile = new File(src, file); File srcFile = new File(src, file);

View File

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

Can't render this file because it contains an unexpected character in line 1 and column 156.