1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2025-01-05 19:09:03 +01:00

Add creator tools dialog for enabling plugins

This commit is contained in:
Henry Le Grys 2021-03-28 18:03:39 +01:00
parent 638c9d9fb5
commit 7aacb4ec01
10 changed files with 190 additions and 8 deletions

View File

@ -15,7 +15,7 @@ import com.skcraft.launcher.creator.model.creator.CreatorConfig;
import com.skcraft.launcher.creator.model.creator.RecentEntry;
import com.skcraft.launcher.creator.model.creator.Workspace;
import com.skcraft.launcher.creator.plugin.CreatorPluginLoader;
import com.skcraft.launcher.creator.plugin.CreatorToolsPlugin;
import com.skcraft.launcher.creator.plugin.CreatorPluginWrapper;
import com.skcraft.launcher.persistence.Persistence;
import com.skcraft.launcher.swing.SwingHelper;
import lombok.Getter;
@ -35,7 +35,7 @@ public class Creator {
@Getter private final File dataDir;
@Getter private final CreatorConfig config;
@Getter private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
@Getter private final List<CreatorToolsPlugin> plugins;
@Getter private final List<CreatorPluginWrapper<?>> plugins;
public Creator() {
this.dataDir = getAppDataDir();

View File

@ -463,6 +463,15 @@ public class PackManagerController {
selectedPack.ifPresent(pack -> SwingHelper.browseDir(pack.getDirectory(), frame));
});
frame.getEditPluginsMenuItem().addActionListener(e -> {
Optional<Pack> optional = getSelectedPack(true);
if (optional.isPresent()) {
PluginSelectionDialog.showPluginDialog(frame, creator, optional.get());
updatePackInWorkspace(optional.get());
}
});
frame.getCheckProblemsMenuItem().addActionListener(e -> {
Optional<Pack> optional = getSelectedPack(true);
@ -654,6 +663,10 @@ public class PackManagerController {
menuItem.addActionListener(e -> frame.getOpenFolderMenuItem().doClick());
popup.add(menuItem);
menuItem = new JMenuItem("Enabled Plugins...");
menuItem.addActionListener(e -> frame.getEditPluginsMenuItem().doClick());
popup.add(menuItem);
menuItem = new JMenuItem("Check for Problems");
menuItem.addActionListener(e -> frame.getCheckProblemsMenuItem().doClick());
popup.add(menuItem);
@ -711,6 +724,10 @@ public class PackManagerController {
}
} while (!canAddPackDir(dir));
if (!creator.getPlugins().isEmpty()) {
PluginSelectionDialog.showPluginDialog(frame, creator, pack);
}
pack.setLocation(dir.getAbsolutePath());
if (pack.getConfigFile().exists()) {

View File

@ -13,6 +13,7 @@ import com.skcraft.launcher.LauncherUtils;
import com.skcraft.launcher.builder.PackageBuilder;
import com.skcraft.launcher.creator.Creator;
import com.skcraft.launcher.creator.model.creator.Pack;
import com.skcraft.launcher.creator.plugin.CreatorPluginWrapper;
import com.skcraft.launcher.creator.plugin.CreatorToolsPlugin;
import lombok.RequiredArgsConstructor;
@ -61,7 +62,12 @@ public class PackBuilder implements Callable<PackBuilder>, ProgressObservable {
"-o", outputDir.getAbsolutePath()
);
for (CreatorToolsPlugin plugin : creator.getPlugins()) {
for (CreatorPluginWrapper<?> wrapper : creator.getPlugins()) {
if (!pack.getEnabledPlugins().contains(wrapper.getInfo().getId())) {
continue;
}
CreatorToolsPlugin plugin = wrapper.getInstance();
if (plugin.getBuilderPlugin() != null) {
args.add("--plugin-class");
args.add(plugin.getBuilderPlugin().getCanonicalName());

View File

@ -39,6 +39,7 @@ public class PackManagerFrame extends JFrame {
@Getter private final JMenuItem quitMenuItem = new JMenuItem("Exit");
@Getter private final JMenuItem editConfigMenuItem = new JMenuItem("Edit modpack.json...");
@Getter private final JMenuItem openFolderMenuItem = new JMenuItem("Open Directory");
@Getter private final JMenuItem editPluginsMenuItem = new JMenuItem("Enabled Plugins...");
@Getter private final JMenuItem checkProblemsMenuItem = new JMenuItem("Scan for Problems...");
@Getter private final JMenuItem testMenuItem = new JMenuItem("Test");
@Getter private final JMenuItem testOnlineMenuItem = new JMenuItem("Test Online");
@ -153,6 +154,7 @@ public class PackManagerFrame extends JFrame {
menuBar.add(menu);
menu.add(editConfigMenuItem);
menu.add(openFolderMenuItem);
menu.add(editPluginsMenuItem);
menu.addSeparator();
menu.add(checkProblemsMenuItem);

View File

@ -0,0 +1,72 @@
package com.skcraft.launcher.creator.dialog;
import com.skcraft.launcher.creator.Creator;
import com.skcraft.launcher.creator.model.creator.Pack;
import com.skcraft.launcher.creator.model.swing.PluginsTableModel;
import com.skcraft.launcher.swing.CheckboxTable;
import com.skcraft.launcher.swing.LinedBoxPanel;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class PluginSelectionDialog extends JDialog {
private final LinedBoxPanel panel = new LinedBoxPanel(false).fullyPadded();
private final JLabel title = new JLabel("Select which plugins to use while building this pack.");
private final CheckboxTable pluginsTable = new CheckboxTable();
private final LinedBoxPanel buttonsPanel = new LinedBoxPanel(true);
private final JButton okButton = new JButton("OK");
private List<PluginsTableModel.PluginModel> plugins;
public PluginSelectionDialog(Window owner, Creator creator, Pack pack) {
super(owner, "Enable Plugins", ModalityType.DOCUMENT_MODAL);
this.plugins = creator.getPlugins().stream()
.map(wrapper -> {
PluginsTableModel.PluginModel model = new PluginsTableModel.PluginModel(wrapper.getInfo().getId());
model.setEnabled(pack.getEnabledPlugins().contains(model.getPluginId()));
return model;
})
.collect(Collectors.toList());
setTitle("Select plugins to enable");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
initComponents();
setPreferredSize(new Dimension(400, 320));
pack();
setLocationRelativeTo(owner);
}
private void initComponents() {
pluginsTable.setModel(new PluginsTableModel(plugins));
pluginsTable.setRowSelectionAllowed(false);
okButton.setMargin(new Insets(0, 10, 0, 10));
buttonsPanel.addGlue();
buttonsPanel.addElement(okButton);
panel.addElement(title);
panel.addElement(pluginsTable);
panel.addElement(buttonsPanel);
add(panel);
okButton.addActionListener(e -> PluginSelectionDialog.this.dispose());
}
public static void showPluginDialog(Window owner, Creator creator, Pack pack) {
PluginSelectionDialog dialog = new PluginSelectionDialog(owner, creator, pack);
dialog.setVisible(true);
Set<String> enabled = dialog.plugins.stream()
.filter(PluginsTableModel.PluginModel::isEnabled)
.map(PluginsTableModel.PluginModel::getPluginId)
.collect(Collectors.toSet());
pack.setEnabledPlugins(enabled);
}
}

View File

@ -13,11 +13,14 @@ import com.skcraft.launcher.persistence.Persistence;
import lombok.Data;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
@Data
public class Pack {
private String location;
private Set<String> enabledPlugins = new HashSet<>();
@JsonIgnore private Workspace workspace;
@JsonIgnore private BuilderConfig cachedConfig;

View File

@ -0,0 +1,70 @@
package com.skcraft.launcher.creator.model.swing;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import javax.swing.table.AbstractTableModel;
import java.util.List;
@RequiredArgsConstructor
public class PluginsTableModel extends AbstractTableModel {
private final List<PluginModel> plugins;
@Override
public int getRowCount() {
return plugins.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return plugins.get(rowIndex).isEnabled();
} else {
return plugins.get(rowIndex).getPluginId();
}
}
@Override
public String getColumnName(int column) {
if (column == 1) {
return "Plugin";
}
return null;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return Boolean.class;
case 1:
return String.class;
default:
return null;
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
plugins.get(rowIndex).setEnabled((Boolean) aValue);
}
}
@Data
public static class PluginModel {
private final String pluginId;
private boolean enabled;
}
}

View File

@ -39,7 +39,7 @@ public class CreatorPluginLoader extends DirectoryWalker {
}
}
public List<CreatorToolsPlugin> loadAll() {
public List<CreatorPluginWrapper<?>> loadAll() {
URLClassLoader pluginClassLoader = new URLClassLoader(
candidates.stream().map(PluginCandidate::getJarUrl).toArray(URL[]::new),
this.getClass().getClassLoader()
@ -53,11 +53,12 @@ public class CreatorPluginLoader extends DirectoryWalker {
.collect(Collectors.toList());
}
private <T extends CreatorToolsPlugin> CreatorToolsPlugin loadPlugin(URLClassLoader classLoader, PluginCandidate candidate) {
private <T extends CreatorToolsPlugin> CreatorPluginWrapper<T> loadPlugin(URLClassLoader classLoader, PluginCandidate candidate) {
try {
Class<T> pluginClass = (Class<T>) classLoader.loadClass(candidate.getInfo().getPluginClass());
return pluginClass.getConstructor().newInstance();
T instance = pluginClass.getConstructor().newInstance();
return new CreatorPluginWrapper<>(candidate.getInfo(), instance);
} catch (ClassNotFoundException e) {
log.warning(candidate.format("Could not find plugin class %s for plugin %s"));
} catch (ClassCastException e) {

View File

@ -0,0 +1,9 @@
package com.skcraft.launcher.creator.plugin;
import lombok.Data;
@Data
public class CreatorPluginWrapper<T extends CreatorToolsPlugin> {
private final CreatorPluginInfo info;
private final T instance;
}

View File

@ -419,8 +419,10 @@ public class PackageBuilder implements Builder {
// Load plugins
BuilderPluginLoader pluginLoader = new BuilderPluginLoader();
pluginLoader.loadClasses(options.getPluginClasses());
pluginLoader.dispatchAcceptOptions(options, args);
if (options.getPluginClasses() != null) {
pluginLoader.loadClasses(options.getPluginClasses());
pluginLoader.dispatchAcceptOptions(options, args);
}
Manifest manifest = new Manifest();
manifest.setMinimumVersion(Manifest.MIN_PROTOCOL_VERSION);