mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
Simplify teleport manager, use scheduled executor for warmup
This commit is contained in:
1 parent
d494797aa4
commit
1a280393ea
3 files changed
+72
-88
No files matched your search
@@ -20,7 +20,6 @@ import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.position.SavedPositionManager;
|
||||
import net.william278.huskhomes.position.Server;
|
||||
import net.william278.huskhomes.teleport.BukkitTeleportManager;
|
||||
import net.william278.huskhomes.teleport.TeleportManager;
|
||||
import net.william278.huskhomes.util.*;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
@@ -151,7 +150,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
|
||||
cache.initialize(database);
|
||||
|
||||
// Prepare the teleport manager
|
||||
this.teleportManager = new BukkitTeleportManager(this);
|
||||
this.teleportManager = new TeleportManager(this);
|
||||
|
||||
// Prepare the home and warp position manager
|
||||
this.savedPositionManager = new SavedPositionManager(database, cache);
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package net.william278.huskhomes.teleport;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* Bukkit implementation of a {@link TeleportManager}, handling timed teleports through a BukkitRunnable
|
||||
*/
|
||||
public class BukkitTeleportManager extends TeleportManager {
|
||||
|
||||
public BukkitTeleportManager(@NotNull HuskHomes implementor) {
|
||||
super(implementor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<TimedTeleport> processTimedTeleport(@NotNull final TimedTeleport teleport) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
CompletableFuture<TimedTeleport> teleportCompletableFuture = new CompletableFuture<>();
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Display countdown action bar message
|
||||
plugin.getLocales().getLocale("teleporting_action_bar_countdown", Integer.toString(teleport.timeLeft))
|
||||
.ifPresent(message -> teleport.getPlayer().sendActionBar(message));
|
||||
|
||||
// Tick (decrement) the timed teleport timer
|
||||
final Optional<TimedTeleport> result = tickTimedTeleport(teleport);
|
||||
if (result.isPresent()) {
|
||||
teleportCompletableFuture.complete(teleport);
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}.runTaskTimerAsynchronously((BukkitHuskHomes) plugin, 0L, 20L);
|
||||
|
||||
return teleportCompletableFuture.join();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,11 +14,14 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Cross-platform teleportation manager
|
||||
*/
|
||||
public abstract class TeleportManager {
|
||||
public class TeleportManager {
|
||||
|
||||
/**
|
||||
* Instance of the implementing plugin
|
||||
@@ -114,23 +117,23 @@ public abstract class TeleportManager {
|
||||
* @return future optionally supplying the player's position, if the player could be found
|
||||
*/
|
||||
private CompletableFuture<Optional<Position>> getPlayerPosition(@NotNull OnlineUser requester, @NotNull String playerName) {
|
||||
final Optional<OnlineUser> localPlayer = plugin.getOnlinePlayers().stream().filter(player ->
|
||||
player.username.equalsIgnoreCase(playerName)).findFirst();
|
||||
if (localPlayer.isPresent()) {
|
||||
return localPlayer.get().getPosition().thenApply(Optional::of);
|
||||
}
|
||||
if (plugin.getSettings().crossServer) {
|
||||
assert plugin.getNetworkMessenger() != null;
|
||||
return plugin.getNetworkMessenger().sendMessage(requester,
|
||||
new Message(Message.MessageType.POSITION_REQUEST,
|
||||
requester.username,
|
||||
playerName,
|
||||
new EmptyPayload(),
|
||||
Message.MessageKind.MESSAGE,
|
||||
plugin.getSettings().clusterId))
|
||||
.thenApply(reply -> Optional.of(Position.fromJson(reply.payload)));
|
||||
}
|
||||
return CompletableFuture.supplyAsync(Optional::empty);
|
||||
final Optional<OnlineUser> localPlayer = plugin.getOnlinePlayers().stream().filter(player ->
|
||||
player.username.equalsIgnoreCase(playerName)).findFirst();
|
||||
if (localPlayer.isPresent()) {
|
||||
return localPlayer.get().getPosition().thenApply(Optional::of);
|
||||
}
|
||||
if (plugin.getSettings().crossServer) {
|
||||
assert plugin.getNetworkMessenger() != null;
|
||||
return plugin.getNetworkMessenger().sendMessage(requester,
|
||||
new Message(Message.MessageType.POSITION_REQUEST,
|
||||
requester.username,
|
||||
playerName,
|
||||
new EmptyPayload(),
|
||||
Message.MessageKind.MESSAGE,
|
||||
plugin.getSettings().clusterId))
|
||||
.thenApply(reply -> Optional.of(Position.fromJson(reply.payload)));
|
||||
}
|
||||
return CompletableFuture.supplyAsync(Optional::empty);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,19 +143,19 @@ public abstract class TeleportManager {
|
||||
* @param position the target {@link Position} to teleport to
|
||||
*/
|
||||
public CompletableFuture<TeleportResult> teleport(@NotNull OnlineUser onlineUser, @NotNull Position position) {
|
||||
final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime;
|
||||
if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) {
|
||||
return processTimedTeleport(new TimedTeleport(onlineUser, position, teleportWarmupTime))
|
||||
.thenApply(teleport -> {
|
||||
if (!teleport.cancelled) {
|
||||
return teleportNow(onlineUser, position);
|
||||
} else {
|
||||
return CompletableFuture.supplyAsync(() -> TeleportResult.CANCELLED);
|
||||
}
|
||||
}).join();
|
||||
} else {
|
||||
return teleportNow(onlineUser, position);
|
||||
}
|
||||
final int teleportWarmupTime = plugin.getSettings().teleportWarmupTime;
|
||||
if (!onlineUser.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node) && teleportWarmupTime > 0) {
|
||||
return processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime))
|
||||
.thenApply(teleport -> {
|
||||
if (!teleport.cancelled) {
|
||||
return teleportNow(onlineUser, position);
|
||||
} else {
|
||||
return CompletableFuture.supplyAsync(() -> TeleportResult.CANCELLED);
|
||||
}
|
||||
}).join();
|
||||
} else {
|
||||
return teleportNow(onlineUser, position);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,12 +210,46 @@ public abstract class TeleportManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a timed teleport, ticking it
|
||||
* Processes a timed teleport
|
||||
*
|
||||
* @param teleport the {@link TimedTeleport} to process
|
||||
* @return a future, returning when the teleport has finished
|
||||
*/
|
||||
private CompletableFuture<TimedTeleport> processTeleportWarmup(@NotNull final TimedTeleport teleport) {
|
||||
// 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
|
||||
plugin.getLocales().getLocale("teleporting_action_bar_countdown", Integer.toString(teleport.timeLeft))
|
||||
.ifPresent(message -> teleport.getPlayer().sendActionBar(message));
|
||||
|
||||
// Tick (decrement) the timed teleport timer
|
||||
final Optional<TimedTeleport> result = tickTeleportWarmup(teleport);
|
||||
if (result.isPresent()) {
|
||||
timedTeleportFuture.complete(teleport);
|
||||
executor.shutdown();
|
||||
}
|
||||
}, 0, 1, TimeUnit.SECONDS);
|
||||
return timedTeleportFuture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ticks a timed teleport, decrementing the time left until the teleport is complete
|
||||
* <p>
|
||||
* A timed teleport will be cancelled if certain criteria are met:
|
||||
* <ul>
|
||||
* <li>The player has left the server</li>
|
||||
* <li>The plugin is disabling</li>
|
||||
* <li>The player has moved beyond the movement threshold from when the warmup started</li>
|
||||
* <li>The player has taken damage (though they may heal, have status ailments or lose/gain hunger)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param teleport the {@link TimedTeleport} being ticked
|
||||
* @return {@code true} if the implementor should cancel the timedTeleport
|
||||
* @return Optional containing the {@link TimedTeleport} after it has been ticked,
|
||||
* or {@link Optional#empty()} if the teleport has been cancelled
|
||||
*/
|
||||
protected final Optional<TimedTeleport> tickTimedTeleport(@NotNull final TimedTeleport teleport) {
|
||||
private Optional<TimedTeleport> tickTeleportWarmup(@NotNull final TimedTeleport teleport) {
|
||||
if (teleport.isDone()) {
|
||||
return Optional.of(teleport);
|
||||
}
|
||||
@@ -242,12 +279,4 @@ public abstract class TeleportManager {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a timed teleport, implemented by platform-specific schedulers
|
||||
*
|
||||
* @param teleport the {@link TimedTeleport} to process
|
||||
* @return a future, returning when the teleport has finished
|
||||
*/
|
||||
public abstract CompletableFuture<TimedTeleport> processTimedTeleport(TimedTeleport teleport);
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user