-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds last death location expression and JUnit test (#7045)
* Adds last death location expression, and tries to add JUnit test * Fixes JUnit test * Adds Skript doc annotations --------- Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com>
- Loading branch information
1 parent
0af4c3a
commit ad654cf
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/main/java/ch/njol/skript/expressions/ExprLastDeathLocation.java
This file contains 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,66 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.classes.Changer.ChangeMode; | ||
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.util.coll.CollectionUtils; | ||
import org.bukkit.Location; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@Name("Last Death Location") | ||
@Description({ | ||
"Gets the last death location of a player, or offline player, if available.", | ||
"Can also be set, reset, and deleted if the player is online." | ||
}) | ||
@Examples({ | ||
"set {_loc} to the last death location of player", | ||
"teleport player to last death location of (random element out of all players)" | ||
}) | ||
@Since("INSERT VERSION") | ||
public class ExprLastDeathLocation extends SimplePropertyExpression<OfflinePlayer, Location> { | ||
|
||
static { | ||
register(ExprLastDeathLocation.class, Location.class, "[last] death location[s]", "offlineplayers"); | ||
} | ||
|
||
@Override | ||
public @Nullable Location convert(OfflinePlayer offlinePlayer) { | ||
return offlinePlayer instanceof Player player | ||
? player.getLastDeathLocation() | ||
: offlinePlayer.getLastDeathLocation(); | ||
} | ||
|
||
@Override | ||
public Class<?> @Nullable [] acceptChange(ChangeMode mode) { | ||
return switch (mode) { | ||
case SET, RESET, DELETE -> CollectionUtils.array(Location.class); | ||
default -> null; | ||
}; | ||
} | ||
|
||
@Override | ||
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) { | ||
Location loc = (delta != null && delta[0] instanceof Location location) ? location : null; | ||
for (OfflinePlayer offlinePlayer : getExpr().getArray(event)) { | ||
if (offlinePlayer instanceof Player player) | ||
player.setLastDeathLocation(loc); | ||
} | ||
} | ||
|
||
@Override | ||
protected String getPropertyName() { | ||
return "last death location"; | ||
} | ||
|
||
@Override | ||
public Class<? extends Location> getReturnType() { | ||
return Location.class; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...java/org/skriptlang/skript/test/tests/syntaxes/expressions/ExprLastDeathLocationTest.java
This file contains 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,75 @@ | ||
package org.skriptlang.skript.test.tests.syntaxes.expressions; | ||
|
||
import ch.njol.skript.lang.Effect; | ||
import ch.njol.skript.lang.TriggerItem; | ||
import ch.njol.skript.lang.util.ContextlessEvent; | ||
import ch.njol.skript.test.runner.SkriptJUnitTest; | ||
import ch.njol.skript.variables.Variables; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.entity.Player; | ||
import org.easymock.EasyMock; | ||
import org.easymock.IArgumentMatcher; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ExprLastDeathLocationTest extends SkriptJUnitTest { | ||
|
||
private Player player; | ||
private Effect get, set; | ||
|
||
@Before | ||
public void setup() { | ||
player = EasyMock.niceMock(Player.class); | ||
set = Effect.parse("set last death location of {_player} to {_location}", null); | ||
get = Effect.parse("set {_loc} to last death location of {_player}", null); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
if (get == null) | ||
Assert.fail("Get statement was null"); | ||
if (set == null) | ||
Assert.fail("Set statement was null"); | ||
|
||
Location location = new Location(Bukkit.getWorld("world"), 0, 0, 0); | ||
|
||
ContextlessEvent event = ContextlessEvent.get(); | ||
Variables.setVariable("player", player, event, true); | ||
Variables.setVariable("location", location, event, true); | ||
|
||
player.setLastDeathLocation(locationMatcher(location)); | ||
EasyMock.expectLastCall(); | ||
EasyMock.replay(player); | ||
TriggerItem.walk(set, event); | ||
EasyMock.verify(player); | ||
|
||
EasyMock.resetToNice(player); | ||
|
||
EasyMock.expect(player.getLastDeathLocation()).andReturn(location); | ||
EasyMock.replay(player); | ||
TriggerItem.walk(get, event); | ||
EasyMock.verify(player); | ||
} | ||
|
||
private <T> T locationMatcher(Location expectedLoc) { | ||
EasyMock.reportMatcher(new IArgumentMatcher() { | ||
@Override | ||
public boolean matches(Object argument) { | ||
if (argument instanceof Location location) { | ||
return location.equals(expectedLoc); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void appendTo(StringBuffer buffer) { | ||
buffer.append("[location matcher]"); | ||
} | ||
}); | ||
|
||
return null; | ||
} | ||
|
||
} |