mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 22:16:34 +00:00
Add TeleportBackEvent, Teleport.Type.BACK type, close #180
This commit is contained in:
6 files changed
+92
-36
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,39 +79,7 @@ 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) {
|
||||||
final Optional<Position> bedPosition = teleporter.getBedSpawnPosition();
|
handleInboundRespawn(teleporter);
|
||||||
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));
|
|
||||||
return;
|
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
|
* Handle when a {@link OnlineUser} leaves the server
|
||||||
*
|
*
|
||||||
@@ -198,7 +207,7 @@ public class EventListener {
|
|||||||
// Display the return by death via /back notification
|
// Display the return by death via /back notification
|
||||||
final boolean canReturnByDeath = plugin.getCommand(BackCommand.class)
|
final boolean canReturnByDeath = plugin.getCommand(BackCommand.class)
|
||||||
.map(command -> onlineUser.hasPermission(command.getPermission())
|
.map(command -> onlineUser.hasPermission(command.getPermission())
|
||||||
&& onlineUser.hasPermission(command.getPermission("death")))
|
&& onlineUser.hasPermission(command.getPermission("death")))
|
||||||
.orElse(false);
|
.orElse(false);
|
||||||
if (plugin.getSettings().doBackCommandReturnByDeath() && canReturnByDeath) {
|
if (plugin.getSettings().doBackCommandReturnByDeath() && canReturnByDeath) {
|
||||||
plugin.getLocales().getLocale("return_by_death_notification")
|
plugin.getLocales().getLocale("return_by_death_notification")
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user