-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd37e02
commit 9b134d9
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Obsidian/Net/Packets/Play/Clientbound/PlayerAbilitiesPacket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Obsidian.Entities; | ||
|
||
namespace Obsidian.Net.Packets.Play.Clientbound; | ||
|
||
[Flags] | ||
public enum PlayerAbility | ||
{ | ||
None = 0x00, | ||
Invulnerable = 0x01, | ||
Flying = 0x02, | ||
AllowFlying = 0x04, | ||
CreativeMode = 0x08 | ||
} | ||
|
||
public class PlayerAbilitiesPacket : IClientboundPacket, IServerboundPacket | ||
{ | ||
public PlayerAbility Abilities { get; set; } = PlayerAbility.None; | ||
|
||
public float FlyingSpeed { get; set; } = 0.05F; | ||
|
||
public float FieldOfViewModifier { get; set; } = 0.1F; | ||
|
||
public int Id { get; } | ||
|
||
public PlayerAbilitiesPacket(bool toClient) | ||
{ | ||
Id = toClient ? 0x36 : 0x20; | ||
} | ||
|
||
public void Serialize(MinecraftStream stream) | ||
{ | ||
using var packetStream = new MinecraftStream(); | ||
packetStream.WriteByte((byte)Abilities); | ||
packetStream.WriteFloat(FlyingSpeed); | ||
packetStream.WriteFloat(FieldOfViewModifier); | ||
|
||
stream.Lock.Wait(); | ||
stream.WriteVarInt(Id.GetVarIntLength() + (int)packetStream.Length); | ||
stream.WriteVarInt(Id); | ||
packetStream.Position = 0; | ||
packetStream.CopyTo(stream); | ||
stream.Lock.Release(); | ||
} | ||
|
||
public void Populate(MinecraftStream stream) | ||
{ | ||
Abilities = (PlayerAbility) stream.ReadByte(); | ||
} | ||
|
||
public void Populate(byte[] data) | ||
{ | ||
using var stream = new MinecraftStream(data); | ||
Populate(stream); | ||
} | ||
|
||
public async ValueTask HandleAsync(Server server, Player player) | ||
{ | ||
if (Abilities.HasFlag(PlayerAbility.Flying) | ||
&& !Abilities.HasFlag(PlayerAbility.AllowFlying) | ||
&& player.Gamemode is not Gamemode.Creative or Gamemode.Spectator) | ||
{ | ||
await player.KickAsync("Cheating is not allowed!"); | ||
} | ||
|
||
player.Abilities |= Abilities; | ||
} | ||
} |