-
-
Notifications
You must be signed in to change notification settings - Fork 399
Facing Relative Vector Offset #7925
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
Open
erenkarakal
wants to merge
11
commits into
SkriptLang:dev/feature
Choose a base branch
from
erenkarakal:feature/facing-relative-vector-offset
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
058cfd4
add facing relative
erenkarakal 264e757
remove warning suppressions
erenkarakal 5d740de
remove nl
erenkarakal 7fc24be
update example
erenkarakal cfd5c7e
rewrite
erenkarakal 60fa4a0
cleanup
erenkarakal dc3e3ed
tests
erenkarakal a044ee1
tests
erenkarakal fb8ca65
Merge branch 'dev/feature' into feature/facing-relative-vector-offset
erenkarakal bc81751
fix
erenkarakal c343060
Merge branch 'dev/feature' into feature/facing-relative-vector-offset
sovdeeth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,66 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.doc.*; | ||
import org.bukkit.Location; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.util.Vector; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
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.lang.Expression; | ||
import ch.njol.skript.lang.ExpressionType; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.skript.lang.util.SimpleExpression; | ||
import ch.njol.util.Kleenean; | ||
import ch.njol.util.coll.CollectionUtils; | ||
import org.joml.Quaternionf; | ||
import org.joml.Vector3f; | ||
|
||
|
||
/** | ||
* @author bi0qaw | ||
*/ | ||
@Name("Vectors - Location Vector Offset") | ||
@Description("Returns the location offset by vectors.") | ||
@Examples({"set {_loc} to {_loc} ~ {_v}"}) | ||
@Since("2.2-dev28") | ||
@Example("set {_loc} to {_loc} ~ {_v}") | ||
@Example(""" | ||
# spawn a tnt 5 blocks infront of player | ||
set {_l} to player's location offset by vector(0, 1, 5) using local axes | ||
spawn tnt at {_l} | ||
""") | ||
@Since("2.2-dev28, INSERT VERSION (facing relative)") | ||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class ExprLocationVectorOffset extends SimpleExpression<Location> { | ||
|
||
static { | ||
Skript.registerExpression(ExprLocationVectorOffset.class, Location.class, ExpressionType.COMBINED, | ||
"%location% offset by [[the] vectors] %vectors%", | ||
"%location% offset by [[the] vectors] %vectors% [facingrelative:using local axes]", | ||
"%location%[ ]~[~][ ]%vectors%"); | ||
} | ||
|
||
@SuppressWarnings("null") | ||
private Expression<Location> location; | ||
|
||
@SuppressWarnings("null") | ||
private Expression<Vector> vectors; | ||
|
||
private boolean usingLocalAxes; | ||
|
||
@Override | ||
@SuppressWarnings({"unchecked", "null"}) | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
location = (Expression<Location>) exprs[0]; | ||
vectors = (Expression<Vector>) exprs[1]; | ||
usingLocalAxes = parseResult.hasTag("facingrelative"); | ||
return true; | ||
} | ||
|
||
@SuppressWarnings("null") | ||
@Override | ||
protected Location[] get(Event e) { | ||
Location l = location.getSingle(e); | ||
if (l == null) | ||
protected Location[] get(Event event) { | ||
Location location = this.location.getSingle(event); | ||
if (location == null) | ||
return null; | ||
Location clone = l.clone(); | ||
for (Vector v : vectors.getArray(e)) | ||
clone.add(v); | ||
|
||
Location clone = location.clone(); | ||
|
||
for (Vector vector : vectors.getArray(event)) { | ||
if (usingLocalAxes) { | ||
clone = getFacingRelativeOffset(clone, vector); | ||
} else { | ||
clone.add(vector); | ||
} | ||
} | ||
return CollectionUtils.array(clone); | ||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
|
@@ -70,8 +75,31 @@ public Class<? extends Location> getReturnType() { | |
} | ||
|
||
@Override | ||
public String toString(@Nullable Event e, boolean debug) { | ||
return location.toString() + " offset by " + vectors.toString(); | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return location.toString(event, debug) + " offset by " + vectors.toString(event, debug) + (usingLocalAxes ? " using local axes" : ""); | ||
erenkarakal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Returns a location offset from the given location, adjusted for the location's rotation. | ||
* <p> | ||
* This behaves similarly to Minecraft's {@code /summon zombie ^ ^ ^1} command, | ||
* where the offset is applied relative to the entity's facing direction. | ||
* | ||
* @see <a href="https://minecraft.wiki/w/Coordinates#Local_coordinates">Local Coordinates</a>. | ||
* @param loc The location | ||
* @param offset The offset | ||
* @return The offset location | ||
*/ | ||
private static Location getFacingRelativeOffset(Location loc, Vector offset) { | ||
float yawRad = (float) Math.toRadians(-loc.getYaw()); | ||
float pitchRad = (float) Math.toRadians(loc.getPitch()); | ||
float rollRad = 0f; | ||
|
||
Quaternionf rotation = new Quaternionf().rotateYXZ(yawRad, pitchRad, rollRad); | ||
Vector3f localOffset = offset.toVector3f(); | ||
rotation.transform(localOffset); | ||
|
||
return loc.add(localOffset.x, localOffset.y, localOffset.z); | ||
Comment on lines
+108
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it'd make the code a bit nicer and standard if this returned a vector instead of a location, so the .add is done in the same spot as the normal add. |
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.