public async Task <bool> EquipItemAsync(Item item, bool ignoreAllChecks = false) { var itemType = (ItemType)(item.ItemComponent.ItemType ?? (int)ItemType.Invalid); if (!ignoreAllChecks) { if (GameObject is Player player) { if (!player.GetComponent <ModularBuilderComponent>().IsBuilding) { if (itemType == ItemType.Model || itemType == ItemType.LootModel || itemType == ItemType.Vehicle || item.Lot == 6086) { return(false); } } } } await OnEquipped.InvokeAsync(item); await MountItemAsync(item.Id); GameObject.Serialize(GameObject); return(true); }
public Item Equip(Item equippable, EquipmentSlot slot) { if (equippable.SlotKind != slot.Kind || !Slots.Contains(slot)) { return(null); } Item previousItem = null; if (SlotsInUse.Contains(slot)) { previousItem = _equipped[slot]; } _equipped[slot] = equippable; SlotsInUse.Add(slot); OnEquipped?.Invoke(slot, equippable); if (previousItem != null) { OnUnequipped?.Invoke(slot, previousItem); } return(previousItem); }
/// <summary> /// Gives the slot a reference to a weapon. Can't equip a weapon if already holding one. /// </summary> /// <param name="weapon"></param> /// <returns>Returns whether the contents of the slot changed.</returns> public bool Equip(IWeapon weapon) { if (!IsEmpty || weapon == null) { return(false); } Weapon = weapon; OnEquipped?.Invoke(weapon); return(true); }
protected InventoryComponent() { Listen(OnDestroyed, () => { OnEquipped.Clear(); OnUnEquipped.Clear(); }); Listen(OnStart, () => { using var cdClient = new CdClientContext(); var component = cdClient.ComponentsRegistryTable.FirstOrDefault(c => c.Id == GameObject.Lot && c.Componenttype == (int)ComponentId.InventoryComponent); var items = cdClient.InventoryComponentTable.Where(i => i.Id == component.Componentid).ToArray(); Items = new Dictionary <EquipLocation, InventoryItem>(); foreach (var item in items) { var cdClientObject = cdClient.ObjectsTable.FirstOrDefault( o => o.Id == item.Itemid ); var itemRegistryEntry = cdClient.ComponentsRegistryTable.FirstOrDefault( r => r.Id == item.Itemid && r.Componenttype == 11 ); if (cdClientObject == default || itemRegistryEntry == default) { Logger.Error($"{item.Itemid} is not a valid item"); continue; } var itemComponent = cdClient.ItemComponentTable.First( i => i.Id == itemRegistryEntry.Componentid ); Debug.Assert(item.Itemid != null, "item.Itemid != null"); Debug.Assert(item.Count != null, "item.Count != null"); Items.TryAdd(itemComponent.EquipLocation, new InventoryItem { InventoryItemId = IdUtilities.GenerateObjectId(), Count = (long)item.Count, LOT = (int)item.Itemid, Slot = -1, InventoryType = -1 }); } }); }
//equipment public void SetEquipment(Equipable equipable) { if (equipable == null) { Debug.LogWarning("Equipping null equipable!\n\n" + new NullReferenceException().StackTrace); return; } if (this.GetEquipment(equipable.slot) != null) { Unequip(equipable.slot); } if (equipable is Holdable holdable) { //if we're equipping something that requires both hands, unequip other hand if (holdable.requiresBothHands) { Unequip(equipable.slot == EquipSlot.LeftHandItem ? EquipSlot.RightHandItem : EquipSlot.LeftHandItem); } //if we're equipping something that doesn't require both hands, check if we already have something //in the other hand that requires both hands, and unequip it else { Holdable otherHand = (Holdable)GetEquipment(equipable.slot == EquipSlot.LeftHandItem ? EquipSlot.RightHandItem : EquipSlot.LeftHandItem); //if true if (otherHand != null && otherHand.requiresBothHands) { Unequip(otherHand.slot); } } } _equipment[(int)equipable.slot] = equipable; _equipment[(int)equipable.slot]?.OnEquip(); OnEquipped?.Invoke(equipable); }
public async Task EquipItemAsync(Item item, bool ignoreAllChecks = false) { if (item?.InventoryItem == null) { Logger.Error($"{item} is not a valid item"); return; } var itemType = (ItemType)(item.ItemComponent.ItemType ?? (int)ItemType.Invalid); if (!ignoreAllChecks) { if (!As <Player>().GetComponent <ModularBuilderComponent>().IsBuilding) { if (itemType == ItemType.Model || itemType == ItemType.LootModel || itemType == ItemType.Vehicle || item.Lot == 6086) { return; } } } /* * Equip proxies */ var proxies = await GenerateProxyItemsAsync(item); if (proxies?.Length > 0) { ProxyItems[item.ObjectId] = proxies; } Logger.Debug($"Equipping {item}"); item.Equipped = true; var items = Items.Select(i => (i.Key, i.Value)).ToArray(); foreach (var(equipLocation, value) in items) { if (!equipLocation.Equals(item.ItemComponent.EquipLocation)) { continue; } var manager = GameObject.GetComponent <InventoryManagerComponent>(); if (manager != default) { var oldItem = manager.FindItem(value.InventoryItemId); await UnEquipItemAsync(oldItem); } else { await UnEquipItemAsync(value.InventoryItemId); } } await OnEquipped.InvokeAsync(item); Items[item.ItemComponent.EquipLocation] = item.InventoryItem; await ChangeEquippedSateOnPlayerAsync(item.ObjectId, true); GameObject.Serialize(GameObject); }