Skip to content

Commit

Permalink
Merge pull request #4 from ReinforceZwei/metadata18
Browse files Browse the repository at this point in the history
Implement 1.8 Entity MetaData
  • Loading branch information
milutinke authored Mar 27, 2023
2 parents 033c615 + 21cd24e commit 4ec3d0c
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 2,048 deletions.
11 changes: 11 additions & 0 deletions MinecraftClient/ChatBots/TelegramBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ await botClient.SendTextMessageAsync(
{
var command = text[1..];

if (command.ToLower().Contains("quit") || command.ToLower().Contains("exit"))
{
await botClient.SendTextMessageAsync(
chatId: chatId,
replyToMessageId: message.MessageId,
text: $"{Translations.bot_TelegramBridge_quit_disabled}",
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
return;;
}

CmdResult result = new();
PerformInternalCommand(command, ref result);

Expand Down
3 changes: 3 additions & 0 deletions MinecraftClient/Mapping/EntityMetaDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ namespace MinecraftClient.Mapping;
public enum EntityMetaDataType
{
Byte,
Short, // 1.8 only
Int, // 1.8 only
Vector3Int, // 1.8 only (not used by the game)
VarInt,
VarLong,
Float,
Expand Down
4 changes: 2 additions & 2 deletions MinecraftClient/Mapping/EntityMetadataPalette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public EntityMetaDataType GetDataType(int typeId)

public static EntityMetadataPalette GetPalette(int protocolVersion)
{
if (protocolVersion < Protocol18Handler.MC_1_9_Version)
throw new NotImplementedException();
if (protocolVersion <= Protocol18Handler.MC_1_8_Version)
return new EntityMetadataPalette18(); // 1.8
else if (protocolVersion <= Protocol18Handler.MC_1_12_2_Version)
return new EntityMetadataPalette1122(); // 1.9 - 1.12.2
else if (protocolVersion <= Protocol18Handler.MC_1_19_2_Version)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace MinecraftClient.Mapping.EntityMetadataPalettes;

public class EntityMetadataPalette18 : EntityMetadataPalette
{
// 1.8 : https://wiki.vg/index.php?title=Entity_metadata&oldid=6220
private readonly Dictionary<int, EntityMetaDataType> entityMetadataMappings = new()
{
{ 0, EntityMetaDataType.Byte },
{ 1, EntityMetaDataType.Short },
{ 2, EntityMetaDataType.Int },
{ 3, EntityMetaDataType.Float },
{ 4, EntityMetaDataType.String },
{ 5, EntityMetaDataType.Slot },
{ 6, EntityMetaDataType.Vector3Int },
{ 7, EntityMetaDataType.Rotation }
};

public override Dictionary<int, EntityMetaDataType> GetEntityMetadataMappingsList()
{
return entityMetadataMappings;
}
}

This file was deleted.

45 changes: 38 additions & 7 deletions MinecraftClient/Protocol/Handlers/DataTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,18 +599,31 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)
}
}

//TODO: Refactor this to use new Entity Metadata Palettes
/// <summary>
/// Read a Entity MetaData and remove it from the cache
/// </summary>
/// <param name="cache"></param>
/// <param name="itemPalette"></param>
/// <param name="metadataPalette"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
/// <exception cref="System.IO.InvalidDataException"></exception>
public Dictionary<int, object?> ReadNextMetadata(Queue<byte> cache, ItemPalette itemPalette, EntityMetadataPalette metadataPalette)
{
if (protocolversion <= Protocol18Handler.MC_1_8_Version)
throw new NotImplementedException(); // Require sepcial implementation

Dictionary<int, object?> data = new();
byte key = ReadNextByte(cache);
byte terminteValue = protocolversion <= Protocol18Handler.MC_1_8_Version
? (byte)0x7f // 1.8 (https://wiki.vg/index.php?title=Entity_metadata&oldid=6220#Entity_Metadata_Format)
: (byte)0xff; // 1.9+

while (key != 0xff)
while (key != terminteValue)
{
int typeId = ReadNextVarInt(cache);
if (protocolversion <= Protocol18Handler.MC_1_8_Version)
key = (byte)(key & 0x1f);

int typeId = protocolversion <= Protocol18Handler.MC_1_8_Version
? key >> 5 // 1.8
: ReadNextVarInt(cache); // 1.9+
EntityMetaDataType type;
try
{
Expand All @@ -626,6 +639,20 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)

switch (type)
{
case EntityMetaDataType.Short: // 1.8 only
value = ReadNextShort(cache);
break;
case EntityMetaDataType.Int: // 1.8 only
value = ReadNextInt(cache);
break;
case EntityMetaDataType.Vector3Int: // 1.8 only
value = new List<int>()
{
ReadNextInt(cache),
ReadNextInt(cache),
ReadNextInt(cache),
};
break;
case EntityMetaDataType.Byte: // byte
value = ReadNextByte(cache);
break;
Expand Down Expand Up @@ -760,7 +787,11 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)
return data;
}

// Currently not handled. Reading data only
/// <summary>
/// Currently not handled. Reading data only
/// </summary>
/// <param name="cache"></param>
/// <param name="itemPalette"></param>
protected void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)
{
if (protocolversion < Protocol18Handler.MC_1_13_Version)
Expand Down
Loading

0 comments on commit 4ec3d0c

Please sign in to comment.