public static void ConsumeItem(MyInventory inventory, IMyInventoryItem item, MyFixedPoint amount, long consumerEntityId = 0) { if (inventory == null || inventory.Owner == null) { return; } var msg = new ConsumeItemMsg() { OwnerEntityId = inventory.Owner.EntityId, InventoryId = inventory.InventoryIdx, ItemId = item.GetDefinitionId(), Amount = amount, ConsumerEntityId = consumerEntityId, }; Sync.Layer.SendMessageToServer(ref msg); }
public static void ConsumeItem(MyInventory inventory, IMyInventoryItem item, MyFixedPoint amount, long consumerEntityId = 0) { if(inventory == null || inventory.Owner == null) return; var msg = new ConsumeItemMsg() { OwnerEntityId = inventory.Owner.EntityId, InventoryId = inventory.InventoryIdx, ItemId = item.GetDefinitionId(), Amount = amount, ConsumerEntityId = consumerEntityId, }; Sync.Layer.SendMessageToServer(ref msg); }
private static void OnConsumeItem(ref ConsumeItemMsg msg, MyNetworkClient sender) { if (!MyEntities.EntityExists(msg.OwnerEntityId) || (msg.ConsumerEntityId != 0 && !MyEntities.EntityExists(msg.ConsumerEntityId))) return; var entity = MyEntities.GetEntityById(msg.OwnerEntityId); if (entity == null) return; var inventoryOwner = entity as IMyInventoryOwner; if (inventoryOwner == null) return; var inventory = inventoryOwner.GetInventory(msg.InventoryId); if (inventory == null) return; if (Sync.IsServer) { var existingAmount = inventory.GetItemAmount(msg.ItemId); if (existingAmount < msg.Amount) msg.Amount = existingAmount; } if (msg.ConsumerEntityId != 0) { entity = MyEntities.GetEntityById(msg.ConsumerEntityId); if (entity == null) return; } if(entity.Components != null) { var statComp = entity.Components.Get<MyEntityStatComponent>() as MyCharacterStatComponent; if(statComp != null) { var definition = MyDefinitionManager.Static.GetDefinition(msg.ItemId) as MyConsumableItemDefinition; statComp.Consume(msg.Amount, definition); var character = entity as MyCharacter; if (character != null) character.StartSecondarySound(definition.EatingSound, true); } } if (Sync.IsServer) { inventory.RemoveItemsOfType(msg.Amount, msg.ItemId); Sync.Layer.SendMessageToAll(ref msg); } }
private static void OnConsumeItem(ref ConsumeItemMsg msg, MyNetworkClient sender) { if (!MyEntities.EntityExists(msg.OwnerEntityId) || (msg.ConsumerEntityId != 0 && !MyEntities.EntityExists(msg.ConsumerEntityId))) { return; } var entity = MyEntities.GetEntityById(msg.OwnerEntityId); if (entity == null) { return; } var inventoryOwner = entity as IMyInventoryOwner; if (inventoryOwner == null) { return; } var inventory = inventoryOwner.GetInventory(msg.InventoryId); if (inventory == null) { return; } if (Sync.IsServer) { var existingAmount = inventory.GetItemAmount(msg.ItemId); if (existingAmount < msg.Amount) { msg.Amount = existingAmount; } } if (msg.ConsumerEntityId != 0) { entity = MyEntities.GetEntityById(msg.ConsumerEntityId); if (entity == null) { return; } } if (entity.Components != null) { var statComp = entity.Components.Get <MyEntityStatComponent>() as MyCharacterStatComponent; if (statComp != null) { var definition = MyDefinitionManager.Static.GetDefinition(msg.ItemId) as MyConsumableItemDefinition; statComp.Consume(msg.Amount, definition); var character = entity as MyCharacter; if (character != null) { character.StartSecondarySound(definition.EatingSound, true); } } } if (Sync.IsServer) { inventory.RemoveItemsOfType(msg.Amount, msg.ItemId); Sync.Layer.SendMessageToAll(ref msg); } }