Preliminary 1.21 Support

This commit is contained in:
Anon 2024-12-06 16:45:48 +01:00
parent 7bd213a154
commit f83e7c5707
18 changed files with 588 additions and 55 deletions

View file

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Inventory.ItemPalettes;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents._1_21;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_21;
public class JukeBoxPlayableComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry)
: StructuredComponent(dataTypes, itemPalette, subComponentRegistry)
{
public bool DirectMode { get; set; }
public string? SongName { get; set; }
public int? SongType { get; set; }
public SoundEventSubComponent? SoundEvent { get; set; }
public string? Description { get; set; }
public float? Duration { get; set; }
public int? Output { get; set; }
public bool ShowTooltip { get; set; }
public override void Parse(Queue<byte> data)
{
DirectMode = dataTypes.ReadNextBool(data);
if (!DirectMode)
SongName = dataTypes.ReadNextString(data);
if (DirectMode)
{
SongType = dataTypes.ReadNextVarInt(data);
if (SongType == 0)
{
SoundEvent =
(SoundEventSubComponent)subComponentRegistry.ParseSubComponent(SubComponents.SoundEvent, data);
Description = dataTypes.ReadNextString(data);
Duration = dataTypes.ReadNextFloat(data);
Output = dataTypes.ReadNextVarInt(data);
}
}
ShowTooltip = dataTypes.ReadNextBool(data);
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
data.AddRange(DataTypes.GetBool(DirectMode));
if (!DirectMode)
{
if (string.IsNullOrEmpty(SongName?.Trim()))
throw new ArgumentNullException($"Can not serialize JukeBoxPlayableComponent due to SongName being null or empty!");
data.AddRange(DataTypes.GetString(SongName));
}
if (DirectMode)
{
if(SongType is null)
throw new ArgumentNullException($"Can not serialize JukeBoxPlayableComponent due to SongType being null!");
data.AddRange(DataTypes.GetVarInt((int)SongType));
if (SongType == 0)
{
if (SoundEvent is null)
throw new ArgumentNullException(
$"Can not serialize JukeBoxPlayableComponent due to SoundEvent being null");
data.AddRange(SoundEvent.Serialize());
if (string.IsNullOrEmpty(Description?.Trim()))
throw new ArgumentNullException(
$"Can not serialize JukeBoxPlayableComponent due to Description being null or empty!");
data.AddRange(DataTypes.GetString(Description));
if (Duration is null)
throw new ArgumentNullException(
$"Can not serialize JukeBoxPlayableComponent due to Duration being null!");
data.AddRange(DataTypes.GetFloat((float)Duration));
if (Output is null)
throw new ArgumentNullException(
$"Can not serialize JukeBoxPlayableComponent due to Description being null!");
data.AddRange(DataTypes.GetVarInt((int)Output));
}
}
data.AddRange(DataTypes.GetBool(ShowTooltip));
return new Queue<byte>(data);
}
}

View file

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents._1_21;
public class SoundEventSubComponent(DataTypes dataTypes, SubComponentRegistry subComponentRegistry) : SubComponent(dataTypes, subComponentRegistry)
{
public int Type { get; set; }
public string? SoundName { get; set; }
public bool HasFixedRange { get; set; }
public float FixedRange { get; set; }
protected override void Parse(Queue<byte> data)
{
Type = dataTypes.ReadNextVarInt(data);
if (Type != 0) return;
SoundName = dataTypes.ReadNextString(data);
HasFixedRange = dataTypes.ReadNextBool(data);
if (HasFixedRange)
FixedRange = dataTypes.ReadNextFloat(data);
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
data.AddRange(DataTypes.GetVarInt(Type));
if (Type != 0) return new Queue<byte>(data);
if (string.IsNullOrEmpty(SoundName?.Trim()))
throw new ArgumentNullException($"Can not serialize SoundEventSubComponent due to SoundName being null or empty!");
data.AddRange(DataTypes.GetString(SoundName));
data.AddRange(DataTypes.GetBool(HasFixedRange));
if(HasFixedRange)
data.AddRange(DataTypes.GetFloat(FixedRange));
return new Queue<byte>(data);
}
}

View file

@ -11,4 +11,5 @@ public abstract class SubComponents
public const string Details = "Details";
public const string Rule = "Rule";
public const string FireworkExplosion = "FireworkExplosion";
public const string SoundEvent = "SoundEvent";
}

View file

@ -25,7 +25,7 @@ public class StructuredComponentsRegistry1206 : StructuredComponentRegistry
RegisterComponent<CustomModelDataComponent>(13, "minecraft:custom_model_data");
RegisterComponent<HideAdditionalTooltipComponent>(14, "minecraft:hide_additional_tooltip");
RegisterComponent<HideTooltipComponent>(15, "minecraft:hide_tooltip");
RegisterComponent<HideTooltipComponent>(16, "minecraft:repair_cost");
RegisterComponent<RepairCostComponent>(16, "minecraft:repair_cost");
RegisterComponent<CreativeSlotLockComponent>(17, "minecraft:creative_slot_lock");
RegisterComponent<EnchantmentGlintOverrideComponent>(18, "minecraft:enchantment_glint_override");
RegisterComponent<IntangibleProjectileComponent>(19, "minecraft:intangible_projectile");

View file

@ -0,0 +1,71 @@
using MinecraftClient.Inventory.ItemPalettes;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_20_6;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_21;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Registries;
public class StructuredComponentsRegistry121 : StructuredComponentRegistry
{
public StructuredComponentsRegistry121(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry)
: base(dataTypes, itemPalette, subComponentRegistry)
{
RegisterComponent<CustomDataComponent>(0, "minecraft:custom_data");
RegisterComponent<MaxStackSizeComponent>(1, "minecraft:max_stack_size");
RegisterComponent<MaxDamageComponent>(2, "minecraft:max_damage");
RegisterComponent<DamageComponent>(3, "minecraft:damage");
RegisterComponent<UnbrekableComponent1206>(4, "minecraft:unbreakable");
RegisterComponent<CustomNameComponent>(5, "minecraft:custom_name");
RegisterComponent<ItemNameComponent>(6, "minecraft:item_name");
RegisterComponent<LoreNameComponent1206>(7, "minecraft:lore");
RegisterComponent<RarityComponent>(8, "minecraft:rarity");
RegisterComponent<EnchantmentsComponent>(9, "minecraft:enchantments");
RegisterComponent<CanPlaceOnComponent>(10, "minecraft:can_place_on");
RegisterComponent<CanBreakComponent>(11, "minecraft:can_break");
RegisterComponent<AttributeModifiersComponent>(12, "minecraft:attribute_modifiers");
RegisterComponent<CustomModelDataComponent>(13, "minecraft:custom_model_data");
RegisterComponent<HideAdditionalTooltipComponent>(14, "minecraft:hide_additional_tooltip");
RegisterComponent<HideTooltipComponent>(15, "minecraft:hide_tooltip");
RegisterComponent<RepairCostComponent>(16, "minecraft:repair_cost");
RegisterComponent<CreativeSlotLockComponent>(17, "minecraft:creative_slot_lock");
RegisterComponent<EnchantmentGlintOverrideComponent>(18, "minecraft:enchantment_glint_override");
RegisterComponent<IntangibleProjectileComponent>(19, "minecraft:intangible_projectile");
RegisterComponent<FoodComponentComponent>(20, "minecraft:food");
RegisterComponent<FireResistantComponent>(21, "minecraft:fire_resistant");
RegisterComponent<ToolComponent>(22, "minecraft:tool");
RegisterComponent<StoredEnchantmentsComponent>(23, "minecraft:stored_enchantments");
RegisterComponent<DyeColorComponent>(24, "minecraft:dyed_color");
RegisterComponent<MapColorComponent>(25, "minecraft:map_color");
RegisterComponent<MapIdComponent>(26, "minecraft:map_id");
RegisterComponent<MapDecorationsComponent>(27, "minecraft:map_decorations");
RegisterComponent<MapPostProcessingComponent>(28, "minecraft:map_post_processing");
RegisterComponent<ChargedProjectilesComponent>(29, "minecraft:charged_projectiles");
RegisterComponent<BundleContentsComponent>(30, "minecraft:bundle_contents");
RegisterComponent<PotionContentsComponent>(31, "minecraft:potion_contents");
RegisterComponent<SuspiciousStewEffectsComponent>(32, "minecraft:suspicious_stew_effects");
RegisterComponent<WritableBlookContentComponent>(33, "minecraft:writable_book_content");
RegisterComponent<WrittenBlookContentComponent>(34, "minecraft:written_book_content");
RegisterComponent<TrimComponent>(35, "minecraft:trim");
RegisterComponent<DebugStickStateComponent>(36, "minecraft:debug_stick_state");
RegisterComponent<EntityDataComponent>(37, "minecraft:entity_data");
RegisterComponent<BucketEntityDataComponent>(38, "minecraft:bucket_entity_data");
RegisterComponent<BlockEntityDataComponent>(39, "minecraft:block_entity_data");
RegisterComponent<InstrumentComponent>(40, "minecraft:instrument");
RegisterComponent<OmniousBottleAmplifierComponent>(41, "minecraft:ominous_bottle_amplifier");
RegisterComponent<JukeBoxPlayableComponent>(42, "minecraft:jukebox_playable");
RegisterComponent<RecipesComponent>(43, "minecraft:recipes");
RegisterComponent<LodestoneTrackerComponent>(44, "minecraft:lodestone_tracker");
RegisterComponent<FireworkExplosionComponent>(45, "minecraft:firework_explosion");
RegisterComponent<FireworksComponent>(46, "minecraft:fireworks");
RegisterComponent<ProfileComponent>(47, "minecraft:profile");
RegisterComponent<NoteBlockSoundComponent>(48, "minecraft:note_block_sound");
RegisterComponent<BannerPatternsComponent>(49, "minecraft:banner_patterns");
RegisterComponent<BaseColorComponent>(50, "minecraft:base_color");
RegisterComponent<PotDecorationsComponent>(51, "minecraft:pot_decorations");
RegisterComponent<ContainerComponent>(52, "minecraft:container");
RegisterComponent<BlockStateComponent>(53, "minecraft:block_state");
RegisterComponent<BeesComponent>(54, "minecraft:bees");
RegisterComponent<LockComponent>(55, "minecraft:lock");
RegisterComponent<ContainerLootComponent>(56, "minecraft:container_loot");
}
}

View file

@ -0,0 +1,15 @@
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_20_6;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents._1_20_6;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents._1_21;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Registries.Subcomponents;
public class SubComponentRegistry121 : SubComponentRegistry1206
{
public SubComponentRegistry121(DataTypes dataTypes) : base(dataTypes)
{
RegisterSubComponent<SoundEventSubComponent>(SubComponents.SoundEvent);
}
}

View file

@ -20,6 +20,7 @@ public class StructuredComponentsHandler
var subcomponentRegistryType = protocolVersion switch
{
Protocol18Handler.MC_1_20_6_Version => typeof(SubComponentRegistry1206),
Protocol18Handler.MC_1_21_Version => typeof(SubComponentRegistry121),
_ => throw new NotSupportedException($"Protocol version {protocolVersion} is not supported for subcomponent registries!")
};
@ -30,6 +31,7 @@ public class StructuredComponentsHandler
var registryType = protocolVersion switch
{
Protocol18Handler.MC_1_20_6_Version => typeof(StructuredComponentsRegistry1206),
Protocol18Handler.MC_1_21_Version => typeof(StructuredComponentsRegistry121),
_ => throw new NotSupportedException($"Protocol version {protocolVersion} is not supported for structured component registries!")
};