Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;

/**
* Builder for Script Commands.
Expand Down Expand Up @@ -171,14 +172,29 @@ protected boolean canGeneratePermission() {
protected @Nullable CompletableFuture<Void> execute(@NotNull CommandContext commandContext) {
CompletableFuture.runAsync(() -> {
CommandSender sender = commandContext.sender();
Player player = null;
if (sender instanceof Player p) player = p;
ScriptCommandContext context = new ScriptCommandContext(ScriptCommandBuilder.this.commandName,
sender);

createLocalVariables(commandContext, context);
Statement.runAll(trigger, context);
Variables.clearLocalVariables(context);

AtomicReference<ScriptCommandContext> context = new AtomicReference<>();

Runnable code = () -> {
createLocalVariables(commandContext, context.get());
Statement.runAll(trigger, context.get());
Variables.clearLocalVariables(context.get());
};

if (sender instanceof Player player && player.getWorld() != null) {
// If a player runs the command, run it in their world
context.set(new PlayerScriptCommandContext(commandName, player));
World world = player.getWorld();
if (world.isInThread()) {
code.run();
} else {
world.execute(code);
}
} else {
// Otherwise run as normal
context.set(new ScriptCommandContext(commandName, sender));
code.run();
}
});
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@
import io.github.syst3ms.skriptparser.log.SkriptLogger;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Config for Skript
*/
public class SkriptConfig {

private final Config config;
private Config config;
private final boolean debug;
private final int maxTargetBlockDistance;
private final ConfigSection effectCommands;
Expand All @@ -40,8 +51,15 @@ public SkriptConfig(Skript skript) {
logger.debug("Checking for update from: " + configVersion);
Semver hySkriptVersion = skript.getPlugin().getManifest().getVersion();
if (configVersion.compareTo(hySkriptVersion) < 0) {
logger.debug("Updating config to version: " + hySkriptVersion);
updateConfig();
logger.info("Updating config to version: " + hySkriptVersion);
try {
// Update the config from the default config
updateConfig(skriptConfigPath, hySkriptVersion.toString());
// Reload the config so we have updated values
this.config = new Config(skriptConfigPath, "/config.sk", logger);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
logger.debug("Config is up to date");
}
Expand All @@ -51,25 +69,20 @@ public SkriptConfig(Skript skript) {

// Set up max-target-block-distance
this.maxTargetBlockDistance = this.config.getInt("max-target-block-distance");
if (this.maxTargetBlockDistance == -1) {
// This would happen if the config is missing this value
// TODO update config
} else if (this.maxTargetBlockDistance < 0) {
if (this.maxTargetBlockDistance < 0) {
logger.error("max-target-block-distance must be greater than or equal to 0", ErrorType.STRUCTURE_ERROR);
}

// Set up effect commands
this.effectCommands = this.config.getConfigSection("effect-commands");
if (this.effectCommands == null) {
logger.error("Effect commands section not found in config.sk", ErrorType.STRUCTURE_ERROR);
// TODO update config
}

// Set up databases
this.databases = this.config.getConfigSection("databases");
if (this.databases == null) {
logger.error("Databases section not found in config.sk", ErrorType.STRUCTURE_ERROR);
// TODO update config
}

// Set up commands generate permissions
Expand Down Expand Up @@ -131,8 +144,71 @@ public boolean getCommandsGeneratePermissions() {
return this.commandsGeneratePermissions;
}

private void updateConfig() {
// TODO update config
@SuppressWarnings("resource")
private void updateConfig(Path userPath, String version) throws IOException {
Set<String> userKeys = Files.lines(userPath)
.map(String::trim)
.filter(l -> !l.startsWith("#") && l.contains(":"))
.map(l -> l.split(":")[0].trim())
.collect(Collectors.toSet());

InputStream stream = SkriptConfig.class.getResourceAsStream("/config.sk");
assert stream != null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
BufferedWriter writer = Files.newBufferedWriter(userPath, StandardOpenOption.APPEND)) {

List<String> commentBuffer = new ArrayList<>();
String line;

while ((line = reader.readLine()) != null) {
String trimmed = line.trim();

if (trimmed.startsWith("#")) {
// It's a comment, save it for the next key we find
commentBuffer.add(line);
} else if (trimmed.contains(":")) {
String key = trimmed.split(":")[0].trim();

if (!userKeys.contains(key)) {
// User is missing this key!
writer.newLine(); // Add spacing for readability

// Write the buffered comments first
for (String comment : commentBuffer) {
writer.write(comment);
writer.newLine();
}
// Write the actual key-value pair
writer.write(line);
writer.newLine();
}
// Clear buffer regardless of whether we wrote it or not
commentBuffer.clear();
} else if (trimmed.isEmpty()) {
commentBuffer.clear();
}
}
}

updateConfigVersion(userPath, version);
}

private void updateConfigVersion(Path userPath, String newVersion) throws IOException {
List<String> lines = Files.readAllLines(userPath);
boolean updated = false;

for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i).trim();
if (line.startsWith("hyskript-version:")) {
lines.set(i, "hyskript-version: " + newVersion);
updated = true;
break;
}
}

if (updated) {
Files.write(userPath, lines);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ private void printFunctions(BsonDocument mainDocs, SkriptRegistration registrati
String since = documentation.getSince();
if (since != null) {
functionDoc.put("since", new BsonArray(List.of(new BsonString(since))));
} else {
Utils.warn(this.sender, "Function '%s' has no since tag!", name);
}

// RETURN TYPE
Expand Down Expand Up @@ -384,6 +386,8 @@ private void printTypes(BsonDocument mainDocs, SkriptRegistration registration)
String since = documentation.getSince();
if (since != null) {
syntaxDoc.put("since", new BsonArray(List.of(new BsonString(since))));
} else {
Utils.warn(this.sender, "Type '%s' has no since tag!", baseName);
}


Expand Down Expand Up @@ -459,6 +463,8 @@ private void printDocumentation(String type, BsonDocument syntaxDoc, SyntaxInfo<
String since = documentation.getSince();
if (since != null) {
syntaxDoc.put("since", new BsonArray(List.of(new BsonString(since))));
} else {
Utils.warn(this.sender, "Syntax '%s' has no since tag!", syntaxInfo.getSyntaxClass().getSimpleName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class EffBreakBlock extends Effect {

public static void register(SkriptRegistration reg) {
reg.newEffect(EffBreakBlock.class, "break %blocks% [with settings %number%]")
.name("Break Block")
.description("Breaks the specified blocks.",
"**Settings**:",
"I don't really know what this does yet, but from testing:",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void register(SkriptRegistration registration) {
.name("Teleport")
.description("Teleport entities to a location.")
.examples("teleport all players to {_location}",
"teleport player to bed location of player")
"teleport player to first element of (respawn locations of player)")
.since("1.0.0")
.register();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import com.github.skriptdev.skript.plugin.elements.expressions.player.ExprGameMode;
import com.github.skriptdev.skript.plugin.elements.expressions.player.ExprPlayerPermissionGroup;
import com.github.skriptdev.skript.plugin.elements.expressions.player.ExprPlayerPermissions;
import com.github.skriptdev.skript.plugin.elements.expressions.player.ExprPlayerSpawns;
import com.github.skriptdev.skript.plugin.elements.expressions.player.ExprPlayerRespawnLocations;
import com.github.skriptdev.skript.plugin.elements.expressions.server.ExprConsole;
import com.github.skriptdev.skript.plugin.elements.expressions.world.ExprChunkAtLocation;
import com.github.skriptdev.skript.plugin.elements.expressions.world.ExprRelativePositionResolve;
Expand Down Expand Up @@ -118,7 +118,7 @@ public static void register(SkriptRegistration registration) {
ExprGameMode.register(registration);
ExprPlayerPermissionGroup.register(registration);
ExprPlayerPermissions.register(registration);
ExprPlayerSpawns.register(registration);
ExprPlayerRespawnLocations.register(registration);

// SERVER
ExprConsole.register(registration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ExprPlayerSpawns implements Expression<Location> {
public class ExprPlayerRespawnLocations implements Expression<Location> {

public static void register(SkriptRegistration reg) {
reg.newExpression(ExprPlayerSpawns.class, Location.class, false,
reg.newExpression(ExprPlayerRespawnLocations.class, Location.class, false,
"[player] respawn locations of %player%",
"[player] respawn locations of %player% in %worlds%")
.name("Player Respawn Locations")
Expand Down Expand Up @@ -57,11 +58,9 @@ public Location[] getValues(@NotNull TriggerContext ctx) {

List<World> worlds = new ArrayList<>();
if (this.worlds != null) {
for (World world : this.worlds.getArray(ctx)) {
worlds.add(world);
}
Collections.addAll(worlds, this.worlds.getArray(ctx));
} else {
Universe.get().getWorlds().forEach((s, world) -> worlds.add(world));
Universe.get().getWorlds().forEach((_, world) -> worlds.add(world));
}

List<Location> spawns = new ArrayList<>();
Expand Down
68 changes: 34 additions & 34 deletions src/main/resources/config.sk
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
# HySkript Config

hyskript-version: $pluginVersion
# Do not change this value manually.
# This is automatically updated when you update HySkript.
# This is used to update the config if it's outdated.
hyskript-version: $pluginVersion

debug: false
# Whether or not to enable debug mode (maybe print more verbose logs to console).
debug: false

max-target-block-distance: 160
# The maximum distance from a Player to a block to be considered a target block.
# Default 160 = 5 chunks
# Increasing this amount could cause performance issues, so test carefully.
# Increasing this amount could cause performance issues, so test thoroughly.
max-target-block-distance: 160

commands-generate-permissions: true
# Hytale by default generates permissions for all commands.
# If you want to disable this, set this to false.
# You can also disable it per command in the command entries.
commands-generate-permissions: true

# Effect commands allow you to execute effects in chat.
effect-commands:
enabled: true
# Whether or not effect commands are enabled.
# Whether or not effect commands are enabled.
enabled: true

allow-ops: false
# Whether or not ops can use effect commands.
# If false, they will require the permission specified below in 'required-permission'.
# Whether or not ops can use effect commands.
# If false, they will require the permission specified below in 'required-permission'.
allow-ops: false

required-permission: skript.hyskript.effect-commands
# The permission required to use effect commands (if allow-ops is false).
# The permission required to use effect commands (if allow-ops is false).
required-permission: skript.hyskript.effect-commands

token: !
# The token used to execute effects in chat.
# The token used to execute effects in chat.
token: !

# Represents the database[s] used by HySkript to save variables to.
# You can add as many databases as you want.
databases:
# dummy: (This is the name of your database)
# type: json-database
# This is the type of database, currently only 'json-database' is supported
# type: json-database
# This is the type of database, currently only 'json-database' is supported
#
# enabled: true
# Whether or not the database is enabled
# enabled: true
# Whether or not the database is enabled
#
# file-type: json
# The file type of the database, currently only 'json' and 'bson' are supported
# 'json' = Will save as a JSON file in text format (Easy to read/change if you need to).
# 'bson' = Will save as a BSON file in binary format (This is what Hytale uses, much smaller file).
# file-type: json
# The file type of the database, currently only 'json' and 'bson' are supported
# 'json' = Will save as a JSON file in text format (Easy to read/change if you need to).
# 'bson' = Will save as a BSON file in binary format (This is what Hytale uses, much smaller file).
#
# file: variables.json
# The file to save variables to.
# file: variables.json
# The file to save variables to.
#
# pattern: .*
# The regex pattern to match variables to.
# '.*' = Will save all variables.
# '(?!-).*' = Will only save variables that don't start with '-' (ex: '{-some_var}' will not be saved).
# pattern: .*
# The regex pattern to match variables to.
# '.*' = Will save all variables.
# '(?!-).*' = Will only save variables that don't start with '-' (ex: '{-some_var}' will not be saved).

default:
type: json-database
enabled: true
file-type: json
file: variables.json
pattern: (?!-).*
default:
type: json-database
enabled: true
file-type: json
file: variables.json
pattern: (?!-).*
Loading