Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed command tab complete not scanning softlink folders #7186

Open
wants to merge 15 commits into
base: dev/feature
Choose a base branch
from
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
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
13 changes: 12 additions & 1 deletion src/main/java/ch/njol/skript/SkriptCommandTabCompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String

// Live update, this will get all old and new (even not loaded) scripts
// TODO Find a better way for caching, it isn't exactly ideal to be calling this method constantly
try (Stream<Path> files = Files.walk(scripts.toPath())) {
try (Stream<Path> files = Files.walk(scripts.toPath(), FileVisitOption.FOLLOW_LINKS)) {
files.map(Path::toFile)
.forEach(file -> {
if (!(enable ? ScriptLoader.getDisabledScriptsFilter() : ScriptLoader.getLoadedScriptsFilter()).accept(file))
Expand All @@ -69,6 +69,17 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
if (file.isHidden())
return;

// When following a soft link on linux, additional processing may be required to detect whether the content pointed to by the symbolic link exists
if (Files.isSymbolicLink(file.toPath())) {
try {
Path resolvedPath = file.toPath().toRealPath();
if (!Files.exists(resolvedPath))
return;
} catch (Exception e) {
return;
}
}

String fileString = file.toString().substring(scriptsPathLength);
if (fileString.isEmpty())
return;
Expand Down
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
31 changes: 26 additions & 5 deletions src/main/java/ch/njol/skript/effects/EffPlaySound.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class EffPlaySound extends Effect {
private static final boolean ENTITY_EMITTER_SOUND = Skript.methodExists(Player.class, "playSound", Entity.class, Sound.class, SoundCategory.class, float.class, float.class);
private static final boolean ENTITY_EMITTER_STRING = Skript.methodExists(Player.class, "playSound", Entity.class, String.class, SoundCategory.class, float.class, float.class);
private static final boolean ENTITY_EMITTER = ENTITY_EMITTER_SOUND || ENTITY_EMITTER_STRING;
private static final boolean SOUND_IS_INTERFACE = Sound.class.isInterface();

public static final Pattern KEY_PATTERN = Pattern.compile("([a-z0-9._-]+:)?([a-z0-9/._-]+)");

Expand Down Expand Up @@ -149,11 +151,8 @@ protected void execute(Event event) {
// validate strings
List<NamespacedKey> validSounds = new ArrayList<>();
for (String sound : sounds.getArray(event)) {
NamespacedKey key = null;
try {
Sound enumSound = Sound.valueOf(sound.toUpperCase(Locale.ENGLISH));
key = enumSound.getKey();
} catch (IllegalArgumentException alternative) {
NamespacedKey key = getSoundKeyFromEnum(sound);
if (key == null) {
sound = sound.toLowerCase(Locale.ENGLISH);
Matcher keyMatcher = KEY_PATTERN.matcher(sound);
if (!keyMatcher.matches())
Expand Down Expand Up @@ -240,4 +239,26 @@ public String toString(@Nullable Event event, boolean debug) {
return builder.toString();
}

@SuppressWarnings({"deprecation", "unchecked", "rawtypes"})
private static @Nullable NamespacedKey getSoundKeyFromEnum(String soundString) {
soundString = soundString.toUpperCase(Locale.ENGLISH);
// Sound.class is an Interface (rather than an enum) as of MC 1.21.3
if (SOUND_IS_INTERFACE) {
try {
Sound sound = Sound.valueOf(soundString);
return sound.getKey();
} catch (IllegalArgumentException ignore) {
}
} else {
try {
Enum soundEnum = Enum.valueOf((Class) Sound.class, soundString);
if (soundEnum instanceof Keyed) {
return ((Keyed) soundEnum).getKey();
}
} catch (IllegalArgumentException ignore) {
}
}
return null;
}

}
51 changes: 32 additions & 19 deletions src/main/java/ch/njol/skript/effects/EffReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,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 @@ -106,20 +108,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 @@ -137,14 +128,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) {
if (replaceFirst)
return "replace first " + needles.toString(event, debug) + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug)
+ "(case sensitive: " + caseSensitive + ")";
return "replace " + needles.toString(event, debug) + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug)
+ "(case sensitive: " + caseSensitive + ")";
return "replace first " + needles.toString(event, debug) + " in " + haystack.toString(event, debug) +
" with " + replacement.toString(event, debug) + "(case sensitive: " + caseSensitive + ")";
return "replace " + needles.toString(event, debug) + " in " + haystack.toString(event, debug) +
" with " + replacement.toString(event, debug) + "(case sensitive: " + caseSensitive + ")";
}

}
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 @@ -340,13 +340,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