Skip to content

Commit

Permalink
Merge patches in to feature branch. (SkriptLang#6061)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moderocky authored Sep 23, 2023
1 parent 27c6275 commit 31e24ad
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 86 deletions.
294 changes: 294 additions & 0 deletions CLOCKWORK_RELEASE_MODEL.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ versions will be supported as soon as possible.
## Download
You can find the downloads for each version with their release notes in the [releases page](https://github.com/SkriptLang/Skript/releases).

Two major feature updates are expected each year in January and July, with monthly patches occurring in between. For full details, please review our [release model](CLOCKWORK_RELEASE_MODEL.md).

## Documentation
Documentation is available [here](https://docs.skriptlang.org/) for the
latest version of Skript.
Expand Down
86 changes: 49 additions & 37 deletions src/main/java/ch/njol/skript/command/ScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,11 @@
*/
package ch.njol.skript.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.config.SectionNode;
import org.skriptlang.skript.lang.script.Script;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.help.GenericCommandHelpTopic;
import org.bukkit.help.HelpMap;
import org.bukkit.help.HelpTopic;
import org.bukkit.help.HelpTopicComparator;
import org.bukkit.help.IndexHelpTopic;
import org.bukkit.plugin.Plugin;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptConfig;
import ch.njol.skript.command.Commands.CommandAliasHelpTopic;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.Trigger;
Expand All @@ -77,6 +44,39 @@
import ch.njol.skript.variables.Variables;
import ch.njol.util.StringUtils;
import ch.njol.util.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.help.GenericCommandHelpTopic;
import org.bukkit.help.HelpMap;
import org.bukkit.help.HelpTopic;
import org.bukkit.help.HelpTopicComparator;
import org.bukkit.help.IndexHelpTopic;
import org.bukkit.plugin.Plugin;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.script.Script;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

/**
* This class is used for user-defined commands.
Expand Down Expand Up @@ -274,9 +274,21 @@ public boolean execute(final CommandSender sender, final String commandLabel, fi
}

Runnable runnable = () -> {
// save previous last usage date to check if the execution has set the last usage date
Date previousLastUsage = null;
if (sender instanceof Player)
previousLastUsage = getLastUsage(((Player) sender).getUniqueId(), event);

// execute the command - may modify the last usage date
execute2(event, sender, commandLabel, rest);
if (sender instanceof Player && !event.isCooldownCancelled())
setLastUsage(((Player) sender).getUniqueId(), event, new Date());

if (sender instanceof Player && !event.isCooldownCancelled()) {
Date lastUsage = getLastUsage(((Player) sender).getUniqueId(), event);
// check if the execution has set the last usage date
// if not, set it to the current date. if it has, we leave it alone so as not to affect the remaining/elapsed time (#5862)
if (Objects.equals(lastUsage, previousLastUsage))
setLastUsage(((Player) sender).getUniqueId(), event, new Date());
}
};
if (Bukkit.isPrimaryThread()) {
runnable.run();
Expand Down Expand Up @@ -516,7 +528,7 @@ public void setRemainingMilliseconds(UUID uuid, Event event, long milliseconds)
assert cooldown != null;
long cooldownMs = cooldown.getMilliSeconds();
if (milliseconds > cooldownMs)
throw new IllegalArgumentException("Remaining time may not be longer than the cooldown");
milliseconds = cooldownMs;
setElapsedMilliSeconds(uuid, event, cooldownMs - milliseconds);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@Name("Is Infinite")
@Description("Checks whether potion effects are infinite.")
@Examples("all of the active potion effects of the player are infinite")
@Since("INSERT VERSION")
@Since("2.7")
public class CondIsInfinite extends PropertyCondition<PotionEffect> {

static {
Expand Down
61 changes: 26 additions & 35 deletions src/main/java/ch/njol/skript/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@
*/
package ch.njol.skript.config;

import ch.njol.skript.Skript;
import ch.njol.skript.config.validate.SectionValidator;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.config.validate.SectionValidator;

/**
* Represents a config file.
*
* @author Peter Güttinger
*/
public class Config implements Comparable<Config> {

boolean simple = false;
boolean simple;

/**
* One level of the indentation, e.g. a tab or 4 spaces.
Expand All @@ -58,8 +58,6 @@ public class Config implements Comparable<Config> {
final String defaultSeparator;
String separator;

String line = "";

int level = 0;

private final SectionNode main;
Expand Down Expand Up @@ -91,11 +89,8 @@ public Config(final InputStream source, final String fileName, @Nullable final F
if (Skript.logVeryHigh())
Skript.info("loading '" + fileName + "'");

final ConfigReader r = new ConfigReader(source);
try {
main = SectionNode.load(this, r);
} finally {
r.close();
try (ConfigReader reader = new ConfigReader(source)) {
main = SectionNode.load(this, reader);
}
} finally {
source.close();
Expand All @@ -106,9 +101,8 @@ public Config(final InputStream source, final String fileName, final boolean sim
this(source, fileName, null, simple, allowEmptySections, defaultSeparator);
}

@SuppressWarnings("resource")
public Config(final File file, final boolean simple, final boolean allowEmptySections, final String defaultSeparator) throws IOException {
this(new FileInputStream(file), "" + file.getName(), simple, allowEmptySections, defaultSeparator);
this(Files.newInputStream(file.toPath()), file.getName(), simple, allowEmptySections, defaultSeparator);
this.file = file.toPath();
}

Expand All @@ -120,7 +114,7 @@ public Config(final Path file, final boolean simple, final boolean allowEmptySec

/**
* For testing
*
*
* @param s
* @param fileName
* @param simple
Expand All @@ -133,7 +127,7 @@ public Config(final String s, final String fileName, final boolean simple, final
}

void setIndentation(final String indent) {
assert indent != null && indent.length() > 0 : indent;
assert indent != null && !indent.isEmpty() : indent;
indentation = indent;
indentationName = (indent.charAt(0) == ' ' ? "space" : "tab");
}
Expand All @@ -156,7 +150,7 @@ public String getFileName() {

/**
* Saves the config to a file.
*
*
* @param f The file to save to
* @throws IOException If the file could not be written to.
*/
Expand All @@ -175,7 +169,7 @@ public void save(final File f) throws IOException {
* Sets this config's values to those in the given config.
* <p>
* Used by Skript to import old settings into the updated config. The return value is used to not modify the config if no new options were added.
*
*
* @param other
* @return Whether the configs' keys differ, i.e. false == configs only differ in values, not keys.
*/
Expand Down Expand Up @@ -203,7 +197,7 @@ public File getFile() {
if (file != null) {
try {
return file.toFile();
} catch(Exception e) {
} catch (Exception e) {
return null; // ZipPath, for example, throws undocumented exception
}
}
Expand Down Expand Up @@ -235,7 +229,7 @@ public String getSaveSeparator() {

/**
* Splits the given path at the dot character and passes the result to {@link #get(String...)}.
*
*
* @param path
* @return <tt>get(path.split("\\."))</tt>
*/
Expand All @@ -247,7 +241,7 @@ public String getByPath(final String path) {

/**
* Gets an entry node's value at the designated path
*
*
* @param path
* @return The entry node's value at the location defined by path or null if it either doesn't exist or is not an entry.
*/
Expand Down Expand Up @@ -284,22 +278,19 @@ public boolean validate(final SectionValidator validator) {
return validator.validate(getMainNode());
}

private void load(final Class<?> c, final @Nullable Object o, final String path) {
for (final Field f : c.getDeclaredFields()) {
f.setAccessible(true);
if (o != null || Modifier.isStatic(f.getModifiers())) {
private void load(final Class<?> cls, final @Nullable Object object, final String path) {
for (final Field field : cls.getDeclaredFields()) {
field.setAccessible(true);
if (object != null || Modifier.isStatic(field.getModifiers())) {
try {
if (OptionSection.class.isAssignableFrom(f.getType())) {
final Object p = f.get(o);
@NonNull
final Class<?> pc = p.getClass();
load(pc, p, path + ((OptionSection) p).key + ".");
} else if (Option.class.isAssignableFrom(f.getType())) {
((Option<?>) f.get(o)).set(this, path);
if (OptionSection.class.isAssignableFrom(field.getType())) {
final OptionSection section = (OptionSection) field.get(object);
@NonNull final Class<?> pc = section.getClass();
load(pc, section, path + section.key + ".");
} else if (Option.class.isAssignableFrom(field.getType())) {
((Option<?>) field.get(object)).set(this, path);
}
} catch (final IllegalArgumentException e) {
assert false;
} catch (final IllegalAccessException e) {
} catch (final IllegalArgumentException | IllegalAccessException e) {
assert false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/effects/EffPotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@Since(
"2.0, 2.2-dev27 (ambient and particle-less potion effects), " +
"2.5 (replacing existing effect), 2.5.2 (potion effects), " +
"INSERT VERSION (icon and infinite)"
"2.7 (icon and infinite)"
)
public class EffPotion extends Effect {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ public class SimpleEvents {
"\t\tsend \"You can't drag your items here!\" to player",
"\t\tcancel event"
)
.since("INSERT VERSION");
.since("2.7");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected Number[] get(Event event) {
return new Long[] {sup};
return new Long[0];
}
return new Long[] {random.nextLong(inf, sup + 1)};
return new Long[] {inf + Math2.mod(random.nextLong(), sup - inf + 1)};
}

return new Double[] {min + random.nextDouble() * (max - min)};
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/ch/njol/skript/structures/StructAliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.aliases.ScriptAliases;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
Expand All @@ -30,6 +31,7 @@
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.entry.EntryContainer;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.structure.Structure;

@Name("Aliases")
Expand All @@ -54,7 +56,11 @@ public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parseResu
node.convertToEntries(0, "=");

// Initialize and load script aliases
Aliases.createScriptAliases(getParser().getCurrentScript()).parser.load(node);
Script script = getParser().getCurrentScript();
ScriptAliases scriptAliases = Aliases.getScriptAliases(script);
if (scriptAliases == null)
scriptAliases = Aliases.createScriptAliases(script);
scriptAliases.parser.load(node);

return true;
}
Expand Down
Loading

0 comments on commit 31e24ad

Please sign in to comment.