Skip to content

Commit

Permalink
Merge branch 'dev/feature' into angle
Browse files Browse the repository at this point in the history
  • Loading branch information
Efnilite authored Dec 6, 2024
2 parents ae2765a + 7c4569b commit 28742ef
Show file tree
Hide file tree
Showing 20 changed files with 396 additions and 153 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.parallel=true

groupid=ch.njol
name=skript
version=2.9.4
version=2.9.5
jarName=Skript.jar
testEnv=java21/paper-1.21.3
testEnvJavaVersion=21
20 changes: 19 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 Expand Up @@ -304,6 +312,16 @@ public static boolean isAir(Material type) {
// cherry
if (Skript.isRunningMinecraft(1, 19, 4))
TREE_TO_SAPLING_MAP.put(TreeType.CHERRY, Material.CHERRY_SAPLING);

// mega pine (2x2 spruce tree with minimal leaves at top)
if (Skript.isRunningMinecraft(1, 20, 5))
TREE_TO_SAPLING_MAP.put(TreeType.MEGA_PINE, Material.SPRUCE_SAPLING);

// pale oak
if (Skript.isRunningMinecraft(1, 21, 3)) {
TREE_TO_SAPLING_MAP.put(TreeType.PALE_OAK, Material.PALE_OAK_SAPLING);
TREE_TO_SAPLING_MAP.put(TreeType.PALE_OAK_CREAKING, Material.PALE_OAK_SAPLING);
}
}

public static Material getTreeSapling(TreeType treeType) {
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
35 changes: 16 additions & 19 deletions src/main/java/ch/njol/skript/effects/EffEnchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
*/
package ch.njol.skript.effects;

import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
Expand All @@ -36,6 +31,11 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.EnchantmentType;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;

/**
* @author Peter Güttinger
Expand Down Expand Up @@ -72,29 +72,26 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye

@Override
protected void execute(Event event) {
ItemType[] items = this.items.getArray(event);
if (items.length == 0) // short circuit
return;
Function<ItemType, ItemType> changeFunction;

if (enchantments != null) {
EnchantmentType[] types = enchantments.getArray(event);
if (types.length == 0)
return;
for (ItemType item : items) {
for (EnchantmentType type : types) {
Enchantment enchantment = type.getType();
assert enchantment != null;
item.addEnchantments(new EnchantmentType(enchantment, type.getLevel()));
}
}
changeFunction = item -> {
item.addEnchantments(types);
return item;
};
} else {
for (ItemType item : items) {
changeFunction = item -> {
item.clearEnchantments();
}
return item;
};
}
this.items.change(event, items.clone(), ChangeMode.SET);

this.items.changeInPlace(event, changeFunction);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return enchantments == null ? "disenchant " + items.toString(event, debug) : "enchant " + items.toString(event, debug) + " with " + enchantments;
Expand Down
45 changes: 29 additions & 16 deletions src/main/java/ch/njol/skript/effects/EffReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
import org.bukkit.event.Event;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;

@Name("Replace")
Expand Down Expand Up @@ -107,20 +109,9 @@ private void replace(Event event, Object[] needles, Expression<?> haystackExpr)
if (replacement == null || haystack == null || haystack.length == 0 || needles == null || needles.length == 0)
return;
if (replaceString) {
if (replaceFirst) {
for (int x = 0; x < haystack.length; x++)
for (Object n : needles) {
assert n != null;
haystack[x] = StringUtils.replaceFirst((String)haystack[x], (String)n, Matcher.quoteReplacement((String)replacement), caseSensitive);
}
} else {
for (int x = 0; x < haystack.length; x++)
for (Object n : needles) {
assert n != null;
haystack[x] = StringUtils.replace((String) haystack[x], (String) n, (String) replacement, caseSensitive);
}
}
haystackExpr.change(event, haystack, ChangeMode.SET);
Function<String, String> replaceFunction = getReplaceFunction(needles, (String) replacement);
//noinspection unchecked
((Expression<String>) haystackExpr).changeInPlace(event, replaceFunction);
} else {
for (Inventory inv : (Inventory[]) haystack)
for (ItemType needle : (ItemType[]) needles)
Expand All @@ -138,14 +129,36 @@ private void replace(Event event, Object[] needles, Expression<?> haystackExpr)
}
}
}


private @NotNull Function<String, String> getReplaceFunction(Object[] needles, String replacement) {
Function<String, String> replaceFunction;
if (replaceFirst) {
replaceFunction = haystackString -> {
for (Object needle : needles) {
assert needle != null;
haystackString = StringUtils.replaceFirst(haystackString, (String) needle, Matcher.quoteReplacement(replacement), caseSensitive);
}
return haystackString;
};
} else {
replaceFunction = haystackString -> {
for (Object needle : needles) {
assert needle != null;
haystackString = StringUtils.replace(haystackString, (String) needle, replacement, caseSensitive);
}
return haystackString;
};
}
return replaceFunction;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);

builder.append("replace");
if (replaceFirst)
builder.append("the first");
builder.append("first");
builder.append(needles, "in", haystack, "with", replacement);
if (caseSensitive)
builder.append("with case sensitivity");
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/ch/njol/skript/events/SimpleEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,10 @@ public class SimpleEvents {
.requiredPlugins("Minecraft 1.14+ (event-entity support)")
.since("1.0, 2.5.3 (event-entity support)");
Skript.registerEvent("Projectile Hit", SimpleEvent.class, ProjectileHitEvent.class, "projectile hit")
.description("Called when a projectile hits an entity or a block.",
"Use the <a href='#damage'>damage event</a> with a <a href='conditions.html#CondIsSet'>check</a> for a <a href='expressions.html#ExprEntity'>projectile</a> " +
"to be able to use the <a href='expressions.html#ExprAttacked'>entity that got hit</a> in the case when the projectile hit a living entity.",
"A damage event will even be fired if the damage is 0, e.g. when throwing snowballs at non-nether mobs.")
.description("Called when a projectile hits an entity or a block.")
.examples("on projectile hit:",
"\tevent-projectile is arrow",
"\tdelete event-projectile")
"\tif victim's health <= 3:",
"\t\tdelete event-projectile")
.since("1.0");

if(Skript.classExists("com.destroystokyo.paper.event.entity.ProjectileCollideEvent"))
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/ch/njol/skript/expressions/ExprBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@
@Since("1.0, 2.5.1 (within/cuboid/chunk)")
public class ExprBlocks extends SimpleExpression<Block> {

private static final boolean SUPPORTS_WORLD_LOADED = Skript.methodExists(Location.class, "isWorldLoaded");

static {
Skript.registerExpression(ExprBlocks.class, Block.class, ExpressionType.COMBINED,
"[(all [[of] the]|the)] blocks %direction% [%locations%]", // TODO doesn't loop all blocks?
"[(all [[of] the]|the)] blocks %direction% [%locations%]",
"[(all [[of] the]|the)] blocks from %location% [on] %direction%",
"[(all [[of] the]|the)] blocks from %location% to %location%",
"[(all [[of] the]|the)] blocks between %location% and %location%",
Expand Down Expand Up @@ -116,7 +118,11 @@ protected Block[] get(Event event) {
return from.stream(event)
.filter(Location.class::isInstance)
.map(Location.class::cast)
.filter(Location::isWorldLoaded)
.filter(location -> {
if (SUPPORTS_WORLD_LOADED)
return location.isWorldLoaded();
return location.getChunk().isLoaded();
})
.map(direction::getRelative)
.map(Location::getBlock)
.toArray(Block[]::new);
Expand Down
Loading

0 comments on commit 28742ef

Please sign in to comment.