Skip to content

Commit 1856cfa

Browse files
committed
[F] Separate info presentation
1 parent c50d166 commit 1856cfa

File tree

6 files changed

+136
-92
lines changed

6 files changed

+136
-92
lines changed

src/main/java/org/hydev/mcpm/client/arguments/parsers/InfoParser.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import net.sourceforge.argparse4j.inf.Namespace;
44
import net.sourceforge.argparse4j.inf.Subparser;
55
import org.hydev.mcpm.client.commands.controllers.InfoController;
6+
import org.hydev.mcpm.client.commands.presenters.InfoPresenter;
7+
import org.hydev.mcpm.client.loader.PluginNotFoundException;
68

79
import java.util.function.Consumer;
810

911
/**
1012
* Command parser for the info use case
1113
*/
12-
public record InfoParser(InfoController controller) implements CommandParser
14+
public record InfoParser(InfoController controller, InfoPresenter presenter) implements CommandParser
1315
{
1416
@Override
1517
public String name()
@@ -26,7 +28,15 @@ public String description()
2628
@Override
2729
public void run(Namespace details, Consumer<String> log)
2830
{
29-
controller.info(details.getString("name"), log);
31+
var name = details.getString("name");
32+
try
33+
{
34+
presenter.present(controller.info(name), log);
35+
}
36+
catch (PluginNotFoundException e)
37+
{
38+
log.accept("&cPlugin not " + name + " not found.");
39+
}
3040
}
3141

3242
@Override
Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,23 @@
11
package org.hydev.mcpm.client.commands.controllers;
22

3-
import org.apache.commons.lang3.StringUtils;
4-
import org.apache.commons.text.WordUtils;
53
import org.hydev.mcpm.client.database.tracker.PluginTracker;
6-
import org.hydev.mcpm.utils.ColorLogger;
7-
8-
import javax.annotation.Nullable;
9-
import java.util.Arrays;
10-
import java.util.List;
11-
import java.util.Map;
12-
import java.util.function.Consumer;
4+
import org.hydev.mcpm.client.loader.PluginNotFoundException;
5+
import org.hydev.mcpm.client.models.PluginYml;
136

147
/**
158
* Controller for the info use case
169
*/
1710
public record InfoController(PluginTracker tracker)
1811
{
19-
private static final int LEN_LEFT = 12;
20-
private static final int LEN_RIGHT = 60;
21-
private static final String KV_PRE = "&7> &f";
22-
private static final String KV_SEP = " : &6";
23-
private static final int LEN_INDENT = LEN_LEFT + ColorLogger.lengthNoColor(KV_PRE + KV_SEP);
24-
25-
/**
26-
* Format a kv pair if value isn't null, return empty string if value is null
27-
*
28-
* @param key Key
29-
* @param rawValue Value
30-
* @return Formatted pair
31-
*/
32-
private static String formatPair(String key, @Nullable Object rawValue)
33-
{
34-
if (rawValue == null) return "";
35-
36-
// If a map is passed in, only show its keys
37-
if (rawValue instanceof Map<?, ?> v)
38-
{
39-
rawValue = v.keySet().stream().toList();
40-
}
41-
42-
String value;
43-
44-
if (rawValue instanceof List<?> v)
45-
{
46-
// If a list is passed in, join it with commas.
47-
value = String.join(", ", v.stream().map(Object::toString).toList());
48-
}
49-
else
50-
{
51-
// If any other types of object is passed in, just make it string
52-
value = rawValue.toString();
53-
}
54-
55-
// Wrap text
56-
value = String.join("\n", Arrays.stream(WordUtils.wrap(value, LEN_RIGHT).split("\n"))
57-
.map(it -> StringUtils.repeat(" ", LEN_INDENT) + "&6" + it).toList()).strip();
58-
59-
return KV_PRE + StringUtils.rightPad(key, LEN_LEFT) + KV_SEP + value + "&r\n";
60-
}
61-
6212
/**
6313
* Run the info command
6414
*
6515
* @param name Name of the plugin
66-
* @param log Logger
16+
*
6717
*/
68-
public void info(String name, Consumer<String> log)
18+
public PluginYml info(String name) throws PluginNotFoundException
6919
{
70-
try
71-
{
72-
var pl = tracker.listInstalled().stream().filter(it -> it.name().equalsIgnoreCase(name)).findFirst()
73-
.orElseThrow(() -> new AssertionError(String.format("Cannot find plugin '%s'", name)));
74-
75-
String msg = "&bPlugin Info:\n" +
76-
formatPair("Name", "&b" + pl.name()) +
77-
formatPair("Main", pl.main()) +
78-
formatPair("Version", pl.version()) +
79-
formatPair("Description", pl.description()) +
80-
formatPair("Author", pl.author()) +
81-
formatPair("Authors", pl.authors()) +
82-
formatPair("Website", pl.website()) +
83-
formatPair("API Version", pl.apiVersion()) +
84-
formatPair("Depend", pl.depend()) +
85-
formatPair("Soft Depend", pl.softdepend()) +
86-
formatPair("Load Before", pl.loadbefore()) +
87-
formatPair("Libraries", pl.libraries()) +
88-
formatPair("Commands", pl.commands());
89-
log.accept(msg);
90-
}
91-
catch (AssertionError e)
92-
{
93-
log.accept("&c" + e.getMessage());
94-
}
20+
return tracker.listInstalled().stream().filter(it -> it.name().equalsIgnoreCase(name)).findFirst()
21+
.orElseThrow(() -> new PluginNotFoundException(String.format("Cannot find plugin '%s'", name)));
9522
}
9623
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.hydev.mcpm.client.commands.presenters;
2+
3+
import org.hydev.mcpm.client.models.PluginYml;
4+
5+
import java.util.function.Consumer;
6+
7+
/**
8+
* Present plugin info command output
9+
*/
10+
public interface InfoPresenter
11+
{
12+
/**
13+
* Present results
14+
*/
15+
void present(PluginYml yml, Consumer<String> log);
16+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.hydev.mcpm.client.display.presenters;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.apache.commons.text.WordUtils;
5+
import org.hydev.mcpm.client.commands.presenters.InfoPresenter;
6+
import org.hydev.mcpm.client.models.PluginYml;
7+
import org.hydev.mcpm.utils.ColorLogger;
8+
9+
import javax.annotation.Nullable;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.function.Consumer;
14+
15+
/**
16+
* Implementation of InfoPresenter
17+
*/
18+
public class KVInfoPresenter implements InfoPresenter
19+
{
20+
private static final int LEN_LEFT = 12;
21+
private static final int LEN_RIGHT = 60;
22+
private static final String KV_PRE = "&7> &f";
23+
private static final String KV_SEP = " : &6";
24+
private static final int LEN_INDENT = LEN_LEFT + ColorLogger.lengthNoColor(KV_PRE + KV_SEP);
25+
26+
/**
27+
* Format a kv pair if value isn't null, return empty string if value is null
28+
*
29+
* @param key Key
30+
* @param rawValue Value
31+
* @return Formatted pair
32+
*/
33+
private static String formatPair(String key, @Nullable Object rawValue)
34+
{
35+
if (rawValue == null) return "";
36+
37+
// If a map is passed in, only show its keys
38+
if (rawValue instanceof Map<?, ?> v)
39+
{
40+
rawValue = v.keySet().stream().toList();
41+
}
42+
43+
String value;
44+
45+
if (rawValue instanceof List<?> v)
46+
{
47+
// If a list is passed in, join it with commas.
48+
value = String.join(", ", v.stream().map(Object::toString).toList());
49+
}
50+
else
51+
{
52+
// If any other types of object is passed in, just make it string
53+
value = rawValue.toString();
54+
}
55+
56+
// Wrap text
57+
value = String.join("\n", Arrays.stream(WordUtils.wrap(value, LEN_RIGHT).split("\n"))
58+
.map(it -> StringUtils.repeat(" ", LEN_INDENT) + "&6" + it).toList()).strip();
59+
60+
return KV_PRE + StringUtils.rightPad(key, LEN_LEFT) + KV_SEP + value + "&r\n";
61+
}
62+
63+
@Override
64+
public void present(PluginYml pl, Consumer<String> log)
65+
{
66+
String msg = "&bPlugin Info:\n" +
67+
formatPair("Name", "&b" + pl.name()) +
68+
formatPair("Main", pl.main()) +
69+
formatPair("Version", pl.version()) +
70+
formatPair("Description", pl.description()) +
71+
formatPair("Author", pl.author()) +
72+
formatPair("Authors", pl.authors()) +
73+
formatPair("Website", pl.website()) +
74+
formatPair("API Version", pl.apiVersion()) +
75+
formatPair("Depend", pl.depend()) +
76+
formatPair("Soft Depend", pl.softdepend()) +
77+
formatPair("Load Before", pl.loadbefore()) +
78+
formatPair("Libraries", pl.libraries()) +
79+
formatPair("Commands", pl.commands());
80+
log.accept(msg);
81+
}
82+
}

src/main/java/org/hydev/mcpm/client/loader/UnloadBoundary.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ public interface UnloadBoundary
1414
* @throws PluginNotFoundException If a loaded plugin of the name isn't found
1515
*/
1616
File unloadPlugin(String name) throws PluginNotFoundException;
17+
18+
/**
19+
* Unload a plugin then delete it
20+
*
21+
* @param name Name of the plugin
22+
* @return Whether the plugin is successfully unloaded and deleted
23+
* @throws PluginNotFoundException Plugin is not found
24+
*/
25+
default boolean unloadAndDeletePlugin(String name) throws PluginNotFoundException
26+
{
27+
return unloadPlugin(name).delete();
28+
}
1729
}

src/main/java/org/hydev/mcpm/client/uninstall/Uninstaller.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import org.hydev.mcpm.client.loader.PluginNotFoundException;
55
import org.hydev.mcpm.client.loader.UnloadBoundary;
66

7-
import java.io.File;
8-
97
import static org.hydev.mcpm.client.uninstall.UninstallResult.State.*;
108

119

@@ -38,22 +36,21 @@ public UninstallResult uninstall(UninstallInput input) {
3836
// this will return the jar of the currently loaded plugin, which is the most precise.
3937
// Since people may still uninstall a plugin when it's not loaded, we ignore the not
4038
// found error here.
41-
File jar = null;
4239
if (unloader != null) {
4340
try {
4441
// In the minecraft server environment, we can find the exact jar of the loaded
4542
// plugin because java's url class loader keeps track of jar file paths.
46-
jar = unloader.unloadPlugin(input.name());
43+
if (input.delete()) {
44+
if (!unloader.unloadAndDeletePlugin(input.name())) {
45+
return new UninstallResult(FAILED_TO_DELETE);
46+
}
47+
}
48+
else {
49+
unloader.unloadPlugin(input.name());
50+
}
4751
}
4852
catch (PluginNotFoundException ignored) { }
4953
}
50-
51-
if (jar != null) {
52-
// Delete file
53-
if (input.delete() && !jar.delete()) {
54-
return new UninstallResult(FAILED_TO_DELETE);
55-
}
56-
}
5754
else if (input.delete()) {
5855
// When unloader is not null, it means that we are in CLI environment, so we need to
5956
// find the plugin of the name

0 commit comments

Comments
 (0)