Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AttributeSystem/StatAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public StatAttribute(AttributeDefinition definition, float baseValue)
{
get
{
if (this.Definition.MaximumValue.HasValue)
if (this.Definition?.MaximumValue.HasValue is true)
{
return Math.Min(this.Definition.MaximumValue.Value, this._statValue);
}
Expand Down
23 changes: 23 additions & 0 deletions src/Web/Shared/Components/Form/ItemStorageField.razor
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@
<button type="button" class="btn-info mb-1" @onclick="@this.OnCreateItemClickAsync" title="Create"><span class="oi oi-plus"></span></button>
@if (this._selectedItem is null)
{
<button type="button" class="btn-secondary mb-1 disabled" title="Duplicate"><span class="oi oi-layers"></span></button>
<button type="button" class="btn-warning mb-1 disabled" title="Delete"><span class="oi oi-trash"></span></button>
}
else
{
<button type="button" class="btn-secondary mb-1" @onclick="@this.OnDuplicateItemClickAsync" title="Duplicate"><span class="oi oi-layers"></span></button>
<button type="button" class="btn-warning mb-1" @onclick="this.OnItemDeleteClickAsync" title="Delete"><span class="oi oi-trash"></span></button>
}
</div>
Expand Down Expand Up @@ -192,4 +194,25 @@
this._selectedItem = item;
}
}

private void OnDuplicateItemClickAsync()
{
if (this._selectedItem is null || this.Value is not { } itemStorage)
{
return;
}

var duplicate = this.PersistenceContext.CreateNew<Item>();
duplicate.AssignValues(this._selectedItem);

// Place the duplicate in the next available slot after the source item
var occupiedSlots = itemStorage.Items.Select(i => i.ItemSlot).ToHashSet();
var nextSlot = Enumerable.Range(this._selectedItem.ItemSlot + 1, byte.MaxValue - this._selectedItem.ItemSlot)
.Select(i => (byte)i)
.FirstOrDefault(s => !occupiedSlots.Contains(s));
duplicate.ItemSlot = nextSlot;

itemStorage.Items.Add(duplicate);
this._selectedItem = duplicate;
}
}
27 changes: 15 additions & 12 deletions src/Web/Shared/Components/Form/ValueListWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,28 @@ public void CopyTo(TValue[] array, int arrayIndex)
/// <inheritdoc />
public bool Remove(TValue item)
{
if (this._innerList.Remove(item))
var wrapperIndex = this.FindIndex(v => Equals(v.Value, item));
if (wrapperIndex >= 0)
{
var wrapperIndex = this.FindIndex(v => Equals(v.Value, item));
if (wrapperIndex >= 0)
{
this[wrapperIndex].PropertyChanged -= this.OnValueChanged;
this.RemoveAt(wrapperIndex);
for (int i = wrapperIndex; i < this._innerList.Count; i++)
{
this[i].Index = i;
}
}

this.RemoveAt(wrapperIndex);
return true;
}

return false;
}

/// <inheritdoc cref="IList{T}.RemoveAt" />
public new void RemoveAt(int index)
{
this[index].PropertyChanged -= this.OnValueChanged;
base.RemoveAt(index);
this._innerList.RemoveAt(index);
for (int i = index; i < this.Count; i++)
{
this[i].Index = i;
}
}

/// <inheritdoc />
public bool IsReadOnly => this._innerList.IsReadOnly;

Expand Down