Skip to content
This repository was archived by the owner on Oct 16, 2022. It is now read-only.

Commit 22c7246

Browse files
committed
Added /sellbox command
1 parent 9dfbd70 commit 22c7246

17 files changed

+549
-13
lines changed

ShopsUI - OpenMod/API/Items/IItemShop.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using OpenMod.Unturned.Users;
1+
using OpenMod.API.Permissions;
2+
using OpenMod.API.Users;
3+
using OpenMod.Extensions.Games.Abstractions.Items;
24
using System.Threading.Tasks;
3-
using OpenMod.API.Permissions;
45

56
namespace ShopsUI.API.Items
67
{
@@ -9,10 +10,10 @@ public interface IItemShop
910
IItemShopData ShopData { get; }
1011

1112
bool CanBuy();
12-
Task<decimal> Buy(UnturnedUser user, int amount = 1);
13+
Task<decimal> Buy(IUser user, IInventory inventory, int amount = 1);
1314

1415
bool CanSell();
15-
Task<decimal> Sell(UnturnedUser user, int amount = 1);
16+
Task<decimal> Sell(IUser user, IInventory inventory, int amount = 1);
1617

1718
Task<bool> IsPermitted(IPermissionActor user);
1819
}

ShopsUI - OpenMod/API/Items/ItemShopExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ namespace ShopsUI.API.Items
55
{
66
public static class ItemShopExtensions
77
{
8+
public static Task<decimal> Buy(this IItemShop shop, UnturnedUser user, int amount = 1)
9+
{
10+
return shop.Buy(user, user.Player.Inventory, amount);
11+
}
12+
13+
public static Task<decimal> Sell(this IItemShop shop, UnturnedUser user, int amount = 1)
14+
{
15+
return shop.Sell(user, user.Player.Inventory, amount);
16+
}
17+
818
public static async Task<bool> TryBuy(this IItemShop shop, UnturnedUser user)
919
{
1020
if (shop.CanBuy()) return false;
1121

12-
await shop.Buy(user);
22+
await shop.Buy(user, user.Player.Inventory);
1323

1424
return true;
1525
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Cysharp.Threading.Tasks;
2+
using OpenMod.API.Ioc;
3+
using OpenMod.Unturned.Users;
4+
5+
namespace ShopsUI.API.SellBox
6+
{
7+
[Service]
8+
public interface ISellBoxManager
9+
{
10+
UniTask OpenSellBox(UnturnedUser user);
11+
}
12+
}

ShopsUI - OpenMod/Commands/Items/CBuy.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using OpenMod.API.Prioritization;
44
using OpenMod.Core.Commands;
55
using OpenMod.Unturned.Users;
6+
using ShopsUI.API.Items;
67
using System;
78

89
namespace ShopsUI.Commands.Items

ShopsUI - OpenMod/Commands/Items/CSell.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using OpenMod.API.Prioritization;
44
using OpenMod.Core.Commands;
55
using OpenMod.Unturned.Users;
6+
using ShopsUI.API.Items;
67
using System;
78

89
namespace ShopsUI.Commands.Items
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Cysharp.Threading.Tasks;
2+
using OpenMod.API.Prioritization;
3+
using OpenMod.Core.Commands;
4+
using OpenMod.Unturned.Commands;
5+
using OpenMod.Unturned.Users;
6+
using ShopsUI.API.SellBox;
7+
using System;
8+
9+
namespace ShopsUI.Commands.Items
10+
{
11+
[Command("sellbox", Priority = Priority.High)]
12+
[CommandDescription("Open a virtual inventory where you can quickly sell items.")]
13+
[CommandActor(typeof(UnturnedUser))]
14+
public class CSellBox : UnturnedCommand
15+
{
16+
private readonly ISellBoxManager _sellBoxManager;
17+
18+
public CSellBox(IServiceProvider serviceProvider,
19+
ISellBoxManager sellBoxManager) : base(serviceProvider)
20+
{
21+
_sellBoxManager = sellBoxManager;
22+
}
23+
24+
protected override async UniTask OnExecuteAsync()
25+
{
26+
var user = (UnturnedUser)Context.Actor;
27+
28+
await _sellBoxManager.OpenSellBox(user);
29+
}
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using OpenMod.Extensions.Games.Abstractions.Items;
2+
using SDG.Unturned;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
7+
namespace ShopsUI.SellBox.Inventory
8+
{
9+
public class SellBoxInventory : IInventory
10+
{
11+
public InteractableStorage Storage { get; }
12+
13+
public SellBoxInventory(InteractableStorage storage)
14+
{
15+
Storage = storage;
16+
}
17+
18+
public IReadOnlyCollection<IInventoryPage> Pages => new[] {new SellBoxInventoryPage(this)};
19+
20+
public bool IsFull => throw new NotImplementedException();
21+
22+
public int Count => Pages.Count;
23+
24+
public IEnumerator<IInventoryPage> GetEnumerator()
25+
{
26+
return Pages.GetEnumerator();
27+
}
28+
29+
IEnumerator IEnumerable.GetEnumerator()
30+
{
31+
return GetEnumerator();
32+
}
33+
}
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Cysharp.Threading.Tasks;
2+
using OpenMod.Extensions.Games.Abstractions.Items;
3+
using OpenMod.Unturned.Items;
4+
using SDG.Unturned;
5+
using System;
6+
using System.Threading.Tasks;
7+
8+
namespace ShopsUI.SellBox.Inventory
9+
{
10+
public class SellBoxInventoryItem : IInventoryItem
11+
{
12+
public SellBoxInventoryItem(SellBoxInventory inventory, ItemJar itemJar)
13+
{
14+
Inventory = inventory;
15+
ItemJar = itemJar;
16+
Item = new UnturnedItem(itemJar.item, DestroyAsync);
17+
}
18+
19+
public SellBoxInventory Inventory { get; }
20+
21+
public ItemJar ItemJar { get; }
22+
23+
IItem IItemInstance.Item => Item;
24+
25+
public UnturnedItem Item { get; }
26+
27+
public Task<bool> DestroyAsync()
28+
{
29+
async UniTask<bool> DestroyTask()
30+
{
31+
await UniTask.SwitchToMainThread();
32+
33+
var items = Inventory.Storage.items;
34+
35+
var index = items.items.IndexOf(ItemJar);
36+
37+
if (index < 0)
38+
{
39+
return false;
40+
}
41+
42+
items.removeItem((byte)index);
43+
44+
return true;
45+
}
46+
47+
return DestroyTask().AsTask();
48+
}
49+
50+
public Task DropAsync()
51+
{
52+
throw new NotImplementedException();
53+
}
54+
}
55+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using OpenMod.Extensions.Games.Abstractions.Items;
2+
using SDG.Unturned;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace ShopsUI.SellBox.Inventory
9+
{
10+
public class SellBoxInventoryPage : IInventoryPage
11+
{
12+
public SellBoxInventory Inventory { get; set; }
13+
14+
IInventory IInventoryPage.Inventory => Inventory;
15+
16+
public List<ItemJar> InventoryItems => Inventory.Storage.items.items;
17+
18+
19+
public SellBoxInventoryPage(SellBoxInventory inventory)
20+
{
21+
Inventory = inventory;
22+
}
23+
24+
public string Name => "Sell Box Storage";
25+
26+
public int Count => Items.Count;
27+
28+
public bool IsReadOnly => false;
29+
30+
public IReadOnlyCollection<IInventoryItem> Items =>
31+
InventoryItems.Select(itemJar => new SellBoxInventoryItem(Inventory, itemJar)).ToList();
32+
33+
public IEnumerator<IInventoryItem> GetEnumerator() => Items.GetEnumerator();
34+
35+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
36+
37+
public int Capacity => throw new NotImplementedException();
38+
39+
public bool IsFull => throw new NotImplementedException();
40+
}
41+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace ShopsUI.SellBox
4+
{
5+
[Serializable]
6+
public class SellBoxDimensions
7+
{
8+
public byte Width { get; set; } = 0;
9+
10+
public byte Height { get; set; } = 0;
11+
}
12+
}

0 commit comments

Comments
 (0)