Skip to content

Commit 150ab3f

Browse files
authored
Merge pull request #13 from SkriptDev/dev/patch
Dev/patch - Future Patch Release
2 parents cbfd182 + 8fafda7 commit 150ab3f

17 files changed

Lines changed: 325 additions & 101 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repositories {
2626
dependencies {
2727
compileOnly("com.hypixel.hytale:Server:${hytaleVersion}")
2828
compileOnly("org.jetbrains:annotations:26.0.2")
29-
implementation("com.github.SkriptDev:skript-parser:1.0.6") {
29+
implementation("com.github.SkriptDev:skript-parser:1.0.7") {
3030
isTransitive = false
3131
}
3232
implementation("com.github.Zoltus:TinyMessage:2.0.1") {

src/main/java/com/github/skriptdev/skript/api/skript/ErrorHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void init() {
2121
for (SyntaxInfo<? extends Effect> effect : SyntaxManager.getEffects()) {
2222
EFFECT_MAP.put(effect.getSyntaxClass().getSimpleName(), effect);
2323
}
24-
for (SkriptEventInfo<?> event : SyntaxManager.getEvents()) {
24+
for (SkriptEventInfo<?> event : SyntaxManager.getTriggers()) {
2525
EVENT_MAP.put(event.getSyntaxClass().getSimpleName(), event);
2626
}
2727
setup();

src/main/java/com/github/skriptdev/skript/api/skript/command/ScriptCommandBuilder.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.hypixel.hytale.component.Store;
1010
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
1111
import com.hypixel.hytale.server.core.command.system.CommandContext;
12+
import com.hypixel.hytale.server.core.command.system.CommandRegistration;
1213
import com.hypixel.hytale.server.core.command.system.CommandSender;
1314
import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
1415
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
@@ -39,6 +40,7 @@
3940
import java.util.Map;
4041
import java.util.Optional;
4142
import java.util.concurrent.CompletableFuture;
43+
import java.util.concurrent.atomic.AtomicReference;
4244

4345
/**
4446
* Builder for Script Commands.
@@ -57,6 +59,7 @@ public static ScriptCommandBuilder create(int commandType, SkriptLogger logger)
5759
private final Map<String, CommandArg> args = new LinkedHashMap<>();
5860
private final Map<String, Argument<?, ?>> argsFromCommand = new LinkedHashMap<>();
5961
private AbstractCommand hyCommand;
62+
private CommandRegistration commandRegistration;
6063

6164
private final SectionConfiguration sec = new SectionConfiguration.Builder()
6265
.addOptionalLiteral("can-generate-permission", Boolean.class)
@@ -171,14 +174,29 @@ protected boolean canGeneratePermission() {
171174
protected @Nullable CompletableFuture<Void> execute(@NotNull CommandContext commandContext) {
172175
CompletableFuture.runAsync(() -> {
173176
CommandSender sender = commandContext.sender();
174-
Player player = null;
175-
if (sender instanceof Player p) player = p;
176-
ScriptCommandContext context = new ScriptCommandContext(ScriptCommandBuilder.this.commandName,
177-
sender);
178-
179-
createLocalVariables(commandContext, context);
180-
Statement.runAll(trigger, context);
181-
Variables.clearLocalVariables(context);
177+
178+
AtomicReference<ScriptCommandContext> context = new AtomicReference<>();
179+
180+
Runnable code = () -> {
181+
createLocalVariables(commandContext, context.get());
182+
Statement.runAll(trigger, context.get());
183+
Variables.clearLocalVariables(context.get());
184+
};
185+
186+
if (sender instanceof Player player && player.getWorld() != null) {
187+
// If a player runs the command, run it in their world
188+
context.set(new PlayerScriptCommandContext(commandName, player));
189+
World world = player.getWorld();
190+
if (world.isInThread()) {
191+
code.run();
192+
} else {
193+
world.execute(code);
194+
}
195+
} else {
196+
// Otherwise run as normal
197+
context.set(new ScriptCommandContext(commandName, sender));
198+
code.run();
199+
}
182200
});
183201
return null;
184202
}
@@ -263,6 +281,12 @@ private void createLocalVariables(CommandContext ctx, TriggerContext triggerCont
263281
});
264282
}
265283

284+
public void unregister() {
285+
if (this.commandRegistration != null) {
286+
this.commandRegistration.unregister();
287+
}
288+
}
289+
266290
public String getCommandName() {
267291
return this.commandName;
268292
}
@@ -275,7 +299,7 @@ public void build(ScriptCommandBuilder parent) {
275299
if (this.hyCommand == null) return;
276300

277301
if (parent == null) {
278-
HySk.getInstance().getCommandRegistry().registerCommand(this.hyCommand);
302+
this.commandRegistration = HySk.getInstance().getCommandRegistry().registerCommand(this.hyCommand);
279303
} else {
280304
parent.hyCommand.addSubCommand(this.hyCommand);
281305
}

src/main/java/com/github/skriptdev/skript/api/skript/config/SkriptConfig.java

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,25 @@
1010
import io.github.syst3ms.skriptparser.log.SkriptLogger;
1111
import org.jetbrains.annotations.Nullable;
1212

13+
import java.io.BufferedReader;
14+
import java.io.BufferedWriter;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.io.InputStreamReader;
18+
import java.nio.file.Files;
1319
import java.nio.file.Path;
20+
import java.nio.file.StandardOpenOption;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Set;
24+
import java.util.stream.Collectors;
1425

1526
/**
1627
* Config for Skript
1728
*/
1829
public class SkriptConfig {
1930

20-
private final Config config;
31+
private Config config;
2132
private final boolean debug;
2233
private final int maxTargetBlockDistance;
2334
private final ConfigSection effectCommands;
@@ -40,8 +51,15 @@ public SkriptConfig(Skript skript) {
4051
logger.debug("Checking for update from: " + configVersion);
4152
Semver hySkriptVersion = skript.getPlugin().getManifest().getVersion();
4253
if (configVersion.compareTo(hySkriptVersion) < 0) {
43-
logger.debug("Updating config to version: " + hySkriptVersion);
44-
updateConfig();
54+
logger.info("Updating config to version: " + hySkriptVersion);
55+
try {
56+
// Update the config from the default config
57+
updateConfig(skriptConfigPath, hySkriptVersion.toString());
58+
// Reload the config so we have updated values
59+
this.config = new Config(skriptConfigPath, "/config.sk", logger);
60+
} catch (IOException e) {
61+
throw new RuntimeException(e);
62+
}
4563
} else {
4664
logger.debug("Config is up to date");
4765
}
@@ -51,25 +69,20 @@ public SkriptConfig(Skript skript) {
5169

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

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

6882
// Set up databases
6983
this.databases = this.config.getConfigSection("databases");
7084
if (this.databases == null) {
7185
logger.error("Databases section not found in config.sk", ErrorType.STRUCTURE_ERROR);
72-
// TODO update config
7386
}
7487

7588
// Set up commands generate permissions
@@ -131,8 +144,71 @@ public boolean getCommandsGeneratePermissions() {
131144
return this.commandsGeneratePermissions;
132145
}
133146

134-
private void updateConfig() {
135-
// TODO update config
147+
@SuppressWarnings("resource")
148+
private void updateConfig(Path userPath, String version) throws IOException {
149+
Set<String> userKeys = Files.lines(userPath)
150+
.map(String::trim)
151+
.filter(l -> !l.startsWith("#") && l.contains(":"))
152+
.map(l -> l.split(":")[0].trim())
153+
.collect(Collectors.toSet());
154+
155+
InputStream stream = SkriptConfig.class.getResourceAsStream("/config.sk");
156+
assert stream != null;
157+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
158+
BufferedWriter writer = Files.newBufferedWriter(userPath, StandardOpenOption.APPEND)) {
159+
160+
List<String> commentBuffer = new ArrayList<>();
161+
String line;
162+
163+
while ((line = reader.readLine()) != null) {
164+
String trimmed = line.trim();
165+
166+
if (trimmed.startsWith("#")) {
167+
// It's a comment, save it for the next key we find
168+
commentBuffer.add(line);
169+
} else if (trimmed.contains(":")) {
170+
String key = trimmed.split(":")[0].trim();
171+
172+
if (!userKeys.contains(key)) {
173+
// User is missing this key!
174+
writer.newLine(); // Add spacing for readability
175+
176+
// Write the buffered comments first
177+
for (String comment : commentBuffer) {
178+
writer.write(comment);
179+
writer.newLine();
180+
}
181+
// Write the actual key-value pair
182+
writer.write(line);
183+
writer.newLine();
184+
}
185+
// Clear buffer regardless of whether we wrote it or not
186+
commentBuffer.clear();
187+
} else if (trimmed.isEmpty()) {
188+
commentBuffer.clear();
189+
}
190+
}
191+
}
192+
193+
updateConfigVersion(userPath, version);
194+
}
195+
196+
private void updateConfigVersion(Path userPath, String newVersion) throws IOException {
197+
List<String> lines = Files.readAllLines(userPath);
198+
boolean updated = false;
199+
200+
for (int i = 0; i < lines.size(); i++) {
201+
String line = lines.get(i).trim();
202+
if (line.startsWith("hyskript-version:")) {
203+
lines.set(i, "hyskript-version: " + newVersion);
204+
updated = true;
205+
break;
206+
}
207+
}
208+
209+
if (updated) {
210+
Files.write(userPath, lines);
211+
}
136212
}
137213

138214
}

0 commit comments

Comments
 (0)