diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java index c820fc84..38cca807 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java +++ b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java @@ -23,7 +23,7 @@ public interface BukkitEventDispatcher extends EventDispatcher { @Override default ITeleportEvent getTeleportEvent(@NotNull Teleport teleport) { - return new TeleportEvent(teleport); + return teleport.getType() == Teleport.Type.BACK ? new TeleportBackEvent(teleport) : new TeleportEvent(teleport); } @Override diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/TeleportBackEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/TeleportBackEvent.java new file mode 100644 index 00000000..1a5ea6bc --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/TeleportBackEvent.java @@ -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; + } + + +} diff --git a/common/src/main/java/net/william278/huskhomes/command/BackCommand.java b/common/src/main/java/net/william278/huskhomes/command/BackCommand.java index 3cadd76a..f201cadf 100644 --- a/common/src/main/java/net/william278/huskhomes/command/BackCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/BackCommand.java @@ -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) { diff --git a/common/src/main/java/net/william278/huskhomes/event/ITeleportBackEvent.java b/common/src/main/java/net/william278/huskhomes/event/ITeleportBackEvent.java new file mode 100644 index 00000000..6e20695b --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/event/ITeleportBackEvent.java @@ -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(); + +} diff --git a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java index dd58b6c5..227f34bd 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventListener.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventListener.java @@ -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 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 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") diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java index dd674bae..b45a133e 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java @@ -184,7 +184,8 @@ public class Teleport { */ public enum Type { TELEPORT(0), - RESPAWN(1); + RESPAWN(1), + BACK(2); private final int typeId;