public void BreakBlock(Player player, BlockCoordinates blockCoordinates) { List <ItemStack> drops = new List <ItemStack>(); Block block = GetBlock(blockCoordinates); drops.Add(block.GetDrops()); if (OnBlockBreak(new BlockBreakEventArgs(player, this, block, drops))) { block.BreakBlock(this); BlockEntity blockEnity = GetBlockEntity(blockCoordinates); if (blockEnity != null) { RemoveBlockEntity(blockCoordinates); drops.AddRange(blockEnity.GetDrops()); } if (player.GameMode != GameMode.Creative) { foreach (ItemStack drop in drops) { DropItem(blockCoordinates, drop); } } } else { var message = McpeUpdateBlock.CreateObject(); message.blocks = new BlockRecords { block }; player.SendPackage(message); } }
public void SetBlock(Block block, bool broadcast = true, bool applyPhysics = true) { ChunkColumn chunk = _worldProvider.GenerateChunkColumn(new ChunkCoordinates(block.Coordinates.X >> 4, block.Coordinates.Z >> 4)); chunk.SetBlock(block.Coordinates.X & 0x0f, block.Coordinates.Y & 0x7f, block.Coordinates.Z & 0x0f, block.Id); chunk.SetMetadata(block.Coordinates.X & 0x0f, block.Coordinates.Y & 0x7f, block.Coordinates.Z & 0x0f, block.Metadata); if (applyPhysics) { ApplyPhysics(block.Coordinates.X, block.Coordinates.Y, block.Coordinates.Z); } if (!broadcast) { return; } var message = McpeUpdateBlock.CreateObject(); message.blockId = block.Id; message.x = block.Coordinates.X; message.y = (byte)block.Coordinates.Y; message.z = block.Coordinates.Z; message.blockMetaAndPriority = (byte)(0xb << 4 | (block.Metadata & 0xf)); RelayBroadcast(message); }
public void SetBlock(Block block, bool broadcast = true, bool applyPhysics = true) { ChunkColumn chunk = _worldProvider.GenerateChunkColumn(new ChunkCoordinates(block.Coordinates.X >> 4, block.Coordinates.Z >> 4)); chunk.SetBlock(block.Coordinates.X & 0x0f, block.Coordinates.Y & 0x7f, block.Coordinates.Z & 0x0f, block.Id); chunk.SetMetadata(block.Coordinates.X & 0x0f, block.Coordinates.Y & 0x7f, block.Coordinates.Z & 0x0f, block.Metadata); if (applyPhysics) { ApplyPhysics(block.Coordinates.X, block.Coordinates.Y, block.Coordinates.Z); } if (!broadcast) { return; } 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 }; RelayBroadcast(message); }
public void SetBlock(Block block, bool broadcast = true, bool applyPhysics = true, bool calculateLight = true) { if (block.Coordinates.Y < 0) { return; } ChunkColumn chunk = _worldProvider.GenerateChunkColumn(new ChunkCoordinates(block.Coordinates.X >> 4, block.Coordinates.Z >> 4)); chunk.SetBlock(block.Coordinates.X & 0x0f, block.Coordinates.Y & 0xff, block.Coordinates.Z & 0x0f, block.Id); chunk.SetMetadata(block.Coordinates.X & 0x0f, block.Coordinates.Y & 0xff, block.Coordinates.Z & 0x0f, block.Metadata); if (applyPhysics) { ApplyPhysics(block.Coordinates.X, block.Coordinates.Y, block.Coordinates.Z); } if (block.LightLevel > 0) { block.BlockLight = (byte)block.LightLevel; chunk.SetBlocklight(block.Coordinates.X & 0x0f, block.Coordinates.Y & 0xff, block.Coordinates.Z & 0x0f, (byte)block.LightLevel); BlockLightCalculations.Calculate(this, block); } if (!broadcast) { return; } var message = McpeUpdateBlock.CreateObject(); message.blockId = block.Id; message.coordinates = block.Coordinates; message.blockMetaAndPriority = (byte)(0xb << 4 | (block.Metadata & 0xf)); RelayBroadcast(message); }
private static void RevertBlockAction(OpenPlayer player, Block block, BlockEntity blockEntity) { var message = McpeUpdateBlock.CreateObject(); message.blockRuntimeId = (uint)block.GetRuntimeId(); message.coordinates = block.Coordinates; message.blockPriority = 0xb; player.SendPacket(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.SendPacket(entityData); } }
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 VanillaCommands.SimpleResponse Worldborder(Player player, int radius = 200, bool centerOnPlayer = false) { Level level = player.Level; BlockCoordinates center = (BlockCoordinates)level.SpawnPoint; if (centerOnPlayer) { center = (BlockCoordinates)player.KnownPosition; } center.Y = 0; var players = level.GetSpawnedPlayers(); for (int y = 0; y < 256; y++) { for (int x = -radius; x <= radius; x++) { for (int z = -radius; z <= radius; z++) { if (x != -radius && x != radius && z != -radius && z != radius) { continue; } var block = new Glass() { Coordinates = center + new BlockCoordinates(x, y, z) }; level.SetBlock(block, false, false, false); List <Player> sendList = new List <Player>(); foreach (var p in players) { if (p.KnownPosition.DistanceTo(center + new BlockCoordinates(x, (int)p.KnownPosition.Y, z)) > p.ChunkRadius * 16) { continue; } sendList.Add(p); } var message = McpeUpdateBlock.CreateObject(); message.blockId = block.Id; message.coordinates = block.Coordinates; message.blockMetaAndPriority = (byte)(0xb << 4 | (block.Metadata & 0xf)); level.RelayBroadcast(sendList.ToArray(), message); } } } return(new VanillaCommands.SimpleResponse() { Body = $"Added world border with radius of {radius} around {center}" }); }
public void Interact(Level world, Player player, short itemId, BlockCoordinates blockCoordinates, short metadata, BlockFace face, Vector3 faceCoords) { // Make sure we are holding the item we claim to be using Block target = GetBlock(blockCoordinates); if (target.Interact(world, player, blockCoordinates, face, faceCoords)) { return; // Handled in block interaction } Item itemInHand = player.Inventory.GetItemInHand(); if (itemInHand.GetType() == typeof(Item)) { Log.Warn($"Generic item in hand when placing block. Can not complete request. Expected item {itemId} and item in hand is {itemInHand?.Id}"); return; // Cheat(?) } if (itemInHand == null || itemInHand.Id != itemId) { if (player.GameMode != GameMode.Creative) { Log.Error($"Wrong item in hand when placing block. Expected item {itemId} but had item {itemInHand?.Id}"); } return; // Cheat(?) } if (itemInHand is ItemBlock) { Block block = GetBlock(itemInHand.GetNewCoordinatesFromFace(blockCoordinates, face)); if (!AllowBuild || !OnBlockPlace(new BlockPlaceEventArgs(player, this, target, block))) { // Revert Block sendBlock = new Block(block.Id) { Coordinates = block.Coordinates, Metadata = (byte)(0xb << 4 | (block.Metadata & 0xf)) }; player.SendPlayerInventory(); var message = McpeUpdateBlock.CreateObject(); message.blocks = new BlockRecords { sendBlock }; player.SendPackage(message); return; } } itemInHand.UseItem(world, player, blockCoordinates, face, faceCoords); }
public void BreakBlock(Player player, BlockCoordinates blockCoordinates) { List <Item> drops = new List <Item>(); Block block = GetBlock(blockCoordinates); drops.AddRange(block.GetDrops()); if (OnBlockBreak(new BlockBreakEventArgs(player, this, block, drops))) { block.BreakBlock(this); BlockEntity blockEnity = GetBlockEntity(blockCoordinates); if (blockEnity != null) { RemoveBlockEntity(blockCoordinates); drops.AddRange(blockEnity.GetDrops()); } if (player.GameMode != GameMode.Creative) { foreach (Item drop in drops) { DropItem(blockCoordinates, drop); } } player.HungerManager.IncreaseExhaustion(0.025f); } else { 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); } }
public void SetBlock(Item i, BlockFace blockface, BlockCoordinates coord, uint id) { if (id != 31) { Block b = Level.GetBlock(i.GetNewCoordinatesFromFace(coord, blockface)); var message = McpeUpdateBlock.CreateObject(); message.blockId = 0; message.coordinates = b.Coordinates; message.blockMetaAndPriority = (byte)(0xb << 4 | (b.Metadata & 0xf)); SendPackage(message); } else { var message = McpeUpdateBlock.CreateObject(); message.blockId = 31; message.coordinates = coord; message.blockMetaAndPriority = (byte)(0xb << 4 | (1 & 0xf)); SendPackage(message); } }
public void Interact(Level world, Player player, short itemId, BlockCoordinates blockCoordinates, short metadata, BlockFace face, Vector3 faceCoords) { // Make sure we are holding the item we claim to be using Block target = GetBlock(blockCoordinates); if (target.Interact(world, player, blockCoordinates, face)) { return; // Handled in block interaction } ItemStack itemStackInHand = player.Inventory.GetItemInHand(); Item itemInHand = itemStackInHand.Item; if (itemInHand == null || itemInHand.Id != itemId) { return; // Cheat(?) } if (itemInHand is ItemBlock) { if (!OnBlockPlace(new BlockPlaceEventArgs(player, this, target))) { Block block = GetBlock(itemInHand.GetNewCoordinatesFromFace(blockCoordinates, face)); 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); return; } } itemInHand.UseItem(world, player, blockCoordinates, face, faceCoords); }
private static void OnMcpeUpdateBlock(Package message) { McpeUpdateBlock msg = (McpeUpdateBlock)message; Log.DebugFormat("No of Blocks: {0}", msg.blocks.Count); }
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 override void HandleMcpeUpdateBlock(McpeUpdateBlock message) { CallPacketHandlers(message); }
public void OpenInventory(Player player) { //Log.Info("Command Executed!"); player.SendMessage("Opening Chest..."); /*BlockCoordinates coords = (BlockCoordinates) player.KnownPosition; * coords.Y = 0;*/ BlockCoordinates coords = new BlockCoordinates(0); //Block past = player.Level.GetBlock(coords); McpeUpdateBlock chest = Package <McpeUpdateBlock> .CreateObject(); chest.blockId = 54; chest.coordinates = coords; chest.blockMetaAndPriority = 0 & 15; player.SendPackage(chest); ChestBlockEntity blockEntity = new ChestBlockEntity { Coordinates = coords }; NbtCompound compound = blockEntity.GetCompound(); compound["CustomName"] = new NbtString("CustomName", "§5§k--§r §l§o§2Virtual Chest§r §5§k--§r"); //player.Level.SetBlockEntity(blockEntity); McpeBlockEntityData chestEntity = Package <McpeBlockEntityData> .CreateObject(); chestEntity.namedtag = new Nbt { NbtFile = new NbtFile { BigEndian = false, UseVarInt = true, RootTag = compound } }; chestEntity.coordinates = coords; player.SendPackage(chestEntity); //player.OpenInventory(coords); Inventory inventory = new Inventory(0, blockEntity, 1, new NbtList()) { Type = 0, WindowsId = 10 }; //inventory.InventoryChange += new Action<Player, MiNET.Inventory, byte, Item>(player.OnInventoryChange); inventory.AddObserver(player); McpeContainerOpen mcpeContainerOpen = Package <McpeContainerOpen> .CreateObject(1L); mcpeContainerOpen.windowId = inventory.WindowsId; mcpeContainerOpen.type = inventory.Type; mcpeContainerOpen.coordinates = coords; mcpeContainerOpen.unknownRuntimeEntityId = 1L; player.SendPackage((Package)mcpeContainerOpen); McpeInventoryContent inventoryContent = Package <McpeInventoryContent> .CreateObject(1L); inventoryContent.inventoryId = (uint)inventory.WindowsId; inventoryContent.input = inventory.Slots; player.SendPackage((Package)inventoryContent); }
public virtual void HandleMcpeUpdateBlock(McpeUpdateBlock message) { }
/// <summary> /// Handles the specified package. /// </summary> /// <param name="message">The package.</param> /// <param name="senderEndpoint">The sender's endpoint.</param> private void HandlePackage(Package message, IPEndPoint senderEndpoint) { if (typeof(McpeBatch) == message.GetType()) { Log.Debug("Processing Batch package"); McpeBatch batch = (McpeBatch)message; var messages = new List <Package>(); // Get bytes byte[] payload = batch.payload; // Decompress bytes MemoryStream stream = new MemoryStream(payload); if (stream.ReadByte() != 0x78) { throw new InvalidDataException("Incorrect ZLib header. Expected 0x78"); } stream.ReadByte(); using (var defStream2 = new DeflateStream(stream, CompressionMode.Decompress, false)) { // Get actual package out of bytes MemoryStream destination = new MemoryStream(); defStream2.CopyTo(destination); destination.Position = 0; NbtBinaryReader reader = new NbtBinaryReader(destination, true); do { int len = reader.ReadInt32(); byte[] internalBuffer = reader.ReadBytes(len); messages.Add(PackageFactory.CreatePackage(internalBuffer[0], internalBuffer) ?? new UnknownPackage(internalBuffer[0], internalBuffer)); } while (destination.Position < destination.Length); // throw new Exception("Have more data"); } foreach (var msg in messages) { HandlePackage(msg, senderEndpoint); msg.PutPool(); } return; } TraceReceive(message); if (typeof(UnknownPackage) == message.GetType()) { return; } if (typeof(McpeDisconnect) == message.GetType()) { McpeDisconnect msg = (McpeDisconnect)message; Log.InfoFormat("Disconnect {1}: {0}", msg.message, Username); SendDisconnectionNotification(); StopClient(); return; } if (typeof(ConnectedPing) == message.GetType()) { ConnectedPing msg = (ConnectedPing)message; SendConnectedPong(msg.sendpingtime); return; } if (typeof(McpeFullChunkData) == message.GetType()) { //McpeFullChunkData msg = (McpeFullChunkData) message; //ChunkColumn chunk = ClientUtils.DecocedChunkColumn(msg.chunkData); //if (chunk != null) //{ // Log.DebugFormat("Chunk X={0}", chunk.x); // Log.DebugFormat("Chunk Z={0}", chunk.z); // //ClientUtils.SaveChunkToAnvil(chunk); //} return; } if (typeof(ConnectionRequestAccepted) == message.GetType()) { Thread.Sleep(50); SendNewIncomingConnection(); //_connectedPingTimer = new Timer(state => SendConnectedPing(), null, 1000, 1000); Thread.Sleep(50); SendLogin(Username); return; } if (typeof(McpeSetSpawnPosition) == message.GetType()) { McpeSetSpawnPosition msg = (McpeSetSpawnPosition)message; _spawn = new Vector3(msg.x, msg.y, msg.z); _level.SpawnX = (int)_spawn.X; _level.SpawnY = (int)_spawn.Y; _level.SpawnZ = (int)_spawn.Z; return; } if (typeof(McpeStartGame) == message.GetType()) { McpeStartGame msg = (McpeStartGame)message; _entityId = msg.entityId; _spawn = new Vector3(msg.x, msg.y, msg.z); _level.LevelName = "Default"; _level.Version = 19133; _level.GameType = msg.gamemode; //ClientUtils.SaveLevel(_level); return; } if (typeof(McpeTileEvent) == message.GetType()) { McpeTileEvent msg = (McpeTileEvent)message; Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Case 1: {0}", msg.case1); Log.DebugFormat("Case 2: {0}", msg.case2); return; } if (typeof(McpeAddEntity) == message.GetType()) { McpeAddEntity msg = (McpeAddEntity)message; Log.DebugFormat("Entity ID: {0}", msg.entityId); Log.DebugFormat("Entity Type: {0}", msg.entityType); Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Yaw: {0}", msg.yaw); Log.DebugFormat("Pitch: {0}", msg.pitch); Log.DebugFormat("Velocity X: {0}", msg.speedX); Log.DebugFormat("Velocity Y: {0}", msg.speedY); Log.DebugFormat("Velocity Z: {0}", msg.speedZ); //Log.DebugFormat("Metadata: {0}", msg.metadata.ToString()); //Log.DebugFormat("Links count: {0}", msg.links); return; } if (typeof(McpeSetEntityData) == message.GetType()) { McpeSetEntityData msg = (McpeSetEntityData)message; Log.DebugFormat("Entity ID: {0}", msg.entityId); MetadataDictionary metadata = msg.metadata; Log.DebugFormat("Metadata: {0}", metadata.ToString()); return; } if (typeof(McpeMovePlayer) == message.GetType()) { //McpeMovePlayer msg = (McpeMovePlayer) message; //Log.DebugFormat("Entity ID: {0}", msg.entityId); //CurrentLocation = new PlayerLocation(msg.x, msg.y + 10, msg.z); //SendMcpeMovePlayer(); return; } if (typeof(McpeUpdateBlock) == message.GetType()) { McpeUpdateBlock msg = (McpeUpdateBlock)message; Log.DebugFormat("No of Blocks: {0}", msg.blocks.Count); return; } if (typeof(McpeLevelEvent) == message.GetType()) { McpeLevelEvent msg = (McpeLevelEvent)message; Log.DebugFormat("Event ID: {0}", msg.eventId); Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Data: {0}", msg.data); return; } if (typeof(McpeContainerSetContent) == message.GetType()) { McpeContainerSetContent msg = (McpeContainerSetContent)message; Log.DebugFormat("Window ID: 0x{0:x2}, Count: {1}", msg.windowId, msg.slotData.Count); var slots = msg.slotData.GetValues(); foreach (var entry in slots) { MetadataSlot slot = (MetadataSlot)entry; //Log.DebugFormat(" - Id: {0}, Metadata: {1}, Count: {2}", slot.Value.Item.Id, slot.Value.Item.Metadata, slot.Value.Count); } return; } if (typeof(McpeCraftingData) == message.GetType()) { string fileName = Path.GetTempPath() + "Recipes_" + Guid.NewGuid() + ".txt"; Log.Info("Writing recipes to filename: " + fileName); FileStream file = File.OpenWrite(fileName); McpeCraftingData msg = (McpeCraftingData)message; IndentedTextWriter writer = new IndentedTextWriter(new StreamWriter(file)); writer.WriteLine("static RecipeManager()"); writer.WriteLine("{"); writer.Indent++; writer.WriteLine("Recipes = new Recipes"); writer.WriteLine("{"); writer.Indent++; foreach (Recipe recipe in msg.recipes) { ShapelessRecipe shapelessRecipe = recipe as ShapelessRecipe; if (shapelessRecipe != null) { writer.WriteLine(string.Format("new ShapelessRecipe(new ItemStack(ItemFactory.GetItem({0}, {1}), {2}),", shapelessRecipe.Result.Id, shapelessRecipe.Result.Metadata, shapelessRecipe.Result.Count)); writer.Indent++; writer.WriteLine("new List<ItemStack>"); writer.WriteLine("{"); writer.Indent++; foreach (var itemStack in shapelessRecipe.Input) { writer.WriteLine(string.Format("new ItemStack(ItemFactory.GetItem({0}, {1}), {2}),", itemStack.Id, itemStack.Metadata, itemStack.Count)); } writer.Indent--; writer.WriteLine("}),"); writer.Indent--; continue; } ShapedRecipe shapedRecipe = recipe as ShapedRecipe; if (shapedRecipe != null) { writer.WriteLine(string.Format("new ShapedRecipe({0}, {1}, new ItemStack(ItemFactory.GetItem({2}, {3}), {4}),", shapedRecipe.Width, shapedRecipe.Height, shapedRecipe.Result.Id, shapedRecipe.Result.Metadata, shapedRecipe.Result.Count)); writer.Indent++; writer.WriteLine("new Item[]"); writer.WriteLine("{"); writer.Indent++; foreach (Item item in shapedRecipe.Input) { writer.WriteLine(string.Format("ItemFactory.GetItem({0}, {1}),", item.Id, item.Metadata)); } writer.Indent--; writer.WriteLine("}),"); writer.Indent--; continue; } } writer.WriteLine("};"); writer.Indent--; writer.WriteLine("}"); writer.Indent--; writer.Flush(); file.Close(); Environment.Exit(0); return; } }
private void HandleMcpeUpdateBlock(BedrockTraceHandler caller, McpeUpdateBlock message) { //if (message.OrderingIndex <= lastNumber) return; //lastNumber = message.OrderingIndex; if (message.storage != 0) { return; } if (message.blockPriority != 3) { return; } if (!_runningBlockMetadataDiscovery) { _resetEventUpdateBlock.Set(); return; } int runtimeId = (int)message.blockRuntimeId; int bid = message.coordinates.X / 2; int meta = (message.coordinates.Y - 100) / 2; //TODO: Fix for doors and beds. They get 2 updates. BlockStateContainer blockstate = caller.Client.BlockPalette[runtimeId]; if (message.coordinates.X % 2 != 0 || message.coordinates.Y % 2 != 0) { Log.Warn($"Update block outside of grid {message.coordinates}, {caller.Client.BlockPalette[runtimeId]}"); if (blockstate.Name.EndsWith("_door")) { if (blockstate.States.First(s => s.Name.Equals("upper_block_bit") && ((BlockStateInt)s).Value == 1) != null) { blockstate.Data = (short)meta; } } else if (blockstate.Name.Equals("minecraft:bed")) { if (blockstate.States.First(s => s.Name.Equals("head_piece_bit") && ((BlockStateInt)s).Value == 0) != null) { blockstate.Data = (short)meta; } } return; } if (blockstate.Id == 0) { return; } if (blockstate.Data == -1) { lock (_lastUpdatedBlockstate) { try { if (bid != blockstate.Id) { Log.Warn($"Wrong id. Expected {blockstate.Id}, got {bid}"); } else { blockstate.Data = (short)meta; Log.Debug($"Correct id. Expected {blockstate.Id}, and got {bid}"); } Log.Debug($"Block update {bid}, {meta}, with blockstate\n{blockstate}"); } finally { Log.Warn($"Got {blockstate.Id}, {meta} storage {message.storage}, {message.blockPriority}"); _lastUpdatedBlockstate = blockstate; _resetEventUpdateBlock.Set(); } } } else { Log.Warn($"Blockstate {runtimeId} {blockstate.Id}, {meta} already had meta set to {blockstate.Data}"); } }
public override void HandleMcpeUpdateBlock(McpeUpdateBlock message) { }
public abstract void HandleMcpeUpdateBlock(McpeUpdateBlock message);
public override void HandleMcpeUpdateBlock(McpeUpdateBlock message) { if (message.storage != 0) { Log.Warn($"UPDATEBLOCK: Unsupported block storage! {message.storage}"); return; } if (_blockStateMap.TryGetValue(message.blockRuntimeId, out var bs)) { IBlockState state = null; var result = BlockFactory.RuntimeIdTable.FirstOrDefault(xx => xx.Name == bs.Name); if (result != null && result.Id >= 0) { var reverseMap = MiNET.Worlds.AnvilWorldProvider.Convert.FirstOrDefault(map => map.Value.Item1 == result.Id); var id = result.Id; if (reverseMap.Value != null) { id = reverseMap.Key; } var res = BlockFactory.GetBlockStateID( (int)id, (byte)bs.Data); if (AnvilWorldProvider.BlockStateMapper.TryGetValue( res, out var res2)) { var t = BlockFactory.GetBlockState(res2); t = ChunkProcessor.TranslateBlockState(t, id, bs.Data); state = t; } else { Log.Info( $"Did not find anvil statemap: {result.Name}"); state = ChunkProcessor.TranslateBlockState( BlockFactory.GetBlockState(result.Name), id, bs.Data); } } if (state == null) { state = ChunkProcessor.TranslateBlockState( BlockFactory.GetBlockState(bs.Name), -1, bs.Data); } if (state != null) { BaseClient.WorldReceiver?.SetBlockState( new BlockCoordinates(message.coordinates.X, message.coordinates.Y, message.coordinates.Z), state); } /* * var result = * BlockFactory.RuntimeIdTable.FirstOrDefault(xx => xx.Name == bs.Name); * * uint res = 0; * bool ss = false; * if (result != null && result.Id >= 0) * { * res = BlockFactory.GetBlockStateID((int) result.Id, (byte) bs.Data); * ss = true; * } * * if (ss && AnvilWorldProvider.BlockStateMapper.TryGetValue(res, out res)) * { * var a = BlockFactory.GetBlockState(res); * BaseClient.WorldReceiver?.SetBlockState( * new BlockCoordinates(message.coordinates.X, message.coordinates.Y, message.coordinates.Z), * a); * } * else * { * * BaseClient.WorldReceiver?.SetBlockState( * new BlockCoordinates(message.coordinates.X, message.coordinates.Y, message.coordinates.Z), * BlockFactory.GetBlockState(bs.Name)); * }*/ } else { Log.Warn($"Received unknown block runtime id."); } }
/// <summary> /// Handles the specified package. /// </summary> /// <param name="message">The package.</param> /// <param name="senderEndpoint">The sender's endpoint.</param> private void HandlePackage(Package message, IPEndPoint senderEndpoint) { if (typeof(McpeBatch) == message.GetType()) { McpeBatch batch = (McpeBatch)message; var messages = new List <Package>(); // Get bytes byte[] payload = batch.payload; // Decompress bytes MemoryStream stream = new MemoryStream(payload); if (stream.ReadByte() != 0x78) { throw new InvalidDataException("Incorrect ZLib header. Expected 0x78 0x9C"); } stream.ReadByte(); using (var defStream2 = new DeflateStream(stream, CompressionMode.Decompress, false)) { // Get actual package out of bytes MemoryStream destination = new MemoryStream(); defStream2.CopyTo(destination); byte[] internalBuffer = destination.ToArray(); messages.Add(PackageFactory.CreatePackage(internalBuffer[0], internalBuffer) ?? new UnknownPackage(internalBuffer[0], internalBuffer)); } foreach (var msg in messages) { HandlePackage(msg, senderEndpoint); } return; } TraceReceive(message); if (typeof(UnknownPackage) == message.GetType()) { return; } if (typeof(McpeDisconnect) == message.GetType()) { McpeDisconnect msg = (McpeDisconnect)message; Log.Debug(msg.message); StopServer(); return; } if (typeof(ConnectedPing) == message.GetType()) { ConnectedPing msg = (ConnectedPing)message; SendConnectedPong(msg.sendpingtime); return; } if (typeof(McpeFullChunkData) == message.GetType()) { McpeFullChunkData msg = (McpeFullChunkData)message; ChunkColumn chunk = ClientUtils.DecocedChunkColumn(msg.chunkData); if (chunk != null) { Log.DebugFormat("Chunk X={0}", chunk.x); Log.DebugFormat("Chunk Z={0}", chunk.z); ClientUtils.SaveChunkToAnvil(chunk); } return; } if (typeof(ConnectionRequestAccepted) == message.GetType()) { Thread.Sleep(50); SendNewIncomingConnection(); var t1 = new Timer(state => SendConnectedPing(), null, 2000, 5000); Thread.Sleep(50); SendLogin("Client12"); return; } if (typeof(McpeSetSpawnPosition) == message.GetType()) { McpeSetSpawnPosition msg = (McpeSetSpawnPosition)message; _spawn = new Vector3(msg.x, msg.y, msg.z); _level.SpawnX = (int)_spawn.X; _level.SpawnY = (int)_spawn.Y; _level.SpawnZ = (int)_spawn.Z; return; } if (typeof(McpeStartGame) == message.GetType()) { McpeStartGame msg = (McpeStartGame)message; _entityId = msg.entityId; _spawn = new Vector3(msg.x, msg.y, msg.z); _level.LevelName = "Default"; _level.Version = 19133; _level.GameType = msg.gamemode; ClientUtils.SaveLevel(_level); return; } if (typeof(McpeTileEvent) == message.GetType()) { McpeTileEvent msg = (McpeTileEvent)message; Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Case 1: {0}", msg.case1); Log.DebugFormat("Case 2: {0}", msg.case2); return; } if (typeof(McpeAddEntity) == message.GetType()) { McpeAddEntity msg = (McpeAddEntity)message; Log.DebugFormat("Entity ID: {0}", msg.entityId); Log.DebugFormat("Entity Type: {0}", msg.entityType); Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Yaw: {0}", msg.yaw); Log.DebugFormat("Pitch: {0}", msg.pitch); Log.DebugFormat("Velocity X: {0}", msg.speedX); Log.DebugFormat("Velocity Y: {0}", msg.speedY); Log.DebugFormat("Velocity Z: {0}", msg.speedZ); Log.DebugFormat("Metadata: {0}", msg.metadata.ToString()); Log.DebugFormat("Links count: {0}", msg.links); return; } if (typeof(McpeSetEntityData) == message.GetType()) { McpeSetEntityData msg = (McpeSetEntityData)message; Log.DebugFormat("Entity ID: {0}", msg.entityId); MetadataDictionary metadata = msg.metadata; Log.DebugFormat("Metadata: {0}", metadata.ToString()); return; } if (typeof(McpeMovePlayer) == message.GetType()) { McpeMovePlayer msg = (McpeMovePlayer)message; Log.DebugFormat("Entity ID: {0}", msg.entityId); _currentLocation = new PlayerLocation(msg.x, msg.y + 10, msg.z); SendMcpeMovePlayer(); return; } if (typeof(McpeUpdateBlock) == message.GetType()) { McpeUpdateBlock msg = (McpeUpdateBlock)message; Log.DebugFormat("No of Blocks: {0}", msg.blocks.Count); return; } if (typeof(McpeLevelEvent) == message.GetType()) { McpeLevelEvent msg = (McpeLevelEvent)message; Log.DebugFormat("Event ID: {0}", msg.eventId); Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Data: {0}", msg.data); return; } }