Files
7d2dXG/Mods/zzz_REBIRTH__Utils/Harmony/Harmony_CarExplodeLoot.cs
Nathaniel Cosford 062dfab2cd Patched
2025-05-30 01:04:40 +09:30

141 lines
6.2 KiB
C#

using Audio;
namespace Harmony.BlockCarExplodeLootPatches
{
[HarmonyPatch(typeof(BlockCarExplodeLoot))]
[HarmonyPatch("Init")]
public class InitPatch
{
public static void Postfix(BlockCarExplodeLoot __instance, ref BlockActivationCommand[] ___cmds)
{
Array.Resize<BlockActivationCommand>(ref ___cmds, 2);
___cmds[0] = new BlockActivationCommand("Search", "search", false, false);
___cmds[1] = new BlockActivationCommand("take", "hand", false, false);
}
}
[HarmonyPatch(typeof(BlockCarExplodeLoot))]
[HarmonyPatch("HasBlockActivationCommands")]
public class HasBlockActivationCommandsPatch
{
public static void Postfix(BlockCarExplodeLoot __instance, ref bool __result, WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, global::EntityAlive _entityFocusing)
{
__result = RebirthUtilities.CanBeLooted(_blockPos, _blockValue, _clrIdx);
}
}
[HarmonyPatch(typeof(BlockCarExplodeLoot))]
[HarmonyPatch("GetBlockActivationCommands")]
public class GetBlockActivationCommandsPatch
{
public static void Postfix(BlockCarExplodeLoot __instance, ref BlockActivationCommand[] __result, WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, global::EntityAlive _entityFocusing,
BlockActivationCommand[] ___cmds
)
{
TileEntityLootContainer tileEntityLootContainer = _world.GetTileEntity(_clrIdx, _blockPos) as TileEntityLootContainer;
bool flag = tileEntityLootContainer == null;
BlockActivationCommand[] result;
if (flag)
{
result = new BlockActivationCommand[0];
}
else
{
//___cmds[0].enabled = true;
bool isEmpty = tileEntityLootContainer.IsEmpty();
bool bTouched = tileEntityLootContainer.bTouched;
//Log.Out("BlockPatches-GetBlockActivationCommands isEmpty: " + isEmpty);
//Log.Out("BlockPatches-GetBlockActivationCommands bTouched: " + bTouched);
___cmds[0].enabled = true;
___cmds[1].enabled = false;
if (__instance.Properties.Contains("FilterTags"))
{
string filterTags = "";
__instance.Properties.ParseString("FilterTags", ref filterTags);
//Log.Out("BlockPatches-GetBlockActivationCommands filterTags: " + filterTags);
if (!filterTags.Contains("SC_automotive"))
{
___cmds[1].enabled = isEmpty && bTouched;
}
}
//Log.Out("BlockPatches-GetBlockActivationCommands HAS TAKE DELAY");
}
__result = ___cmds;
}
}
[HarmonyPatch(typeof(BlockCarExplodeLoot))]
[HarmonyPatch("OnBlockActivated")]
[HarmonyPatch(new Type[] { typeof(string), typeof(WorldBase), typeof(int), typeof(Vector3i), typeof(BlockValue), typeof(EntityPlayerLocal) })]
public class OnBlockActivatedPatch
{
public static bool Prefix(BlockLoot __instance, ref bool __result, string _commandName, WorldBase _world, int _cIdx, Vector3i _blockPos, BlockValue _blockValue, EntityPlayerLocal _player)
{
bool canBeLooted = RebirthUtilities.CanBeLooted(_blockPos, _blockValue, _cIdx);
if (_commandName == "Search" && !RebirthUtilities.CanBeLooted(_blockPos, _blockValue, _cIdx))
{
Manager.PlayInsidePlayerHead("ui_denied");
GameManager.ShowTooltip(_player, Localization.Get("ttBelongsToTrader"), string.Empty, "ui_denied", null);
return false;
}
return true;
}
public static void Postfix(BlockCarExplodeLoot __instance, ref bool __result, string _commandName, WorldBase _world, int _cIdx, Vector3i _blockPos, BlockValue _blockValue, EntityPlayerLocal _player)
{
float takeDelay = RebirthVariables.takeDelay;
string replacementBlockName = "";
int numBlocks = 1;
bool isReplacementItem = false;
if (__instance.Properties.Contains("TakeDelay"))
{
__instance.Properties.ParseFloat("TakeDelay", ref takeDelay);
}
if (__instance.Properties.Contains("ReplacementBlockName"))
{
__instance.Properties.ParseString("ReplacementBlockName", ref replacementBlockName);
}
if (__instance.Properties.Contains("numBlocks"))
{
__instance.Properties.ParseInt("numBlocks", ref numBlocks);
}
if (__instance.Properties.Contains("isReplacementItem"))
{
__instance.Properties.ParseBool("isReplacementItem", ref isReplacementItem);
}
//Log.Out("BlockPatches-OnBlockActivated _commandName: " + _commandName);
if (_commandName == "take")
{
/*Log.Out("BlockPatches-OnBlockActivated takeDelay: " + takeDelay);
Log.Out("BlockPatches-OnBlockActivated replacementBlockName: " + replacementBlockName);
Log.Out("BlockPatches-OnBlockActivated numBlocks: " + numBlocks);
Log.Out("BlockPatches-OnBlockActivated isReplacementItem: " + isReplacementItem);*/
if (((World)_world).IsWithinTraderArea(_blockPos))
{
bool optionProtectTrader = RebirthVariables.customProtectTraderArea;
if (optionProtectTrader)
{
Manager.PlayInsidePlayerHead("ui_denied");
GameManager.ShowTooltip(_player, Localization.Get("ttBelongsToTrader"), string.Empty, "ui_denied", null);
return;
}
}
RebirthUtilities.TakeItemWithTimer(_world, _cIdx, _blockPos, _blockValue, _player, takeDelay, null, replacementBlockName, numBlocks, isReplacementItem);
__result = true;
}
}
}
}