-
-
Notifications
You must be signed in to change notification settings - Fork 393
ExprMidpointLocations #7888
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
Absolutionism
wants to merge
13
commits into
SkriptLang:dev/feature
Choose a base branch
from
Absolutionism:dev/ExprCenterLocations
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
ExprMidpointLocations #7888
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4a96ab0
Initial Commit
Absolutionism a989358
Update ExprCenterLocations.sk
Absolutionism 7138126
Update ExprCenterLocations.java
Absolutionism 7043d15
Requested Changes
Absolutionism de43526
Update ExprMidpointLocations.java
Absolutionism 50efb63
Allow Vectors
Absolutionism 27ee183
Test + Error Update
Absolutionism 99c014d
Merge branch 'dev/feature' into dev/ExprCenterLocations
Absolutionism abe7dd7
Update ExprMidpoint.java
Absolutionism dd9b502
Update ExprMidpoint.java
Absolutionism 936b4c8
Update ExprMidpoint.java
Absolutionism 47bf66b
Merge branch 'dev/feature' into dev/ExprCenterLocations
Absolutionism 7f728c2
Merge branch 'dev/feature' into dev/ExprCenterLocations
Absolutionism 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
90 changes: 90 additions & 0 deletions
90
src/main/java/ch/njol/skript/expressions/ExprMidpointLocations.java
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.config.Node; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Example; | ||
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.SyntaxStringBuilder; | ||
import ch.njol.skript.lang.util.SimpleExpression; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.Location; | ||
import org.bukkit.World; | ||
import org.bukkit.event.Event; | ||
import org.bukkit.util.Vector; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.log.runtime.SyntaxRuntimeErrorProducer; | ||
|
||
@Name("Midpoint of Locations") | ||
@Description("Get the midpoint from two locations in the same world.") | ||
@Example(""" | ||
set {_center} to the midpoint between location(0, 0, 0) and location(10, 10, 10) | ||
set {_centerBlock} to the block at {_center} | ||
""") | ||
@Since("INSERT VERSION") | ||
public class ExprMidpointLocations extends SimpleExpression<Location> implements SyntaxRuntimeErrorProducer { | ||
|
||
static { | ||
Skript.registerExpression(ExprMidpointLocations.class, Location.class, ExpressionType.COMBINED, | ||
"[the] mid[-]point (of|between) %location% and %location%"); | ||
Absolutionism marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private Expression<Location> location1; | ||
private Expression<Location> location2; | ||
private Node node; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
//noinspection unchecked | ||
location1 = (Expression<Location>) exprs[0]; | ||
//noinspection unchecked | ||
location2 = (Expression<Location>) exprs[1]; | ||
node = getParser().getNode(); | ||
return true; | ||
} | ||
|
||
@Override | ||
protected Location @Nullable [] get(Event event) { | ||
Location loc1 = location1.getSingle(event); | ||
Location loc2 = location2.getSingle(event); | ||
if (loc1 == null || loc2 == null) { | ||
return null; | ||
} else if (loc1.getWorld() != loc2.getWorld()) { | ||
error("Cannot get the midpoint of two locations in different worlds."); | ||
return null; | ||
} | ||
World world = loc1.getWorld(); | ||
Vector vector = loc1.toVector().getMidpoint(loc2.toVector()); | ||
return new Location[] {vector.toLocation(world)}; | ||
} | ||
|
||
@Override | ||
public boolean isSingle() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Class<? extends Location> getReturnType() { | ||
return Location.class; | ||
} | ||
|
||
@Override | ||
public Node getNode() { | ||
return node; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return new SyntaxStringBuilder(event, debug) | ||
.append("the midpoint between") | ||
.append(location1) | ||
.append("and") | ||
.append(location2) | ||
.toString(); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/test/skript/tests/syntaxes/expressions/ExprMidpointLocations.sk
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
test "midpoint locations - simple": | ||
set {_loc1} to location(0, 0, 0) | ||
set {_loc2} to {_loc1} ~ vector(10, 10, 10) | ||
set {_center} to the midpoint between {_loc1} and {_loc2} | ||
assert {_center} is location(5, 5, 5) with "Simple midpoint loc1 -> loc2 is incorrect" | ||
set {_center2} to the midpoint between {_loc2} and {_loc1} | ||
assert {_center2} is location(5, 5, 5) with "Simple midpoint loc2 -> loc1 is incorrect" | ||
|
||
test "midpoint locations - complex": | ||
set {_loc1} to location(10.3, 38.6, 72.9) | ||
set {_loc2} to location(-24.2, 63.8, 598.1) | ||
set {_center} to the midpoint between {_loc1} and {_loc2} | ||
assert {_center} is location(-6.95, 51.2, 335.5) with "Complex midpoint loc1 -> loc2 is incorrect" | ||
set {_center2} to the midpoint between {_loc2} and {_loc1} | ||
assert {_center2} is location(-6.95, 51.2, 335.5) with "Complex midpoint loc2 -> loc1 is incorrect" | ||
|
||
test "midpoint locations - negative": | ||
set {_loc1} to location(-48.2, -33.6, -97.8) | ||
set {_loc2} to location(-257.3, -59.3, -327.4) | ||
set {_center} to the midpoint between {_loc1} and {_loc2} | ||
assert {_center} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc1 -> loc2 is incorrect" | ||
set {_center2} to the midpoint between {_loc2} and {_loc1} | ||
assert {_center2} is location(-152.75, -46.45, -212.6) with "Negative midpoint loc2 -> loc1 is incorrect" | ||
|
||
test "midpoint location - world error": | ||
set {_loc1} to location(0, 0, 0) | ||
set {_loc2} to location(0, 0, 0, "world_the_end") | ||
# TODO: Change to catch runtime errors | ||
set {_center} to the midpoint between {_loc1} and {_loc2} | ||
assert {_center} is not set with "Should not be able to get a center location from locations in different worlds" |
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.