Add submodule MinecraftProtocolLibrary

This commit is contained in:
BruceChen 2022-12-23 00:50:20 +08:00
parent 87026e1bfb
commit 3f1de66af3
62 changed files with 1093 additions and 450 deletions

View file

@ -1,171 +0,0 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Inventory;
using MinecraftClient.Protocol.Message;
namespace MinecraftClient.Mapping
{
/// <summary>
/// Represents an entity evolving into a Minecraft world
/// </summary>
public class Entity
{
/// <summary>
/// ID of the entity on the Minecraft server
/// </summary>
public int ID;
/// <summary>
/// UUID of the entity if it is a player.
/// </summary>
public Guid UUID;
/// <summary>
/// Nickname of the entity if it is a player.
/// </summary>
public string? Name;
/// <summary>
/// CustomName of the entity.
/// </summary>
public string? CustomNameJson;
/// <summary>
/// IsCustomNameVisible of the entity.
/// </summary>
public bool IsCustomNameVisible;
/// <summary>
/// CustomName of the entity.
/// </summary>
public string? CustomName;
/// <summary>
/// Latency of the entity if it is a player.
/// </summary>
public int Latency;
/// <summary>
/// Entity type
/// </summary>
public EntityType Type;
/// <summary>
/// Entity location in the Minecraft world
/// </summary>
public Location Location;
/// <summary>
/// Entity head yaw
/// </summary>
/// <remarks>Untested</remarks>
public float Yaw = 0;
/// <summary>
/// Entity head pitch
/// </summary>
/// <remarks>Untested</remarks>
public float Pitch = 0;
/// <summary>
/// Used in Item Frame, Falling Block and Fishing Float.
/// See https://wiki.vg/Object_Data for details.
/// </summary>
/// <remarks>Untested</remarks>
public int ObjectData = -1;
/// <summary>
/// Health of the entity
/// </summary>
public float Health;
/// <summary>
/// Item of the entity if ItemFrame or Item
/// </summary>
public Item Item;
/// <summary>
/// Entity pose in the Minecraft world
/// </summary>
public EntityPose Pose;
/// <summary>
/// Entity metadata
/// </summary>
public Dictionary<int, object?>? Metadata;
/// <summary>
/// Entity equipment
/// </summary>
public Dictionary<int, Item> Equipment;
/// <summary>
/// Create a new entity based on Entity ID, Entity Type and location
/// </summary>
/// <param name="ID">Entity ID</param>
/// <param name="type">Entity Type Enum</param>
/// <param name="location">Entity location</param>
public Entity(int ID, EntityType type, Location location)
{
this.ID = ID;
Type = type;
Location = location;
Health = 1.0f;
Equipment = new Dictionary<int, Item>();
Item = new Item(ItemType.Air, 0, null);
}
/// <summary>
/// Create a new entity based on Entity ID, Entity Type and location
/// </summary>
/// <param name="ID">Entity ID</param>
/// <param name="type">Entity Type Enum</param>
/// <param name="location">Entity location</param>
public Entity(int ID, EntityType type, Location location, byte yaw, byte pitch, int objectData)
{
this.ID = ID;
Type = type;
Location = location;
Health = 1.0f;
Equipment = new Dictionary<int, Item>();
Item = new Item(ItemType.Air, 0, null);
Yaw = yaw * (1 / 256) * 360; // to angle in 360 degree
Pitch = pitch * (1 / 256) * 360;
ObjectData = objectData;
}
/// <summary>
/// Create a new entity based on Entity ID, Entity Type, location, name and UUID
/// </summary>
/// <param name="ID">Entity ID</param>
/// <param name="type">Entity Type Enum</param>
/// <param name="location">Entity location</param>
/// <param name="uuid">Player uuid</param>
/// <param name="name">Player name</param>
public Entity(int ID, EntityType type, Location location, Guid uuid, string? name, byte yaw, byte pitch)
{
this.ID = ID;
Type = type;
Location = location;
UUID = uuid;
Name = name;
Health = 1.0f;
Equipment = new Dictionary<int, Item>();
Item = new Item(ItemType.Air, 0, null);
Yaw = yaw * (1 / 256) * 360; // to angle in 360 degree
Pitch = pitch * (1 / 256) * 360;
}
public static string GetTypeString(EntityType type)
{
string typeStr = type.ToString();
string? trans = ChatParser.TranslateString("entity.minecraft." + typeStr.ToUnderscoreCase());
return string.IsNullOrEmpty(trans) ? typeStr : trans;
}
public string GetTypeString()
{
return GetTypeString(Type);
}
}
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping.EntityPalettes
{

View file

@ -1,13 +0,0 @@
namespace MinecraftClient.Mapping
{
public enum EntityPose
{
Standing = 0,
FallFlying = 1,
Sleeping = 2,
Swimming = 3,
SpinAttack = 4,
Sneaking = 5,
Dying = 6,
}
}

View file

@ -1,136 +0,0 @@
namespace MinecraftClient.Mapping
{
/// <summary>
/// Represents Minecraft Entity Types
/// </summary>
/// <remarks>
/// Generated from registries.json using the --generator flag on the client
/// Typical steps to handle new entity IDs for newer Minecraft versions:
/// 1. Generate registries.json using data reporting on Vanilla Minecraft (https://wiki.vg/Data_Generators) or download it from: https://github.com/PixiGeko/Minecraft-generated-data
/// 2. Generate temporary EntityTypeXXX.cs and EntityPaletteXXX.cs using the --generator flag on the client
/// 3. Perform a diff with existing versions, add missing entries in EntityType.cs and EntityTypeExtensions.cs
/// 4. If existing entity IDs were not randomized by Mojang, simply add missing entries to the latest existing EntityPaletteXXX.cs
/// 5. If existing entity IDs were randomized, add a new palette as EntityPaletteXXX.cs into the codebase (worst case)
/// </remarks>
public enum EntityType
{
Allay,
AreaEffectCloud,
ArmorStand,
Arrow,
Axolotl,
Bat,
Bee,
Blaze,
Boat,
Cat,
CaveSpider,
ChestBoat,
ChestMinecart,
Chicken,
Cod,
CommandBlockMinecart,
Cow,
Creeper,
Dolphin,
Donkey,
DragonFireball,
Drowned,
Egg,
ElderGuardian,
EndCrystal,
EnderDragon,
EnderPearl,
Enderman,
Endermite,
Evoker,
EvokerFangs,
ExperienceBottle,
ExperienceOrb,
EyeOfEnder,
FallingBlock,
Fireball,
FireworkRocket,
FishingBobber,
Fox,
Frog,
FurnaceMinecart,
Ghast,
Giant,
GlowItemFrame,
GlowSquid,
Goat,
Guardian,
Hoglin,
HopperMinecart,
Horse,
Husk,
Illusioner,
IronGolem,
Item,
ItemFrame,
LeashKnot,
LightningBolt,
Llama,
LlamaSpit,
MagmaCube,
Marker,
Minecart,
Mooshroom,
Mule,
Ocelot,
Painting,
Panda,
Parrot,
Phantom,
Pig,
Piglin,
PiglinBrute,
Pillager,
Player,
PolarBear,
Potion,
Pufferfish,
Rabbit,
Ravager,
Salmon,
Sheep,
Shulker,
ShulkerBullet,
Silverfish,
Skeleton,
SkeletonHorse,
Slime,
SmallFireball,
SnowGolem,
Snowball,
SpawnerMinecart,
SpectralArrow,
Spider,
Squid,
Stray,
Strider,
Tadpole,
Tnt,
TntMinecart,
TraderLlama,
Trident,
TropicalFish,
Turtle,
Vex,
Villager,
Vindicator,
WanderingTrader,
Warden,
Witch,
Wither,
WitherSkeleton,
WitherSkull,
Wolf,
Zoglin,
Zombie,
ZombieHorse,
ZombieVillager,
ZombifiedPiglin,
}
}

View file

@ -1,121 +0,0 @@
namespace MinecraftClient.Mapping
{
public static class EntityTypeExtensions
{
/// <summary>
/// Return TRUE if the Entity is an hostile mob
/// </summary>
/// <remarks>New mobs added in newer Minecraft versions might be absent from the list</remarks>
/// <returns>TRUE if hostile</returns>
public static bool IsHostile(this EntityType e)
{
switch (e)
{
case EntityType.Blaze:
case EntityType.CaveSpider:
case EntityType.Creeper:
case EntityType.Drowned:
case EntityType.Enderman:
case EntityType.Endermite:
case EntityType.Evoker:
case EntityType.Ghast:
case EntityType.Guardian:
case EntityType.Hoglin:
case EntityType.Husk:
case EntityType.Illusioner:
case EntityType.MagmaCube:
case EntityType.Phantom:
case EntityType.Piglin:
case EntityType.PiglinBrute:
case EntityType.Pillager:
case EntityType.Ravager:
case EntityType.Shulker:
case EntityType.Silverfish:
case EntityType.Skeleton:
case EntityType.Slime:
case EntityType.Spider:
case EntityType.Stray:
case EntityType.Vex:
case EntityType.Vindicator:
case EntityType.Witch:
case EntityType.WitherSkeleton:
case EntityType.Zoglin:
case EntityType.Zombie:
case EntityType.ZombieVillager:
case EntityType.ZombifiedPiglin:
return true;
default:
return false;
}
}
/// <summary>
/// Return TRUE if the Entity is a passive mob
/// </summary>
/// <remarks>New mobs added in newer Minecraft versions might be absent from the list</remarks>
/// <returns>TRUE if a passive mob</returns>
public static bool IsPassive(this EntityType e)
{
switch (e)
{
case EntityType.Bat:
case EntityType.Cat:
case EntityType.Chicken:
case EntityType.Cod:
case EntityType.Cow:
case EntityType.Dolphin:
case EntityType.Donkey:
case EntityType.Fox:
case EntityType.Frog:
case EntityType.GlowSquid:
case EntityType.Goat:
case EntityType.Horse:
case EntityType.IronGolem:
case EntityType.Llama:
case EntityType.Mooshroom:
case EntityType.Mule:
case EntityType.Ocelot:
case EntityType.Panda:
case EntityType.Parrot:
case EntityType.Pig:
case EntityType.Salmon:
case EntityType.Sheep:
case EntityType.Silverfish:
case EntityType.SnowGolem:
case EntityType.Squid:
case EntityType.Turtle:
case EntityType.Villager:
case EntityType.WanderingTrader:
case EntityType.Wolf:
case EntityType.ZombieHorse:
case EntityType.SkeletonHorse:
return true;
default:
return false;
}
}
/// <summary>
/// Indicates whether the entity type contains an inner item
/// </summary>
/// <returns>TRUE if item holder (Item Entity, ItemFrame...)</returns>
public static bool ContainsItem(this EntityType e)
{
switch (e)
{
case EntityType.GlowItemFrame:
case EntityType.Item:
case EntityType.ItemFrame:
case EntityType.EyeOfEnder:
case EntityType.Egg:
case EntityType.EnderPearl:
case EntityType.Potion:
case EntityType.Fireball:
case EntityType.FireworkRocket:
return true;
default:
return false;
};
}
}
}

View file

@ -1,9 +0,0 @@
namespace MinecraftClient.Mapping
{
public enum InteractType
{
Interact = 0,
Attack = 1,
InteractAt = 2,
}
}

View file

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.Mapping
{
public record MapData
{
public int MapId { init; get; }
public byte Scale { init; get; }
public bool TrackingPosition { init; get; }
public bool Locked { init; get; }
public List<MapIcon> Icons { init; get; } = new();
public byte ColumnsUpdated { init; get; }
public byte RowsUpdated { init; get; }
public byte MapCoulmnX { init; get; }
public byte MapRowZ { init; get; }
public byte[]? Colors { init; get; }
}
}

View file

@ -140,17 +140,10 @@ namespace MinecraftClient.Mapping
/// <param name="timeout">How long to wait before stopping computation</param>
/// <remarks>When location is unreachable, computation will reach timeout, then optionally fallback to a close location within maxOffset</remarks>
/// <returns>A list of locations, or null if calculation failed</returns>
public static Queue<Location>? CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, TimeSpan timeout)
public static async Task<Queue<Location>?> CalculatePath(World world, Location start, Location goal, bool allowUnsafe, int maxOffset, int minOffset, TimeSpan timeout)
{
CancellationTokenSource cts = new();
Task<Queue<Location>?> pathfindingTask = Task.Factory.StartNew(() => Movement.CalculatePath(world, start, goal, allowUnsafe, maxOffset, minOffset, cts.Token));
pathfindingTask.Wait(timeout);
if (!pathfindingTask.IsCompleted)
{
cts.Cancel();
pathfindingTask.Wait();
}
return pathfindingTask.Result;
CancellationTokenSource cts = new(timeout);
return await Task.Run(() => { return CalculatePath(world, start, goal, allowUnsafe, maxOffset, minOffset, cts.Token); }, cts.Token);
}
/// <summary>

View file

@ -1,4 +1,5 @@
using System;
using MinecraftClient.EntityHandler;
namespace MinecraftClient.Mapping
{