Skip to content

Commit 6dacd66

Browse files
committed
feat: add download and skript dependency system
1 parent c0c57da commit 6dacd66

10 files changed

Lines changed: 464 additions & 69 deletions

File tree

build.gradle

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
id 'com.gradleup.shadow' version '9.2.2'
77
}
88

9-
group = 'de.skriptlibrary.SkLibraryAddon'
9+
group = 'de.skriptlibrary.SkLibrary'
1010
version = '0.0.1'
1111
def targetJavaVersion = 21
1212

@@ -18,6 +18,11 @@ repositories {
1818

1919
dependencies {
2020
compileOnly("io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT")
21+
22+
compileOnly("org.projectlombok:lombok:1.18.42")
23+
annotationProcessor("org.projectlombok:lombok:1.18.42")
24+
25+
implementation(group: 'org.bstats', name: 'bstats-bukkit', version: '3.1.0')
2126
}
2227

2328
compileJava {
@@ -30,6 +35,11 @@ tasks {
3035
// This is the only required configuration besides applying the plugin.
3136
// Your plugin's jar (or shadowJar if present) will be used automatically.
3237
minecraftVersion("1.21.8")
38+
39+
downloadPlugins {
40+
github("SkriptLang", "Skript", "2.13.1", "Skript-2.13.1.jar")
41+
modrinth("plugmanx", "3.0.2")
42+
}
3343
}
3444
}
3545

@@ -50,6 +60,9 @@ processResources {
5060

5161
shadowJar {
5262
archiveFileName = project.name + "-" + project.version + ".jar"
63+
64+
relocate("org.bstats", "de.skriptlibrary.SkLibrary.metrics")
65+
5366
exclude('META-INF/**')
5467
}
5568

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'SkLibraryAddon'
1+
rootProject.name = 'SkLibrary'
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package de.skriptlibrary.SkLibrary;
2+
3+
import de.skriptlibrary.SkLibrary.commands.SkLibraryCommand;
4+
import de.skriptlibrary.SkLibrary.skripts.SkriptManager;
5+
import org.bstats.bukkit.Metrics;
6+
import org.bstats.charts.SimplePie;
7+
import org.bukkit.Bukkit;
8+
import org.bukkit.plugin.Plugin;
9+
import org.bukkit.plugin.java.JavaPlugin;
10+
11+
import java.io.IOException;
12+
13+
public final class SkLibrary extends JavaPlugin {
14+
15+
public static SkLibrary instance;
16+
public static SkriptManager skriptManager;
17+
18+
@Override
19+
public void onEnable() {
20+
21+
instance = this;
22+
23+
skriptManager = new SkriptManager();
24+
25+
try {
26+
skriptManager.LoadAllSkripts();
27+
} catch (IOException e) {
28+
throw new RuntimeException(e);
29+
}
30+
31+
Metrics metrics = new Metrics(this, 27995);
32+
Plugin skriptPlugin = Bukkit.getServer().getPluginManager().getPlugin("Skript");
33+
34+
if (skriptPlugin != null) {
35+
metrics.addCustomChart(new SimplePie("skript_version", () -> skriptPlugin.getPluginMeta().getVersion()));
36+
}
37+
38+
39+
getCommand("sklibrary").setExecutor(new SkLibraryCommand());
40+
41+
}
42+
43+
@Override
44+
public void onDisable() {
45+
Bukkit.getServer().getCommandMap().getKnownCommands().remove("sklibrary");
46+
Bukkit.getServer().getCommandMap().getKnownCommands().remove("sklibrary:sklibrary");
47+
}
48+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package de.skriptlibrary.SkLibrary.commands;
2+
3+
import de.skriptlibrary.SkLibrary.SkLibrary;
4+
import de.skriptlibrary.SkLibrary.skripts.Skript;
5+
import net.kyori.adventure.text.Component;
6+
import net.kyori.adventure.text.TextComponent;
7+
import net.kyori.adventure.text.event.HoverEvent;
8+
import net.kyori.adventure.text.format.TextColor;
9+
import org.bukkit.ChatColor;
10+
import org.bukkit.command.Command;
11+
import org.bukkit.command.CommandSender;
12+
import org.bukkit.command.TabExecutor;
13+
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
15+
16+
import java.io.IOException;
17+
import java.net.URISyntaxException;
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
public class SkLibraryCommand implements TabExecutor {
22+
23+
@Override
24+
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] args) {
25+
String usageMessage = "Usage: /SkLibrary <download/info/list/reload>";
26+
27+
if (args.length == 0) {
28+
sender.sendMessage(usageMessage);
29+
return true;
30+
}
31+
32+
switch(args[0].toLowerCase()) {
33+
case "download":
34+
if (args.length == 1) {
35+
sender.sendMessage("Please specify a Skript id!");
36+
return true;
37+
}
38+
39+
if (!SkLibrary.skriptManager.getAvailableSkripts().contains(args[1])) {
40+
sender.sendMessage(ChatColor.RED + "There is no Skript with that id!");
41+
return true;
42+
}
43+
44+
String id = args[1];
45+
String version = args.length >= 3 ? args[2] : "latest";
46+
47+
if (SkLibrary.skriptManager.SkriptExists(id)) {
48+
sender.sendMessage(ChatColor.RED + "There is already a Skript with that id!");
49+
return true;
50+
}
51+
52+
try {
53+
SkLibrary.skriptManager.DownloadSkript(id, version);
54+
} catch (IOException | URISyntaxException e) {
55+
sender.sendMessage(ChatColor.RED + "Failed to download the Skript with that id and version! (" + e.getMessage() + ")");
56+
return true;
57+
}
58+
break;
59+
/*case "update":
60+
sender.sendMessage("Update");
61+
break;*/
62+
case "list":
63+
List<Skript> skripts = SkLibrary.skriptManager.getSkripts();
64+
65+
TextComponent tc = Component.text("Loaded Skripts: \n");
66+
67+
for (int i = 0; i < skripts.size(); i++) {
68+
69+
Skript skript = skripts.get(i);
70+
71+
TextComponent htc = Component.text(skript.Name).color(TextColor.fromHexString("#00cc00"));
72+
73+
htc = htc.append(Component.text( " (" + skript.id + ") - " + skript.Version));
74+
75+
if (!skript.SkriptDependencies.isEmpty()) {
76+
htc = htc.append(Component.text("\n\nskript dependencies:\n"));
77+
78+
for (Map.Entry<String, String> dep : skript.SkriptDependencies.entrySet()) {
79+
htc = htc.append(Component.text("- " + dep.getKey()/* + ": " + dep.getValue()*/));
80+
}
81+
}
82+
83+
tc = tc.append(Component.text(skript.Name).hoverEvent(HoverEvent.showText(htc)).color(TextColor.fromHexString("#00cc00")));
84+
85+
if (i != skripts.size() - 1) {
86+
tc = tc.append(Component.text(", "));
87+
}
88+
}
89+
90+
sender.sendMessage(tc);
91+
92+
break;
93+
case "reload":
94+
try {
95+
SkLibrary.skriptManager.LoadAllSkripts();
96+
SkLibrary.skriptManager.LoadAvailableSkripts();
97+
sender.sendMessage(ChatColor.GREEN + "Sucessfully loaded SkLibrary!");
98+
} catch (IOException | URISyntaxException e) {
99+
throw new RuntimeException(e);
100+
}
101+
break;
102+
case "info":
103+
sender.sendMessage("Version: " + SkLibrary.instance.getPluginMeta().getVersion());
104+
break;
105+
default:
106+
sender.sendMessage(usageMessage);
107+
break;
108+
109+
}
110+
111+
return true;
112+
}
113+
114+
@Override
115+
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] args) {
116+
117+
return switch (args.length) {
118+
case 1 -> List.of("download", /*"update", */"info", "list", "reload");
119+
case 2 -> switch (args[0].toLowerCase()) {
120+
case "download" -> SkLibrary.skriptManager.getAvailableSkripts();
121+
default -> List.of();
122+
};
123+
default -> List.of();
124+
};
125+
}
126+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package de.skriptlibrary.SkLibrary.skripts;
2+
3+
import de.skriptlibrary.SkLibrary.SkLibrary;
4+
5+
import java.io.IOException;
6+
import java.net.URISyntaxException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.*;
10+
11+
public class Skript {
12+
13+
public String id;
14+
public String Name;
15+
public String Version;
16+
public EnumSet<SkriptFlag> Flags;
17+
public Map<String, String> AddonDependencies;
18+
public Map<String, String> SkriptDependencies;
19+
20+
public void DownloadSkriptDependencies() {
21+
this.SkriptDependencies.forEach((key, value) -> {
22+
try {
23+
if (!Files.exists(Path.of(SkLibrary.skriptManager.SkLibrarySkriptsPath.toString(), key, key + ".sk"))) {
24+
SkLibrary.skriptManager.DownloadSkript(key, value);
25+
}
26+
} catch (IOException | URISyntaxException e) {
27+
throw new RuntimeException(e);
28+
}
29+
});
30+
}
31+
32+
public Skript(
33+
String id,
34+
String name,
35+
String version
36+
) {
37+
this(id, name, version, EnumSet.noneOf(SkriptFlag.class), new HashMap<>(), new HashMap<>());
38+
}
39+
40+
public Skript(
41+
String id,
42+
String name,
43+
String version,
44+
EnumSet<SkriptFlag> flags
45+
) {
46+
this(id, name, version, flags, new HashMap<>(), new HashMap<>());
47+
}
48+
49+
public Skript(
50+
String id,
51+
String name,
52+
String version,
53+
EnumSet<SkriptFlag> flags,
54+
Map<String, String> addonDependencies
55+
) {
56+
this(id, name, version, flags, addonDependencies, new HashMap<>());
57+
}
58+
59+
public Skript(
60+
String id,
61+
String name,
62+
String version,
63+
EnumSet<SkriptFlag> flags,
64+
Map<String, String> addonDependencies,
65+
Map<String, String> skriptDependencies
66+
) {
67+
this.id = id;
68+
this.Name = name;
69+
this.Version = version;
70+
this.Flags = flags;
71+
this.AddonDependencies = addonDependencies;
72+
this.SkriptDependencies = skriptDependencies;
73+
}
74+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package de.skriptlibrary.SkLibrary.skripts;
2+
3+
public enum SkriptFlag {
4+
Standalone("standalone"),
5+
Library("library");
6+
7+
public final String id;
8+
9+
SkriptFlag(String id) {
10+
this.id = id;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return this.id;
16+
}
17+
18+
public static SkriptFlag fromString(String value) {
19+
for (SkriptFlag skriptFlag : values()) {
20+
if (skriptFlag.id.equalsIgnoreCase(value)) {
21+
return skriptFlag;
22+
}
23+
}
24+
25+
throw new IllegalArgumentException("Unknown SkriptFlag: " + value);
26+
}
27+
}

0 commit comments

Comments
 (0)