mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-04-26 00:46:46 +00:00
Theres documentation at https://sylvessa.zip/fourkit/ now. And a bunch of other changes. Check the discord server for a more comprehensive list
210 lines
6.7 KiB
C#
210 lines
6.7 KiB
C#
namespace Minecraft.Server.FourKit.Entity;
|
|
|
|
using System.Runtime.InteropServices;
|
|
using Minecraft.Server.FourKit.Command;
|
|
using Minecraft.Server.FourKit.Inventory;
|
|
using Minecraft.Server.FourKit.Net;
|
|
|
|
/// <summary>
|
|
/// Represents a player connected to the server.
|
|
/// </summary>
|
|
public class Player : HumanEntity, OfflinePlayer, CommandSender
|
|
{
|
|
private float _saturation = 5.0f;
|
|
private float _walkSpeed = 0.2f;
|
|
private Guid _playerUniqueId;
|
|
private string? _displayName;
|
|
|
|
internal bool IsOnline { get; set; }
|
|
|
|
internal Player(int entityId, string name)
|
|
{
|
|
SetEntityIdInternal(entityId);
|
|
SetEntityTypeInternal(EntityType.PLAYER);
|
|
SetNameInternal(name);
|
|
IsOnline = true;
|
|
_playerInventory._holder = this;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override EntityType getType() => EntityType.PLAYER;
|
|
|
|
/// <inheritdoc/>
|
|
public override EntityType GetType() => EntityType.PLAYER;
|
|
|
|
/// <inheritdoc/>
|
|
public override bool teleport(Location location)
|
|
{
|
|
NativeBridge.TeleportPlayer?.Invoke(getEntityId(), location.X, location.Y, location.Z);
|
|
SetLocation(location);
|
|
return true;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public Player? getPlayer() => IsOnline ? this : null;
|
|
|
|
/// <summary>
|
|
/// Gets the "friendly" name to display of this player.
|
|
/// This may include color. If no custom display name has been set,
|
|
/// this returns the player's <see cref="HumanEntity.getName"/>.
|
|
/// </summary>
|
|
/// <returns>The display name.</returns>
|
|
public string getDisplayName() => _displayName ?? getName();
|
|
|
|
/// <summary>
|
|
/// Sets the "friendly" name to display of this player.
|
|
/// </summary>
|
|
/// <param name="name">The display name, or <c>null</c> to reset to <see cref="HumanEntity.getName"/>.</param>
|
|
public void setDisplayName(string? name)
|
|
{
|
|
_displayName = name;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public bool isOnline() => IsOnline;
|
|
|
|
/// <summary>
|
|
/// Returns the UUID that uniquely identifies this player across sessions.
|
|
/// This is the player-specific UUID, not the entity UUID.
|
|
/// </summary>
|
|
/// <returns>The player's unique identifier.</returns>
|
|
public new Guid getUniqueId() => _playerUniqueId;
|
|
|
|
/// <summary>
|
|
/// Gets the player's current saturation level.
|
|
/// Saturation acts as a buffer before hunger begins to deplete.
|
|
/// </summary>
|
|
/// <returns>The current saturation level.</returns>
|
|
public float getSaturation() => _saturation;
|
|
|
|
/// <summary>
|
|
/// Gets the current allowed speed that a client can walk.
|
|
/// The default value is 0.2.
|
|
/// </summary>
|
|
/// <returns>The current walk speed.</returns>
|
|
public float getWalkSpeed() => _walkSpeed;
|
|
|
|
/// <summary>
|
|
/// Sets the speed at which a client will walk.
|
|
/// This calls into the native server to apply the change.
|
|
/// </summary>
|
|
/// <param name="value">The new walk speed.</param>
|
|
public void setWalkSpeed(float value)
|
|
{
|
|
_walkSpeed = value;
|
|
NativeBridge.SetWalkSpeed?.Invoke(getEntityId(), value);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void sendMessage(string message)
|
|
{
|
|
if (string.IsNullOrEmpty(message) || NativeBridge.SendMessage == null)
|
|
return;
|
|
if (message.Length > FourKit.MAX_CHAT_LENGTH)
|
|
message = message[..FourKit.MAX_CHAT_LENGTH];
|
|
|
|
IntPtr ptr = Marshal.StringToCoTaskMemUTF8(message);
|
|
try
|
|
{
|
|
NativeBridge.SendMessage(getEntityId(), ptr, System.Text.Encoding.UTF8.GetByteCount(message));
|
|
}
|
|
finally
|
|
{
|
|
Marshal.FreeCoTaskMem(ptr);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public void sendMessage(string[] messages)
|
|
{
|
|
foreach (var msg in messages)
|
|
sendMessage(msg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Kicks player with the default <see cref="DisconnectReason.KICKED"/> reason.
|
|
/// </summary>
|
|
public void kickPlayer()
|
|
{
|
|
NativeBridge.KickPlayer?.Invoke(getEntityId(), (int)DisconnectReason.KICKED);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bans the player by UID with the specified reason and disconnects them.
|
|
/// </summary>
|
|
/// <param name="reason">The ban reason.</param>
|
|
/// <returns><c>true</c> if the ban was applied successfully.</returns>
|
|
public bool banPlayer(string reason)
|
|
{
|
|
if (NativeBridge.BanPlayer == null) return false;
|
|
IntPtr ptr = Marshal.StringToCoTaskMemUTF8(reason ?? string.Empty);
|
|
try
|
|
{
|
|
int byteLen = System.Text.Encoding.UTF8.GetByteCount(reason ?? string.Empty);
|
|
return NativeBridge.BanPlayer(getEntityId(), ptr, byteLen) != 0;
|
|
}
|
|
finally
|
|
{
|
|
Marshal.FreeCoTaskMem(ptr);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Bans the player's IP address with the specified reason.
|
|
/// </summary>
|
|
/// <param name="reason">The ban reason.</param>
|
|
/// <returns><c>true</c> if the IP ban was applied successfully.</returns>
|
|
public bool banPlayerIp(string reason)
|
|
{
|
|
if (NativeBridge.BanPlayerIp == null) return false;
|
|
IntPtr ptr = Marshal.StringToCoTaskMemUTF8(reason ?? string.Empty);
|
|
try
|
|
{
|
|
int byteLen = System.Text.Encoding.UTF8.GetByteCount(reason ?? string.Empty);
|
|
return NativeBridge.BanPlayerIp(getEntityId(), ptr, byteLen) != 0;
|
|
}
|
|
finally
|
|
{
|
|
Marshal.FreeCoTaskMem(ptr);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the socket address of this player.
|
|
/// </summary>
|
|
/// <returns>The player's socket address, or <c>null</c> if the address could not be determined.</returns>
|
|
public InetSocketAddress? getAddress()
|
|
{
|
|
if (NativeBridge.GetPlayerAddress == null)
|
|
return null;
|
|
|
|
const int ipBufSize = 64;
|
|
IntPtr ipBuf = Marshal.AllocCoTaskMem(ipBufSize);
|
|
IntPtr portBuf = Marshal.AllocCoTaskMem(sizeof(int));
|
|
try
|
|
{
|
|
int result = NativeBridge.GetPlayerAddress(getEntityId(), ipBuf, ipBufSize, portBuf);
|
|
if (result == 0)
|
|
return null;
|
|
|
|
string? ip = Marshal.PtrToStringAnsi(ipBuf);
|
|
int port = Marshal.ReadInt32(portBuf);
|
|
|
|
if (string.IsNullOrEmpty(ip))
|
|
return null;
|
|
|
|
return new InetSocketAddress(new InetAddress(ip), port);
|
|
}
|
|
finally
|
|
{
|
|
Marshal.FreeCoTaskMem(ipBuf);
|
|
Marshal.FreeCoTaskMem(portBuf);
|
|
}
|
|
}
|
|
|
|
// INTERNAL
|
|
internal void SetSaturationInternal(float saturation) => _saturation = saturation;
|
|
internal void SetWalkSpeedInternal(float walkSpeed) => _walkSpeed = walkSpeed;
|
|
internal void SetPlayerUniqueIdInternal(Guid id) => _playerUniqueId = id;
|
|
}
|