public void SetBlockEntity(BlockEntity blockEntity, bool broadcast = true) { ChunkColumn chunk = _worldProvider.GenerateChunkColumn(new ChunkCoordinates(blockEntity.Coordinates.X >> 4, blockEntity.Coordinates.Z >> 4)); chunk.SetBlockEntity(blockEntity.Coordinates, blockEntity.GetCompound()); if (blockEntity.UpdatesOnTick) { BlockEntities.Add(blockEntity); } if (!broadcast) { return; } Nbt nbt = new Nbt { NbtFile = new NbtFile { BigEndian = false, RootTag = blockEntity.GetCompound() } }; var entityData = new McpeTileEntityData { namedtag = nbt, x = blockEntity.Coordinates.X, y = (byte)blockEntity.Coordinates.Y, z = blockEntity.Coordinates.Z }; RelayBroadcast(entityData); }
public void Write(Nbt nbt) { var file = nbt.NbtFile; file.BigEndian = false; Write(file.SaveToBuffer(NbtCompression.None)); }
public ItemStack(ItemType type, short count, Nbt nbt, string[] placeOn, string[] canBreak) { Type = type; Count = count; NbtData = nbt; CanBePlacedOn = placeOn; CanBreak = canBreak; }
public void BreakBlock(Player player, BlockCoordinates blockCoordinates) { List <Item> drops = new List <Item>(); Block block = GetBlock(blockCoordinates); BlockEntity blockEntity = GetBlockEntity(blockCoordinates); drops.AddRange(block.GetDrops()); if (!AllowBreak || !OnBlockBreak(new BlockBreakEventArgs(player, this, block, drops))) { // Revert var message = McpeUpdateBlock.CreateObject(); message.blockId = block.Id; message.coordinates = block.Coordinates; message.blockMetaAndPriority = (byte)(0xb << 4 | (block.Metadata & 0xf)); player.SendPackage(message); // Revert block entity if exists if (blockEntity != null) { Nbt nbt = new Nbt { NbtFile = new NbtFile { BigEndian = false, RootTag = blockEntity.GetCompound() } }; var entityData = McpeBlockEntityData.CreateObject(); entityData.namedtag = nbt; entityData.coordinates = blockEntity.Coordinates; player.SendPackage(entityData); } } else { block.BreakBlock(this); if (blockEntity != null) { RemoveBlockEntity(blockCoordinates); drops.AddRange(blockEntity.GetDrops()); } if (player.GameMode != GameMode.Creative) { foreach (Item drop in drops) { DropItem(blockCoordinates, drop); } } player.HungerManager.IncreaseExhaustion(0.025f); } }
public void Write(Nbt nbt) { NbtFile file = nbt.NbtFile; file.BigEndian = false; file.UseVarInt = this is McpeBlockEntityData; Write(file.SaveToBuffer(NbtCompression.None)); }
public Nbt ReadNbt() { Nbt nbt = new Nbt(); NbtFile file = new NbtFile(); file.BigEndian = false; nbt.NbtFile = file; file.LoadFromStream(_reader.BaseStream, NbtCompression.None); return(nbt); }
public Nbt ReadNbt() { Nbt nbt = new Nbt(); NbtFile file = new NbtFile(); file.BigEndian = false; file.UseVarInt = this is McpeBlockEntityData; nbt.NbtFile = file; file.LoadFromStream(_reader.BaseStream, NbtCompression.None); return(nbt); }
/// <summary> /// Returns the hash code for this item stack. /// </summary> /// <returns></returns> public override int GetHashCode() { unchecked { int hashCode = _Id.GetHashCode(); hashCode = (hashCode * 397) ^ _Count.GetHashCode(); hashCode = (hashCode * 397) ^ _Metadata.GetHashCode(); hashCode = (hashCode * 397) ^ Index; hashCode = (hashCode * 397) ^ (Nbt != null ? Nbt.GetHashCode() : 0); return(hashCode); } }
public static Nbt ReadLegacyNbt(Stream stream) { Nbt nbt = new Nbt(); NbtFile nbtFile = new NbtFile(); nbtFile.BigEndian = false; nbtFile.UseVarInt = true; nbtFile.AllowAlternativeRootTag = true; nbt.NbtFile = nbtFile; nbtFile.LoadFromStream(stream, NbtCompression.None); return(nbt); }
public string GetDisplayName() { if (Nbt != null) { if (Nbt.TryGet("display", out NbtCompound display)) { if (display.TryGet("Name", out NbtString name)) { return(name.Value); } } } return(DisplayName); }
public override string ToString() { StringBuilder stringBuilder = new StringBuilder(); if (IsTag) { stringBuilder.Append('#'); } stringBuilder.Append(Resource.ToString()); if (Nbt != null) { stringBuilder.Append(Nbt.ToSnbt()); } return(stringBuilder.ToString()); }
public override void HandleMcpeBlockEntityData(McpeBlockEntityData message) { BlockCoordinates coordinates = message.coordinates; Nbt nbt = message.namedtag; ChunkColumn chunk = _worldProvider.GenerateChunkColumn((ChunkCoordinates)coordinates, true); if (chunk == null) { //Log.Warn($"Got block entity for non existing chunk at {coordinates}\n{nbt.NbtFile.RootTag}"); _futureBlockEntities.TryAdd(coordinates, (NbtCompound)nbt.NbtFile.RootTag); } else { //Log.Warn($"Got block entity for existing chunk at {coordinates}\n{nbt.NbtFile.RootTag}"); chunk.SetBlockEntity(coordinates, (NbtCompound)nbt.NbtFile.RootTag); } }
public void BlockEntityTest() { NbtFile file = new NbtFile(); file.BigEndian = false; var compound = file.RootTag = new NbtCompound(string.Empty); compound.Add(new NbtString("Text1", "first line")); compound.Add(new NbtString("Text2", "second line")); compound.Add(new NbtString("Text3", "third line")); compound.Add(new NbtString("Text4", "forth line")); compound.Add(new NbtString("id", "Sign")); compound.Add(new NbtInt("x", 6)); compound.Add(new NbtInt("y", 6)); compound.Add(new NbtInt("z", 6)); Console.WriteLine(file.ToString()); Nbt nbt = new Nbt(); nbt.NbtFile = file; McpeTileEntityData message = new McpeTileEntityData() { x = 6, y = 6, z = 6, namedtag = nbt }; Assert.NotNull(message.Encode()); Console.WriteLine(ByteArrayToString(message.Encode())); var b = new byte[] { 0xb8, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x06, 0x0a, 0x00, 0x00, 0x08, 0x02, 0x00, 0x49, 0x64, 0x04, 0x00, 0x53, 0x69, 0x67, 0x6e, 0x03, 0x01, 0x00, 0x78, 0x06, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00, 0x79, 0x06, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00, 0x7a, 0x06, 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x54, 0x65, 0x78, 0x74, 0x31, 0x0a, 0x00, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x08, 0x05, 0x00, 0x54, 0x65, 0x78, 0x74, 0x32, 0x0b, 0x00, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x08, 0x05, 0x00, 0x54, 0x65, 0x78, 0x74, 0x33, 0x0a, 0x00, 0x74, 0x68, 0x69, 0x72, 0x64, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x08, 0x05, 0x00, 0x54, 0x65, 0x78, 0x74, 0x34, 0x0a, 0x00, 0x66, 0x6f, 0x72, 0x74, 0x68, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x00, }; }
public string GetDisplayName() { if (Nbt != null) { if (Nbt.TryGet("display", out NbtCompound display)) { if (display.TryGet("Name", out NbtString name)) { if (ValidateJSON(name.Value)) { return(ChatParser.ParseText(name.Value)); } return(name.Value); } } } return(DisplayName); }
public string GetDisplayName() { if (Nbt != null) { if (Nbt.TryGet("display", out NbtCompound display)) { if (display.TryGet("Name", out NbtString name)) { if (Alex.ServerType == ServerType.Java) { return(ChatParser.ParseText(name.Value)); } return(name.Value); } } } return(DisplayName); }
public static Nbt ReadNbt(Stream stream, bool useVarInt = false) { Nbt nbt = new Nbt(); NbtFile nbtFile = new NbtFile(); nbtFile.BigEndian = false; nbtFile.UseVarInt = useVarInt; nbt.NbtFile = nbtFile; nbtFile.LoadFromStream(stream, NbtCompression.None); /* * var reader = new NbtBinaryReader(stream, false) { * Selector = null, * UseVarInt = useVarInt * }; * * nbtFile.RootTag = ReadUnknownTag(reader);*/ return(nbt); }
/// <summary> /// Gets the slot data. /// </summary> /// <returns></returns> /// <remarks></remarks> public byte[] GetData() { byte[] data = new byte[0] .Concat(Packet.MakeShort(ID)).ToArray(); if (ID == -1) { return(data); } data = data.Concat(new byte[] { Count }) .Concat(Packet.MakeShort(Metadata)).ToArray(); if (CanEnchant(ID)) { MemoryStream ms = new MemoryStream(); GZipStream gzs = new GZipStream(ms, CompressionMode.Compress, false); Nbt.SaveFile(gzs); gzs.Close(); byte[] b = ms.GetBuffer(); data = data.Concat(Packet.MakeShort((short)b.Length)).Concat(b).ToArray(); } return(data); }
public void Open(Player player) { if (!_horse.IsTamed) { return; } player.SetOpenInventory(this); McpeUpdateEquipment equ = McpeUpdateEquipment.CreateObject(); equ.entityId = _horse.EntityId; equ.windowId = 2; equ.windowType = 12; Nbt nbt = new Nbt { NbtFile = new NbtFile { BigEndian = false, UseVarInt = true, RootTag = GetNbt() } }; equ.namedtag = nbt; player.SendPacket(equ); McpeInventoryContent containerSetContent = McpeInventoryContent.CreateObject(); containerSetContent.inventoryId = 2; containerSetContent.input = new ItemStacks() { Slot0, Slot1 }; player.SendPacket(containerSetContent); }
public void WriteTo(MinecraftStream stream) { stream.WriteInt16(Id); if (Empty) { return; } stream.WriteInt8(Count); stream.WriteInt16(Metadata); if (Nbt == null) { stream.WriteInt16(-1); return; } var mStream = new MemoryStream(); Nbt.SaveToStream(mStream, NbtCompression.GZip); var buffer = mStream.GetBuffer(); stream.WriteInt16((short)buffer.Length); stream.WriteUInt8Array(buffer); }
public void DisplaySelection(bool forceDisplay = false, bool forceHide = false) { if (!forceDisplay && _structureBlockBlockEntity != null && (forceHide || _structureBlockBlockEntity.ShowBoundingBox != ShowSelection)) { bool showBoundingBox = !forceHide && ShowSelection; if (_structureBlockBlockEntity.ShowBoundingBox == showBoundingBox) { return; } _structureBlockBlockEntity.ShowBoundingBox = showBoundingBox; var nbt = new Nbt { NbtFile = new NbtFile { BigEndian = false, UseVarInt = true, RootTag = _structureBlockBlockEntity.GetCompound() } }; var entityData = McpeBlockEntityData.CreateObject(); entityData.namedtag = nbt; entityData.coordinates = _structureBlockBlockEntity.Coordinates; Player.SendPacket(entityData); } if (forceHide) { return; } if (!forceDisplay && !ShowSelection) { return; // don't render at all } if (forceDisplay && ShowSelection) { return; // Will be rendered on regular tick instead } if (!Monitor.TryEnter(_sync)) { return; } try { BoundingBox box = GetSelection().GetAdjustedBoundingBox(); if (!forceDisplay && box == _currentDisplayedSelection) { return; } _currentDisplayedSelection = box; int minX = (int)Math.Min(box.Min.X, box.Max.X); int maxX = (int)(Math.Max(box.Min.X, box.Max.X) + 1); int minY = (int)Math.Max(0, Math.Min(box.Min.Y, box.Max.Y)); int maxY = (int)(Math.Min(255, Math.Max(box.Min.Y, box.Max.Y)) + 1); int minZ = (int)Math.Min(box.Min.Z, box.Max.Z); int maxZ = (int)(Math.Max(box.Min.Z, box.Max.Z) + 1); int width = maxX - minX; int height = maxY - minY; int depth = maxZ - minZ; if (_structureBlock != null) { { var block = Player.Level.GetBlock(_structureBlock.Coordinates); var updateBlock = McpeUpdateBlock.CreateObject(); updateBlock.blockRuntimeId = (uint)block.GetRuntimeId(); updateBlock.coordinates = _structureBlock.Coordinates; updateBlock.blockPriority = 0xb; Player.SendPacket(updateBlock); } _structureBlock = null; _structureBlockBlockEntity = null; } _structureBlock = new StructureBlock { StructureBlockType = "save", Coordinates = new BlockCoordinates(minX, 255, minZ), }; { var updateBlock = McpeUpdateBlock.CreateObject(); updateBlock.blockRuntimeId = (uint)_structureBlock.GetRuntimeId(); updateBlock.coordinates = _structureBlock.Coordinates; updateBlock.blockPriority = 0xb; Player.SendPacket(updateBlock); } _structureBlockBlockEntity = new StructureBlockBlockEntity { ShowBoundingBox = true, Coordinates = _structureBlock.Coordinates, Offset = new BlockCoordinates(0, minY - _structureBlock.Coordinates.Y, 0), Size = new BlockCoordinates(width, height, depth) }; { Log.Debug($"Structure:\n{box}\n{_structureBlockBlockEntity.GetCompound()}"); var nbt = new Nbt { NbtFile = new NbtFile { BigEndian = false, UseVarInt = true, RootTag = _structureBlockBlockEntity.GetCompound() } }; var entityData = McpeBlockEntityData.CreateObject(); entityData.namedtag = nbt; entityData.coordinates = _structureBlockBlockEntity.Coordinates; Player.SendPacket(entityData); } return; } catch (Exception e) { Log.Error("Display selection", e); } finally { Monitor.Exit(_sync); } }
public MetadataNbt(Nbt nbtCompound) { Nbt = nbtCompound; }
public override int GetHashCode() => ID.GetHashCode() ^ Damage.GetHashCode() ^ Count.GetHashCode() ^ Index.GetHashCode() ^ (Nbt?.GetHashCode() ?? 0);
/// <inheritdoc /> public override void FromStream(BinaryReader reader) { Nbt = NetworkUtils.ReadNbt(reader.BaseStream, true); }