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

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MinecraftClient.EntityHandler
{
public record Effect
{
public EffectType Type { init; get; }
public int EffectLevel { init; get; }
public ulong StartTick { init; get; }
public int DurationInTick { init; get; }
public bool IsFromBeacon { init; get; }
public bool ShowParticles { init; get; }
public bool ShowIcon { init; get; }
public Dictionary<string, object>? FactorData { init; get; } = null;
/* Factor Data
Name Type
padding_duration TAG_INT
factor_start TAG_FLOAT
factor_target TAG_FLOAT
factor_current TAG_FLOAT
effect_changed_timestamp TAG_INT
factor_previous_frame TAG_FLOAT
had_effect_last_tick TAG_BOOLEAN
*/
}
}

View file

@ -0,0 +1,41 @@
namespace MinecraftClient.EntityHandler
{
/// <summary>
/// Represents a Minecraft effects
/// </summary>
public enum EffectType
{
Speed = 1,
Slowness = 2,
Haste = 3,
MiningFatigue = 4,
Strength = 5,
InstantHealth = 6,
InstantDamage = 7,
JumpBoost = 8,
Nausea = 9,
Regeneration = 10,
Resistance = 11,
FireResistance = 12,
WaterBreathing = 13,
Invisibility = 14,
Blindness = 15,
NightVision = 16,
Hunger = 17,
Weakness = 18,
Poison = 19,
Wither = 20,
HealthBoost = 21,
Absorption = 22,
Saturation = 23,
Glowing = 24,
Levitation = 25,
Luck = 26,
BadLuck = 27,
SlowFalling = 28,
ConduitPower = 29,
DolphinsGrace = 30,
BadOmen = 31,
HerooftheVillage = 32,
}
}

View file

@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Inventory;
using MinecraftClient.Mapping;
using MinecraftClient.Protocol.Message;
namespace MinecraftClient.EntityHandler
{
/// <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

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

View file

@ -0,0 +1,136 @@
namespace MinecraftClient.EntityHandler
{
/// <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

@ -0,0 +1,121 @@
namespace MinecraftClient.EntityHandler
{
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

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