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
+69 -13

No files matched your search

@@ -23,7 +23,7 @@ public interface BukkitEventDispatcher extends EventDispatcher {
@Override @Override
default ITeleportEvent getTeleportEvent(@NotNull Teleport teleport) { default ITeleportEvent getTeleportEvent(@NotNull Teleport teleport) {
return new TeleportEvent(teleport); return teleport.getType() == Teleport.Type.BACK ? new TeleportBackEvent(teleport) : new TeleportEvent(teleport);
} }
@Override @Override
@@ -0,0 +1,34 @@
package net.william278.huskhomes.event;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.Teleport;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
public class TeleportBackEvent extends TeleportEvent implements ITeleportBackEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();
public TeleportBackEvent(@NotNull Teleport teleport) {
super(teleport);
}
@Override
@NotNull
public Position getLastPosition() {
return (Position) getTeleport().getTarget();
}
@NotNull
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
@SuppressWarnings("unused")
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}
@@ -33,6 +33,7 @@ public class BackCommand extends InGameCommand {
.teleporter(executor) .teleporter(executor)
.target(lastPosition.get()) .target(lastPosition.get())
.economyActions(EconomyHook.Action.BACK_COMMAND) .economyActions(EconomyHook.Action.BACK_COMMAND)
.type(Teleport.Type.BACK)
.toTimedTeleport() .toTimedTeleport()
.execute(); .execute();
} catch (TeleportationException e) { } 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,6 +79,26 @@ public class EventListener {
private void handleInboundTeleport(@NotNull OnlineUser teleporter) { private void handleInboundTeleport(@NotNull OnlineUser teleporter) {
plugin.getDatabase().getCurrentTeleport(teleporter).ifPresent(teleport -> { plugin.getDatabase().getCurrentTeleport(teleporter).ifPresent(teleport -> {
if (teleport.getType() == Teleport.Type.RESPAWN) { if (teleport.getType() == Teleport.Type.RESPAWN) {
handleInboundRespawn(teleporter);
return;
}
try {
teleporter.teleportLocally((Position) teleport.getTarget(), plugin.getSettings().doAsynchronousTeleports());
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin, new String[0]);
}
plugin.getDatabase().setCurrentTeleport(teleporter, null);
teleport.displayTeleportingComplete(teleporter);
});
}
/**
* 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(); final Optional<Position> bedPosition = teleporter.getBedSpawnPosition();
if (bedPosition.isEmpty()) { if (bedPosition.isEmpty()) {
plugin.getSpawn().ifPresent(spawn -> { plugin.getSpawn().ifPresent(spawn -> {
@@ -112,17 +132,6 @@ public class EventListener {
} }
plugin.getDatabase().setCurrentTeleport(teleporter, null); plugin.getDatabase().setCurrentTeleport(teleporter, null);
plugin.getDatabase().setRespawnPosition(teleporter, bedPosition.orElse(null)); plugin.getDatabase().setRespawnPosition(teleporter, bedPosition.orElse(null));
return;
}
try {
teleporter.teleportLocally((Position) teleport.getTarget(), plugin.getSettings().doAsynchronousTeleports());
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin, new String[0]);
}
plugin.getDatabase().setCurrentTeleport(teleporter, null);
teleport.displayTeleportingComplete(teleporter);
});
} }
/** /**
@@ -184,7 +184,8 @@ public class Teleport {
*/ */
public enum Type { public enum Type {
TELEPORT(0), TELEPORT(0),
RESPAWN(1); RESPAWN(1),
BACK(2);
private final int typeId; private final int typeId;