Add TeleportWarmupEvent

This commit is contained in:
William278 committed 2022-09-02 19:36:22 +03:00
1 parent d72469145b
commit 80bfcb0747
7 files changed
+145 -45

No files matched your search

@@ -3,6 +3,7 @@ package net.william278.huskhomes.event;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TimedTeleport;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
@@ -14,6 +15,8 @@ public interface EventDispatcher {
CompletableFuture<ITeleportEvent> dispatchTeleportEvent(@NotNull Teleport teleport);
CompletableFuture<ITeleportWarmupEvent> dispatchTeleportWarmupEvent(@NotNull TimedTeleport teleport, int duration);
CompletableFuture<IHomeSaveEvent> dispatchHomeSaveEvent(@NotNull Home home);
CompletableFuture<IHomeDeleteEvent> dispatchHomeDeleteEvent(@NotNull Home home);
@@ -0,0 +1,26 @@
package net.william278.huskhomes.event;
import net.william278.huskhomes.teleport.TimedTeleport;
import org.jetbrains.annotations.NotNull;
/**
* Representation of an event that fires when a timed teleport warmup starts
*/
public interface ITeleportWarmupEvent extends CancellableEvent {
/**
* The duration of the timed teleport before the user is teleported
*
* @return the teleport warmup duration
*/
int getWarmupDuration();
/**
* The {@link TimedTeleport} being processed
*
* @return the timed teleport that has started
*/
@NotNull
TimedTeleport getTimedTeleport();
}
@@ -381,54 +381,62 @@ public class TeleportManager {
}
/**
* Processes a timed teleport
* Start the processing of a {@link TimedTeleport} warmup
*
* @param teleport the {@link TimedTeleport} to process
* @return a future, returning when the teleport has finished
*/
private CompletableFuture<TimedTeleport> processTeleportWarmup(@NotNull final TimedTeleport teleport) {
// Mark the player as warming up
currentlyOnWarmup.add(teleport.getPlayer().uuid);
// Display the message
plugin.getLocales().getLocale("teleporting_warmup_start", Integer.toString(teleport.timeLeft))
.ifPresent(teleport.getPlayer()::sendMessage);
// Create a scheduled executor to tick the timed teleport
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
final CompletableFuture<TimedTeleport> timedTeleportFuture = new CompletableFuture<>();
executor.scheduleAtFixedRate(() -> {
// Display countdown action bar message
if (teleport.timeLeft > 0) {
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_WARMUP)
.ifPresent(sound -> teleport.getPlayer().playSound(sound));
plugin.getLocales().getLocale("teleporting_action_bar_warmup", Integer.toString(teleport.timeLeft))
.ifPresent(message -> {
switch (plugin.getSettings().teleportWarmupDisplay) {
case ACTION_BAR -> teleport.getPlayer().sendActionBar(message);
case MESSAGE -> teleport.getPlayer().sendMessage(message);
}
});
} else {
plugin.getLocales().getLocale("teleporting_action_bar_processing")
.ifPresent(message -> {
switch (plugin.getSettings().teleportWarmupDisplay) {
case ACTION_BAR -> teleport.getPlayer().sendActionBar(message);
case MESSAGE -> teleport.getPlayer().sendMessage(message);
}
});
// Execute the warmup start event
return plugin.getEventDispatcher().dispatchTeleportWarmupEvent(teleport, teleport.timeLeft).thenApply(event -> {
// Handle event cancellation
if (event.isCancelled()) {
teleport.cancelled = true;
return teleport;
}
// Tick (decrement) the timed teleport timer
final Optional<TimedTeleport> result = tickTeleportWarmup(teleport);
if (result.isPresent()) {
currentlyOnWarmup.remove(teleport.getPlayer().uuid);
timedTeleportFuture.complete(teleport);
executor.shutdown();
}
}, 0, 1, TimeUnit.SECONDS);
return timedTeleportFuture;
// Mark the player as warming up
currentlyOnWarmup.add(teleport.getPlayer().uuid);
// Display the message
plugin.getLocales().getLocale("teleporting_warmup_start", Integer.toString(teleport.timeLeft))
.ifPresent(teleport.getPlayer()::sendMessage);
// Create a scheduled executor to tick the timed teleport
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
final CompletableFuture<TimedTeleport> timedTeleportFuture = new CompletableFuture<>();
executor.scheduleAtFixedRate(() -> {
// Display countdown action bar message
if (teleport.timeLeft > 0) {
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_WARMUP)
.ifPresent(sound -> teleport.getPlayer().playSound(sound));
plugin.getLocales().getLocale("teleporting_action_bar_warmup", Integer.toString(teleport.timeLeft))
.ifPresent(message -> {
switch (plugin.getSettings().teleportWarmupDisplay) {
case ACTION_BAR -> teleport.getPlayer().sendActionBar(message);
case MESSAGE -> teleport.getPlayer().sendMessage(message);
}
});
} else {
plugin.getLocales().getLocale("teleporting_action_bar_processing")
.ifPresent(message -> {
switch (plugin.getSettings().teleportWarmupDisplay) {
case ACTION_BAR -> teleport.getPlayer().sendActionBar(message);
case MESSAGE -> teleport.getPlayer().sendMessage(message);
}
});
}
// Tick (decrement) the timed teleport timer
final Optional<TimedTeleport> result = tickTeleportWarmup(teleport);
if (result.isPresent()) {
currentlyOnWarmup.remove(teleport.getPlayer().uuid);
timedTeleportFuture.complete(teleport);
executor.shutdown();
}
}, 0, 1, TimeUnit.SECONDS);
return timedTeleportFuture.join();
});
}
/**
@@ -24,7 +24,7 @@ public enum TeleportResult {
FAILED_ALREADY_TELEPORTING(false),
/**
* Returns if the {@link Teleport} warmup could not be started because they were moving
* Returns if the {@link TimedTeleport} warmup could not be started because they were moving
*/
FAILED_MOVING(false),
@@ -9,9 +9,9 @@ import org.jetbrains.annotations.NotNull;
*/
public class TimedTeleport {
private final OnlineUser onlineUser;
private final Position targetPosition;
private final Position startLocation;
public final OnlineUser onlineUser;
public final Position targetPosition;
public final Position startLocation;
private final double startHealth;
protected int timeLeft;
public boolean cancelled = false;