Skip to content

Commit

Permalink
1.09_13-pre5, more intuitive addons list
Browse files Browse the repository at this point in the history
  • Loading branch information
Moresteck committed Jun 28, 2021
1 parent db5e7d2 commit 99ca3ee
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 73 deletions.
31 changes: 19 additions & 12 deletions src/main/java/org/betacraft/launcher/Addon.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.betacraft.launcher;

import java.awt.Color;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -50,7 +51,7 @@ public JScrollPane getInfo() {
return scrlPane;
}

public static ArrayList<Addon> addons = new ArrayList<Addon>();
public static HashMap<String, Addon> addons = new HashMap<String, Addon>();

public static void loadAddons() {
try {
Expand All @@ -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 {
Expand All @@ -83,20 +84,25 @@ 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;
}

// 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;
Expand All @@ -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
Expand Down
39 changes: 21 additions & 18 deletions src/main/java/org/betacraft/launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))) {
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
73 changes: 30 additions & 43 deletions src/main/java/org/betacraft/launcher/SelectAddons.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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();
}
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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());
}
}
}
}
}

0 comments on commit 99ca3ee

Please sign in to comment.