Skip to content

Commit

Permalink
Updated to updater v-1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
KarmaDeb committed Aug 13, 2020
1 parent 485961f commit cae737d
Show file tree
Hide file tree
Showing 11 changed files with 413 additions and 180 deletions.
58 changes: 38 additions & 20 deletions src/main/java/ml/karmaconfigs/ModPackUpdater/CreateFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class CreateFrame {
private final Utils utils = new Utils();

public static JFrame creatorFrame;
public static JFrame errorFrame;

public static final JFileChooser chooser = new JFileChooser();

Expand All @@ -39,6 +40,7 @@ public final class CreateFrame {
private static JCheckBox unzipDebug;

private static final JComboBox<String> version = new JComboBox<>();
private static String selected = "";

private File mcFolder = FilesUtilities.getConfig.getMinecraftDir();

Expand Down Expand Up @@ -290,6 +292,9 @@ public void windowActivated(WindowEvent e) {
for (String v : ListVersions.listing.versions()) {
version.addItem(v);
}
if (contains(selected)) {
version.setSelectedItem(selected);
}
} catch (Throwable ex) {
utils.log(ex);
}
Expand All @@ -300,29 +305,33 @@ public void windowDeactivated(WindowEvent e) {

}
});

version.addActionListener(e -> selected = String.valueOf(version.getSelectedItem()));
} else {
JFrame errorFrame = new JFrame();
errorFrame.setPreferredSize(new Dimension(500, 100));
if (errorFrame == null) {
errorFrame = new JFrame();
errorFrame.setPreferredSize(new Dimension(500, 100));

try {
errorFrame.setIconImage(ImageIO.read((MainFrame.class).getResourceAsStream("/logo.png")));
} catch (Throwable e) {
e.printStackTrace();
}
try {
errorFrame.setIconImage(ImageIO.read((MainFrame.class).getResourceAsStream("/logo.png")));
} catch (Throwable e) {
e.printStackTrace();
}

JPanel infoPanel = new JPanel();
JLabel error = new JLabel();
error.setText("<html>No forge/fabric version detected,<br>please install a forge or fabric<br>version before using the modpack creator</html>");
JPanel infoPanel = new JPanel();
JLabel error = new JLabel();
error.setText("<html>No forge version detected,<br>please install a forge version<br>before using the modpack creator</html>");

infoPanel.add(error);
infoPanel.add(error);

errorFrame.setTitle("Something went wrong...");
errorFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
errorFrame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
errorFrame.setLocation(dim.width / 2 - errorFrame.getSize().width / 2, dim.height / 2 - errorFrame.getSize().height / 2);
errorFrame.setResizable(false);
errorFrame.add(infoPanel);
errorFrame.setTitle("Something went wrong...");
errorFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
errorFrame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
errorFrame.setLocation(dim.width / 2 - errorFrame.getSize().width / 2, dim.height / 2 - errorFrame.getSize().height / 2);
errorFrame.setResizable(false);
errorFrame.add(infoPanel);
}
errorFrame.setVisible(true);
}
}
Expand All @@ -332,6 +341,15 @@ private void disableResize(JSplitPane... panes) {
pane.setEnabled(false);
}
}

private boolean contains(String value) {
for (int i = 0; i < version.getItemCount(); i++) {
if (version.getItemAt(i).equals(value)) {
return true;
}
}
return false;
}
}

class ListVersions {
Expand All @@ -347,15 +365,15 @@ static String[] versions() throws Throwable {
if (versions != null && !Arrays.asList(versions).isEmpty()) {
for (File version : versions) {
String name = version.getName();
if (name.contains("forge") || name.contains("fabric")) {
if (name.contains("forge")) {
File json = new File(version, name + ".json");
if (json.exists()) {
FileReader reader = new FileReader(json);
JSONParser jsonParser = new JSONParser();
JSONObject info = (JSONObject) jsonParser.parse(reader);

if (info.containsKey("id")) {
if (info.get("id").toString().contains("forge") || info.get("id").toString().contains("fabric")) {
if (info.get("id").toString().contains("forge")) {
names.add(name);
}
}
Expand Down
194 changes: 194 additions & 0 deletions src/main/java/ml/karmaconfigs/ModPackUpdater/LaunchFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package ml.karmaconfigs.ModPackUpdater;

import ml.karmaconfigs.ModPackUpdater.Utils.Files.FilesUtilities;
import ml.karmaconfigs.ModPackUpdater.Utils.Launcher.Launch;
import ml.karmaconfigs.ModPackUpdater.Utils.ModPack.Modpack;
import ml.karmaconfigs.ModPackUpdater.Utils.Utils;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public final class LaunchFrame {

public static JFrame launcherFrame;

private static final JTextField name = new JTextField("Player");
private static final JTextField minMemory = new JTextField("1024");

private static final JButton launch = new JButton("Launch");

private final Modpack modpack;

public LaunchFrame(Modpack modpack) {
this.modpack = modpack;
if (launcherFrame == null) {
launcherFrame = new JFrame();

launcherFrame.setTitle("Launch modpack " + modpack.getName());
launcherFrame.setPreferredSize(new Dimension(500, 300));
try {
launcherFrame.setIconImage(ImageIO.read((MainFrame.class).getResourceAsStream("/logo.png")));
} catch (Throwable e) {
e.printStackTrace();
}

JSplitPane launchParameters = new JSplitPane(JSplitPane.VERTICAL_SPLIT, name, minMemory);
launchParameters.setEnabled(false);
JPanel panel = new JPanel();
panel.add(launch);
JSplitPane launchSplitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, launchParameters, launch);
launchSplitter.setEnabled(false);
launchSplitter.setDividerSize(190);

launcherFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
launcherFrame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
launcherFrame.setLocation(dim.width / 2 - launcherFrame.getSize().width / 2, dim.height / 2 - launcherFrame.getSize().height / 2);
launcherFrame.setResizable(false);
launcherFrame.add(launchSplitter);
}
minMemory.setText(FilesUtilities.getConfig.getClientMemory());
name.setText(FilesUtilities.getConfig.getClientName());
launcherFrame.setVisible(true);
}

public final void display() {
if (modpack.exists()) {
minMemory.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (!Character.isDigit(e.getKeyChar())) {
e.consume();
}
}
});

name.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (name.getText().length() >= 14) {
e.consume();
}
if (!e.isConsumed()) {
if (!Character.isLetterOrDigit(e.getKeyChar()) && !String.valueOf(e.getKeyChar()).equals("_")) {
e.consume();
}
}
}
});

minMemory.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
String correct = onlyNumbers(minMemory.getText());
FilesUtilities.getConfig.saveClientMem(correct);
}

@Override
public void removeUpdate(DocumentEvent e) {
String correct = onlyNumbers(minMemory.getText());
FilesUtilities.getConfig.saveClientMem(correct);
}

@Override
public void changedUpdate(DocumentEvent e) {
String correct = onlyNumbers(minMemory.getText());
FilesUtilities.getConfig.saveClientMem(correct);
}
});

name.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
String correct = noSpecial(name.getText());
FilesUtilities.getConfig.saveClientName(correct);
}

@Override
public void removeUpdate(DocumentEvent e) {
String correct = noSpecial(name.getText());
FilesUtilities.getConfig.saveClientName(correct);
}

@Override
public void changedUpdate(DocumentEvent e) {
String correct = noSpecial(name.getText());
FilesUtilities.getConfig.saveClientName(correct);
}
});

launch.addActionListener(e -> {
new Launch();
launcherFrame.setVisible(false);
});
} else {
JFrame errorFrame = new JFrame();
errorFrame.setPreferredSize(new Dimension(500, 100));

try {
errorFrame.setIconImage(ImageIO.read((MainFrame.class).getResourceAsStream("/logo.png")));
} catch (Throwable e) {
e.printStackTrace();
}

JPanel infoPanel = new JPanel();
JLabel error = new JLabel();
error.setText("<html>The current modpack seems<br>to be null, make sure it's downloaded<br>and installed and try again</html>");

infoPanel.add(error);

errorFrame.setTitle("Something went wrong...");
errorFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
errorFrame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
errorFrame.setLocation(dim.width / 2 - errorFrame.getSize().width / 2, dim.height / 2 - errorFrame.getSize().height / 2);
errorFrame.setResizable(false);
errorFrame.add(infoPanel);
errorFrame.setVisible(true);
}
}

private String onlyNumbers(String original) {
if (!original.isEmpty()) {
StringBuilder formatted = new StringBuilder();

for (int i = 0; i < original.length(); i++) {
String letter = String.valueOf(original.charAt(i));
if (letter.matches(".*[0-9].*")) {
formatted.append(letter);
}
}

return formatted.toString();
} else {
return "1024";
}
}

private String noSpecial(String name) {
if (!name.isEmpty()) {
StringBuilder formatted = new StringBuilder();

int max = 14;
if (name.length() < max) {
max = name.length();
}

for (int i = 0; i < max; i++) {
String letter = String.valueOf(name.charAt(i));
if (letter.matches(".*[aA-zZ].*") || letter.matches(".*[0-9].*") || letter.matches("_")) {
formatted.append(letter);
}
}

return formatted.toString();
} else {
return "Player";
}
}
}
Loading

0 comments on commit cae737d

Please sign in to comment.