mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 14:38:53 +00:00
Add TeleportWarmupEvent
This commit is contained in:
1 parent
d72469145b
commit
80bfcb0747
7 files changed
+145
-45
No files matched your search
@@ -4,6 +4,7 @@ import net.william278.huskhomes.BukkitHuskHomes;
|
|||||||
import net.william278.huskhomes.position.Home;
|
import net.william278.huskhomes.position.Home;
|
||||||
import net.william278.huskhomes.position.Warp;
|
import net.william278.huskhomes.position.Warp;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
|
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@@ -28,6 +29,17 @@ public class BukkitEventDispatcher implements EventDispatcher {
|
|||||||
return dispatchFuture;
|
return dispatchFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<ITeleportWarmupEvent> dispatchTeleportWarmupEvent(@NotNull TimedTeleport teleport, int duration) {
|
||||||
|
final CompletableFuture<ITeleportWarmupEvent> dispatchFuture = new CompletableFuture<>();
|
||||||
|
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||||
|
final TeleportWarmupEvent event = new TeleportWarmupEvent(teleport, duration);
|
||||||
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
|
dispatchFuture.complete(event);
|
||||||
|
});
|
||||||
|
return dispatchFuture;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<IHomeSaveEvent> dispatchHomeSaveEvent(@NotNull Home home) {
|
public CompletableFuture<IHomeSaveEvent> dispatchHomeSaveEvent(@NotNull Home home) {
|
||||||
final CompletableFuture<IHomeSaveEvent> dispatchFuture = new CompletableFuture<>();
|
final CompletableFuture<IHomeSaveEvent> dispatchFuture = new CompletableFuture<>();
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package net.william278.huskhomes.event;
|
||||||
|
|
||||||
|
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class TeleportWarmupEvent extends Event implements ITeleportWarmupEvent, Cancellable {
|
||||||
|
private static final HandlerList HANDLER_LIST = new HandlerList();
|
||||||
|
@NotNull
|
||||||
|
private final TimedTeleport warp;
|
||||||
|
private final int duration;
|
||||||
|
private boolean cancelled;
|
||||||
|
|
||||||
|
public TeleportWarmupEvent(@NotNull TimedTeleport warp, int duration) {
|
||||||
|
this.warp = warp;
|
||||||
|
this.duration = duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public HandlerList getHandlers() {
|
||||||
|
return HANDLER_LIST;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HandlerList getHandlerList() {
|
||||||
|
return HANDLER_LIST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCancelled(boolean cancelled) {
|
||||||
|
this.cancelled = cancelled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCancelled() {
|
||||||
|
return cancelled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getWarmupDuration() {
|
||||||
|
return duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public TimedTeleport getTimedTeleport() {
|
||||||
|
return warp;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package net.william278.huskhomes.event;
|
|||||||
import net.william278.huskhomes.position.Home;
|
import net.william278.huskhomes.position.Home;
|
||||||
import net.william278.huskhomes.position.Warp;
|
import net.william278.huskhomes.position.Warp;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
|
import net.william278.huskhomes.teleport.TimedTeleport;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -14,6 +15,8 @@ public interface EventDispatcher {
|
|||||||
|
|
||||||
CompletableFuture<ITeleportEvent> dispatchTeleportEvent(@NotNull Teleport teleport);
|
CompletableFuture<ITeleportEvent> dispatchTeleportEvent(@NotNull Teleport teleport);
|
||||||
|
|
||||||
|
CompletableFuture<ITeleportWarmupEvent> dispatchTeleportWarmupEvent(@NotNull TimedTeleport teleport, int duration);
|
||||||
|
|
||||||
CompletableFuture<IHomeSaveEvent> dispatchHomeSaveEvent(@NotNull Home home);
|
CompletableFuture<IHomeSaveEvent> dispatchHomeSaveEvent(@NotNull Home home);
|
||||||
|
|
||||||
CompletableFuture<IHomeDeleteEvent> dispatchHomeDeleteEvent(@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
|
* @param teleport the {@link TimedTeleport} to process
|
||||||
* @return a future, returning when the teleport has finished
|
* @return a future, returning when the teleport has finished
|
||||||
*/
|
*/
|
||||||
private CompletableFuture<TimedTeleport> processTeleportWarmup(@NotNull final TimedTeleport teleport) {
|
private CompletableFuture<TimedTeleport> processTeleportWarmup(@NotNull final TimedTeleport teleport) {
|
||||||
|
// Execute the warmup start event
|
||||||
// Mark the player as warming up
|
return plugin.getEventDispatcher().dispatchTeleportWarmupEvent(teleport, teleport.timeLeft).thenApply(event -> {
|
||||||
currentlyOnWarmup.add(teleport.getPlayer().uuid);
|
// Handle event cancellation
|
||||||
|
if (event.isCancelled()) {
|
||||||
// Display the message
|
teleport.cancelled = true;
|
||||||
plugin.getLocales().getLocale("teleporting_warmup_start", Integer.toString(teleport.timeLeft))
|
return teleport;
|
||||||
.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
|
// Mark the player as warming up
|
||||||
final Optional<TimedTeleport> result = tickTeleportWarmup(teleport);
|
currentlyOnWarmup.add(teleport.getPlayer().uuid);
|
||||||
if (result.isPresent()) {
|
|
||||||
currentlyOnWarmup.remove(teleport.getPlayer().uuid);
|
// Display the message
|
||||||
timedTeleportFuture.complete(teleport);
|
plugin.getLocales().getLocale("teleporting_warmup_start", Integer.toString(teleport.timeLeft))
|
||||||
executor.shutdown();
|
.ifPresent(teleport.getPlayer()::sendMessage);
|
||||||
}
|
|
||||||
}, 0, 1, TimeUnit.SECONDS);
|
// Create a scheduled executor to tick the timed teleport
|
||||||
return timedTeleportFuture;
|
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),
|
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),
|
FAILED_MOVING(false),
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
*/
|
*/
|
||||||
public class TimedTeleport {
|
public class TimedTeleport {
|
||||||
|
|
||||||
private final OnlineUser onlineUser;
|
public final OnlineUser onlineUser;
|
||||||
private final Position targetPosition;
|
public final Position targetPosition;
|
||||||
private final Position startLocation;
|
public final Position startLocation;
|
||||||
private final double startHealth;
|
private final double startHealth;
|
||||||
protected int timeLeft;
|
protected int timeLeft;
|
||||||
public boolean cancelled = false;
|
public boolean cancelled = false;
|
||||||
|
|||||||
Reference in new issue
Block a user