Skip to content

Commit

Permalink
Fix issue when WE do not give back paid items
Browse files Browse the repository at this point in the history
Fix problem with World Edit do not give back paid items when delete blocks or objects from the world
  • Loading branch information
Tavren committed Jan 5, 2025
1 parent a1f27f3 commit 870a2df
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
36 changes: 34 additions & 2 deletions EcoWorldEdit/StrangeItemProtection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using Eco.Core.Utils;
using Eco.Core.Items;
using Eco.Core.Utils;
using Eco.Gameplay.Items;
using Eco.Gameplay.Objects;
using Eco.Gameplay.Players;
using Eco.Gameplay.StrangeCloudGameplay;
using Eco.Gameplay.Systems.EcoMarketplace;
using Eco.Shared;
using Eco.Shared.Utils;
using Eco.World.Blocks;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -15,7 +19,7 @@ namespace Eco.Mods.WorldEdit
{
internal class StrangeItemProtection : Singleton<StrangeItemProtection>
{

//TODO: Maybe in future that will be used for made cache
public void Initialize() { }

public static Result CanCreateBlock(User user, Type blockType)
Expand Down Expand Up @@ -64,5 +68,33 @@ public static void IncrementUsedItem(User user, Item item, int amount)
currenAmount += amount;
user.StrangeItemManagement.TypeToCountCollected[item.Type] = currenAmount;
}

public static void DecrementUsed(User user, Block block)
{
if(block is WorldObjectBlock worldObjectBlock)
{
WorldObject worldObject = worldObjectBlock.WorldObjectHandle.Object;
Item creatingItem = WorldObjectItem.GetCreatingItemTemplateFromType(worldObject.GetType());
DecrementUsedItem(user, creatingItem);
}
else
{
if (BlockItem.FirstCreatingItem(block.GetType()) is { } item && item.IsPaidItem())
{
DecrementUsedItem(user, item);
}
}

}

public static void DecrementUsedItem(User user, Item item) => DecrementUsedItem(user, item, 1);
public static void DecrementUsedItem(User user, Item item, int amount)
{
if (!item.IsPaidItem()) return;
//TODO: THIS IS NOT THREADSAFE!!!
int currenAmount = user.StrangeItemManagement.TypeToCountCollected.GetOr(item.Type, 0);
currenAmount -= amount;
user.StrangeItemManagement.TypeToCountCollected[item.Type] = Mathf.Max(currenAmount, 0);
}
}
}
7 changes: 3 additions & 4 deletions EcoWorldEdit/WorldEditBlockManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public static void RestorePlantBlock(Type type, Vector3i position, IWorldEditBlo
/// <summary>Directly delete or set block WITHOUT safety checks. From ECO v.11 follow the restrictions against paid content</summary>
protected void SetBlockInternal(Type type, Vector3i position)
{
if (type is null) { World.DeleteBlock(position); } else {
if (type is null) { this.ClearPosition(position, true); } else {
Result result = StrangeItemProtection.CanCreateBlock(this._userSession.User, type);
if (result.Failed)
{
Expand All @@ -267,8 +267,9 @@ protected void SetBlockInternal(Type type, Vector3i position)
private void ClearPosition(Vector3i position, bool delete = false)
{
Block block = World.GetBlock(position);
if (block is EmptyBlock || block is ImpenetrableStoneBlock) return;

if (block is EmptyBlock) return;
StrangeItemProtection.DecrementUsed(this._userSession.User, block);

switch (block)
{
Expand All @@ -283,8 +284,6 @@ private void ClearPosition(Vector3i position, bool delete = false)
case WorldObjectBlock worldObjectBlock:
worldObjectBlock.WorldObjectHandle.Object.Destroy();
break;
case ImpenetrableStoneBlock _:
return;
case PlantBlock plantBlock:
case TreeBlock treeBlock:
Plant plant = EcoSim.PlantSim.GetPlant(position);
Expand Down

0 comments on commit 870a2df

Please sign in to comment.