From 99ca3eedd83e1dd03ac1ebc93a69e1cfbbe7e375 Mon Sep 17 00:00:00 2001 From: Moresteck Date: Mon, 28 Jun 2021 13:32:23 +0100 Subject: [PATCH] 1.09_13-pre5, more intuitive addons list --- .../java/org/betacraft/launcher/Addon.java | 31 +++++--- .../java/org/betacraft/launcher/Launcher.java | 39 +++++----- .../org/betacraft/launcher/SelectAddons.java | 73 ++++++++----------- 3 files changed, 70 insertions(+), 73 deletions(-) diff --git a/src/main/java/org/betacraft/launcher/Addon.java b/src/main/java/org/betacraft/launcher/Addon.java index 21a4d72..3db80c8 100644 --- a/src/main/java/org/betacraft/launcher/Addon.java +++ b/src/main/java/org/betacraft/launcher/Addon.java @@ -1,6 +1,5 @@ package org.betacraft.launcher; -import java.awt.Color; import java.io.File; import java.io.FilenameFilter; import java.io.InputStream; @@ -9,24 +8,26 @@ import java.net.URL; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Scanner; import javax.swing.JEditorPane; -import javax.swing.JOptionPane; import javax.swing.JScrollPane; -import javax.swing.border.MatteBorder; public class Addon { + public static String addonVer = "1.09_13"; public String name; + public String onlinehash; public boolean online; public JScrollPane info; // When you click on "Show info" button, you get a new window popping, // with just the page embed about the addon. Same for mods. TODO - public Addon(String name, boolean online) { + public Addon(String name, String hash, boolean online) { this.name = name; + this.onlinehash = hash; this.online = online; this.info = getInfo(); } @@ -38,7 +39,7 @@ public JScrollPane getInfo() { pane.setContentType("text/html;charset=UTF-8"); pane.addHyperlinkListener(WebsitePanel.EXTERNAL_HYPERLINK_LISTENER); try { - pane.setPage(new URL("http://files.betacraft.pl/launcher/assets/addons/" + this.name + ".html")); + pane.setPage(new URL("http://files.betacraft.pl/launcher/assets/addons/" + addonVer + "/" + this.name + ".html")); } catch (Exception ex) { ex.printStackTrace(); Logger.printException(ex); @@ -50,7 +51,7 @@ public JScrollPane getInfo() { return scrlPane; } - public static ArrayList addons = new ArrayList(); + public static HashMap addons = new HashMap(); public static void loadAddons() { try { @@ -61,7 +62,7 @@ public boolean accept(File dir, String fileName) { } }); - final URL url = new URL("http://files.betacraft.pl/launcher/assets/addons/1.09_10/list.txt"); + final URL url = new URL("http://files.betacraft.pl/launcher/assets/addons/" + addonVer + "/list.txt"); InputStream onlineListStream = null; try { @@ -83,7 +84,8 @@ public boolean accept(File dir, String fileName) { // If connection failed, load the offline list if (onlineListStream == null) { for (String s : offlineAddons) { - addons.add(new Addon(s.substring(0, s.length() -4), false)); + String name = s.substring(0, s.length() -4); + addons.put(name, new Addon(name, null, false)); } return; } @@ -91,12 +93,16 @@ public boolean accept(File dir, String fileName) { // Scan the offline list for online duplicates, Scanner onlineListScanner = new Scanner(onlineListStream, "UTF-8"); for (String ver : scan(onlineListScanner)) { + if (ver == null) continue; + String[] split = ver.split("`"); + String addonname = split[0]; + String addonhash = split[1]; for (int i = 0; i < offlineAddons.length; i++) { - if (offlineAddons[i] != null && ver != null) { + if (offlineAddons[i] != null) { // From x.class to x // If the addon from offline list matches the addon from online list - if (offlineAddons[i].substring(0, offlineAddons[i].length() -4).equals(ver)) { + if (offlineAddons[i].substring(0, offlineAddons[i].length() -4).equals(addonname)) { // ... Then remove it from the offline addons list // Otherwise it would appear doubled in the list offlineAddons[i] = null; @@ -105,14 +111,15 @@ public boolean accept(File dir, String fileName) { } // Add the online addon to the addons list - addons.add(new Addon(ver, true)); + addons.put(addonname, new Addon(addonname, addonhash, true)); } // Add offline addons to the addons list for (int i = 0; i < offlineAddons.length; i++) { // Skip previously removed duplicates if (offlineAddons[i] == null) continue; - addons.add(new Addon(offlineAddons[i].substring(0, offlineAddons[i].length() -4), false)); + String name = offlineAddons[i].substring(0, offlineAddons[i].length() -4); + addons.put(name, new Addon(name, null, false)); } // Close the connection diff --git a/src/main/java/org/betacraft/launcher/Launcher.java b/src/main/java/org/betacraft/launcher/Launcher.java index ee780eb..64b65b8 100644 --- a/src/main/java/org/betacraft/launcher/Launcher.java +++ b/src/main/java/org/betacraft/launcher/Launcher.java @@ -49,7 +49,7 @@ /** Main class */ public class Launcher { - public static String VERSION = "1.09_13-pre4"; // TODO Always update this + public static String VERSION = "1.09_13-pre5"; // TODO Always update this public static Instance currentInstance; public static boolean forceUpdate = false; @@ -427,9 +427,8 @@ public static void initStartup() { if (!Launcher.isLaunchMethodReady(Launcher.currentInstance.version) || Launcher.forceUpdate) { Launcher.downloadLaunchMethod(Launcher.currentInstance.version); } - if (!Launcher.areAddonsReady(Launcher.currentInstance.version) || Launcher.forceUpdate) { - Launcher.downloadAddons(Launcher.currentInstance.version); - } + + Launcher.readyAddons(Launcher.currentInstance, Launcher.forceUpdate); if (OS.isMac()) { if ("true".equalsIgnoreCase(info.getEntry("macos-mousefix"))) { @@ -631,14 +630,6 @@ public static void downloadLaunchMethod(String version) { } } - public static void downloadAddons(String version) { - for (String s : Launcher.currentInstance.addons) { - if (!downloadWithButtonOutput("http://files.betacraft.pl/launcher/assets/addons/1.09_10/" + s + ".jar", new File(BC.get() + "launcher" + File.separator + "addons", s + ".jar")).isPositive()) { - JOptionPane.showMessageDialog(Window.mainWindow, "Couldn't download addon: " + s, "Error", JOptionPane.ERROR_MESSAGE); - } - } - } - public static boolean isLaunchMethodReady(String version) { boolean bol1 = true; VersionInfo json = Release.getReleaseByName(version).getInfo(); @@ -650,14 +641,26 @@ public static boolean isLaunchMethodReady(String version) { return bol1; } - public static boolean areAddonsReady(String version) { - boolean bol2 = true; - for (String s : Launcher.currentInstance.addons) { - if (new File(BC.get() + "launcher" + File.separator + "addons", s + ".jar").exists() == false) { - bol2 = false; + public static void readyAddons(Instance instance, boolean force) { + for (String s : instance.addons) { + boolean download = false; + + Addon a = Addon.addons.get(s); + File destination = new File(BC.get() + "launcher" + File.separator + "addons", s + ".jar"); + + if (!destination.exists()) { + download = true; + } else if (a.online) { + String filehash = Util.getSHA1(destination); + if (!filehash.equalsIgnoreCase(a.onlinehash)) { + download = true; + } + } + + if (download && !downloadWithButtonOutput("http://files.betacraft.pl/launcher/assets/addons/" + Addon.addonVer + "/" + s + ".jar", destination).isPositive()) { + JOptionPane.showMessageDialog(Window.mainWindow, "Couldn't download addon: " + s, "Error", JOptionPane.ERROR_MESSAGE); } } - return bol2; } public static boolean isVersionReady(String version) { diff --git a/src/main/java/org/betacraft/launcher/SelectAddons.java b/src/main/java/org/betacraft/launcher/SelectAddons.java index d10d84d..ae8d684 100644 --- a/src/main/java/org/betacraft/launcher/SelectAddons.java +++ b/src/main/java/org/betacraft/launcher/SelectAddons.java @@ -7,13 +7,14 @@ import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; +import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; @@ -22,7 +23,6 @@ public class SelectAddons extends JFrame implements ActionListener, LanguageElement { static JScrollPane listScroller; - static JButton more_button; static JButton OK; static Order order = Order.FROM_OLDEST; static JPanel panel; @@ -47,7 +47,6 @@ public SelectAddons() { public void update() { this.setTitle(Lang.ADDON_LIST_TITLE); - more_button.setText(Lang.ADDON_SHOW_INFO); OK.setText(Lang.OPTIONS_OK); this.pack(); } @@ -66,12 +65,6 @@ protected void makeList(boolean addoninfo) { constr.gridwidth = GridBagConstraints.RELATIVE; constr.weightx = 1.0; - more_button = new JButton(Lang.ADDON_SHOW_INFO); - more_button.addActionListener(this); - - panel.add(more_button, constr); - this.add(panel, BorderLayout.NORTH); - panel = new JPanel(); panel.setLayout(new GridBagLayout()); constr.gridy = 2; @@ -86,62 +79,65 @@ protected void makeList(boolean addoninfo) { panel = new JPanel(); panel.setLayout(new GridBagLayout()); - //constr.fill = GridBagConstraints.HORIZONTAL; - //constr.insets = new Insets(5, 5, 0, 5); - //constr.gridwidth = 1; - //constr.gridheight = 1; - //constr.weightx = 0.0; - //constr.gridx = 1; - //if (addoninfo) { - //JScrollPane pane = new JScrollPane(); - //pane.setPreferredSize(new Dimension(100, 100)); - //panel.add(pane, constr); - //} - //this.add(panel, BorderLayout.CENTER); } protected void updateList() { checkboxes.clear(); JPanel listpanel = new JPanel(); listpanel.setLayout(new GridBagLayout()); - listpanel.setMaximumSize(new Dimension(282, 300)); + //listpanel.setMaximumSize(new Dimension(282, 300)); GridBagConstraints constr1 = new GridBagConstraints(); constr1.gridx = 0; constr1.gridy = 0; constr1.fill = GridBagConstraints.HORIZONTAL; - constr1.weightx = 1.0; - //constr1.insets = new Insets(10, 10, 0, 10); - for (Addon item : Addon.addons) { - JCheckBox checkbox = new JCheckBox(item.name); + constr1.weightx = 0.0; + constr1.insets = new Insets(5, 5, 0, 5); + + for (Addon item : Addon.addons.values()) { + JCheckBox checkbox = new JCheckBox(); for (String addon : Launcher.currentInstance.addons) { if (addon.equals(item.name)) checkbox.setSelected(true); } - listpanel.add(checkbox, constr1); - checkbox.addFocusListener(new FocusListener() { + JLabel label = new JLabel(item.name); + label.addMouseListener(new MouseListener() { - @Override - public void focusGained(FocusEvent arg0) { - lastFocus = (JCheckBox) arg0.getSource(); + public void mousePressed(MouseEvent e) { + String name = ((JLabel)e.getSource()).getText(); + Addon a = Addon.addons.get(name); + new BrowserWindow(a.getInfo()); } - public void focusLost(FocusEvent arg0) {} + + public void mouseClicked(MouseEvent e) {} + public void mouseReleased(MouseEvent e) {} + public void mouseEntered(MouseEvent e) {} + public void mouseExited(MouseEvent e) {} }); + + listpanel.add(checkbox, constr1); + constr1.gridx = 1; + constr1.weightx = 1.0; + listpanel.add(label, constr1); + constr1.gridx = 0; + constr1.weightx = 0.0; + checkboxes.add(checkbox); constr1.gridy++; } - //listpanel.validate(); constr.weighty = 1.0; constr.gridheight = GridBagConstraints.RELATIVE; constr.gridy = 1; + constr.insets = new Insets(5, 5, 5, 5); if (listScroller != null) panel.remove(listScroller); listScroller = new JScrollPane(listpanel); listScroller.setWheelScrollingEnabled(true); + listScroller.getVerticalScrollBar().setUnitIncrement(10); panel.add(listScroller, constr); } @@ -158,20 +154,11 @@ public void saveAddons() { Launcher.currentInstance.saveInstance(); } - JCheckBox lastFocus = null; - @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == OK) { saveAddons(); Window.addonsList = null; - } else if (e.getSource() == more_button) { - if (lastFocus == null) return; - for (Addon a : Addon.addons) { - if (a.name.equals(lastFocus.getText())) { - new BrowserWindow(a.getInfo()); - } - } } } }