|
| 1 | +package ml.karmaconfigs.ModPackUpdater; |
| 2 | + |
| 3 | +import ml.karmaconfigs.ModPackUpdater.Utils.Files.FilesUtilities; |
| 4 | +import ml.karmaconfigs.ModPackUpdater.Utils.Launcher.Launch; |
| 5 | +import ml.karmaconfigs.ModPackUpdater.Utils.ModPack.Modpack; |
| 6 | +import ml.karmaconfigs.ModPackUpdater.Utils.Utils; |
| 7 | + |
| 8 | +import javax.imageio.ImageIO; |
| 9 | +import javax.swing.*; |
| 10 | +import javax.swing.event.DocumentEvent; |
| 11 | +import javax.swing.event.DocumentListener; |
| 12 | +import java.awt.*; |
| 13 | +import java.awt.event.KeyAdapter; |
| 14 | +import java.awt.event.KeyEvent; |
| 15 | + |
| 16 | +public final class LaunchFrame { |
| 17 | + |
| 18 | + public static JFrame launcherFrame; |
| 19 | + |
| 20 | + private static final JTextField name = new JTextField("Player"); |
| 21 | + private static final JTextField minMemory = new JTextField("1024"); |
| 22 | + |
| 23 | + private static final JButton launch = new JButton("Launch"); |
| 24 | + |
| 25 | + private final Modpack modpack; |
| 26 | + |
| 27 | + public LaunchFrame(Modpack modpack) { |
| 28 | + this.modpack = modpack; |
| 29 | + if (launcherFrame == null) { |
| 30 | + launcherFrame = new JFrame(); |
| 31 | + |
| 32 | + launcherFrame.setTitle("Launch modpack " + modpack.getName()); |
| 33 | + launcherFrame.setPreferredSize(new Dimension(500, 300)); |
| 34 | + try { |
| 35 | + launcherFrame.setIconImage(ImageIO.read((MainFrame.class).getResourceAsStream("/logo.png"))); |
| 36 | + } catch (Throwable e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + |
| 40 | + JSplitPane launchParameters = new JSplitPane(JSplitPane.VERTICAL_SPLIT, name, minMemory); |
| 41 | + launchParameters.setEnabled(false); |
| 42 | + JPanel panel = new JPanel(); |
| 43 | + panel.add(launch); |
| 44 | + JSplitPane launchSplitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, launchParameters, launch); |
| 45 | + launchSplitter.setEnabled(false); |
| 46 | + launchSplitter.setDividerSize(190); |
| 47 | + |
| 48 | + launcherFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); |
| 49 | + launcherFrame.pack(); |
| 50 | + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); |
| 51 | + launcherFrame.setLocation(dim.width / 2 - launcherFrame.getSize().width / 2, dim.height / 2 - launcherFrame.getSize().height / 2); |
| 52 | + launcherFrame.setResizable(false); |
| 53 | + launcherFrame.add(launchSplitter); |
| 54 | + } |
| 55 | + minMemory.setText(FilesUtilities.getConfig.getClientMemory()); |
| 56 | + name.setText(FilesUtilities.getConfig.getClientName()); |
| 57 | + launcherFrame.setVisible(true); |
| 58 | + } |
| 59 | + |
| 60 | + public final void display() { |
| 61 | + if (modpack.exists()) { |
| 62 | + minMemory.addKeyListener(new KeyAdapter() { |
| 63 | + @Override |
| 64 | + public void keyTyped(KeyEvent e) { |
| 65 | + if (!Character.isDigit(e.getKeyChar())) { |
| 66 | + e.consume(); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + name.addKeyListener(new KeyAdapter() { |
| 72 | + @Override |
| 73 | + public void keyTyped(KeyEvent e) { |
| 74 | + if (name.getText().length() >= 14) { |
| 75 | + e.consume(); |
| 76 | + } |
| 77 | + if (!e.isConsumed()) { |
| 78 | + if (!Character.isLetterOrDigit(e.getKeyChar()) && !String.valueOf(e.getKeyChar()).equals("_")) { |
| 79 | + e.consume(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + minMemory.getDocument().addDocumentListener(new DocumentListener() { |
| 86 | + @Override |
| 87 | + public void insertUpdate(DocumentEvent e) { |
| 88 | + String correct = onlyNumbers(minMemory.getText()); |
| 89 | + FilesUtilities.getConfig.saveClientMem(correct); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void removeUpdate(DocumentEvent e) { |
| 94 | + String correct = onlyNumbers(minMemory.getText()); |
| 95 | + FilesUtilities.getConfig.saveClientMem(correct); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void changedUpdate(DocumentEvent e) { |
| 100 | + String correct = onlyNumbers(minMemory.getText()); |
| 101 | + FilesUtilities.getConfig.saveClientMem(correct); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + name.getDocument().addDocumentListener(new DocumentListener() { |
| 106 | + @Override |
| 107 | + public void insertUpdate(DocumentEvent e) { |
| 108 | + String correct = noSpecial(name.getText()); |
| 109 | + FilesUtilities.getConfig.saveClientName(correct); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void removeUpdate(DocumentEvent e) { |
| 114 | + String correct = noSpecial(name.getText()); |
| 115 | + FilesUtilities.getConfig.saveClientName(correct); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void changedUpdate(DocumentEvent e) { |
| 120 | + String correct = noSpecial(name.getText()); |
| 121 | + FilesUtilities.getConfig.saveClientName(correct); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + launch.addActionListener(e -> { |
| 126 | + new Launch(); |
| 127 | + launcherFrame.setVisible(false); |
| 128 | + }); |
| 129 | + } else { |
| 130 | + JFrame errorFrame = new JFrame(); |
| 131 | + errorFrame.setPreferredSize(new Dimension(500, 100)); |
| 132 | + |
| 133 | + try { |
| 134 | + errorFrame.setIconImage(ImageIO.read((MainFrame.class).getResourceAsStream("/logo.png"))); |
| 135 | + } catch (Throwable e) { |
| 136 | + e.printStackTrace(); |
| 137 | + } |
| 138 | + |
| 139 | + JPanel infoPanel = new JPanel(); |
| 140 | + JLabel error = new JLabel(); |
| 141 | + error.setText("<html>The current modpack seems<br>to be null, make sure it's downloaded<br>and installed and try again</html>"); |
| 142 | + |
| 143 | + infoPanel.add(error); |
| 144 | + |
| 145 | + errorFrame.setTitle("Something went wrong..."); |
| 146 | + errorFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); |
| 147 | + errorFrame.pack(); |
| 148 | + Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); |
| 149 | + errorFrame.setLocation(dim.width / 2 - errorFrame.getSize().width / 2, dim.height / 2 - errorFrame.getSize().height / 2); |
| 150 | + errorFrame.setResizable(false); |
| 151 | + errorFrame.add(infoPanel); |
| 152 | + errorFrame.setVisible(true); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private String onlyNumbers(String original) { |
| 157 | + if (!original.isEmpty()) { |
| 158 | + StringBuilder formatted = new StringBuilder(); |
| 159 | + |
| 160 | + for (int i = 0; i < original.length(); i++) { |
| 161 | + String letter = String.valueOf(original.charAt(i)); |
| 162 | + if (letter.matches(".*[0-9].*")) { |
| 163 | + formatted.append(letter); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return formatted.toString(); |
| 168 | + } else { |
| 169 | + return "1024"; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private String noSpecial(String name) { |
| 174 | + if (!name.isEmpty()) { |
| 175 | + StringBuilder formatted = new StringBuilder(); |
| 176 | + |
| 177 | + int max = 14; |
| 178 | + if (name.length() < max) { |
| 179 | + max = name.length(); |
| 180 | + } |
| 181 | + |
| 182 | + for (int i = 0; i < max; i++) { |
| 183 | + String letter = String.valueOf(name.charAt(i)); |
| 184 | + if (letter.matches(".*[aA-zZ].*") || letter.matches(".*[0-9].*") || letter.matches("_")) { |
| 185 | + formatted.append(letter); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + return formatted.toString(); |
| 190 | + } else { |
| 191 | + return "Player"; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments