Changes resource loading
Some checks failed
KnarCraft/Minecraft-Server-Launcher/master There was a failure building this commit
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:
parent
f841d73e2d
commit
c59cbcefbb
@ -20,9 +20,7 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static net.knarcraft.minecraftserverlauncher.Shared.downloadFile;
|
||||
import static net.knarcraft.minecraftserverlauncher.Shared.readFile;
|
||||
import static net.knarcraft.minecraftserverlauncher.Shared.stringBetween;
|
||||
import static net.knarcraft.minecraftserverlauncher.Shared.*;
|
||||
//Java 8 required.
|
||||
|
||||
/**
|
||||
@ -186,17 +184,17 @@ public class Main {
|
||||
/**
|
||||
* Checks if a newer version is available
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws IOException <p>If the update check fails.</p>
|
||||
*/
|
||||
private static void checkForUpdate() throws IOException {
|
||||
Scanner file;
|
||||
try {
|
||||
file = new Scanner(new File("config/currentversion.csv"));
|
||||
} catch (FileNotFoundException e) {
|
||||
file = new Scanner(Main.class.getResourceAsStream("/config/currentversion.csv"));
|
||||
Scanner file = getResourceAsScanner("currentversion.csv");
|
||||
if (!file.hasNextLine()) {
|
||||
throw new FileNotFoundException("File currentversion.csv is invalid");
|
||||
}
|
||||
|
||||
String oldType = file.nextLine();
|
||||
if (!file.hasNextLine()) {
|
||||
throw new FileNotFoundException("File currentversion.csv is invalid");
|
||||
}
|
||||
String oldVer = file.nextLine();
|
||||
file.close();
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.knarcraft.minecraftserverlauncher;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
@ -16,6 +17,30 @@ import java.util.Scanner;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
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.
|
||||
*
|
||||
|
@ -16,6 +16,7 @@ import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.Executors;
|
||||
@ -193,7 +194,7 @@ public class Profile {
|
||||
if (name == null) { //If a user cancels or crosses out window
|
||||
return;
|
||||
}
|
||||
if (name.equals("") && !name.contains("!") && !name.contains("?") && !name.contains(";")) {
|
||||
if (name.equals("") && !name.matches("^[!?;]+$")) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"Profile name can't be blank.",
|
||||
@ -228,11 +229,7 @@ public class Profile {
|
||||
|
||||
public static void removeProfile(String name) {
|
||||
if (profiles.size() > 1) {
|
||||
for (int i = 0; i < profiles.size(); i++) {
|
||||
if (profiles.get(i).name.equals((name))) {
|
||||
profiles.remove(i);
|
||||
}
|
||||
}
|
||||
profiles.removeIf(profile -> profile.name.equals(name));
|
||||
}
|
||||
}
|
||||
|
||||
@ -376,7 +373,7 @@ public class Profile {
|
||||
throw new FileNotFoundException("Unable to save to the profiles file.");
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
} catch (IOException | NullPointerException e) {
|
||||
if (gui != null) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
@ -434,18 +431,31 @@ public class Profile {
|
||||
JOptionPane.ERROR_MESSAGE
|
||||
);
|
||||
System.exit(1);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (profiles.size() == 0) {
|
||||
addProfile("Default");
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
} catch (FileNotFoundException | NoSuchElementException e) {
|
||||
JOptionPane.showMessageDialog(
|
||||
null,
|
||||
"A profiles file was not found. Default profile was created.",
|
||||
"Info",
|
||||
JOptionPane.INFORMATION_MESSAGE
|
||||
);
|
||||
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");
|
||||
}
|
||||
gui.update();
|
||||
@ -549,7 +559,7 @@ public class Profile {
|
||||
String url = Objects.requireNonNull(type).getDownloadURL(), name = type.getName(), newestVersion;
|
||||
AdvancedServerType advType = type instanceof AdvancedServerType ? (AdvancedServerType) type : null;
|
||||
for (String version : type.getVersions()) {
|
||||
Boolean success;
|
||||
boolean success;
|
||||
printToGui("Downloading: " + name + version + ".jar");
|
||||
File file = new File(jarDir + type.getName() + version + ".jar");
|
||||
Path filePath = Paths.get(jarDir + type.getName() + version + ".jar");
|
||||
|
@ -6,6 +6,8 @@ import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static net.knarcraft.minecraftserverlauncher.Shared.getResourceAsScanner;
|
||||
|
||||
/**
|
||||
* Contains the bare minimum to be a functional server type.
|
||||
*
|
||||
@ -80,11 +82,10 @@ public class ServerType {
|
||||
if (serverTypes.isEmpty()) {
|
||||
Scanner file;
|
||||
try {
|
||||
file = new Scanner(new File("config/servertypes.csv"));
|
||||
file = getResourceAsScanner("servertypes.csv");
|
||||
} catch (FileNotFoundException e) {
|
||||
file = new Scanner(ServerType.class.getResourceAsStream("/config/servertypes.csv"));
|
||||
throw new ConfigurationException("Server type configuration file is missing.");
|
||||
}
|
||||
|
||||
while (file.hasNextLine()) {
|
||||
String[] str = file.nextLine().split(";", -1);
|
||||
int len = str.length;
|
||||
|
@ -19,6 +19,8 @@ import java.util.concurrent.Executors;
|
||||
|
||||
import static java.awt.Frame.NORMAL;
|
||||
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.
|
||||
@ -60,7 +62,7 @@ public class GUI implements ActionListener {
|
||||
/**
|
||||
* Create the application window.
|
||||
*/
|
||||
public GUI() {
|
||||
public GUI() throws IOException {
|
||||
initialize(440, 170);
|
||||
loadMessages();
|
||||
this.globalPlayers = new ArrayList<>();
|
||||
@ -72,7 +74,7 @@ public class GUI implements ActionListener {
|
||||
* @param width The preferred width
|
||||
* @param height The preferred height
|
||||
*/
|
||||
public GUI(int width, int height) {
|
||||
public GUI(int width, int height) throws IOException {
|
||||
initialize(width, height);
|
||||
loadMessages();
|
||||
this.globalPlayers = new ArrayList<>();
|
||||
@ -117,11 +119,7 @@ public class GUI implements ActionListener {
|
||||
* @param name The name of the player to remove.
|
||||
*/
|
||||
public void removePlayer(String name) {
|
||||
for (int i = 0; i < this.globalPlayers.size(); i++) {
|
||||
if (this.globalPlayers.get(i).equals(name)) {
|
||||
this.globalPlayers.remove(i);
|
||||
}
|
||||
}
|
||||
globalPlayers.removeIf(playerName -> playerName.equals(name));
|
||||
this.updatePlayers();
|
||||
}
|
||||
|
||||
@ -165,7 +163,7 @@ public class GUI implements ActionListener {
|
||||
/**
|
||||
* Creates the GUI,
|
||||
*/
|
||||
private void initialize(int width, int height) {
|
||||
private void initialize(int width, int height) throws IOException {
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (ClassNotFoundException |
|
||||
@ -179,12 +177,9 @@ public class GUI implements ActionListener {
|
||||
frame = new JFrame("Minecraft server launcher");
|
||||
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||
frame.getContentPane().setPreferredSize(new Dimension(width, height));
|
||||
ImageIcon img;
|
||||
try {
|
||||
img = new ImageIcon(ImageIO.read(GUI.class.getResourceAsStream("/files/GUIIcon.png")));
|
||||
} catch (IOException | IllegalArgumentException e) {
|
||||
img = new ImageIcon("files/GUIIcon.png");
|
||||
}
|
||||
|
||||
ImageIcon img = new ImageIcon(ImageIO.read(getResourceAsStream("GUIIcon.png")));
|
||||
|
||||
frame.setIconImage(img.getImage());
|
||||
|
||||
JMenuBar menuBar = new JMenuBar();
|
||||
@ -839,15 +834,11 @@ public class GUI implements ActionListener {
|
||||
/**
|
||||
* Loads popup messages from a text file.
|
||||
*/
|
||||
private void loadMessages() {
|
||||
Scanner file;
|
||||
try {
|
||||
file = new Scanner(new File("config/menumsg.csv"));
|
||||
} catch (FileNotFoundException e) {
|
||||
file = new Scanner(GUI.class.getResourceAsStream("/config/menumsg.csv"));
|
||||
}
|
||||
private void loadMessages() throws FileNotFoundException {
|
||||
Scanner file = getResourceAsScanner("menumsg.csv");
|
||||
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"));
|
||||
switch (line[0]) {
|
||||
case "setup":
|
||||
@ -895,7 +886,7 @@ public class GUI implements ActionListener {
|
||||
return;
|
||||
}
|
||||
}
|
||||
String files[] = src.list();
|
||||
String[] files = src.list();
|
||||
if (files != null) {
|
||||
for (String file : files) {
|
||||
File srcFile = new File(src, file);
|
||||
|
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB |
Can't render this file because it contains an unexpected character in line 1 and column 156.
|
Loading…
Reference in New Issue
Block a user