-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement player abilities packet #432
Changes from all commits
6248ae2
94b7c45
77ce704
fa7f8f6
9227f37
83299c7
f459632
dd37e02
9b134d9
891929d
f7f354b
74a9cba
d406adb
009ae2a
0de8026
b54567d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Obsidian.API._Types; | ||
|
||
[Flags] | ||
public enum PlayerAbility | ||
{ | ||
None = 0x00, | ||
Invulnerable = 0x01, | ||
Flying = 0x02, | ||
AllowFlying = 0x04, | ||
CreativeMode = 0x08 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using Obsidian.API._Types; | ||
using Obsidian.Entities; | ||
|
||
namespace Obsidian.Net.Packets.Play.Clientbound; | ||
|
||
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); | ||
} | ||
Comment on lines
+8
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the Field attribute on the packet properties so the source generators can write Serialize and Populate methods for you There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently the SG does not like it because it does not have a parameter-less constructor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Packet SG doesn't use/look at constructors 🤨 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to make sure the class is set to partial ^ ^ |
||
|
||
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be made accessible through the api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍