Skip to content

Conversation

cheeezburga
Copy link
Member

Problem

The current method of providing the example scripts isn't ideal, and doesn't allow addons to extend this useful function.

This PR gives addons a way to add their own example scripts, allowing for another method of providing documentation to new and experienced Skript players alike.

Solution

This PR introduces a new way to create and track the example scripts. It now uses a hidden .examples-loaded file in the Skript folder to identify which examples need to be created on plugin enable, and which have already been handled. This is a better alternative, as before Skript used to just go off of whether the scripts folder existed, which isn't a sure way to get this info.

The example scripts are stored as ExampleScript records, and loaded from the resources where they're currently stored.

It also provides SkriptAddon#registerExampleScripts so that addons are able to provide example scripts. With this change, the example scripts have been separated into different folders based on the addon name.

Testing Completed

Manually tested going from old versions of Skript to this one, and from this version of Skript to itself.

Haven't included tests for this as I'm unsure how to validate that, once Skript has loaded them once, they don't get loaded again. My own testing confirms that the behaviour works as intended, but unsure how to prove it via code.

Supporting Information

With this PR, given the updated way that example scripts are created and tracked, all example scripts will be created again if they were deleted. I think this is pretty minor, and don't think it's possible to avoid.

Not massively attached to this implementation, just thought it might be a nice addition and put together a solution quickly, so thoughts and feedback are appreciated.


Completes: none
Related: none

@cheeezburga cheeezburga requested review from a team as code owners September 15, 2025 05:40
@cheeezburga cheeezburga requested review from APickledWalrus and Pesekjak and removed request for a team September 15, 2025 05:40
@skriptlang-automation skriptlang-automation bot added the needs reviews A PR that needs additional reviews label Sep 15, 2025
@Fusezion
Copy link
Contributor

Haven't looked over this much but regarding your comment about it always readding the examples can we not just add a config toggle?

And does this load example skripts on server start or is it unloaded until reloaded?

Copy link
Member

@Efnilite Efnilite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good 🔥

/**
* Represents an example script bundled with Skript or an addon.
*/
public record ExampleScript(String name, String content) {
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added in an ExampleScriptProvider interface, is that sort of what you were thinking with this?

Copy link
Member

@sovdeeth sovdeeth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this will continually generate the -examples directory if i delete it from my installation, instead of only generating on first install. Is this correct, and if so, that needs to be fixed.

@skriptlang-automation skriptlang-automation bot removed the needs reviews A PR that needs additional reviews label Sep 16, 2025
@sovdeeth sovdeeth added the feature Pull request adding a new feature. label Sep 16, 2025
@cheeezburga
Copy link
Member Author

It seems like this will continually generate the -examples directory if i delete it from my installation, instead of only generating on first install. Is this correct, and if so, that needs to be fixed.

I think there was a bug or two in the initial implementation, but all sorted now. Done some testing and it all works as intended, as far as I can see.

Comment on lines +34 to +45
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);
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some duplicate code here, can we de duplicate it?

/**
* Persists the list of installed example scripts. Hides the metadata file on Windows systems.
*/
private void flushInstalled() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush implies deletion/removal to me. Perhaps a better name would be writeTrackingFile or similar

boolean dirty = false;
File baseDir = new File(scriptsDir, "-examples/" + plugin);
for (ExampleScript script : scripts) {
String key = plugin + "/" + script.name();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any concerns about operating systems and different path separation characters? I know we use File.separator elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Pull request adding a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants