public WorldEditBlock(Type blockType, Vector3i position, Vector3i originalPosition, IWorldEditBlockData blockData) : this() { this.BlockType = blockType ?? throw new ArgumentNullException(nameof(blockType)); this.Position = position; this.OriginalPosition = originalPosition; this.BlockData = blockData; }
public static void RestorePlantBlock(Type type, Vector3i position, IWorldEditBlockData blockData) { if (blockData == null) { return; } WorldEditPlantBlockData plantBlockData = (WorldEditPlantBlockData)blockData; PlantSpecies plantSpecies = null; try { plantSpecies = EcoSim.AllSpecies.OfType <PlantSpecies>().First(species => species.GetType() == plantBlockData.PlantType); } catch (InvalidOperationException) { //TODO: Temporary support for the old serialized format! Should be done with migration! plantSpecies = EcoSim.AllSpecies.OfType <PlantSpecies>().First(species => species.Name == plantBlockData.PlantType.Name); } if (plantSpecies == null) { return; } Plant plant = EcoSim.PlantSim.SpawnPlant(plantSpecies, position, true); plant.YieldPercent = plantBlockData.YieldPercent; plant.Dead = plantBlockData.Dead; plant.DeadType = plantBlockData.DeadType; plant.GrowthPercent = plantBlockData.GrowthPercent; plant.DeathTime = plantBlockData.DeathTime; plant.Tended = plantBlockData.Tended; }
public static void RestoreWorldObjectBlock(Type type, Vector3i position, IWorldEditBlockData blockData, UserSession session) { if (blockData == null) { return; } WorldEditWorldObjectBlockData worldObjectBlockData = (WorldEditWorldObjectBlockData)blockData; ClearWorldObjectPlace(worldObjectBlockData.WorldObjectType, position, worldObjectBlockData.Rotation, session); WorldObject worldObject = null; try { worldObject = WorldObjectManager.ForceAdd(worldObjectBlockData.WorldObjectType, session.User, position, worldObjectBlockData.Rotation, true); } catch (Exception e) { Log.WriteException(e); } if (worldObject == null) { Log.WriteErrorLineLoc($"Unable spawn WorldObject {worldObjectBlockData.WorldObjectType} at {position}"); return; } if (worldObject.HasComponent <StorageComponent>() && worldObjectBlockData.Components.ContainsKey(typeof(StorageComponent))) { StorageComponent storageComponent = worldObject.GetComponent <StorageComponent>(); List <InventoryStack> inventoryStacks; object component = worldObjectBlockData.Components[typeof(StorageComponent)]; if (component is JArray) { JArray jArray = (JArray)component; inventoryStacks = jArray.ToObject <List <InventoryStack> >(); } else { inventoryStacks = (List <InventoryStack>)component; } foreach (InventoryStack stack in inventoryStacks) { if (stack.ItemType == null) { continue; } Result result = storageComponent.Inventory.TryAddItems(stack.ItemType, stack.Quantity); if (result.Failed) { session.Player.ErrorLocStr(result.Message.Trim()); try { storageComponent.Inventory.AddItems(stack.GetItemStack()); } catch (InvalidOperationException) { /*Already show error to user*/ } } } } if (worldObject.HasComponent <CustomTextComponent>() && worldObjectBlockData.Components.ContainsKey(typeof(CustomTextComponent))) { CustomTextComponent textComponent = worldObject.GetComponent <CustomTextComponent>(); textComponent.TextData.Text = (string)worldObjectBlockData.Components[typeof(CustomTextComponent)]; } }
public void RotateBlock(AffineTransform transform, float degrees, float radians) { this.Position = transform.Apply(this.Position); if (this.IsWorldObjectBlock()) { WorldEditWorldObjectBlockData worldObjectBlockData = (WorldEditWorldObjectBlockData)this.BlockData; worldObjectBlockData.SetRotation(worldObjectBlockData.Rotation * QuaternionUtils.FromAxisAngle(Vector3.Up, radians)); this.BlockData = worldObjectBlockData; } else if (!this.IsPlantBlock() && !this.BlockType.Equals(typeof(EmptyBlock))) { if (BlockUtils.HasRotatedVariants(this.BlockType, out Type[] variants))