Skip to content

Commit

Permalink
Adds last death location expression and JUnit test (#7045)
Browse files Browse the repository at this point in the history
* 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
cheeezburga and sovdeeth authored Oct 13, 2024
1 parent 0af4c3a commit ad654cf
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
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;
}

}
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;
}

}

0 comments on commit ad654cf

Please sign in to comment.