Port NBT impl from LibAC Dart
This commit is contained in:
parent
0a022634c1
commit
ad7b619706
55 changed files with 3226 additions and 2983 deletions
142
NBT/API/NbtUtils.cs
Normal file
142
NBT/API/NbtUtils.cs
Normal file
|
@ -0,0 +1,142 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LibAC.NBT.API;
|
||||
using LibAC.Utilities;
|
||||
|
||||
namespace LibAC.NBT.API
|
||||
{
|
||||
public static class NbtUtils
|
||||
{
|
||||
public static void WriteBoolean(CompoundTag tag, string name, bool value)
|
||||
{
|
||||
tag.Put(name, ByteTag.ValueOf(value ? (byte)1 : (byte)0));
|
||||
}
|
||||
|
||||
public static bool ReadBoolean(CompoundTag tag, string name)
|
||||
{
|
||||
if (tag.ContainsKey(name))
|
||||
{
|
||||
return tag.Get(name).AsByte() == 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void WriteVector2d(CompoundTag tag, string name, Vector2d pos)
|
||||
{
|
||||
var posTag = new ListTag();
|
||||
posTag.Add(DoubleTag.ValueOf(pos.X));
|
||||
posTag.Add(DoubleTag.ValueOf(pos.Z));
|
||||
tag.Put(name, posTag);
|
||||
}
|
||||
|
||||
public static Vector2d ReadVector2d(CompoundTag tag, string name)
|
||||
{
|
||||
if (tag.ContainsKey(name))
|
||||
{
|
||||
var listTag = (ListTag)tag.Get(name);
|
||||
return new Vector2d(listTag.Get(0).AsDouble(), listTag.Get(1).AsDouble());
|
||||
}
|
||||
return Vector2d.ZERO;
|
||||
}
|
||||
|
||||
public static void WriteVector2i(CompoundTag tag, string name, Vector2i pos)
|
||||
{
|
||||
var posTag = new ListTag();
|
||||
posTag.Add(IntTag.ValueOf(pos.X));
|
||||
posTag.Add(IntTag.ValueOf(pos.Z));
|
||||
tag.Put(name, posTag);
|
||||
}
|
||||
|
||||
public static Vector2i ReadVector2i(CompoundTag tag, string name)
|
||||
{
|
||||
if (tag.ContainsKey(name))
|
||||
{
|
||||
var listTag = (ListTag)tag.Get(name);
|
||||
return new Vector2i(listTag.Get(0).AsInt(), listTag.Get(1).AsInt());
|
||||
}
|
||||
return Vector2i.ZERO;
|
||||
}
|
||||
|
||||
public static void WriteVector3d(CompoundTag tag, string name, Vector3d pos)
|
||||
{
|
||||
var posTag = new ListTag();
|
||||
posTag.Add(DoubleTag.ValueOf(pos.X));
|
||||
posTag.Add(DoubleTag.ValueOf(pos.Y));
|
||||
posTag.Add(DoubleTag.ValueOf(pos.Z));
|
||||
tag.Put(name, posTag);
|
||||
}
|
||||
|
||||
public static Vector3d ReadVector3d(CompoundTag tag, string name)
|
||||
{
|
||||
if (tag.ContainsKey(name))
|
||||
{
|
||||
var listTag = (ListTag)tag.Get(name);
|
||||
return new Vector3d(
|
||||
listTag.Get(0).AsDouble(),
|
||||
listTag.Get(1).AsDouble(),
|
||||
listTag.Get(2).AsDouble());
|
||||
}
|
||||
return Vector3d.ZERO;
|
||||
}
|
||||
|
||||
public static void WriteVector3i(CompoundTag tag, string name, Vector3i pos)
|
||||
{
|
||||
var posTag = new ListTag();
|
||||
posTag.Add(IntTag.ValueOf(pos.X));
|
||||
posTag.Add(IntTag.ValueOf(pos.Y));
|
||||
posTag.Add(IntTag.ValueOf(pos.Z));
|
||||
tag.Put(name, posTag);
|
||||
}
|
||||
|
||||
public static Vector3i ReadVector3i(CompoundTag tag, string name)
|
||||
{
|
||||
if (tag.ContainsKey(name))
|
||||
{
|
||||
var listTag = (ListTag)tag.Get(name);
|
||||
return new Vector3i(
|
||||
listTag.Get(0).AsInt(),
|
||||
listTag.Get(1).AsInt(),
|
||||
listTag.Get(2).AsInt());
|
||||
}
|
||||
return Vector3i.ZERO;
|
||||
}
|
||||
|
||||
private static int[] MsbLsbToIntArray(long msb, long lsb)
|
||||
{
|
||||
return new[]
|
||||
{
|
||||
(int)(msb >> 32),
|
||||
(int)msb,
|
||||
(int)(lsb >> 32),
|
||||
(int)lsb
|
||||
};
|
||||
}
|
||||
|
||||
private static int[] UuidToIntArray(NbtUUID id)
|
||||
{
|
||||
return MsbLsbToIntArray(id.GetMostSignificantBits(), id.GetLeastSignificantBits());
|
||||
}
|
||||
|
||||
private static NbtUUID UuidFromIntArray(int[] values)
|
||||
{
|
||||
return new NbtUUID(
|
||||
((long)values[0] << 32) | (values[1] & 0xFFFFFFFFL),
|
||||
((long)values[2] << 32) | (values[3] & 0xFFFFFFFFL));
|
||||
}
|
||||
|
||||
public static void WriteUUID(CompoundTag tag, string name, NbtUUID id)
|
||||
{
|
||||
tag.Put(name, IntArrayTag.ValueOf(UuidToIntArray(id).ToList()));
|
||||
}
|
||||
|
||||
public static NbtUUID ReadUUID(CompoundTag tag, string name)
|
||||
{
|
||||
if (!tag.ContainsKey(name))
|
||||
{
|
||||
return NbtUUID.ZERO;
|
||||
}
|
||||
return UuidFromIntArray(tag.Get(name).AsIntArray().ToArray());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue