Skip to content

Commit

Permalink
refactor with eld-plugin, added partial reload for groovier
Browse files Browse the repository at this point in the history
  • Loading branch information
eric2788 committed Jan 7, 2025
1 parent fbe40d2 commit 86a8379
Show file tree
Hide file tree
Showing 72 changed files with 668 additions and 91 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: build artifacts
on:
push:
branches:
- develop
- '!master'
- '**'
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -22,4 +23,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: groovier-plugin
path: './groovier-plugin/target'
path: './outputs'
39 changes: 39 additions & 0 deletions groovier-plugin/groovier-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.groovier</groupId>
<artifactId>groovier-plugin</artifactId>
<version>0.0.55-SNAPSHOT</version>
</parent>

<artifactId>groovier-common</artifactId>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import com.ericlam.mc.groovier.scriptloaders.*;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.multibindings.Multibinder;

import org.jetbrains.annotations.NotNull;

import javax.inject.Provider;
Expand All @@ -19,13 +22,13 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;

public class GroovierCore implements GroovierAPI, GroovierAddon {

private final GroovierModule groovierModule = new GroovierModule();
public abstract class GroovierCore implements GroovierAPI, GroovierAddon {

protected final GroovierModule groovierModule = new GroovierModule();
private static GroovierAPI api;

public GroovierCore() {
Expand Down Expand Up @@ -54,15 +57,15 @@ public static GroovierAPI getApi() {
private GroovierLifeCycle lifeCycle;


public void onLoad(ScriptPlugin plugin) {
protected void onLoad(ScriptPlugin plugin) {
this.plugin = plugin;
groovierModule.bindScriptPlugin(plugin);
plugin.copyResources();
}


public void onEnable() {
injector = Guice.createInjector(groovierModule);
protected void onEnable(Injector injector) {
this.injector = injector;
loader = injector.getInstance(GroovierScriptLoader.class);
loader.addClassPath();
loader.loadAllScripts().whenComplete((v, e) -> {
Expand All @@ -83,6 +86,15 @@ public CompletableFuture<Void> reloadAllScripts() {
return loader.reloadAllScripts();
}

public CompletableFuture<Void> reloadScript(Class<? extends ScriptLoader> script) {
return loader.reloadScript(script);
}

public Set<ScriptLoader> getScriptLoaders() {
return this.injector.getInstance(new Key<>() {
});
}

@Override
public void addScriptLoader(Class<? extends ScriptLoader> scriptLoader) {
this.groovierModule.addReloadable(scriptLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class GroovierScriptLoader implements ScriptManager {

@Override
CompletableFuture<Void> reloadScript(Class<? extends ScriptLoader> loader) {
return this.reloadAllScripts(this.loaders.findAll { loader.isAssignableFrom(it.class) })
return this.reloadAllScripts(this.loaders.findAll { loader.isInstance(it) })
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.ericlam.mc.groovier.bungee

import com.ericlam.mc.groovier.GroovierCore
import com.ericlam.mc.groovier.ScriptLoader
import com.ericlam.mc.groovier.ScriptLoadingException
import net.md_5.bungee.api.ChatColor
import net.md_5.bungee.api.CommandSender
import net.md_5.bungee.api.chat.TextComponent
import net.md_5.bungee.api.plugin.Command
import net.md_5.bungee.api.plugin.Plugin
import net.md_5.bungee.api.plugin.TabExecutor

class BungeeGroovierCommand extends Command implements TabExecutor {

private final GroovierCore core
private final Plugin plugin

BungeeGroovierCommand(GroovierCore core, Plugin plugin) {
super("groovier", "groovier.use")
this.core = core
this.plugin = plugin
}

@Override
void execute(CommandSender sender, String[] args) {
if (hasPermission(sender)) {
sender.sendMessage(TextComponent.fromLegacy("${ChatColor.RED}no permission."))
}
if (args.length == 0) {
sender.sendMessage(TextComponent.fromLegacy("Usage: /groovier reload [script] | version"))
return
}
if (args[0].equalsIgnoreCase("reload")) {
if (args.length == 1) {
core.reloadAllScripts().whenComplete((v, ex) -> {
if (ex != null) {
if (ex instanceof ScriptLoadingException) {
sender.sendMessage(TextComponent.fromLegacy("${ChatColor.GOLD}Script is still loading, please wait until complete."))
} else {
sender.sendMessage(TextComponent.fromLegacy("${ChatColor.RED}Failed to reload scripts: " + ex.getMessage()))
ex.printStackTrace()
}
} else {
sender.sendMessage(TextComponent.fromLegacy("${ChatColor.GREEN}Successfully reloaded scripts"))
}
})
} else {
def script = args[1]
try {
def scriptClass = Class.forName(script) as Class<? extends ScriptLoader>
core.reloadScript(scriptClass).whenComplete((v, ex) -> {
if (ex != null) {
if (ex instanceof ScriptLoadingException) {
sender.sendMessage(TextComponent.fromLegacy("${ChatColor.GOLD}script ${scriptClass.simpleName} is still loading, please wait until complete."))
} else {
sender.sendMessage(TextComponent.fromLegacy("${ChatColor.RED}Failed to reload script ${scriptClass.simpleName}: " + ex.getMessage()))
ex.printStackTrace()
}
} else {
sender.sendMessage(TextComponent.fromLegacy("${ChatColor.GREEN}Successfully reloaded script ${scriptClass.simpleName}"))
}
})
} catch (ClassNotFoundException ignored) {
sender.sendMessage(TextComponent.fromLegacy("${ChatColor.RED}Script class $script not found."))
}
}
return
} else if (args[0].equalsIgnoreCase("version")) {
sender.sendMessage(TextComponent.fromLegacy("Groovier v${plugin.getDescription().getVersion()} by ${plugin.getDescription().getAuthor()}"))
return
}
sender.sendMessage(TextComponent.fromLegacy("Usage: /groovier reload [script] | version"))
}

@Override
Iterable<String> onTabComplete(CommandSender sender, String[] args) {
if (args.length == 1) {
return ["reload", "version"]
}
if (args.length == 2 && args[0].equalsIgnoreCase("reload")) {
return core.scriptLoaders
.stream()
.map { it.class.name }
.toList()
}
return null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.ericlam.mc.groovier.spigot

import com.ericlam.mc.groovier.GroovierCore
import com.ericlam.mc.groovier.ScriptLoader
import com.ericlam.mc.groovier.ScriptLoadingException
import org.bukkit.ChatColor
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.plugin.java.JavaPlugin
import org.jetbrains.annotations.NotNull

class SpigotGroovierCommand implements CommandExecutor, TabCompleter {

private final JavaPlugin plugin
private final GroovierCore core

SpigotGroovierCommand(JavaPlugin plugin, GroovierCore core) {
this.plugin = plugin
this.core = core
}

@Override
boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!sender.hasPermission("groovier.use")) {
sender.sendMessage("${ChatColor.RED}no permission")
return false
}
if (args.length == 0) {
sender.sendMessage("Usage: /groovier reload [script] | version")
return true
}
var cmd = args[0].toLowerCase()
switch (cmd) {
case "reload":
if (args.length == 1) {
core.reloadAllScripts().whenComplete { v, ex ->
if (ex != null) {
if (ex instanceof ScriptLoadingException) {
sender.sendMessage("${ChatColor.GOLD}Script is still loading, please wait until complete.")
} else {
sender.sendMessage("${ChatColor.RED}Failed to reload scripts: " + ex.getMessage())
ex.printStackTrace()
}
} else {
sender.sendMessage("${ChatColor.GREEN}Successfully reloaded scripts")
}
}
} else {
def script = args[1]
try {
def scriptClass = Class.forName(script) as Class<? extends ScriptLoader>
core.reloadScript(scriptClass).whenComplete((v, ex) -> {
if (ex != null) {
if (ex instanceof ScriptLoadingException) {
sender.sendMessage("${ChatColor.GOLD}script ${scriptClass.simpleName} is still loading, please wait until complete.")
} else {
sender.sendMessage("${ChatColor.RED}Failed to reload script ${scriptClass.simpleName}: " + ex.getMessage())
ex.printStackTrace()
}
} else {
sender.sendMessage("${ChatColor.GREEN}Successfully reloaded script ${scriptClass.simpleName}")
}
})
}catch (ClassNotFoundException ignored) {
sender.sendMessage("${ChatColor.RED}Script class $script not found.")
}
}
return true
case "version":
sender.sendMessage("Groovier v${plugin.getDescription().getVersion()} by ${plugin.getDescription().getAuthors().join(", ")}")
return true
default:
sender.sendMessage("Usage: /groovier reload [script] | version")
return true
}
}

@Override
List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length == 1) {
return ["reload", "version"]
}
if (args.length == 2 && args[0].equalsIgnoreCase("reload")) {
return core.scriptLoaders
.stream()
.map { it.class.name }
.toList()
}
return null
}
}
Loading

0 comments on commit 86a8379

Please sign in to comment.