From 0a492aaf9998e2049b0157942962dcd71b2a3a61 Mon Sep 17 00:00:00 2001 From: ShimmyMySherbet Date: Mon, 5 Dec 2022 00:26:16 +1000 Subject: [PATCH] Disable vault cache and improve restriction enforcement --- SherbetVaults/Database/Tables/VaultItemsTable.cs | 14 +++++++++----- SherbetVaults/Models/Config/Translations.cs | 5 +++-- SherbetVaults/Models/Data/VaultItems.cs | 8 +++++++- SherbetVaults/Models/RestrictionTool.cs | 2 -- SherbetVaults/Models/VaultManager.cs | 16 ++++++++-------- .../Models/VaultStoreDeniedException.cs | 11 +++++++++++ SherbetVaults/SherbetVaults.csproj | 1 + 7 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 SherbetVaults/Models/VaultStoreDeniedException.cs diff --git a/SherbetVaults/Database/Tables/VaultItemsTable.cs b/SherbetVaults/Database/Tables/VaultItemsTable.cs index 2856f34..86a59c3 100644 --- a/SherbetVaults/Database/Tables/VaultItemsTable.cs +++ b/SherbetVaults/Database/Tables/VaultItemsTable.cs @@ -19,14 +19,18 @@ public VaultItemsTable(SherbetVaultsPlugin plugin, string tableName) : base(tabl public async Task OpenVault(ulong playerID, string vaultID, VaultConfig config) { var items = await QueryAsync("SELECT * FROM @TABLE WHERE PlayerID=@0 AND VaultID=@1", playerID, vaultID); - var vi = new VaultItems(playerID, vaultID, Plugin); - vi.loadSize(config.Width, config.Height); + + var vaultItems = new VaultItems(playerID, vaultID, Plugin); + vaultItems.loadSize(config.Width, config.Height); + foreach (var item in items) { - vi.loadItem(item.X, item.Y, item.Rot, item.GetItem()); + vaultItems.loadItem(item.X, item.Y, item.Rot, item.GetItem()); } - vi.EnableSync(); - return vi; + + vaultItems.EnableSync(); + + return vaultItems; } public async Task AddItem(ulong playerID, string vaultID, Item item, byte rot, byte x, byte y) => diff --git a/SherbetVaults/Models/Config/Translations.cs b/SherbetVaults/Models/Config/Translations.cs index 3121c25..6adb31d 100644 --- a/SherbetVaults/Models/Config/Translations.cs +++ b/SherbetVaults/Models/Config/Translations.cs @@ -16,8 +16,9 @@ public partial class SherbetVaultsPlugin { "VaultAliases_Set", "[color=cyan]Vault alias created: {0}➔{1}[/color]" }, { "VaultAliases_Removed", "[color=cyan]Removed alias {0}[/color]" }, { "VaultAliases_Remove_NotFound", "[color=cyan]No alias by that name found[/color]" }, - { "VaultAliases_List", "[color=cyan]Aliases: {1}[/color]" }, - { "Restrictions_Blacklisted", "[color=red]You cannot store that item in your vault[/color]" } + { "VaultAliases_List", "Aliases: {1}" }, + { "Restrictions_Blacklisted", "[color=red]You cannot store that item in your vault[/color]" }, + { "VaultAliases_Disabled", "[color=red]Vault aliases are disabled on this server[/color]" } }; } } \ No newline at end of file diff --git a/SherbetVaults/Models/Data/VaultItems.cs b/SherbetVaults/Models/Data/VaultItems.cs index b6e80dc..01129ad 100644 --- a/SherbetVaults/Models/Data/VaultItems.cs +++ b/SherbetVaults/Models/Data/VaultItems.cs @@ -1,5 +1,6 @@ using System.Threading; using System.Threading.Tasks; +using Rocket.Core.Logging; using RocketExtensions.Models; using RocketExtensions.Utilities; using RocketExtensions.Utilities.ShimmyMySherbet.Extensions; @@ -68,7 +69,12 @@ private void ItemAdded(byte page, byte index, ItemJar jar) items.Remove(jar); Player.Player.inventory.tryAddItem(jar.item, true, false); - return; + + Logger.Log($"({Player.DisplayName}) tried to store blacklisted item ({jar.item.id}) in vault {VaultID}."); + Logger.Log("Please disregard the following error message."); + + // Throw an error to prevent the item being stored client-side + throw new VaultStoreDeniedException(); } Database.Enqueue(async (table) => diff --git a/SherbetVaults/Models/RestrictionTool.cs b/SherbetVaults/Models/RestrictionTool.cs index 7bd02f6..2344e1a 100644 --- a/SherbetVaults/Models/RestrictionTool.cs +++ b/SherbetVaults/Models/RestrictionTool.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using Rocket.API; -using Rocket.Core.Logging; using RocketExtensions.Models; using SDG.Unturned; using SherbetVaults.Models.Config.Restrictions; @@ -38,7 +37,6 @@ public bool IsPermitted(ushort itemID, LDMPlayer player, out RestrictionGroup ma if (asset == null) { - Logger.LogWarning($"Player {player.PlayerID} tried to put an unrecognized item in their vault. Item ID: {itemID}"); return false; } diff --git a/SherbetVaults/Models/VaultManager.cs b/SherbetVaults/Models/VaultManager.cs index c45271b..a7b4013 100644 --- a/SherbetVaults/Models/VaultManager.cs +++ b/SherbetVaults/Models/VaultManager.cs @@ -20,14 +20,14 @@ public VaultManager(SherbetVaultsPlugin vaultsPlugin) public async Task GetVault(ulong playerID, string vaultID, bool allowCache = true) { - if (EnableCache && allowCache) - { - var cached = VaultsCache.GetStorage(playerID, vaultID); - if (cached != null) - { - return cached; - } - } + //if (EnableCache && allowCache) + //{ + // var cached = VaultsCache.GetStorage(playerID, vaultID); + // if (cached != null) + // { + // return cached; + // } + //} var vaultConfig = VaultsPlugin.VaultSelector.GetVaultConfig(vaultID); diff --git a/SherbetVaults/Models/VaultStoreDeniedException.cs b/SherbetVaults/Models/VaultStoreDeniedException.cs new file mode 100644 index 0000000..68739db --- /dev/null +++ b/SherbetVaults/Models/VaultStoreDeniedException.cs @@ -0,0 +1,11 @@ +using System; + +namespace SherbetVaults.Models +{ + public class VaultStoreDeniedException : Exception + { + public VaultStoreDeniedException() : base("A player attemped to store an item in their vault that is blacklisted") + { + } + } +} \ No newline at end of file diff --git a/SherbetVaults/SherbetVaults.csproj b/SherbetVaults/SherbetVaults.csproj index 6b48d73..5c1417b 100644 --- a/SherbetVaults/SherbetVaults.csproj +++ b/SherbetVaults/SherbetVaults.csproj @@ -442,6 +442,7 @@ +