-
-
Notifications
You must be signed in to change notification settings - Fork 408
Example Script API #8191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev/feature
Are you sure you want to change the base?
Example Script API #8191
Changes from all commits
e9ff7b6
6d7e59c
a49bac4
d89c320
ee3c29a
87fedee
4291cd6
812bf92
e0cb303
7e080d9
09f7638
10d1f16
9ad7df1
4e86408
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,43 @@ | ||||||
package ch.njol.skript.examples; | ||||||
|
||||||
import java.io.IOException; | ||||||
import java.io.InputStream; | ||||||
import java.nio.charset.StandardCharsets; | ||||||
import java.util.Collection; | ||||||
import java.util.List; | ||||||
|
||||||
public final class CoreExampleScripts { | ||||||
|
||||||
public static final List<ExampleScript> EXAMPLES = List.of( | ||||||
load("chest menus.sk"), | ||||||
load("commands.sk"), | ||||||
load("events.sk"), | ||||||
load("experimental features/for loops.sk"), | ||||||
load("experimental features/queues.sk"), | ||||||
load("experimental features/script reflection.sk"), | ||||||
load("functions.sk"), | ||||||
load("loops.sk"), | ||||||
load("options and meta.sk"), | ||||||
load("text formatting.sk"), | ||||||
load("timings.sk"), | ||||||
load("variables.sk") | ||||||
Comment on lines
+12
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it'd be nice to separate the bukkit-based examples out into a different list where feasible |
||||||
); | ||||||
|
||||||
private CoreExampleScripts() {} | ||||||
|
||||||
public static Collection<ExampleScript> all() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
this method should probably be moved to ExampleScript if it becomes an interface |
||||||
return EXAMPLES; | ||||||
} | ||||||
|
||||||
private static ExampleScript load(String name) { | ||||||
String path = "scripts/-examples/" + name; | ||||||
try (InputStream in = CoreExampleScripts.class.getClassLoader().getResourceAsStream(path)) { | ||||||
if (in == null) | ||||||
throw new IllegalStateException("Missing example script " + path); | ||||||
String content = new String(in.readAllBytes(), StandardCharsets.UTF_8); | ||||||
return new ExampleScript(name, content); | ||||||
} catch (IOException e) { | ||||||
throw new RuntimeException("Failed to load example script " + path, e); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ch.njol.skript.examples; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
/** | ||
* Represents an example script bundled with Skript or an addon. | ||
*/ | ||
public record ExampleScript(String name, String content) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe this could be an interface where all the example scripts are registered? this can then be extended by addons and registered with SkriptAddon |
||
|
||
/** | ||
* Loads an example script from a resource contained within an addon JAR. | ||
* | ||
* @param plugin The plugin providing the resource | ||
* @param resourcePath The path to the resource inside the plugin | ||
* @param outputName The name of the file to install the example as | ||
* @return A new {@link ExampleScript} containing the resource's content | ||
* @throws IOException If the resource cannot be found or read | ||
*/ | ||
public static ExampleScript fromResource(JavaPlugin plugin, String resourcePath, String outputName) throws IOException { | ||
try (InputStream in = plugin.getResource(resourcePath)) { | ||
if (in == null) | ||
throw new IOException("Resource not found: " + resourcePath); | ||
String content = new String(in.readAllBytes(), StandardCharsets.UTF_8); | ||
return new ExampleScript(outputName, content); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package ch.njol.skript.examples; | ||
|
||
import ch.njol.skript.Skript; | ||
import org.bukkit.Bukkit; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.DosFileAttributeView; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
public final class ExampleScriptManager { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs javadocs in this class |
||
private Set<String> installed; | ||
private File installedFile; | ||
|
||
public ExampleScriptManager() {} | ||
|
||
private void loadInstalled(File scriptsDir) { | ||
File parent = scriptsDir.getParentFile(); | ||
installedFile = new File(parent == null ? scriptsDir : parent, ".loaded_examples"); | ||
installed = new LinkedHashSet<>(); | ||
if (!installedFile.exists()) | ||
return; | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(installedFile), StandardCharsets.UTF_8))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
if (!line.isEmpty()) | ||
installed.add(line); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load installed examples", e); | ||
} | ||
} | ||
|
||
private void flushInstalled() { | ||
if (installedFile == null || installed == null) | ||
return; | ||
File parent = installedFile.getParentFile(); | ||
if (parent != null && !parent.exists() && !parent.mkdirs()) // failed to create directory for installed examples | ||
return; | ||
boolean isWindows = System.getProperty("os.name").startsWith("Windows"); | ||
Path installedPath = installedFile.toPath(); | ||
DosFileAttributeView dosView = null; | ||
if (isWindows && Files.exists(installedPath)) { | ||
dosView = Files.getFileAttributeView(installedPath, DosFileAttributeView.class); | ||
if (dosView != null) { | ||
try { | ||
if (dosView.readAttributes().isHidden()) | ||
dosView.setHidden(false); | ||
} catch (IOException ignored) {} | ||
} | ||
} | ||
|
||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(installedFile), StandardCharsets.UTF_8))) { | ||
for (String entry : installed) { | ||
writer.write(entry); | ||
writer.newLine(); | ||
} | ||
} catch (IOException e) { // failed to save installed examples | ||
return; | ||
} | ||
|
||
if (isWindows) { | ||
try { | ||
if (dosView != null) { | ||
dosView.setHidden(true); | ||
} else { | ||
Files.setAttribute(installedPath, "dos:hidden", true); | ||
} | ||
} catch (Exception ignored) {} | ||
} | ||
} | ||
|
||
public void installExamples(String plugin, Collection<ExampleScript> scripts, File scriptsDir) { | ||
loadInstalled(scriptsDir); | ||
boolean dirty = false; | ||
File baseDir = new File(scriptsDir, "-examples/" + plugin); | ||
for (ExampleScript script : scripts) { | ||
String key = plugin + "/" + script.name(); | ||
if (installed.add(key)) { | ||
dirty = true; | ||
File file = new File(baseDir, script.name()); | ||
file.getParentFile().mkdirs(); | ||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) { | ||
writer.write(script.content()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to write example script " + file, e); | ||
} | ||
} | ||
} | ||
if (dirty) | ||
flushInstalled(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably be package-private and have a getter to enforce usage through SkriptAddon