Skip to content

Commit

Permalink
Add the new entity types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Nov 24, 2024
1 parent 53af5cb commit 8de7905
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 146 deletions.
36 changes: 28 additions & 8 deletions Obsidian.API/_Enums/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,58 @@

public enum EntityType : int
{
AcaciaBoat,
AcaciaChestBoat,
Allay,
AreaEffectCloud,
Armadillo,
ArmorStand,
Arrow,
Axolotl,
BambooChestRaft,
BambooRaft,
Bat,
Bee,
BirchBoat,
BirchChestBoat,
Blaze,
BlockDisplay,
Boat,
Bogged,
Breeze,
BreezeWindCharge,
Camel,
Cat,
CaveSpider,
ChestBoat,
CherryBoat,
CherryChestBoat,
ChestMinecart,
Chicken,
Cod,
CommandBlockMinecart,
Cow,
Creaking,
CreakingTransient,
Creeper,
DarkOakBoat,
DarkOakChestBoat,
Dolphin,
Donkey,
DragonFireball,
Drowned,
Egg,
ElderGuardian,
EndCrystal,
EnderDragon,
EnderPearl,
Enderman,
Endermite,
EnderDragon,
EnderPearl,
EndCrystal,
Evoker,
EvokerFangs,
ExperienceBottle,
ExperienceOrb,
EyeOfEnder,
FallingBlock,
Fireball,
FireworkRocket,
Fox,
Frog,
Expand All @@ -63,19 +74,26 @@ public enum EntityType : int
Item,
ItemDisplay,
ItemFrame,
OminousItemSpawner,
Fireball,
JungleBoat,
JungleChestBoat,
LeashKnot,
LightningBolt,
Llama,
LlamaSpit,
MagmaCube,
MangroveBoat,
MangroveChestBoat,
Marker,
Minecart,
Mooshroom,
Mule,
OakBoat,
OakChestBoat,
Ocelot,
OminousItemSpawner,
Painting,
PaleOakBoat,
PaleOakChestBoat,
Panda,
Parrot,
Phantom,
Expand All @@ -98,11 +116,13 @@ public enum EntityType : int
Slime,
SmallFireball,
Sniffer,
SnowGolem,
Snowball,
SnowGolem,
SpawnerMinecart,
SpectralArrow,
Spider,
SpruceBoat,
SpruceChestBoat,
Squid,
Stray,
Strider,
Expand Down
3 changes: 2 additions & 1 deletion Obsidian.API/_Enums/Gamemode.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace Obsidian.API;

public enum Gamemode : byte
public enum Gamemode : sbyte
{
None = -1,
Survival,
Creative,
Adventure,
Expand Down
45 changes: 45 additions & 0 deletions Obsidian.API/_Types/CommonPlayerSpawnInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Obsidian.API;
public readonly struct CommonPlayerSpawnInfo : INetworkSerializable<CommonPlayerSpawnInfo>
{
public required int DimensionType { get; init; }

public required string DimensionName { get; init; }
public long HashedSeed { get; init; }
public Gamemode Gamemode { get; init; }
public Gamemode PreviousGamemode { get; init; }

public bool Debug { get; init; }

public bool Flat { get; init; }
public bool HasDeathLocation => !string.IsNullOrEmpty(this.DeathDimensionName);
public string? DeathDimensionName { get; init; }
public Vector? DeathLocation { get; init; }

public int PortalCooldown { get; init; }

public int SeaLevel { get; init; }

public static void Write(CommonPlayerSpawnInfo value, INetStreamWriter writer)
{
writer.WriteVarInt(value.DimensionType);
writer.WriteString(value.DimensionName);

writer.WriteLong(value.HashedSeed);

writer.WriteByte(value.Gamemode);
writer.WriteByte(value.PreviousGamemode);

writer.WriteBoolean(value.Debug);
writer.WriteBoolean(value.Flat);
writer.WriteBoolean(value.HasDeathLocation);

if (value.HasDeathLocation)
{
writer.WriteString(value.DeathDimensionName!);
writer.WritePosition(value.DeathLocation!.Value);
}

writer.WriteVarInt(value.PortalCooldown);
writer.WriteVarInt(value.SeaLevel);
}
}
49 changes: 27 additions & 22 deletions Obsidian/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -571,22 +571,32 @@ internal async Task ConnectAsync()
await QueuePacketAsync(new LoginPacket
{
EntityId = id,
Gamemode = Player.Gamemode,
DimensionNames = CodecRegistry.Dimensions.All.Keys.ToList(),
DimensionType = codec.Id,
DimensionName = codec.Name,
HashedSeed = 0,
CommonPlayerSpawnInfo = new()
{
Gamemode = Player.Gamemode,
DimensionType = codec.Id,
DimensionName = codec.Name,
HashedSeed = 0,
Flat = false
},
ReducedDebugInfo = false,
EnableRespawnScreen = true,
Flat = false
});

await QueuePacketAsync(new SetDefaultSpawnPositionPacket(Player.world.LevelData.SpawnPosition, 0));
await SendTimeUpdateAsync();
await SendWeatherUpdateAsync();

await SendServerBrand();

await SendCommandsAsync();

//Information has to be sent in a certain order or the client will auto throw a network protocol error.
await SendPlayerInfoAsync();
await this.QueuePacketAsync(new GameEventPacket(ChangeGameStateReason.StartWaitingForLevelChunks));
await SendInfoAsync();

await QueuePacketAsync(new GameEventPacket(ChangeGameStateReason.StartWaitingForLevelChunks));

Player.TeleportId = Globals.Random.Next(0, 999);
await QueuePacketAsync(new PlayerPositionPacket
Expand All @@ -599,7 +609,6 @@ await QueuePacketAsync(new PlayerPositionPacket
});

await Player.UpdateChunksAsync(distance: 7);
//await SendInfoAsync();
await this.server.EventDispatcher.ExecuteEventAsync(new PlayerJoinEventArgs(Player, this.server, DateTimeOffset.Now));
}

Expand All @@ -608,22 +617,18 @@ internal async Task SendInfoAsync()
{
if (Player is null)
throw new UnreachableException("Player is null, which means the client has not yet logged in.");

await QueuePacketAsync(new ContainerSetContentPacket(0, Player.Inventory.ToList())
{
StateId = Player.Inventory.StateId++,
CarriedItem = Player.GetHeldItem(),
});

//await QueuePacketAsync(new SetDefaultSpawnPositionPacket(Player.world.LevelData.SpawnPosition, 0));

//await SendTimeUpdateAsync();
//await SendWeatherUpdateAsync();
//await QueuePacketAsync(new ContainerSetContentPacket(0, Player.Inventory.ToList())
//{
// StateId = Player.Inventory.StateId++,
// CarriedItem = Player.GetHeldItem(),
//});

//await QueuePacketAsync(new SetEntityDataPacket
//{
// EntityId = this.Player.EntityId,
// Entity = this.Player
//});
await QueuePacketAsync(new SetEntityDataPacket
{
EntityId = this.Player.EntityId,
Entity = this.Player
});
}

internal async Task DisconnectAsync(ChatMessage reason)
Expand Down
17 changes: 10 additions & 7 deletions Obsidian/Entities/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,16 @@ public async Task RespawnAsync(DataKept dataKept = DataKept.Metadata)

await client.QueuePacketAsync(new RespawnPacket
{
DimensionType = codec.Name,
DimensionName = world.DimensionName,
Gamemode = Gamemode,
PreviousGamemode = Gamemode,
HashedSeed = 0,
IsFlat = false,
IsDebug = false,
CommonPlayerSpawnInfo = new()
{
DimensionType = codec.Id,
DimensionName = world.DimensionName,
Gamemode = Gamemode,
PreviousGamemode = Gamemode,
HashedSeed = 0,
Flat = false,
Debug = false,
},
DataKept = dataKept,
});

Expand Down
2 changes: 0 additions & 2 deletions Obsidian/Net/MinecraftStream.Writing.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.CodeAnalysis;
using Obsidian.API.Advancements;
using Obsidian.API.Crafting;
using Obsidian.API.Events;
using Obsidian.API.Inventory;
using Obsidian.API.Registry.Codecs.ArmorTrims.TrimMaterial;
using Obsidian.API.Registry.Codecs.ArmorTrims.TrimPattern;
Expand All @@ -20,7 +19,6 @@
using Obsidian.Net.WindowProperties;
using Obsidian.Registries;
using Obsidian.Serialization.Attributes;
using Org.BouncyCastle.Bcpg;
using System.Buffers.Binary;
using System.Text;
using System.Text.Json;
Expand Down
2 changes: 1 addition & 1 deletion Obsidian/Net/Packets/Common/ClientInformationPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ public async override ValueTask HandleAsync(Server server, Player player)
ParticleStatus = ParticleStatus
};

await player.client.SendInfoAsync();

}
}
64 changes: 6 additions & 58 deletions Obsidian/Net/Packets/Play/Clientbound/LoginPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Obsidian.Net.Packets.Play.Clientbound;
public partial class LoginPacket
{
[Field(0)]
public int EntityId { get; init; }
public required int EntityId { get; init; }

[Field(1)]
public bool Hardcore { get; init; } = false;

[Field(2)]
public List<string> DimensionNames { get; init; }
public required List<string> DimensionNames { get; init; }

[Field(3), VarLength]
private const int MaxPlayers = 0;
Expand All @@ -31,43 +31,10 @@ public partial class LoginPacket
[Field(8)]
public bool DoLimitedCrafting { get; init; } = false;

[Field(9), VarLength]
public int DimensionType { get; init; }
[Field(9)]
public required CommonPlayerSpawnInfo CommonPlayerSpawnInfo { get; init; }

[Field(10)]
public string DimensionName { get; init; }

[Field(11)]
public long HashedSeed { get; init; }

[Field(12), ActualType(typeof(byte))]
public Gamemode Gamemode { get; init; } = Gamemode.Survival;

[Field(13), ActualType(typeof(sbyte))]
public Gamemode PreviousGamemode { get; init; } = Gamemode.Survival;

[Field(14)]
public bool Debug { get; init; } = false;

[Field(15)]
public bool Flat { get; init; } = false;

[Field(16)]
public bool HasDeathLocation { get; init; }

[Field(17), Condition("HasDeathLocation")]
public string? DeathDimensionName { get; init; }

[Field(18), Condition("HasDeathLocation")]
public Vector? DeathLocation { get; init; }

[Field(19), VarLength]
public int PortalCooldown { get; init; }

[Field(20), VarLength]
public int SeaLevel { get; init; }

[Field(21)]
public bool EnforcesSecureChat { get; init; }

public override void Serialize(INetStreamWriter writer)
Expand All @@ -76,7 +43,7 @@ public override void Serialize(INetStreamWriter writer)
writer.WriteBoolean(this.Hardcore);

writer.WriteVarInt(this.DimensionNames.Count);
foreach(var dimName in this.DimensionNames)
foreach (var dimName in this.DimensionNames)
writer.WriteString(dimName);

writer.WriteVarInt(MaxPlayers);
Expand All @@ -87,26 +54,7 @@ public override void Serialize(INetStreamWriter writer)
writer.WriteBoolean(this.EnableRespawnScreen);
writer.WriteBoolean(this.DoLimitedCrafting);

writer.WriteVarInt(this.DimensionType);
writer.WriteString(this.DimensionName);

writer.WriteLong(this.HashedSeed);

writer.WriteByte(this.Gamemode);
writer.WriteByte(this.PreviousGamemode);

writer.WriteBoolean(this.Debug);
writer.WriteBoolean(this.Flat);
writer.WriteBoolean(this.HasDeathLocation);

if (this.HasDeathLocation)
{
writer.WriteString(this.DeathDimensionName!);
writer.WritePosition(this.DeathLocation!.Value);
}

writer.WriteVarInt(this.PortalCooldown);
writer.WriteVarInt(this.SeaLevel);
CommonPlayerSpawnInfo.Write(this.CommonPlayerSpawnInfo, writer);

writer.WriteBoolean(this.EnforcesSecureChat);
}
Expand Down
Loading

0 comments on commit 8de7905

Please sign in to comment.