public static void RestoreBlockOffset(WorldEditBlock block, Vector3i offsetPos, UserSession session) { RestoreBlock(block, ApplyOffset(block.Position, offsetPos), session); }
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)]; } }
private static void ClearWorldObjectPlace(Type worldObjectType, Vector3i position, Quaternion rotation, UserSession session) { List <BlockOccupancy> blockOccupancies = WorldObject.GetOccupancy(worldObjectType); if (!blockOccupancies.Any(x => x.BlockType != null)) { return; } foreach (var blockOccupancy in blockOccupancies) { if (blockOccupancy.BlockType != null) { Vector3i worldPos = position + rotation.RotateVector(blockOccupancy.Offset).XYZi; if (!session.ExecutingCommand.PerformingUndo) { session.ExecutingCommand.AddBlockChangedEntry(worldPos); //Do not record changes when doing undo } ClearPosition(worldPos); } } }