Skip to content

Commit fc78273

Browse files
committed
Merge branch 'feat-spigot-framework'
2 parents 8898a16 + 15df852 commit fc78273

24 files changed

+838
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ build
3030
.gradle
3131
data
3232
.mcpm
33+
*.pyc

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ For example, you can test the progress bar with:
3131

3232
If you don't have JDK 19 installed or if you don't know where it's installed, you can use our JDK downloader tool to download a local version of JDK 19 without installing on the system. (TODO: Add tutorial after merging PR #8)
3333

34+
## Development
35+
36+
Setup and start a testing minecraft server:
37+
38+
1. `pip install -r requirements.txt`
39+
2. `python3 -m tools.start_server`
40+
41+
Rebuild & update our MCPM plugin while the server is running:
42+
43+
1. `python3 -m tools.update_build`
44+
2. `plugman reload mcpm` (Inside the server's command prompt)
45+
3446
## Brainstorm
3547

3648
Server file/endpoint structure:

build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
import org.apache.tools.ant.filters.ReplaceTokens
2+
13
plugins {
24
id 'java'
35
id 'maven-publish'
46
id 'checkstyle'
7+
id "com.github.johnrengelman.shadow" version "7.1.2"
58
}
69

710
group 'org.hydev.csc207'
811
version '0.1.0.51'
912

1013
repositories {
1114
mavenCentral()
15+
16+
// Spigot MC
17+
maven {
18+
url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
19+
content {
20+
includeGroup 'org.bukkit'
21+
includeGroup 'org.spigotmc'
22+
}
23+
}
1224
}
1325

1426
java {
@@ -28,6 +40,12 @@ checkstyle {
2840

2941

3042
dependencies {
43+
// The Spigot API with no shadowing. Requires the OSS repo.
44+
compileOnly 'org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT'
45+
46+
// https://mvnrepository.com/artifact/net.sourceforge.argparse4j/argparse4j
47+
implementation 'net.sourceforge.argparse4j:argparse4j:0.9.0'
48+
3149
// Apache HTTP Client
3250
// https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5-fluent
3351
implementation 'org.apache.httpcomponents.client5:httpclient5-fluent:5.2-beta1'
@@ -42,6 +60,9 @@ dependencies {
4260
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
4361
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.0-rc1'
4462

63+
// https://mvnrepository.com/artifact/com.google.guava/guava
64+
implementation 'com.google.guava:guava:31.1-jre'
65+
4566
// YAML parser
4667
// https://mvnrepository.com/artifact/org.yaml/snakeyaml
4768
implementation group: 'org.yaml', name: 'snakeyaml', version: '1.33'
@@ -57,6 +78,13 @@ dependencies {
5778
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
5879
}
5980

81+
processResources {
82+
from(sourceSets.main.resources.srcDirs) {
83+
filter ReplaceTokens, tokens: [version: version]
84+
}
85+
duplicatesStrategy = 'include'
86+
}
87+
6088
publishing {
6189
repositories {
6290
maven {

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
requests
2+
hypy_utils>=1.0.16
3+
py-cpuinfo
4+
tqdm
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.hydev.mcpm;
2+
3+
import net.sourceforge.argparse4j.ArgumentParsers;
4+
import net.sourceforge.argparse4j.inf.ArgumentParser;
5+
import net.sourceforge.argparse4j.inf.ArgumentParserException;
6+
7+
/**
8+
* Static collection of argument parsers for the command line
9+
*
10+
* @author Azalea (https://github.com/hykilpikonna)
11+
* @since 2022-10-30
12+
*/
13+
public class ArgsParser
14+
{
15+
public static final ArgumentParser LOADER;
16+
17+
static
18+
{
19+
LOADER = ArgumentParsers.newFor("loader").build().defaultHelp(true);
20+
LOADER.addArgument("name");
21+
}
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.hydev.mcpm;
2+
3+
import net.sourceforge.argparse4j.inf.ArgumentParserException;
4+
import org.hydev.mcpm.client.injector.*;
5+
6+
import java.util.function.Consumer;
7+
8+
import static java.lang.String.format;
9+
import static org.hydev.mcpm.utils.Sugar.sub;
10+
import static org.hydev.mcpm.utils.Sugar.subFrom;
11+
12+
13+
/**
14+
* Controller of the program
15+
*
16+
* @author Azalea (https://github.com/hykilpikonna)
17+
* @since 2022-10-30
18+
*/
19+
public class Controller
20+
{
21+
private final LoadBoundary loader;
22+
private final UnloadBoundary unloader;
23+
private final ReloadBoundary reloader;
24+
25+
public Controller()
26+
{
27+
var pl = new PluginLoader();
28+
loader = pl;
29+
unloader = pl;
30+
reloader = pl;
31+
}
32+
33+
/**
34+
* Run a command
35+
*
36+
* @param args Command-line arguments (not including the command name/path)
37+
* @param log Callback logger
38+
*/
39+
public void runCommand(String[] args, Consumer<String> log)
40+
{
41+
if (args.length == 0) return;
42+
43+
var cmd = args[0].toLowerCase();
44+
switch (cmd)
45+
{
46+
case "load", "unload", "reload" ->
47+
{
48+
try
49+
{
50+
var a = ArgsParser.LOADER.parseArgs(subFrom(args, 1));
51+
var name = a.getString("name");
52+
53+
try
54+
{
55+
switch (cmd)
56+
{
57+
case "load" -> loader.loadPlugin(name);
58+
case "unload" -> unloader.unloadPlugin(name);
59+
case "reload" -> reloader.reloadPlugin(name);
60+
}
61+
log.accept(format("Plugin %s %sed", name, cmd));
62+
}
63+
catch (PluginNotFoundException e)
64+
{
65+
log.accept(format("Plugin %s not found", name));
66+
}
67+
}
68+
catch (ArgumentParserException e)
69+
{
70+
log.accept("Error: " + e.getMessage());
71+
log.accept(ArgsParser.LOADER.formatHelp());
72+
}
73+
}
74+
default ->
75+
{
76+
log.accept("Usage: /mcpm <install/uninstall/update/search/load/unload/reload/help>");
77+
}
78+
}
79+
}
80+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.hydev.mcpm;
2+
3+
import org.bukkit.command.Command;
4+
import org.bukkit.command.CommandExecutor;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.plugin.java.JavaPlugin;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.util.Arrays;
10+
import java.util.Objects;
11+
import java.util.logging.Logger;
12+
13+
import static java.util.Objects.requireNonNull;
14+
15+
/**
16+
* Entrypoint for the Spigot plugin adapter of our program
17+
*/
18+
public class SpigotEntry extends JavaPlugin implements CommandExecutor
19+
{
20+
private Logger log;
21+
private Controller controller;
22+
23+
/**
24+
* onEnable() is called when our plugin is loaded on a Spigot server.
25+
*/
26+
@Override
27+
public void onEnable()
28+
{
29+
// Initialize logger
30+
log = getLogger();
31+
log.info("Enabled!");
32+
33+
// Initialize controller
34+
controller = new Controller();
35+
36+
// Register mcpm command
37+
requireNonNull(this.getCommand("mcpm")).setExecutor(this);
38+
}
39+
40+
/**
41+
* onDisable() is called when our plugin is unloaded on a Spigot server.
42+
*/
43+
@Override
44+
public void onDisable()
45+
{
46+
log.info("Disabled!");
47+
}
48+
49+
@Override
50+
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args)
51+
{
52+
controller.runCommand(args, sender::sendMessage);
53+
return true;
54+
}
55+
}

src/main/java/org/hydev/mcpm/client/database/DatabaseLoader.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.hydev.mcpm.client.database;
22

3-
import org.hydev.mcpm.client.models.Plugin;
3+
import org.hydev.mcpm.client.models.PluginModel;
44

55
import java.util.HashMap;
66
import java.util.List;
@@ -15,13 +15,13 @@
1515
public class DatabaseLoader
1616
{
1717
/** Name index: map[lower-cased name] = Plugin of that name */
18-
private final Map<String, Plugin> nameIndex;
18+
private final Map<String, PluginModel> nameIndex;
1919

2020
/** Keyword index: map[lower keyword] = List[plugins that contain the keyword either in name or description] */
21-
private final Map<String, List<Plugin>> keywordIndex;
21+
private final Map<String, List<PluginModel>> keywordIndex;
2222

2323
/** Command index: map[lower command name/alias] = List[plugins that provide the command or alias] */
24-
private final Map<String, List<Plugin>> commandIndex;
24+
private final Map<String, List<PluginModel>> commandIndex;
2525

2626
/**
2727
* Load database, create index for faster searching through the database
@@ -44,7 +44,7 @@ public DatabaseLoader(String path)
4444
* @param name Name of the plugin
4545
* @return Plugin of that name, or null if not found
4646
*/
47-
public Plugin findByName(String name)
47+
public PluginModel findByName(String name)
4848
{
4949
// TODO: Implement this
5050
throw new UnsupportedOperationException("TODO");
@@ -63,7 +63,7 @@ public Plugin findByName(String name)
6363
* @param keyword Keyword
6464
* @return List of packages matching the keyword, or empty list
6565
*/
66-
public List<Plugin> searchByKeyword(String keyword)
66+
public List<PluginModel> searchByKeyword(String keyword)
6767
{
6868
// TODO: Implement this
6969
throw new UnsupportedOperationException("TODO");
@@ -75,7 +75,7 @@ public List<Plugin> searchByKeyword(String keyword)
7575
* @param command Command name
7676
* @return Plugins that provides the command, or empty list
7777
*/
78-
public List<Plugin> searchByCommand(String command)
78+
public List<PluginModel> searchByCommand(String command)
7979
{
8080
// TODO: Implement this
8181
throw new UnsupportedOperationException("TODO");
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.hydev.mcpm.client.injector;
2+
3+
import java.io.File;
4+
5+
/**
6+
* Interface for loading a locally installed plugin.
7+
*
8+
* @author Azalea (https://github.com/hykilpikonna)
9+
* @since 2022-10-29
10+
*/
11+
public interface LoadBoundary
12+
{
13+
/**
14+
* Dynamically load a local plugin through JVM reflections and classloader hacks
15+
*
16+
* @param name Loaded plugin name
17+
* @return True if success, false if failed
18+
* @throws PluginNotFoundException Plugin of the name is not found in the plugins directory
19+
*/
20+
public boolean loadPlugin(String name) throws PluginNotFoundException;
21+
22+
/**
23+
* Dynamically load a local plugin through JVM reflections and classloader hacks
24+
*
25+
* @param jar Local jar file path
26+
* @return True if success, false if failed
27+
*/
28+
public boolean loadPlugin(File jar);
29+
}

0 commit comments

Comments
 (0)