-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
256 additions
and
33 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/org/allaymc/scriptpluginext/ScriptPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.allaymc.scriptpluginext; | ||
|
||
/** | ||
* @author daoge_cmd | ||
*/ | ||
public interface ScriptPlugin { | ||
boolean canResetContext(); | ||
|
||
void resetContext(); | ||
} |
9 changes: 3 additions & 6 deletions
9
src/main/java/org/allaymc/scriptpluginext/ScriptPluginDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
package org.allaymc.scriptpluginext; | ||
|
||
import lombok.Getter; | ||
import org.allaymc.server.plugin.SimplePluginDescriptor; | ||
import org.allaymc.api.plugin.PluginDescriptor; | ||
|
||
/** | ||
* @author daoge_cmd | ||
*/ | ||
@SuppressWarnings("FieldMayBeFinal") | ||
@Getter | ||
public class ScriptPluginDescriptor extends SimplePluginDescriptor { | ||
private int debugPort = -1; | ||
public interface ScriptPluginDescriptor extends PluginDescriptor { | ||
int getDebugPort(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/org/allaymc/scriptpluginext/ScriptPluginExtensionCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.allaymc.scriptpluginext; | ||
|
||
import org.allaymc.api.command.SimpleCommand; | ||
import org.allaymc.api.command.tree.CommandContext; | ||
import org.allaymc.api.command.tree.CommandTree; | ||
import org.allaymc.api.plugin.PluginContainer; | ||
import org.allaymc.api.server.Server; | ||
|
||
/** | ||
* @author daoge_cmd | ||
*/ | ||
public class ScriptPluginExtensionCommand extends SimpleCommand { | ||
public ScriptPluginExtensionCommand() { | ||
super("se", "The main command of the script plugin extension."); | ||
} | ||
|
||
@Override | ||
public void prepareCommandTree(CommandTree tree) { | ||
tree.getRoot() | ||
.key("resetctx") | ||
.str("plugin") | ||
.optional() | ||
.exec(context -> { | ||
String pluginName = context.getResult(1); | ||
if (pluginName.isBlank()) { | ||
// Reset ctx of all script plugins | ||
Server.getInstance().getPluginManager().getEnabledPlugins().forEach((name, container) -> tryResetContextOf(context, name, container)); | ||
} else { | ||
var container = Server.getInstance().getPluginManager().getEnabledPlugin(pluginName); | ||
if (container == null) { | ||
context.addError("Plugin not found: " + pluginName); | ||
return context.fail(); | ||
} | ||
|
||
tryResetContextOf(context, pluginName, container); | ||
} | ||
context.addOutput("Done"); | ||
return context.success(); | ||
}); | ||
} | ||
|
||
protected void tryResetContextOf(CommandContext context, String name, PluginContainer container) { | ||
var plugin = container.plugin(); | ||
if (plugin instanceof ScriptPlugin sp && sp.canResetContext()) { | ||
context.addOutput("Resetting context of " + name); | ||
try { | ||
sp.resetContext(); | ||
} catch (Throwable t) { | ||
context.addError("Failed to reset context of " + name, t); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/org/allaymc/scriptpluginext/js/JSPluginSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.allaymc.scriptpluginext.js; | ||
|
||
import lombok.SneakyThrows; | ||
import org.allaymc.api.plugin.PluginSource; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author daoge_cmd | ||
*/ | ||
public class JSPluginSource implements PluginSource { | ||
|
||
public static final Path JS_PLUGIN_FOLDER = Path.of("jsplugins"); | ||
// Because the js plugin itself is also a folder, set the root of the data folder to another directory to avoid conflicts. | ||
public static final Path JS_PLUGIN_DATA_FOLDER = Path.of("jsplugindata"); | ||
|
||
@SneakyThrows | ||
public JSPluginSource() { | ||
if (!Files.exists(JS_PLUGIN_FOLDER)) { | ||
Files.createDirectory(JS_PLUGIN_FOLDER); | ||
} | ||
if (!Files.exists(JS_PLUGIN_DATA_FOLDER)) { | ||
Files.createDirectory(JS_PLUGIN_DATA_FOLDER); | ||
} | ||
} | ||
|
||
@SneakyThrows | ||
public static Path getOrCreateDataFolder(String pluginName) { | ||
var dataFolder = JS_PLUGIN_DATA_FOLDER.resolve(pluginName); | ||
if (!Files.exists(dataFolder)) { | ||
Files.createDirectory(dataFolder); | ||
} | ||
return dataFolder; | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public Set<Path> find() { | ||
try (var stream = Files.list(JS_PLUGIN_FOLDER)) { | ||
return stream.collect(Collectors.toSet()); | ||
} | ||
} | ||
} |
Oops, something went wrong.