WIP: Added some strctured components

This commit is contained in:
Anon 2024-09-11 19:12:31 +02:00
parent 63b027d84a
commit 76e873ed54
36 changed files with 857 additions and 231 deletions

View file

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents._1_20_6;
public class AttributeSubComponent1206(DataTypes dataTypes, SubComponentRegistry subComponentRegistry) : SubComponent(dataTypes, subComponentRegistry)
{
public int TypeId { get; set; }
public Guid Uuid { get; set; }
public string? Name { get; set; }
public double Value { get; set; }
public int Operation { get; set; }
public int Slot { get; set; }
protected override void Parse(Queue<byte> data)
{
TypeId = dataTypes.ReadNextVarInt(data);
Uuid = dataTypes.ReadNextUUID(data);
Name = dataTypes.ReadNextString(data);
Value = dataTypes.ReadNextDouble(data);
Operation = dataTypes.ReadNextVarInt(data);
Slot = dataTypes.ReadNextVarInt(data);
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
data.AddRange(DataTypes.GetVarInt(TypeId));
data.AddRange(DataTypes.GetUUID(Uuid));
if (string.IsNullOrEmpty(Name?.Trim()))
throw new ArgumentNullException($"Can not serialize AttributeSubComponent due to Name being null or empty!");
data.AddRange(DataTypes.GetString(Name));
data.AddRange(DataTypes.GetDouble(Value));
data.AddRange(DataTypes.GetVarInt(Operation));
data.AddRange(DataTypes.GetVarInt(Slot));
return new Queue<byte>(data);
}
}

View file

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents._1_20_6;
public class BlockPredicateSubcomponent1206(DataTypes dataTypes, SubComponentRegistry subComponentRegistry) : SubComponent(dataTypes, subComponentRegistry)
{
public bool HasBlocks { get; set; }
public BlockSetSubcomponent1206? BlockSet { get; set; }
public bool HasProperities { get; set; }
public List<PropertySubComponent1206>? Properties { get; set; }
public bool HasNbt { get; set; }
public Dictionary<string, object>? Nbt { get; set; }
protected override void Parse(Queue<byte> data)
{
HasBlocks = dataTypes.ReadNextBool(data);
if (HasBlocks)
BlockSet = (BlockSetSubcomponent1206)subComponentRegistry.ParseSubComponent(SubComponents.BlockSet, data);
HasProperities = dataTypes.ReadNextBool(data);
if (HasProperities)
{
Properties = new();
var numberOfProperties = dataTypes.ReadNextVarInt(data);
for (var i = 0; i < numberOfProperties; i++)
Properties.Add((PropertySubComponent1206)subComponentRegistry.ParseSubComponent(SubComponents.Property, data));
}
HasNbt = dataTypes.ReadNextBool(data);
if (HasNbt)
Nbt = dataTypes.ReadNextNbt(data);
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
// Block Sets
data.AddRange(DataTypes.GetBool(HasBlocks));
if (HasBlocks)
{
if(BlockSet == null)
throw new ArgumentNullException($"Can not serialize a BlockPredicate when the BlockSet is empty but HasBlocks is true!");
data.AddRange(BlockSet.Serialize());
}
// Properites
data.AddRange(DataTypes.GetBool(HasProperities));
if (HasProperities)
{
if(Properties == null || Properties.Count == 0)
throw new ArgumentNullException($"Can not serialize a BlockPredicate when the Properties is empty but HasProperties is true!");
foreach (var property in Properties)
data.AddRange(property.Serialize());
}
// NBT
if (HasNbt)
{
if(Nbt == null)
throw new ArgumentNullException($"Can not serialize a BlockPredicate when the Nbt is empty but HasNbt is true!");
data.AddRange(DataTypes.GetNbt(Nbt));
}
return new Queue<byte>(data);
}
}

View file

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents._1_20_6;
public class BlockSetSubcomponent1206(DataTypes dataTypes, SubComponentRegistry subComponentRegistry) : SubComponent(dataTypes, subComponentRegistry)
{
public int Type { get; set; }
public string? TagName { get; set; }
public List<int>? BlockIds { get; set; }
protected override void Parse(Queue<byte> data)
{
Type = DataTypes.ReadNextVarInt(data);
if (Type == 0)
TagName = dataTypes.ReadNextString(data);
if (Type == 0) return;
BlockIds = [];
for (var i = 0; i < Type - 1; i++)
BlockIds.Add(dataTypes.ReadNextVarInt(data));
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
data.AddRange(DataTypes.GetVarInt(Type));
if (Type == 0)
{
if (string.IsNullOrEmpty(TagName?.Trim()))
throw new ArgumentNullException($"Can not serialize an empty tag name when the Block Set type is 0!");
data.AddRange(DataTypes.GetString(TagName));
}
if (Type == 0) return new Queue<byte>(data);
if(BlockIds == null || BlockIds.Count == 0)
throw new ArgumentNullException($"Can not serialize an empty list of Block IDs in a Block Set when the type is not 0!");
for(var i = 0; i < Type - 1; i++)
data.AddRange(DataTypes.GetVarInt(BlockIds[i]));
return new Queue<byte>(data);
}
}

View file

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents._1_20_6;
public class PropertySubComponent1206(DataTypes dataTypes, SubComponentRegistry subComponentRegistry) : SubComponent(dataTypes, subComponentRegistry)
{
public string? Name { get; set; }
public bool IsExactMatch { get; set; }
public string? ExactValue { get; set; }
public string? MinValue { get; set; }
public string? MaxValue { get; set; }
protected override void Parse(Queue<byte> data)
{
Name = dataTypes.ReadNextString(data);
IsExactMatch = dataTypes.ReadNextBool(data);
if (IsExactMatch)
ExactValue = dataTypes.ReadNextString(data);
else // Ranged Match
{
MinValue = dataTypes.ReadNextString(data);
MaxValue = dataTypes.ReadNextString(data);
}
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
if (string.IsNullOrEmpty(Name?.Trim()))
throw new ArgumentNullException($"Can not serialize a Property sub-component if the Name is null or empty!");
data.AddRange(DataTypes.GetString(Name));
data.AddRange(DataTypes.GetBool(IsExactMatch));
if (IsExactMatch)
{
if (string.IsNullOrEmpty(ExactValue?.Trim()))
throw new ArgumentNullException($"Can not serialize a Property sub-component if the ExactValue is null or empty when the type is Exact Match!");
data.AddRange(DataTypes.GetString(ExactValue));
}
else
{
if (string.IsNullOrEmpty(MinValue?.Trim()) || string.IsNullOrEmpty(MaxValue?.Trim()))
throw new ArgumentNullException($"Can not serialize a Property sub-component if the MinValue or MaxValue is null or empty when the type is not Exact Match!");
data.AddRange(DataTypes.GetString(MinValue));
data.AddRange(DataTypes.GetString(MaxValue));
}
return new Queue<byte>(data);
}
}

View file

@ -0,0 +1,9 @@
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents;
public class SubComponents
{
public const string BlockPredicate = "BlockPredicate";
public const string BlockSet = "BlockSet";
public const string Property = "Property";
public const string Attribute = "Attribute";
}

View file

@ -1,19 +0,0 @@
using System.Collections.Generic;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents;
public class TestSubComonent(DataTypes dataTypes) : SubComponent(dataTypes)
{
public int Test { get; set; }
public override void Parse(Queue<byte> data)
{
Test = DataTypes.ReadNextVarInt(data);
}
public override Queue<byte> Serialize()
{
throw new System.NotImplementedException();
}
}