Add TeleportBackEvent, Teleport.Type.BACK type, close #180

This commit is contained in:
William committed 2023-04-03 12:11:11 +01:00
1 parent 0af761d5d6
commit dfeab6f5fc
6 files changed
+92 -36

No files matched your search

@@ -33,6 +33,7 @@ public class BackCommand extends InGameCommand {
.teleporter(executor)
.target(lastPosition.get())
.economyActions(EconomyHook.Action.BACK_COMMAND)
.type(Teleport.Type.BACK)
.toTimedTeleport()
.execute();
} catch (TeleportationException e) {
@@ -0,0 +1,11 @@
package net.william278.huskhomes.event;
import net.william278.huskhomes.position.Position;
import org.jetbrains.annotations.NotNull;
public interface ITeleportBackEvent extends ITeleportEvent {
@NotNull
Position getLastPosition();
}
@@ -79,39 +79,7 @@ public class EventListener {
private void handleInboundTeleport(@NotNull OnlineUser teleporter) {
plugin.getDatabase().getCurrentTeleport(teleporter).ifPresent(teleport -> {
if (teleport.getType() == Teleport.Type.RESPAWN) {
final Optional<Position> bedPosition = teleporter.getBedSpawnPosition();
if (bedPosition.isEmpty()) {
plugin.getSpawn().ifPresent(spawn -> {
if (plugin.getSettings().doCrossServer() && !spawn.getServer().equals(plugin.getServerName())) {
plugin.runLater(() -> {
try {
Teleport.builder(plugin)
.teleporter(teleporter)
.target(spawn)
.updateLastPosition(false)
.toTeleport().execute();
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin, new String[0]);
}
}, 40L);
} else {
try {
teleporter.teleportLocally(spawn, plugin.getSettings().doAsynchronousTeleports());
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin, new String[0]);
}
}
teleporter.sendTranslatableMessage("block.minecraft.spawn.not_valid");
});
} else {
try {
teleporter.teleportLocally(bedPosition.get(), plugin.getSettings().doAsynchronousTeleports());
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin, new String[0]);
}
}
plugin.getDatabase().setCurrentTeleport(teleporter, null);
plugin.getDatabase().setRespawnPosition(teleporter, bedPosition.orElse(null));
handleInboundRespawn(teleporter);
return;
}
@@ -125,6 +93,47 @@ public class EventListener {
});
}
/**
* Handle an inbound global respawn
*
* @param teleporter the user to handle the checks for
*/
private void handleInboundRespawn(@NotNull OnlineUser teleporter) {
final Optional<Position> bedPosition = teleporter.getBedSpawnPosition();
if (bedPosition.isEmpty()) {
plugin.getSpawn().ifPresent(spawn -> {
if (plugin.getSettings().doCrossServer() && !spawn.getServer().equals(plugin.getServerName())) {
plugin.runLater(() -> {
try {
Teleport.builder(plugin)
.teleporter(teleporter)
.target(spawn)
.updateLastPosition(false)
.toTeleport().execute();
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin, new String[0]);
}
}, 40L);
} else {
try {
teleporter.teleportLocally(spawn, plugin.getSettings().doAsynchronousTeleports());
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin, new String[0]);
}
}
teleporter.sendTranslatableMessage("block.minecraft.spawn.not_valid");
});
} else {
try {
teleporter.teleportLocally(bedPosition.get(), plugin.getSettings().doAsynchronousTeleports());
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin, new String[0]);
}
}
plugin.getDatabase().setCurrentTeleport(teleporter, null);
plugin.getDatabase().setRespawnPosition(teleporter, bedPosition.orElse(null));
}
/**
* Handle when a {@link OnlineUser} leaves the server
*
@@ -198,7 +207,7 @@ public class EventListener {
// Display the return by death via /back notification
final boolean canReturnByDeath = plugin.getCommand(BackCommand.class)
.map(command -> onlineUser.hasPermission(command.getPermission())
&& onlineUser.hasPermission(command.getPermission("death")))
&& onlineUser.hasPermission(command.getPermission("death")))
.orElse(false);
if (plugin.getSettings().doBackCommandReturnByDeath() && canReturnByDeath) {
plugin.getLocales().getLocale("return_by_death_notification")
@@ -184,7 +184,8 @@ public class Teleport {
*/
public enum Type {
TELEPORT(0),
RESPAWN(1);
RESPAWN(1),
BACK(2);
private final int typeId;