From e31f6596d27f83c01c44de1f1605bdf7f9f70734 Mon Sep 17 00:00:00 2001 From: Eren KARA Date: Sat, 24 May 2025 15:13:39 +0300 Subject: [PATCH 1/5] update attached block --- .../skript/expressions/ExprAttachedBlock.java | 87 ++++++++++++++----- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java index 5eab6b5d8a6..3ac1c093aa1 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java +++ b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java @@ -1,40 +1,81 @@ package ch.njol.skript.expressions; import ch.njol.skript.Skript; -import ch.njol.skript.doc.Description; -import ch.njol.skript.doc.Examples; -import ch.njol.skript.doc.Name; -import ch.njol.skript.doc.Since; -import ch.njol.skript.expressions.base.SimplePropertyExpression; +import ch.njol.skript.doc.*; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; import org.bukkit.block.Block; import org.bukkit.entity.AbstractArrow; -import org.bukkit.entity.Arrow; import org.bukkit.entity.Projectile; +import org.bukkit.event.Event; import org.jetbrains.annotations.Nullable; -@Name("Arrow Attached Block") -@Description("Returns the attached block of an arrow.") -@Examples("set hit block of last shot arrow to diamond block") -@Since("2.8.0") -public class ExprAttachedBlock extends SimplePropertyExpression { +import java.util.ArrayList; +import java.util.List; - private static final boolean HAS_ABSTRACT_ARROW = Skript.classExists("org.bukkit.entity.AbstractArrow"); +@Name("Arrow Attached Block") +@Description({ + "Returns the attached block of an arrow.", + "If running Paper 1.21.4+, the plural version of the expression should be used as the single version is unreliable." +}) +@Example("set hit block of last shot arrow to diamond block") +@Example(""" + on projectile hit: + wait 1 tick + break attached blocks of event-projectile + kill event-projectile + """) +@Since("2.8.0, INSERT VERSION (multiple blocks)") +public class ExprAttachedBlock extends SimpleExpression { static { - register(ExprAttachedBlock.class, Block.class, "(attached|hit) block", "projectiles"); + Skript.registerExpression(ExprAttachedBlock.class, Block.class, ExpressionType.PROPERTY, + "[the] (attached|hit) block[multiple:s] of %projectiles%", + "%projectiles%'[s] (attached|hit) block[multiple:s]" + ); + } + + // TODO - remove this when 1.21.4 is the minimum supported version + private static final boolean SUPPORTS_MULTIPLE = Skript.methodExists(AbstractArrow.class, "getAttachedBlocks"); + + private boolean isMultiple; + private Expression projectiles; + + @Override + public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + isMultiple = parseResult.hasTag("multiple"); + projectiles = (Expression) expressions[0]; + + if (!SUPPORTS_MULTIPLE && isMultiple) { + Skript.error("The plural version of this expression is only available in Paper 1.21.4+."); + return false; + } + + return true; } @Override - @Nullable - public Block convert(Projectile projectile) { - if (HAS_ABSTRACT_ARROW) { - if (projectile instanceof AbstractArrow) { - return ((AbstractArrow) projectile).getAttachedBlock(); + protected Block @Nullable [] get(Event event) { + List blocks = new ArrayList<>(); + + for (Projectile projectile : projectiles.getAll(event)) { + if (projectile instanceof AbstractArrow abstractArrow) { + if (isMultiple) { + blocks.addAll(abstractArrow.getAttachedBlocks()); + } else { + blocks.add(abstractArrow.getAttachedBlock()); + } } - } else if (projectile instanceof Arrow) { - return ((Arrow) projectile).getAttachedBlock(); } - return null; + return blocks.toArray(new Block[0]); + } + + @Override + public boolean isSingle() { + return !isMultiple; } @Override @@ -43,8 +84,8 @@ public Class getReturnType() { } @Override - public String getPropertyName() { - return "attached block"; + public String toString(@Nullable Event event, boolean debug) { + return "attached block" + (isMultiple ? "s" : "") + " of " + projectiles.toString(event, debug); } } From 5b0ca3343e64484d934029158271df8a9fb9e1f8 Mon Sep 17 00:00:00 2001 From: Eren KARA Date: Sat, 24 May 2025 15:22:52 +0300 Subject: [PATCH 2/5] update description --- src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java index 3ac1c093aa1..623f9b4d8c4 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java +++ b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java @@ -19,7 +19,7 @@ @Name("Arrow Attached Block") @Description({ "Returns the attached block of an arrow.", - "If running Paper 1.21.4+, the plural version of the expression should be used as the single version is unreliable." + "If running Paper 1.21.4+, the plural version of the expression should be used as it is more reliable compared to the single version." }) @Example("set hit block of last shot arrow to diamond block") @Example(""" From ea377fad47ec59774a0c226515214dd7a0b864b4 Mon Sep 17 00:00:00 2001 From: Eren KARA Date: Tue, 27 May 2025 12:01:17 +0300 Subject: [PATCH 3/5] property expression --- .../skript/expressions/ExprAttachedBlock.java | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java index 623f9b4d8c4..8775148415a 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java +++ b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java @@ -2,10 +2,11 @@ import ch.njol.skript.Skript; import ch.njol.skript.doc.*; +import ch.njol.skript.expressions.base.PropertyExpression; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; import ch.njol.skript.lang.SkriptParser; -import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.util.Kleenean; import org.bukkit.block.Block; import org.bukkit.entity.AbstractArrow; @@ -29,7 +30,7 @@ kill event-projectile """) @Since("2.8.0, INSERT VERSION (multiple blocks)") -public class ExprAttachedBlock extends SimpleExpression { +public class ExprAttachedBlock extends PropertyExpression { static { Skript.registerExpression(ExprAttachedBlock.class, Block.class, ExpressionType.PROPERTY, @@ -42,26 +43,19 @@ public class ExprAttachedBlock extends SimpleExpression { private static final boolean SUPPORTS_MULTIPLE = Skript.methodExists(AbstractArrow.class, "getAttachedBlocks"); private boolean isMultiple; - private Expression projectiles; @Override - public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { isMultiple = parseResult.hasTag("multiple"); - projectiles = (Expression) expressions[0]; - - if (!SUPPORTS_MULTIPLE && isMultiple) { - Skript.error("The plural version of this expression is only available in Paper 1.21.4+."); - return false; - } - + setExpr((Expression) expressions[0]); return true; } @Override - protected Block @Nullable [] get(Event event) { + protected Block[] get(Event event, Projectile[] source) { List blocks = new ArrayList<>(); - for (Projectile projectile : projectiles.getAll(event)) { + for (Projectile projectile : source) { if (projectile instanceof AbstractArrow abstractArrow) { if (isMultiple) { blocks.addAll(abstractArrow.getAttachedBlocks()); @@ -70,22 +64,23 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is } } } + return blocks.toArray(new Block[0]); } @Override - public boolean isSingle() { - return !isMultiple; + public Class getReturnType() { + return Block.class; } @Override - public Class getReturnType() { - return Block.class; + public boolean isSingle() { + return !isMultiple; } @Override public String toString(@Nullable Event event, boolean debug) { - return "attached block" + (isMultiple ? "s" : "") + " of " + projectiles.toString(event, debug); + return "attached block" + (isMultiple ? "s" : ""); } -} +} \ No newline at end of file From 4ece490d9de42cbb67aaeb985c2781971453f599 Mon Sep 17 00:00:00 2001 From: Eren KARA Date: Tue, 27 May 2025 12:02:38 +0300 Subject: [PATCH 4/5] multiple check --- .../java/ch/njol/skript/expressions/ExprAttachedBlock.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java index 8775148415a..9bde246acb4 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java +++ b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java @@ -48,6 +48,12 @@ public class ExprAttachedBlock extends PropertyExpression { public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { isMultiple = parseResult.hasTag("multiple"); setExpr((Expression) expressions[0]); + + if (!SUPPORTS_MULTIPLE && isMultiple) { + Skript.error("The plural version of this expression is only available in Paper 1.21.4+."); + return false; + } + return true; } From 0555c0b1fa74b322fc0570cd4915464c850562b9 Mon Sep 17 00:00:00 2001 From: Eren KARA Date: Fri, 6 Jun 2025 17:43:45 +0300 Subject: [PATCH 5/5] requested changes --- .../njol/skript/expressions/ExprAttachedBlock.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java index 9bde246acb4..3bb47b30bdc 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java +++ b/src/main/java/ch/njol/skript/expressions/ExprAttachedBlock.java @@ -5,7 +5,6 @@ import ch.njol.skript.expressions.base.PropertyExpression; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.util.Kleenean; import org.bukkit.block.Block; @@ -14,8 +13,8 @@ import org.bukkit.event.Event; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; @Name("Arrow Attached Block") @Description({ @@ -54,12 +53,16 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is return false; } + if (SUPPORTS_MULTIPLE && !isMultiple) { + Skript.warning("It is recommended to use the plural version of this expression instead."); + } + return true; } @Override protected Block[] get(Event event, Projectile[] source) { - List blocks = new ArrayList<>(); + Set blocks = new HashSet<>(); for (Projectile projectile : source) { if (projectile instanceof AbstractArrow abstractArrow) { @@ -89,4 +92,4 @@ public String toString(@Nullable Event event, boolean debug) { return "attached block" + (isMultiple ? "s" : ""); } -} \ No newline at end of file +}