/// <summary> /// Overrides the current structure (read: removes all previous blocks) with the serialized one in the buffer. /// Lazely validates the data and returns false, if it is found invalid. /// No checks are not made, #GetStructureErrors and #GetNotConnectedBlocks should be called after this method. /// </summary> public bool Deserialize(BitBuffer buffer) { foreach (RealPlacedBlock block in _blocks.Values.OfType <RealPlacedBlock>().ToList()) { RemoveBlock(block); } try { while (buffer.TotalBitsLeft >= RealPlacedBlock.SerializedBitsSize) { ushort type = (ushort)buffer.ReadBits(BlockFactory.BlockTypeSerializedBitsSize); if (type >= BlockFactory.TypeCount) { return(false); } BlockPosition position = BlockPosition.Deserialize(buffer); byte rotation = Rotation.Deserialize(buffer); BlockInfo info = BlockFactory.GetInfo(BlockFactory.GetType(type)); bool result = info is SingleBlockInfo single ? AddBlock(position, single, rotation) : AddBlock(position, (MultiBlockInfo)info, rotation); if (!result) { return(false); } } return(true); } catch (Exception e) { Debug.Log("Exception caught while deserializing into an EditableStructure: " + e); return(false); } }
private void ShowPreview(BlockPosition position, byte rotation) { Destroy(_previewObject); _previousPreviewPosition = position; BlockInfo info = BlockFactory.GetInfo(BlockFactory.GetType(_blockType)); Color color; if (_structure.CanAddBlock(position, info, rotation)) { color = Color.white; } else { if (_structure.IsPositionOccupied(position)) { return; } else { color = Color.red; } } RealPlacedBlock block; if (info is SingleBlockInfo single) { block = BlockFactory.MakeSinglePlaced(_structure.transform, single, rotation, position); } else { // ReSharper disable once UnusedVariable block = BlockFactory.MakeMultiPlaced(_structure.transform, (MultiBlockInfo)info, rotation, position, out PlacedMultiBlockPart[] parts); if (block == null) { return; } } _previewObject = block.gameObject; _previewObject.gameObject.name = "PreviewBlock"; BlockUtilities.RemoveCollider(_previewObject, true); color.a = 0.5f; BlockUtilities.SetColor(_previewObject, color, true); }
private void Place() { // ReSharper disable once UnusedVariable if (!GetSelectedBlock(out GameObject block, out BlockPosition position, out byte rotation)) { return; } BlockInfo info = BlockFactory.GetInfo(BlockFactory.GetType(_blockType)); if (_structure.TryAddBlock(position, info, rotation)) { ColorNotConnectedBlocks(); ShowPreview(position, rotation); } }
private void Deserialize(BitBuffer buffer) { while (buffer.TotalBitsLeft >= RealPlacedBlock.SerializedBitsSize) { ushort type = (ushort)buffer.ReadBits(BlockFactory.BlockTypeSerializedBitsSize); BlockPosition position = BlockPosition.Deserialize(buffer); BlockInfo info = BlockFactory.GetInfo(BlockFactory.GetType(type)); if (info.Type == BlockType.Mainframe) { _mainframePosition = position; } byte rotation = Rotation.Deserialize(buffer); RealLiveBlock block; if (info is SingleBlockInfo single) { block = BlockFactory.MakeSingleLive(transform, single, rotation, position); } else { block = BlockFactory.MakeMultiLive(transform, (MultiBlockInfo)info, rotation, position, out LiveMultiBlockPart[] parts); foreach (LiveMultiBlockPart part in parts) { _blocks.Add(part.Position, part); } } Health += info.Health; Mass += info.Mass; _blocks.Add(position, block); if (SystemFactory.Create(this, block, out BotSystem system)) { _systems.Add(position, system); } } }
public static Item GetItem(string name) { string[] data = name.Replace("minecraft:", "").Replace(" ", "_").ToUpper().Split(':'); int id = 0; int meta = 0; if (data.Length == 1) { int.TryParse(data[0], out id); } if (data.Length == 2) { int.TryParse(data[0], out id); int.TryParse(data[1], out meta); } try { ItemFactory factory = new ItemFactory(); id = (int)factory.GetType().GetField(data[0]).GetValue(factory); } catch { try { BlockFactory factory = new BlockFactory(); id = (int)factory.GetType().GetField(data[0]).GetValue(factory); } catch { } } Item item = Item.Get(id, meta); return(item); }
private void Switch() { _blockType = (ushort)((_blockType + 1) % BlockFactory.TypeCount); ShowPreview(); Debug.Log("Switched to: " + BlockFactory.GetType(_blockType)); }