mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
Networking rework, further refactoring
This commit is contained in:
1 parent
1cebdac9b8
commit
4c90e5498c
88 files changed
+1116
-1364
No files matched your search
@@ -0,0 +1,168 @@
|
||||
package net.william278.huskhomes.user;
|
||||
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.william278.huskhomes.BukkitHuskHomes;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.teleport.TeleportResult;
|
||||
import net.william278.huskhomes.util.BukkitAdapter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.metadata.MetadataValue;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Bukkit implementation of an {@link OnlineUser}
|
||||
*/
|
||||
public class BukkitUser extends OnlineUser {
|
||||
|
||||
/**
|
||||
* Number of ticks to wait before {@link #sendPluginMessage(String, byte[]) sending a plugin message}.
|
||||
* </p>
|
||||
* This is needed because it is not possible to have players dispatch plugin messages in certain circumstance,
|
||||
* such as on the tick that they join the server.
|
||||
*/
|
||||
private static final long PLUGIN_MESSAGE_DISPATCH_DELAY = 3L;
|
||||
|
||||
// Instance of the implementing plugin
|
||||
private final BukkitHuskHomes plugin;
|
||||
|
||||
// The Bukkit player
|
||||
private final Player player;
|
||||
|
||||
private BukkitUser(@NotNull Player player) {
|
||||
super(player.getUniqueId(), player.getName());
|
||||
this.plugin = BukkitHuskHomes.getInstance();
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapt a {@link Player} to a {@link OnlineUser}
|
||||
*
|
||||
* @param player the online {@link Player} to adapt
|
||||
* @return the adapted {@link OnlineUser}
|
||||
*/
|
||||
@NotNull
|
||||
public static BukkitUser adapt(@NotNull Player player) {
|
||||
return new BukkitUser(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an online {@link BukkitUser} by their exact username
|
||||
*
|
||||
* @param username the UUID of the player to find
|
||||
* @return an {@link Optional} containing the {@link BukkitUser} if found; {@link Optional#empty()} otherwise
|
||||
*/
|
||||
public static Optional<BukkitUser> get(@NotNull String username) {
|
||||
final Player player = Bukkit.getPlayerExact(username);
|
||||
if (player != null) {
|
||||
return Optional.of(adapt(player));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position getPosition() {
|
||||
return new Position(BukkitAdapter.adaptLocation(player.getLocation())
|
||||
.orElseThrow(() -> new IllegalStateException("Failed to get the position of a BukkitPlayer (null)")),
|
||||
plugin.getServerName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Position> getBedSpawnPosition() {
|
||||
return Optional.ofNullable(player.getBedSpawnLocation()).flatMap(BukkitAdapter::adaptLocation)
|
||||
.map(location -> new Position(location, plugin.getServerName()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getHealth() {
|
||||
return player.getHealth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NotNull String node) {
|
||||
return player.hasPermission(node);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public @NotNull Map<String, Boolean> getPermissions() {
|
||||
return player.getEffectivePermissions().stream().collect(
|
||||
Collectors.toMap(PermissionAttachmentInfo::getPermission,
|
||||
PermissionAttachmentInfo::getValue, (a, b) -> b));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull Audience getAudience() {
|
||||
return plugin.getAudiences().player(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<TeleportResult> teleportLocally(@NotNull Location location, boolean asynchronous) {
|
||||
final Optional<org.bukkit.Location> bukkitLocation = BukkitAdapter.adaptLocation(location);
|
||||
if (bukkitLocation.isEmpty()) {
|
||||
return CompletableFuture.completedFuture(TeleportResult.FAILED_INVALID_WORLD);
|
||||
}
|
||||
assert bukkitLocation.get().getWorld() != null;
|
||||
if (!bukkitLocation.get().getWorld().getWorldBorder().isInside(bukkitLocation.get())) {
|
||||
return CompletableFuture.completedFuture(TeleportResult.FAILED_ILLEGAL_COORDINATES);
|
||||
}
|
||||
final CompletableFuture<TeleportResult> resultCompletableFuture = new CompletableFuture<>();
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (asynchronous) {
|
||||
PaperLib.teleportAsync(player, bukkitLocation.get(), PlayerTeleportEvent.TeleportCause.PLUGIN)
|
||||
.thenAccept(result -> resultCompletableFuture.complete(
|
||||
result ? TeleportResult.COMPLETED_LOCALLY : TeleportResult.FAILED_INVALID_WORLD));
|
||||
} else {
|
||||
player.teleport(bukkitLocation.get(), PlayerTeleportEvent.TeleportCause.PLUGIN);
|
||||
resultCompletableFuture.complete(TeleportResult.COMPLETED_LOCALLY);
|
||||
}
|
||||
});
|
||||
return resultCompletableFuture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMoving() {
|
||||
// Get the player momentum and return if they are moving
|
||||
return player.getVelocity().length() >= 0.1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVanished() {
|
||||
// Return the value of the player's "vanished" metadata tag if they have it
|
||||
return player.getMetadata("vanished")
|
||||
.stream()
|
||||
.map(MetadataValue::asBoolean)
|
||||
.findFirst()
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a Bukkit plugin message
|
||||
*
|
||||
* @implNote This is dispatched after {@link #PLUGIN_MESSAGE_DISPATCH_DELAY a delay} to ensure that the player
|
||||
* is ready to send receive messages
|
||||
*/
|
||||
public void sendPluginMessage(@NotNull String channel, final byte[] message) {
|
||||
Bukkit.getScheduler().runTaskLater(plugin,
|
||||
() -> player.sendPluginMessage(plugin, channel, message), PLUGIN_MESSAGE_DISPATCH_DELAY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Player} wrapped by this {@link BukkitUser}
|
||||
*
|
||||
* @return the {@link Player} wrapped by this {@link BukkitUser}
|
||||
*/
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user