Skip to content

Commit

Permalink
Merge branch 'dev/patch' into patch/fixEffEnchant
Browse files Browse the repository at this point in the history
  • Loading branch information
sovdeeth authored Nov 23, 2024
2 parents 10d804e + e2ddd35 commit 0de69a3
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 31 deletions.
10 changes: 9 additions & 1 deletion src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.util.slot.Slot;
import com.destroystokyo.paper.profile.PlayerProfile;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class ItemUtils {
// Introduced in Paper 1.21
public static final boolean HAS_RESET = Skript.methodExists(Damageable.class, "resetDamage");
public static final boolean CAN_CREATE_PLAYER_PROFILE = Skript.methodExists(Bukkit.class, "createPlayerProfile", UUID.class, String.class);
// paper does not do texture lookups by default
public static final boolean REQUIRES_TEXTURE_LOOKUP = Skript.classExists("com.destroystokyo.paper.profile.PlayerProfile") && Skript.isRunningMinecraft(1, 19, 4);

/**
* Gets damage/durability of an item, or 0 if it does not have damage.
Expand Down Expand Up @@ -165,7 +168,12 @@ public static void setHeadOwner(ItemType skull, OfflinePlayer player) {

SkullMeta skullMeta = (SkullMeta) meta;

if (player.getName() != null) {
if (REQUIRES_TEXTURE_LOOKUP) {
PlayerProfile profile = player.getPlayerProfile();
if (!profile.hasTextures())
profile.complete(true); // BLOCKING MOJANG API CALL
skullMeta.setPlayerProfile(profile);
} else if (player.getName() != null) {
skullMeta.setOwningPlayer(player);
} else if (CAN_CREATE_PLAYER_PROFILE) {
//noinspection deprecation
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/ch/njol/skript/classes/data/DefaultOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,53 @@ public class DefaultOperations {
static {
// Number - Number
Arithmetics.registerOperation(Operator.ADDITION, Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return left.longValue() + right.longValue();
if (Utils.isInteger(left, right)) {
long result = left.longValue() + right.longValue();
// catches overflow, from Math.addExact(long, long)
if (((left.longValue() ^ result) & (right.longValue() ^ result)) >= 0)
return result;
}
return left.doubleValue() + right.doubleValue();
});
Arithmetics.registerOperation(Operator.SUBTRACTION, Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return left.longValue() - right.longValue();
if (Utils.isInteger(left, right)) {
long result = left.longValue() - right.longValue();
// catches overflow, from Math.addExact(long, long)
if (((left.longValue() ^ result) & (right.longValue() ^ result)) >= 0)
return result;
}
return left.doubleValue() - right.doubleValue();
});
Arithmetics.registerOperation(Operator.MULTIPLICATION, Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return left.longValue() * right.longValue();
return left.doubleValue() * right.doubleValue();
if (!Utils.isInteger(left, right))
return left.doubleValue() * right.doubleValue();

// catch overflow, from Math.multiplyExact(long, long)
long longLeft = left.longValue();
long longRight = right.longValue();
long ax = Math.abs(longLeft);
long ay = Math.abs(longRight);

long result = left.longValue() * right.longValue();

if (((ax | ay) >>> 31 != 0)) {
// Some bits greater than 2^31 that might cause overflow
// Check the result using the divide operator
// and check for the special case of Long.MIN_VALUE * -1
if (((longRight != 0) && (result / longRight != longLeft)) ||
(longLeft == Long.MIN_VALUE && longRight == -1)) {
return left.doubleValue() * right.doubleValue();
}
}
return result;
});
Arithmetics.registerOperation(Operator.DIVISION, Number.class, (left, right) -> left.doubleValue() / right.doubleValue());
Arithmetics.registerOperation(Operator.EXPONENTIATION, Number.class, (left, right) -> Math.pow(left.doubleValue(), right.doubleValue()));
Arithmetics.registerDifference(Number.class, (left, right) -> {
if (Utils.isInteger(left, right))
return Math.abs(left.longValue() - right.longValue());
return Math.abs(left.doubleValue() - right.doubleValue());
double result = Math.abs(left.doubleValue() - right.doubleValue());
if (Utils.isInteger(left, right) && result < Long.MAX_VALUE && result > Long.MIN_VALUE)
return (long) result;
return result;
});
Arithmetics.registerDefaultValue(Number.class, () -> 0L);

Expand Down
67 changes: 49 additions & 18 deletions src/main/java/ch/njol/skript/expressions/ExprJoinSplit.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package ch.njol.skript.expressions;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import ch.njol.skript.SkriptConfig;
import ch.njol.skript.lang.Literal;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

Expand All @@ -36,14 +38,11 @@
import ch.njol.util.Kleenean;
import ch.njol.util.StringUtils;

/**
* @author Peter Güttinger
*/
@Name("Join & Split")
@Description("Joins several texts with a common delimiter (e.g. \", \"), or splits a text into multiple texts at a given delimiter.")
@Examples({
"message \"Online players: %join all players with \"\" | \"\"%\" # %all players% would use the default \"x, y, and z\"",
"set {_s::*} to the string argument split at \",\""
"message \"Online players: %join all players' names with \"\" | \"\"%\" # %all players% would use the default \"x, y, and z\"",
"set {_s::*} to the string argument split at \",\""
})
@Since("2.1, 2.5.2 (regex support), 2.7 (case sensitivity)")
public class ExprJoinSplit extends SimpleExpression<String> {
Expand All @@ -61,33 +60,47 @@ public class ExprJoinSplit extends SimpleExpression<String> {
private boolean regex;
private boolean caseSensitivity;

@SuppressWarnings("null")
private Expression<String> strings;
@Nullable
private Expression<String> delimiter;
private @Nullable Expression<String> delimiter;

private @Nullable Pattern pattern;

@Override
@SuppressWarnings({"unchecked", "null"})
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
join = matchedPattern == 0;
regex = matchedPattern >= 3;
caseSensitivity = SkriptConfig.caseSensitive.value() || parseResult.hasTag("case");
//noinspection unchecked
strings = (Expression<String>) exprs[0];
//noinspection unchecked
delimiter = (Expression<String>) exprs[1];
if (!join && delimiter instanceof Literal) {
String stringPattern = ((Literal<String>) delimiter).getSingle();
try {
this.pattern = compilePattern(stringPattern);
} catch (PatternSyntaxException e) {
Skript.error("'" + stringPattern + "' is not a valid regular expression");
return false;
}
}
return true;
}

@Override
@Nullable
protected String[] get(Event event) {
protected String @Nullable [] get(Event event) {
String[] strings = this.strings.getArray(event);
String delimiter = this.delimiter != null ? this.delimiter.getSingle(event) : "";
if (strings.length == 0 || delimiter == null)
return new String[0];
if (join) {
if (join)
return new String[] {StringUtils.join(strings, delimiter)};
} else {
return strings[0].split(regex ? delimiter : (caseSensitivity ? "" : "(?i)") + Pattern.quote(delimiter), -1);
try {
Pattern pattern = this.pattern;
if (pattern == null)
pattern = compilePattern(delimiter);
return pattern.split(strings[0], -1);
} catch (PatternSyntaxException e) {
return new String[0];
}
}

Expand All @@ -103,10 +116,28 @@ public Class<? extends String> getReturnType() {

@Override
public String toString(@Nullable Event event, boolean debug) {
if (join)
return "join " + strings.toString(event, debug) + (delimiter != null ? " with " + delimiter.toString(event, debug) : "");
return (regex ? "regex " : "") + "split " + strings.toString(event, debug) + (delimiter != null ? " at " + delimiter.toString(event, debug) : "")
+ (regex ? "" : "(case sensitive: " + caseSensitivity + ")");
StringBuilder builder = new StringBuilder();
if (join) {
builder.append("join ").append(strings.toString(event, debug));
if (delimiter != null)
builder.append(" with ").append(delimiter.toString(event, debug));
return builder.toString();
}

assert delimiter != null;
if (regex)
builder.append("regex ");
builder.append("split ")
.append(strings.toString(event, debug))
.append(" at ")
.append(delimiter.toString(event, debug));
if (!regex)
builder.append(" (case sensitive: ").append(caseSensitivity).append(")");
return builder.toString();
}

private Pattern compilePattern(String delimiter) {
return Pattern.compile(regex ? delimiter : (caseSensitivity ? "" : "(?i)") + Pattern.quote(delimiter));
}

}
4 changes: 2 additions & 2 deletions src/main/resources/lang/turkish.lang
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ skript command:
test: Skript'in dahili testleri için kullanılır.

invalid script: <grey>'<gold>%s<grey>'<red> adlı script scripts klasöründe bulunamadı!
invalid folder: <grey>'<gold>%s<grey>'<red> adlı klasör scripts klasöründe bulunamadı!
invalid folder: <grey>'<gold>%s<grey>'<red> adlı klasör scripts klasöründe bulunamadı!
reload:
warning line info: <gold><bold>Satır %s:<gray> (%s)<reset>\n
error line info: <light red><bold>Satır %s:<gray> (%s)<reset>\n
Expand Down Expand Up @@ -165,7 +165,7 @@ aliases:
unknown variation: %s varyasyonu daha önce tanımlanmadı.
missing aliases: Bu Minecraft ID'lerinin herhangi bir alias'ı yoktur:
empty alias: Alias'ın tanımlı bir Minecraft ID'si veya tag'i yok.
invalid minecraft id: Minecraft ID %s geçerli değil
invalid minecraft id: Minecraft ID %s geçerli değil
useless variation: Varyasyonun Minecraft ID'si veya tag'i yok, bu yüzden gereksiz.
invalid tags: Belirtilen tagler geçerli JSON'da tanımlı değil.
unexpected section: Section'lara burada izin verilmiyor.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test "regex exceptions not handled":
parse:
set {_split::*} to regex split "test" at "\b{_name}\b"
assert last parse logs is "'\b{_name}\b' is not a valid regular expression" with "regex split did not error with invalid regex literal"

set {_pattern} to "\b{_name}\b"
set {_split::*} to regex split "test" at {_pattern}
assert {_split::*} is not set with "regex split returned a value with invalid regex expression"

assert regex split "apple,banana;cherry" at "[,;]" is "apple", "banana" and "cherry" with "regex split did not split correctly with literal"
set {_pattern} to "[,;]"
assert regex split "apple,banana;cherry" at {_pattern} is "apple", "banana" and "cherry" with "regex split did not split correctly with expression"
34 changes: 34 additions & 0 deletions src/test/skript/tests/regressions/7209-long overflow.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
test "long overflow, addition":
set {_double-const} to 9000000000000000000.0
set {_long-const} to 9000000000000000000

loop 100 times:
set {_double} to {_double} + {_double-const}
set {_long} to {_long} + {_long-const}
assert {_double} is {_long} with "long value did not overflow to a double"

assert {_long} is 9000000000000000000 * 100.0 with "wrong final value"

test "long underflow, subtraction":
set {_double-const} to 9000000000000000000.0
set {_long-const} to 9000000000000000000

loop 100 times:
set {_double} to {_double} - {_double-const}
set {_long} to {_long} - {_long-const}
assert {_double} is {_long} with "long value did not overflow to a double"

assert {_long} is 9000000000000000000 * -100.0 with "wrong final value"

test "long overflow, multiplication":
set {_double} to 10.0
set {_long} to 10

loop 100 times:
set {_double} to {_double} * 10
set {_long} to {_long} * 10
assert {_double} is {_long} with "long value did not overflow to a double"

# the 6 is due to floating point error
assert {_long} is 100000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with "wrong final value"

0 comments on commit 0de69a3

Please sign in to comment.