Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.84")]
[assembly: AssemblyFileVersion("0.1.84")]
[assembly: AssemblyVersion("0.1.85")]
[assembly: AssemblyFileVersion("0.1.85")]
3 changes: 2 additions & 1 deletion Source/Core/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace PraetorisClient
public class PraetorisClientPlugin : BaseUnityPlugin
{
private const string ModName = "PraetorisClient";
private const string ModVersion = "0.1.84";
private const string ModVersion = "0.1.85";
private const string Author = "warpalicious";
private const string ModGUID = Author + "." + ModName;
private const string EpicLootGuid = "randyknapp.mods.epicloot";
Expand Down Expand Up @@ -197,6 +197,7 @@ private void Update()
RpcTraceTelemetry.BackgroundUpdate();

SurtlingBoatFeature.Update();
ServerChestPlacement.Update();
ServerGuideFeature.ServerGuide.Update();
}

Expand Down
5 changes: 0 additions & 5 deletions Source/ServerChest/ServerChest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ internal static bool TryGetByInventory(Inventory inventory, out ServerChest serv
return InventoryOwners.TryGetValue(inventory, out serverChest);
}

internal static void ForgetInventory(Inventory inventory)
{
InventoryOwners.Remove(inventory);
}

internal static bool IsServerChestPrefab(ZDO zdo)
{
return zdo != null && zdo.GetPrefab() == PrefabName.GetStableHashCode();
Expand Down
4 changes: 4 additions & 0 deletions Source/ServerChest/ServerChestIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ internal static bool TryGetSenderIdentity(long sender, string requestedName, str
platformId = localPlatformId;
}
}
else
{
return false;
}

return !string.IsNullOrWhiteSpace(characterName) && !string.IsNullOrWhiteSpace(platformId);
}
Expand Down
12 changes: 0 additions & 12 deletions Source/ServerChest/ServerChestPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,6 @@ private static void Postfix(InventoryGui __instance)
return;
}

if (ServerChestPiece.TryRestoreVanillaWoodChest(currentContainer, out bool restoredVanillaWoodChest))
{
if (GridWasCompacted || restoredVanillaWoodChest)
{
RestoreGrid(containerGrid);
containerGrid.UpdateInventory(currentContainer.GetInventory(), null, null);
GridWasCompacted = false;
}

return;
}

if (!ServerChest.IsServerChest(currentContainer))
{
if (GridWasCompacted)
Expand Down
160 changes: 19 additions & 141 deletions Source/ServerChest/ServerChestPiece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ namespace PraetorisClient.ServerChestFeature
{
internal static class ServerChestPiece
{
internal const string BasePrefabName = "piece_chest_wood";
private const string BasePrefabName = "TreasureChest_dvergrtower";
private static bool _registered;
private static ContainerShape? _vanillaWoodChestShape;

internal static void Initialize()
{
Expand All @@ -33,24 +32,31 @@ private static void Register()
PieceConfig pieceConfig = new()
{
Name = "Server Chest",
Description = "Receives admin-delivered items for one registered player.",
Description = "A chest used to receive items from server admins. Limit one per player.",
PieceTable = PieceTables.Hammer,
Category = PieceCategories.Misc
Category = PieceCategories.Misc,
CraftingStation = "piece_workbench",
Requirements = new[] { new RequirementConfig("Wood", 10) }
};

_vanillaWoodChestShape ??= CaptureVanillaWoodChestShape();

CustomPiece customPiece = new(ServerChest.PrefabName, BasePrefabName, pieceConfig);
GameObject prefab = customPiece.PiecePrefab;
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(ServerChest.PrefabName, BasePrefabName);
if (prefab == null)
{
PraetorisClientPlugin.Log.LogError("Failed to create ServerChest prefab from " + BasePrefabName + ".");
return;
}

Piece buildPiece = prefab.GetComponent<Piece>() ?? prefab.AddComponent<Piece>();
buildPiece.m_canBeRemoved = true;
if (!Application.isBatchMode)
{
Sprite icon = RenderManager.Instance.Render(prefab);
if (icon != null)
buildPiece.m_icon = icon;
}
ConfigurePrefab(prefab);
CustomPiece customPiece = new(prefab, false, pieceConfig);
PieceManager.Instance.AddPiece(customPiece);
RestoreVanillaWoodChestPrefab();
_registered = true;
PrefabManager.OnVanillaPrefabsAvailable -= Register;
}
Expand All @@ -63,14 +69,16 @@ private static void ConfigurePrefab(GameObject prefab)
container.m_name = "Server Chest";
container.m_width = ServerChest.MaxColumns;
container.m_height = ServerChest.MaxRows;
container.m_defaultItems.m_drops.Clear();
container.m_defaultItems = new DropTable();
container.m_autoDestroyEmpty = false;
container.m_discoverStat = PlayerStatType.None;
}

Piece piece = prefab.GetComponent<Piece>();
if (piece != null)
{
piece.m_name = "Server Chest";
piece.m_description = "Receives admin-delivered items for one registered player.";
piece.m_description = "A chest used to receive items from server admins. Limit one per player.";
}

if (prefab.GetComponent<ServerChest>() == null)
Expand All @@ -79,135 +87,5 @@ private static void ConfigurePrefab(GameObject prefab)
}
}

internal static bool TryRestoreVanillaWoodChest(Container container, out bool restored)
{
restored = false;
if (!IsVanillaWoodChest(container))
{
return false;
}

ContainerShape? shape = _vanillaWoodChestShape;
if (shape == null)
{
return true;
}

restored |= RestoreContainerComponent(container, shape.Value);
Inventory inventory = container.GetInventory();
ServerChest serverChest = container.GetComponent<ServerChest>();
if (serverChest != null)
{
if (inventory != null)
{
ServerChest.ForgetInventory(inventory);
}

Object.Destroy(serverChest);
restored = true;
}

if (inventory == null || (inventory.GetWidth() == shape.Value.Width && inventory.GetHeight() == shape.Value.Height))
{
return true;
}

int capacity = shape.Value.Width * shape.Value.Height;
if (inventory.NrOfItems() > capacity)
{
PraetorisClientPlugin.Log.LogWarning("Leaving " + BasePrefabName + " at expanded size because it has more stacks than vanilla capacity.");
return true;
}

ServerChest.CompactInventory(inventory, shape.Value.Width);
ServerChest.ApplyInventoryShape(inventory, shape.Value.Width, shape.Value.Height);
restored = true;
return true;
}

private static ContainerShape? CaptureVanillaWoodChestShape()
{
GameObject basePrefab = PrefabManager.Instance.GetPrefab(BasePrefabName);
Container? container = basePrefab != null ? basePrefab.GetComponent<Container>() : null;
if (container == null)
{
PraetorisClientPlugin.Log.LogWarning("Unable to capture vanilla wood chest container shape.");
return null;
}

return new ContainerShape(container.m_name, container.m_width, container.m_height);
}

private static void RestoreVanillaWoodChestPrefab()
{
ContainerShape? shape = _vanillaWoodChestShape;
if (shape == null)
{
return;
}

GameObject basePrefab = PrefabManager.Instance.GetPrefab(BasePrefabName);
Container? container = basePrefab != null ? basePrefab.GetComponent<Container>() : null;
if (container == null)
{
return;
}

RestoreContainerComponent(container, shape.Value);
ServerChest serverChest = container.GetComponent<ServerChest>();
if (serverChest != null)
{
Object.Destroy(serverChest);
}
}

private static bool RestoreContainerComponent(Container container, ContainerShape shape)
{
bool restored = false;
if (container.m_name != shape.Name)
{
container.m_name = shape.Name;
restored = true;
}

if (container.m_width != shape.Width)
{
container.m_width = shape.Width;
restored = true;
}

if (container.m_height != shape.Height)
{
container.m_height = shape.Height;
restored = true;
}

return restored;
}

private static bool IsVanillaWoodChest(Container container)
{
if (container == null)
{
return false;
}

string objectName = container.gameObject.name;
return objectName == BasePrefabName || objectName.StartsWith(BasePrefabName + "(");
}

private readonly struct ContainerShape
{
internal ContainerShape(string name, int width, int height)
{
Name = name;
Width = width;
Height = height;
}

internal string Name { get; }
internal int Width { get; }
internal int Height { get; }
}
}
}
Loading
Loading