Skip to content

Commit

Permalink
cleanup + delay implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RenderBr committed Feb 9, 2023
1 parent 6003c19 commit d904946
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
9 changes: 7 additions & 2 deletions Supplier/Api/SupplierApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ public class SupplierApi
{
public SupplierApi()
{
// this is done to see if a connection can even be made to auxiliary
// it will also force Auxiliary to auto-gen a config
StorageProvider.GetMongoCollection<InfiniteChest>("InfiniteChests");
Console.WriteLine("SupplierAPI has connected to the database successfully.");
}

public string? WorldName { get; set; }

#region Add Chest
public async void AddChest(Chest chest)
public async void AddChest(Chest chest, int delay)
{
// add the chest to our database, marking it as infinite
await IModel.CreateAsync(CreateRequest.Bson<InfiniteChest>(x =>
Expand All @@ -28,6 +30,9 @@ await IModel.CreateAsync(CreateRequest.Bson<InfiniteChest>(x =>
x.X = chest.x;
x.Y = chest.y;

// set the delay, will be in miliseconds
x.Delay = delay;

// assign chests world
x.World = WorldName;

Expand Down Expand Up @@ -60,7 +65,7 @@ public async Task<InfiniteChest> RetrieveChest(int _x, int _y)
#region Remove Chest
public async void RemoveChest(int _x, int _y)
{
await StorageProvider.GetMongoCollection<InfiniteChest>("InfiniteChests").Find(x => x.X == _x && x.Y == _y && (x.World == WorldName || string.IsNullOrEmpty(x.World))).First().DeleteAsync();
await StorageProvider.GetMongoCollection<InfiniteChest>("InfiniteChests").Find(x => x.X == _x && x.Y == _y && (x.World == WorldName || string.IsNullOrEmpty(x.World))).First().DeleteAsync();
}
#endregion
}
Expand Down
14 changes: 10 additions & 4 deletions Supplier/Commands/AdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class AdminCommands : TSModuleBase<TSCommandContext>
#region /infchest <subcmd> - manage infinite chests - tbc.admin
[Command("infchest")]
[Description("Command used to manage infinite chest.")]
public IResult InfChest(string sub = "")
public IResult InfChest(string sub = "", int arg = 0)
{
var plrState = Context.Player.GetPlayerOperationState();

// inform player if command is being used while a state is already set
if (plrState.InfChestAdd || plrState.InfChestAddBulk || plrState.InfChestDelete && sub != "help")
if (plrState.InfChestAdd || plrState.InfChestAddBulk || (plrState.InfChestDelete && sub != "help"))
{
Info("If an existing chest selection operation is currently unfulfilled, it will be overriden with the new request.");
}
Expand All @@ -26,12 +26,18 @@ public IResult InfChest(string sub = "")
// if /infchest add was executed
case "add":
{
// set the delay for the chest, if no args, will be set to zero (or no delay)
Context.Player.SetData<int>("delay", arg);

plrState.InfChestAdd = true;
return Success("Open a chest to make it infinite. Type /cancel to cancel.");
}
// if /infchest addbulk was executed
case "addbulk":
{
// set the delay for the chest, if no args, will be set to zero (or no delay)
Context.Player.SetData<int>("delay", arg);

plrState.InfChestAddBulk = true;
return Success("Open chests to make them infinite, type /cancel to stop.");
}
Expand All @@ -51,8 +57,8 @@ public IResult InfChest(string sub = "")
default:
{
Info("Help commands for /infchest:");
Info("/infchest add - allows the user to create an infinite chest");
Info("/infchest addbulk - allows the user to continuously create infinite chests until /cancel is used");
Info("/infchest add (seconds) - allows the user to create an infinite chest");
Info("/infchest addbulk (seconds) - allows the user to continuously create infinite chests until /cancel is used");
Info("/infchest del - deletes an infinite chest");
Info("/infchest delbulk - deletes infinite chests until /cancel is used");
return Info("/infchest help - shows this help message");
Expand Down
2 changes: 1 addition & 1 deletion Supplier/PlayerOperationState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

namespace Supplier;
public class PlayerOperationState
{
Expand Down Expand Up @@ -52,5 +51,6 @@ public void SetAllOperationStatesFalse()
_infChestAdd = false;
_infChestAddBulk = false;
_infChestDelete = false;
_infChestDelBulk = false;
}
}
21 changes: 16 additions & 5 deletions Supplier/Supplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using CSF.TShock;
using MongoDB.Driver;
using Supplier.Api;
using Supplier.Models;
using Supplier.Extensions;
using Supplier.Models;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
Expand All @@ -17,9 +17,9 @@ public class Supplier : TerrariaPlugin
{
#region Plugin Metadata
public override string Name => "Supplier";
public override string Description => "An infinite-chests successor, allows chests to be filled with items";
public override string Description => "An infinite-chests successor, allows chests to be re-filled with items";
public override string Author => "Average";
public override Version Version => new Version(1, 1);
public override Version Version => new Version(1, 2);
#endregion
private readonly TSCommandFramework _fx;

Expand Down Expand Up @@ -76,6 +76,11 @@ private async void OnItemChange(object sender, ChestItemEventArgs e) // called w
if (string.IsNullOrEmpty(entity.World))
entity.World = core.WorldName;

if (entity.Delay > 0) // <---- if the chest has a delay, wait for the delay to expire
{
await Task.Delay(entity.Delay);
}

// loop through each item slot in the chest
for (var i = 0; i < entity.Items.Count; i++)
{
Expand Down Expand Up @@ -123,15 +128,21 @@ private async void OnChestOpen(object sender, ChestOpenEventArgs e) // called wh
}

// check if chest already is infinite
var entity = await core.RetrieveChest(chest.x,chest.y);
var entity = await core.RetrieveChest(chest.x, chest.y);

//if already infinite,
if (entity != null) { player.SendErrorMessage("This chest is already infinite."); return; } // <---- inform the player and return

// make an attempt to add the chest
try
{
core.AddChest(chest);
int delay = 0;

// set the delay
if (player.GetData<int>("delay") != 0)
delay = player.GetData<int>("delay");

core.AddChest(chest, delay);
}
catch (Exception ex) // inform the player if for some reason it doesn't work (and send error to console)
{
Expand Down

0 comments on commit d904946

Please sign in to comment.