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); }
/// <summary> /// Handles the entity data. /// </summary> /// <param name="message">The message.</param> private void HandleEntityData(McpeTileEntityData message) { Log.DebugFormat("x: {0}", message.x); Log.DebugFormat("y: {0}", message.y); Log.DebugFormat("z: {0}", message.z); Log.DebugFormat("NBT {0}", message.namedtag.NbtFile); var blockEntity = Level.GetBlockEntity(new BlockCoordinates(message.x, message.y, message.z)); if (blockEntity == null) { return; } blockEntity.SetCompound(message.namedtag.NbtFile.RootTag); Level.SetBlockEntity(blockEntity); }
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, }; }
/// <summary> /// Handles the entity data. /// </summary> /// <param name="message">The message.</param> protected virtual void HandleEntityData(McpeTileEntityData message) { Log.DebugFormat("x: {0}", message.x); Log.DebugFormat("y: {0}", message.y); Log.DebugFormat("z: {0}", message.z); Log.DebugFormat("NBT {0}", message.namedtag.NbtFile); var blockEntity = Level.GetBlockEntity(new BlockCoordinates(message.x, message.y, message.z)); if (blockEntity == null) return; blockEntity.SetCompound(message.namedtag.NbtFile.RootTag); Level.SetBlockEntity(blockEntity); }
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 Block sendBlock = new Block(block.Id) { Coordinates = block.Coordinates, Metadata = (byte)(0xb << 4 | (block.Metadata & 0xf)) }; var message = McpeUpdateBlock.CreateObject(); message.blocks = new BlockRecords { sendBlock }; 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 = McpeTileEntityData.CreateObject(); entityData.namedtag = nbt; entityData.x = blockEntity.Coordinates.X; entityData.y = (byte)blockEntity.Coordinates.Y; entityData.z = blockEntity.Coordinates.Z; 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); } }