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

826 lines
34 KiB
C#

using Audio;
using Platform;
using System.Collections;
using System.Collections.Generic;
namespace Harmony.BlockPatches
{
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("OnBlockLoaded")]
public class OnBlockLoadedPatch
{
public static IEnumerator UpdateBlock(WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue)
{
yield return new WaitForSeconds(1f);
_world.SetBlockRPC(_clrIdx, _blockPos, _blockValue);
}
public static void Postfix(Block __instance, WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue)
{
if (_world.IsEditor() || GameUtils.IsWorldEditor() || GameUtils.IsPlaytesting())
{
return;
}
if (RebirthUtilities.ScenarioSkip())
{
if (_blockValue.Block is BlockPoweredLight || _blockValue.Block is BlockLight)
{
//Log.Out("BlockPatches-OnBlockLoaded IsServer: " + SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer);
bool _isOn = ((uint)_blockValue.meta & 2U) > 0U;
if (_isOn)
{
bool isWithinTraderArea = ((World)_world).GetTraderAreaAt(_blockPos) != null;
if (!isWithinTraderArea)
{
//Log.Out("BlockPatches-OnBlockLoaded IS ON blockLight: " + blockLight.blockName);
_blockValue.meta = 0;
//RebirthVariables.blockChanges.Add(new BlockChangeInfo(_clrIdx, _blockPos, _blockValue));
//_world.SetBlockRPC(_clrIdx, _blockPos, _blockValue);
GameManager.Instance.StartCoroutine(UpdateBlock(_world, _clrIdx, _blockPos, _blockValue));
}
}
}
/*else if (_blockValue.Block is BlockLight blockLight)
{
List<BlockChangeInfo> blockChanges = new List<BlockChangeInfo>();
_blockValue = blockLight.SetLightState((WorldBase)_world, _clrIdx, _blockPos, _blockValue, false);
blockChanges.Add(new BlockChangeInfo(_clrIdx, _blockPos, _blockValue));
if (blockChanges.Count > 0)
GameManager.Instance.StartCoroutine(UpdateBlocks(blockChanges));
}*/
}
}
}
/*[HarmonyPatch(typeof(Block))]
[HarmonyPatch("OnBlockAdded")]
public class OnBlockAddedPatch
{
public static bool Prefix(Block __instance, WorldBase _world, Chunk _chunk, Vector3i _blockPos, BlockValue _blockValue)
{
Log.Out("BlockPatches-OnBlockAdded START");
if (_blockValue.Block.Properties.Contains("BedrollRebirth"))
{
Log.Out("BlockPatches-OnBlockAdded 1");
if (!SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer)
{
Log.Out("BlockPatches-OnBlockAdded 2");
return false;
}
ulong tickRate = 20UL;
if (_blockValue.Block.Properties.Contains("TickRate"))
{
tickRate = (ulong)StringParsers.ParseFloat(__instance.Properties.Values["TickRate"], 0, -1, NumberStyles.Any) * 20UL;
Log.Out("BlockPatches-OnBlockAdded tickRate: " + tickRate);
return false;
}
Log.Out("BlockPatches-OnBlockAdded Schedule Update");
_world.GetWBT().AddScheduledBlockUpdate(_chunk.ClrIdx, _blockPos, __instance.blockID, tickRate);
}
return true;
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("UpdateTick")]
public class UpdateTickPatch
{
public static bool Prefix(Block __instance, ref bool __result, WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, bool _bRandomTick, ulong _ticksIfLoaded, GameRandom _rnd)
{
Log.Out("BlockPatches-UpdateTick START");
if (_blockValue.Block.Properties.Contains("BedrollRebirth"))
{
Log.Out("BlockPatches-UpdateTick 1");
if (!SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer)
{
Log.Out("BlockPatches-UpdateTick 2");
return false;
}
int minMax = 10;
int minHeight = 2;
List<Entity> entitiesInBounds = GameManager.Instance.World.GetEntitiesInBounds(typeof(EntityNPCRebirth), BoundsUtils.BoundsForMinMax(_blockPos.x - minMax, _blockPos.y - minHeight, _blockPos.z - minMax, _blockPos.x + minMax, _blockPos.y + minHeight, _blockPos.z + minMax), new List<Entity>());
Log.Out("BlockPatches-UpdateTick entitiesInBounds: " + entitiesInBounds.Count);
if (entitiesInBounds.Count > 0)
{
for (int x = 0; x < entitiesInBounds.Count; x++)
{
EntityNPCRebirth entity = (EntityNPCRebirth)entitiesInBounds[x];
if (entity != null)
{
Log.Out("BlockPatches-UpdateTick ADD BUFF TO entity: " + entity.EntityClass.entityClassName);
entity.Buffs.AddBuff("buffBedrollAOEEffect");
}
}
}
ulong tickRate = 20UL;
if (_blockValue.Block.Properties.Contains("TickRate"))
{
tickRate = (ulong)StringParsers.ParseFloat(__instance.Properties.Values["TickRate"], 0, -1, NumberStyles.Any) * 20UL;
Log.Out("BlockPatches-UpdateTick tickRate: " + tickRate);
return false;
}
Log.Out("BlockPatches-UpdateTick Schedule Update");
_world.GetWBT().AddScheduledBlockUpdate(_clrIdx, _blockPos, __instance.blockID, tickRate);
}
return true;
}
}*/
/*[HarmonyPatch(typeof(Block))]
[HarmonyPatch("GetBlockByName")]
public class GetBlockByNamePatch
{
public static bool Prefix(Block __instance, ref Block __result, string _blockname, bool _caseInsensitive)
{
if (_blockname == "cntWoodBurningStove_PickedUp")
{
_blockname = "campfire";
}
if (Block.nameToBlock == null)
{
__result = (Block)null;
return false;
}
Block blockByName;
if (_caseInsensitive)
Block.nameToBlockCaseInsensitive.TryGetValue(_blockname, out blockByName);
else
Block.nameToBlock.TryGetValue(_blockname, out blockByName);
__result = blockByName;
return false;
}
}*/
/*[HarmonyPatch(typeof(Block))]
[HarmonyPatch("OnBlockDestroyedByExplosion")]
public class OnBlockDestroyedByExplosionPatch
{
public static bool Prefix(Block __instance, ref Block.DestroyedResult __result, WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, int _playerThatStartedExpl)
{
Log.Out("BlockPatches-OnBlockDestroyedByExplosion START");
return true;
}
}*/
/*[HarmonyPatch(typeof(Block))]
[HarmonyPatch("IsMovementBlocked")]
[HarmonyPatch(new[] { typeof(IBlockAccess), typeof(Vector3i), typeof(BlockValue), typeof(BlockFace) })]
public class IsMovementBlockedPatch
{
public static bool Prefix(Block __instance, ref bool __result, IBlockAccess _world, Vector3i _blockPos, BlockValue _blockValue)
{
Log.Out("IsMovementBlocked A");
return true;
}
}*/
/*[HarmonyPatch(typeof(Block))]
[HarmonyPatch("OnEntityCollidedWithBlock")]
public class OnEntityCollidedWithBlockPatch
{
public static bool Prefix(Block __instance, ref bool __result, WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, Entity _entity)
{
Log.Out("BlockPatches-OnEntityCollidedWithBlock _entity: " + _entity.EntityClass.entityClassName);
if (_entity is EntityZombieSDX)
{
EntityZombieSDX entity = (EntityZombieSDX) _entity;
if (entity != null)
{
Log.Out("BlockPatches-OnEntityCollidedWithBlock Jumping: " + entity.Jumping);
}
}
return true;
}
}*/
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("CanPlaceBlockAt")]
public class CanPlaceBlockAtPatch
{
public static MethodInfo overlapsWithOtherBlock = AccessTools.Method(typeof(Block), "overlapsWithOtherBlock", new Type[] { typeof(WorldBase), typeof(int), typeof(Vector3i), typeof(BlockValue) });
public static bool Prefix(Block __instance, ref bool __result, WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, bool _bOmitCollideCheck = false)
{
bool optionProtectTrader = RebirthVariables.customProtectTraderArea;
bool isWithinTraderArea = ((World)_world).GetTraderAreaAt(_blockPos) != null;
string blockName = _blockValue.Block.GetBlockName();
bool cannotPlace = isWithinTraderArea && optionProtectTrader && !(blockName == "keystoneBlock");
//Log.Out("BlockPatches-CanPlaceBlockAt 1, optionProtectTrader: " + optionProtectTrader);
//Log.Out("BlockPatches-CanPlaceBlockAt 1, isWithinTraderArea: " + isWithinTraderArea);
//Log.Out("BlockPatches-CanPlaceBlockAt 1, blockName: " + blockName);
//Log.Out("BlockPatches-CanPlaceBlockAt 1, cannotPlace: " + cannotPlace);
if (_blockPos.y > 253)
{
//Log.Out("BlockPatches-CanPlaceBlockAt 2");
__result = false;
return false;
}
bool isAllowedBlock = RebirthUtilities.IsBlockTraderAllowed(blockName);
//Log.Out("BlockPatches-CanPlaceBlockAt 1, isAllowedBlock: " + isAllowedBlock);
if (!GameManager.Instance.IsEditMode() && cannotPlace)
{
//Log.Out("BlockPatches-CanPlaceBlockAt CANNOT PLACE");
if (isAllowedBlock)
{
//Log.Out("BlockPatches-CanPlaceBlockAt IS ALLOWED");
__result = true;
return false;
}
else
{
//Log.Out("BlockPatches-CanPlaceBlockAt IS NOT ALLOWED");
__result = false;
return false;
}
}
Block block = _blockValue.Block;
__result = (!block.isMultiBlock || _blockPos.y + block.multiBlockPos.dim.y < 254) && (GameManager.Instance.IsEditMode() || !block.bRestrictSubmergedPlacement || !__instance.IsUnderwater(_world, _blockPos, _blockValue)) && (GameManager.Instance.IsEditMode() || _bOmitCollideCheck || !(bool)overlapsWithOtherBlock.Invoke(__instance, new object[] { _world, _clrIdx, _blockPos, _blockValue }));
return false;
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("GetMapColor")]
public class GetMapColorPatch
{
public static bool Prefix(Block __instance, ref Color __result, BlockValue _blockValue, Vector3 _normal, int _yPos)
{
if (!GameManager.Instance.World.IsEditor() && !GameUtils.IsPlaytesting() && __instance.Properties.Values.ContainsKey("IgnoreMapColor"))
{
//Log.Out("BlockPatches-GetMapColor Block Name: " + __instance.GetBlockName());
string biomeName = RebirthUtilities.GetBiomeName(_normal);
//Log.Out("BlockPatches-GetMapColor Player Position: " + entityPlayerLocal.position);
//Log.Out("BlockPatches-GetMapColor _normalX: " + _normal.x);
//Log.Out("BlockPatches-GetMapColor _yPos: " + _yPos);
//Log.Out("BlockPatches-GetMapColor _normalZ: " + _normal.z);
//Log.Out("BlockPatches-GetMapColor _blockValue.parent: " + _blockValue.parent);
string mapColor = "";
//Log.Out("BlockPatches-GetMapColor biomeName: " + biomeName);
if (biomeName == "wasteland")
{
BlockValue ground = Block.GetBlockValue("terrDestroyedStone");
if (ground.Block.Properties.Values.ContainsKey("Map.Color"))
{
mapColor = ground.Block.Properties.Values["Map.Color"];
}
}
else if (biomeName == "desert")
{
BlockValue ground = Block.GetBlockValue("terrDesertGround");
if (ground.Block.Properties.Values.ContainsKey("Map.Color"))
{
mapColor = ground.Block.Properties.Values["Map.Color"];
}
}
else if (biomeName == "forest" || biomeName == "pine_forest")
{
BlockValue ground = Block.GetBlockValue("terrForestGround");
if (ground.Block.Properties.Values.ContainsKey("Map.Color"))
{
mapColor = ground.Block.Properties.Values["Map.Color"];
}
}
else if (biomeName == "burnt_forest")
{
BlockValue ground = Block.GetBlockValue("terrBurntForestGround");
if (ground.Block.Properties.Values.ContainsKey("Map.Color"))
{
mapColor = ground.Block.Properties.Values["Map.Color"];
}
}
else if (biomeName == "snow")
{
BlockValue ground = Block.GetBlockValue("terrSnow");
if (ground.Block.Properties.Values.ContainsKey("Map.Color"))
{
mapColor = ground.Block.Properties.Values["Map.Color"];
}
}
//Log.Out("BlockPatches-GetMapColor mapColor: " + mapColor);
if (mapColor.Trim().Length > 0)
{
__result = StringParsers.ParseColor32(mapColor);
}
return false;
}
return true;
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("DamageBlock")]
public class DamageBlockPatch
{
public static bool Prefix(Block __instance, ref int __result, WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, int _damagePoints, int _entityIdThatDamaged, ItemActionAttack.AttackHitInfo _attackHitInfo, bool _bUseHarvestTool, bool _bBypassMaxDamage)
{
PlatformUserIdentifierAbs getContainerOwner = null;
bool isOwner = false;
if (__instance.GetBlockName().Contains("farmPlotBlock"))
{
EntityPlayer player = _world.GetEntity(_entityIdThatDamaged) as EntityPlayer;
if (player != null)
{
if (player.inventory.holdingItem.GetItemName() == "meleeHandPlayer")
{
__result = 0;
return false;
}
}
}
bool optionProtectTrader = RebirthVariables.customProtectTraderArea;
bool isWithinTraderArea = ((World)_world).GetTraderAreaAt(_blockPos) != null;
if (!optionProtectTrader && isWithinTraderArea)
{
if (__instance is BlockWorkstation)
{
TileEntityWorkstation tileEntityWorkstation = _world.GetTileEntity(_clrIdx, _blockPos) as TileEntityWorkstation;
if (tileEntityWorkstation != null && tileEntityWorkstation.isPlayerPlaced)
{
return true;
//Log.Out("BlockPatches-DamageBlock tileEntityWorkstation.entityId: " + tileEntityWorkstation.entityId);
}
__result = 0;
return false;
}
}
if (__instance is BlockSecureLootSigned)
{
//Log.Out("BlockPatches-DamageBlock 1");
TileEntitySecureLootContainerSigned tileEntitySecureLootContainer = _world.GetTileEntity(_clrIdx, _blockPos) as TileEntitySecureLootContainerSigned;
getContainerOwner = tileEntitySecureLootContainer.GetOwner();
isOwner = tileEntitySecureLootContainer.IsOwner(PlatformManager.InternalLocalUserIdentifier);
}
if (__instance is BlockSecureLoot)
{
//Log.Out("BlockPatches-DamageBlock 2");
TileEntitySecureLootContainer tileEntitySecureLootContainer = _world.GetTileEntity(_clrIdx, _blockPos) as TileEntitySecureLootContainer;
getContainerOwner = tileEntitySecureLootContainer.GetOwner();
isOwner = tileEntitySecureLootContainer.IsOwner(PlatformManager.InternalLocalUserIdentifier);
}
if (getContainerOwner == null)
{
//Log.Out("BlockPatches-DamageBlock 3");
__result = __instance.OnBlockDamaged(_world, _clrIdx, _blockPos, _blockValue, _damagePoints, _entityIdThatDamaged, _attackHitInfo, _bUseHarvestTool, _bBypassMaxDamage, 0);
return false;
}
else
{
//Log.Out("BlockPatches-DamageBlock 4");
if (!isOwner)
{
//Log.Out("BlockPatches-DamageBlock 5");
global::EntityAlive entityAlive = _world.GetEntity(_entityIdThatDamaged) as global::EntityAlive;
if (entityAlive != null)
{
string entityClassName = entityAlive.EntityClass.entityClassName;
//Log.Out("DamageBlock:entityClassName: " + entityClassName);
if (entityClassName == "playerMale" || entityClassName == "playerFemale")
{
int @playerKillingMode = GameStats.GetInt(EnumGameStats.PlayerKillingMode);
//Log.Out("DamageBlock:EnumPlayerKillingMode-@playerKillingMode: " + @playerKillingMode);
if (@playerKillingMode == 0)
{
//Log.Out("DamageBlock:EnumPlayerKillingMode: No Killing");
__result = 0;
return false;
}
else
{
//Log.Out("DamageBlock:EnumPlayerKillingMode: Killing Allowed");
__result = __instance.OnBlockDamaged(_world, _clrIdx, _blockPos, _blockValue, _damagePoints, _entityIdThatDamaged, _attackHitInfo, _bUseHarvestTool, _bBypassMaxDamage, 0);
return false;
}
}
else
{
//Log.Out("BlockPatches-DamageBlock 5");
__result = __instance.OnBlockDamaged(_world, _clrIdx, _blockPos, _blockValue, _damagePoints, _entityIdThatDamaged, _attackHitInfo, _bUseHarvestTool, _bBypassMaxDamage, 0);
return false;
}
}
else
{
//Log.Out("BlockPatches-DamageBlock 6");
__result = __instance.OnBlockDamaged(_world, _clrIdx, _blockPos, _blockValue, _damagePoints, _entityIdThatDamaged, _attackHitInfo, _bUseHarvestTool, _bBypassMaxDamage, 0);
return false;
}
}
else
{
//Log.Out("BlockPatches-DamageBlock 7");
__result = __instance.OnBlockDamaged(_world, _clrIdx, _blockPos, _blockValue, _damagePoints, _entityIdThatDamaged, _attackHitInfo, _bUseHarvestTool, _bBypassMaxDamage, 0);
return false;
}
}
return true;
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("OnBlockDamaged")]
public class OnBlockDamagedPatch
{
public static bool Prefix(Block __instance, ref int __result, WorldBase _world, int _clrIdx, Vector3i _blockPos, BlockValue _blockValue, int _damagePoints, int _entityIdThatDamaged, bool _bUseHarvestTool, bool _bBypassMaxDamage, int _recDepth,
int ___Stage2Health
)
{
if (__instance.GetBlockName() == "radioHam")
{
bool isWithinTraderArea = ((World)_world).GetTraderAreaAt(_blockPos) != null;
if (isWithinTraderArea)
{
__result = 0;
return false;
}
}
if (__instance.GetBlockName().Contains("farmPlotBlock"))
{
ChunkCluster chunkCluster = _world.ChunkClusters[_clrIdx];
if (chunkCluster == null)
{
__result = 0;
}
if (__instance.isMultiBlock && _blockValue.ischild)
{
return true;
}
else
{
Block block2 = _blockValue.Block;
int damage = _blockValue.damage;
bool flag = damage >= block2.MaxDamage;
int num = damage + _damagePoints;
//Log.Out("BlockPatches-OnBlockDamaged damage: " + damage);
//Log.Out("BlockPatches-OnBlockDamaged num: " + num);
//Log.Out("BlockPatches-OnBlockDamaged _damagePoints: " + _damagePoints);
//Log.Out("BlockPatches-OnBlockDamaged block2.MaxDamage: " + block2.MaxDamage);
if (num > block2.MaxDamage)
{
//Log.Out("BlockPatches-OnBlockDamaged B");
EntityPlayer player = _world.GetEntity(_entityIdThatDamaged) as EntityPlayer;
if (player)
{
//Log.Out("BlockPatches-OnBlockDamaged C");
RebirthUtilities.addToPlayerBag(ItemClass.GetItem("farmPlotBlockVariantHelper"), player, 1);
}
}
}
}
if (!RebirthUtilities.IsHordeNight())
{
//Log.Out("BlockPatches-OnBlockDamaged 1");
if (!GameManager.IsDedicatedServer)
{
//Log.Out("BlockPatches-OnBlockDamaged 2");
//Log.Out("StackTrace: '{0}'", Environment.StackTrace);
Entity entity = _world.GetEntity(_entityIdThatDamaged);
if (entity is EntityPlayerLocal)
{
//Log.Out("BlockPatches-OnBlockDamaged 3");
EntityPlayerLocal playerLocal = (EntityPlayerLocal)entity;
if (playerLocal.inventory.holdingItem.GetItemName() == "meleeHandPlayer")
{
Log.Out("BlockPatches-OnBlockDamaged block: " + __instance.GetBlockName() + " / position: " + _blockPos);
//Log.Out("BlockPatches-OnBlockDamaged _blockPos: " + _blockPos);
}
}
}
//Log.Out("BlockPatches-OnBlockDamaged SurfaceCategory: " + __instance.blockMaterial.SurfaceCategory);
}
bool isDedicated = GameManager.IsDedicatedServer;
bool isClient = SingletonMonoBehaviour<ConnectionManager>.Instance.IsClient;
bool isSinglePlayer = SingletonMonoBehaviour<ConnectionManager>.Instance.IsSinglePlayer;
//Log.Out("BlockPatches-OnBlockDamaged isDedicated: " + isDedicated);
//Log.Out("BlockPatches-OnBlockDamaged isClient: " + isClient);
//Log.Out("BlockPatches-OnBlockDamaged isSinglePlayer: " + isSinglePlayer);
//Log.Out("BlockPatches-OnBlockDamaged GetBlockName: " + __instance.GetBlockName());
/*if (__instance.GetBlockName() == "cntGasPumpFull" && (isDedicated || (isClient && !isSinglePlayer)))
{
//Log.Out("BlockPatches-OnBlockDamaged 1");
Entity entity = _world.GetEntity(_entityIdThatDamaged);
if (entity is EntityPlayerLocal)
{
//Log.Out("BlockPatches-OnBlockDamaged 2");
EntityPlayerLocal playerLocal = (EntityPlayerLocal)entity;
if (playerLocal.inventory.holdingItem.HasAnyTags(FastTags<TagGroup.Global>.Parse("salvagingSkill")))
{
//Log.Out("BlockPatches-OnBlockDamaged 3");
__result = 0;
return false;
}
}
}*/
return true;
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("Init")]
public class InitPatch
{
public static void Postfix(Block __instance, ref BlockActivationCommand[] ___cmds)
{
bool isEditor = GameModeEditWorld.TypeName.Equals(GamePrefs.GetString(EnumGamePrefs.GameMode));
if (isEditor)
{
return;
}
Array.Resize<BlockActivationCommand>(ref ___cmds, 1);
//___cmds[0] = new BlockActivationCommand("cancel", "x", false, false);
___cmds[0] = new BlockActivationCommand("take", "hand", false, false);
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("HasBlockActivationCommands")]
public class HasBlockActivationCommandsPatch
{
public static bool Prefix(Block __instance, ref bool __result, WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, global::EntityAlive _entityFocusing,
bool ___CanPickup
)
{
__result = false;
bool preventPickup = false;
if (_blockValue.Block.Properties.Values.ContainsKey("PreventPickUp"))
{
StringParsers.TryParseBool(__instance.Properties.Values["PreventPickUp"], out preventPickup);
}
if (preventPickup)
{
__result = false;
return false;
}
//bool flag = _world.GetTileEntity(_clrIdx, _blockPos) is TileEntityLootContainer;
bool flag2 = ___CanPickup;
bool flag3 = __instance.Properties.Contains("TakeDelay");
if (EffectManager.GetValue(PassiveEffects.BlockPickup, null, 0f, _entityFocusing, null, _blockValue.Block.Tags, true, true, true, true, true, 1, false) > 0f)
{
flag2 = true;
}
__result = flag2 || flag3;
bool optionProtectTrader = RebirthVariables.customProtectTraderArea;
bool cantLoot = !optionProtectTrader && ((World)_world).IsWithinTraderArea(_blockPos);
string blockName = _blockValue.Block.GetBlockName();
if (RebirthUtilities.IsBlockTraderAllowed(blockName))
{
__result = true;
return false;
}
if (cantLoot)
{
__result = false;
}
//Log.Out("BlockPatches-HasBlockActivationCommands ___CanPickup: " + flag2);
//Log.Out("BlockPatches-HasBlockActivationCommands TakeDelay: " + flag3);
//Log.Out("BlockPatches-HasBlockActivationCommands __result: " + __result);
return false;
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("GetActivationText")]
public class GetActivationTextPatch
{
public static bool Prefix(Block __instance, ref string __result, WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, global::EntityAlive _entityFocusing,
bool ___CanPickup
)
{
//Log.Out("BlockPatches-GetActivationText START");
if (_world.IsEditor())
{
return true;
}
Block block = _blockValue.Block;
if (!___CanPickup && EffectManager.GetValue(PassiveEffects.BlockPickup, null, 0f, _entityFocusing, null, _blockValue.Block.Tags, true, true, true, true, true, 1, false) <= 0f)
{
//Log.Out("BlockPatches-GetActivationText 1");
__result = null;
return false;
}
if (!_world.CanPickupBlockAt(_blockPos, _world.GetGameManager().GetPersistentLocalPlayer()))
{
//Log.Out("BlockPatches-GetActivationText 2");
__result = null;
return false;
}
string key = block.GetBlockName();
if (!string.IsNullOrEmpty(block.PickedUpItemValue))
{
//Log.Out("BlockPatches-GetActivationText 3");
key = block.PickedUpItemValue;
}
else if (!string.IsNullOrEmpty(block.PickupTarget))
{
//Log.Out("BlockPatches-GetActivationText 4");
key = block.PickupTarget;
}
__result = string.Format(Localization.Get("pickupPrompt"), Localization.Get(key));
//Log.Out("BlockPatches-GetActivationText 5");
return false;
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("GetBlockActivationCommands")]
public class GetBlockActivationCommandsPatch
{
public static bool Prefix(Block __instance, ref BlockActivationCommand[] __result, WorldBase _world, BlockValue _blockValue, int _clrIdx, Vector3i _blockPos, global::EntityAlive _entityFocusing,
BlockActivationCommand[] ___cmds,
bool ___CanPickup
)
{
if (_world.IsEditor())
{
return true;
}
___cmds[0].enabled = false;
//___cmds[1].enabled = false;
bool flag3 = ___CanPickup;
if (EffectManager.GetValue(PassiveEffects.BlockPickup, null, 0f, _entityFocusing, null, _blockValue.Block.Tags, true, true, true, true, true, 1, false) > 0f)
{
//Log.Out("BlockPatches-GetBlockActivationCommands 1");
flag3 = true;
}
if (flag3)
{
//Log.Out("BlockPatches-GetBlockActivationCommands 2");
//___cmds[0].enabled = false;
___cmds[0].enabled = true;
}
if (__instance.Properties.Contains("TakeDelay"))
{
//Log.Out("BlockPatches-GetBlockActivationCommands 3");
___cmds[0].enabled = true;
//___cmds[1].enabled = true;
}
__result = ___cmds;
return false;
}
}
[HarmonyPatch(typeof(Block))]
[HarmonyPatch("OnBlockActivated")]
[HarmonyPatch(new Type[] { typeof(string), typeof(WorldBase), typeof(int), typeof(Vector3i), typeof(BlockValue), typeof(EntityPlayerLocal) })]
public class OnBlockActivatedPatch
{
public static void Postfix(Block __instance, string _commandName, WorldBase _world, int _cIdx, Vector3i _blockPos, BlockValue _blockValue, EntityPlayerLocal _player)
{
if (_world.IsEditor())
{
return;
}
float takeDelay = RebirthVariables.takeDelay;
string replacementBlockName = "";
int numBlocks = 1;
bool isReplacementItem = false;
if (((World)_world).IsWithinTraderArea(_blockPos))
{
bool optionProtectTrader = RebirthVariables.customProtectTraderArea;
string blockName = _blockValue.Block.GetBlockName();
bool isAllowed = false;
if (RebirthUtilities.IsBlockTraderAllowed(blockName))
{
isAllowed = true;
}
if (!isAllowed && optionProtectTrader)
{
Manager.PlayInsidePlayerHead("ui_denied");
GameManager.ShowTooltip(_player as EntityPlayerLocal, Localization.Get("ttBelongsToTrader"), string.Empty, "ui_denied", null);
return;
}
}
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);
}
bool preventTimer = false;
if (_blockValue.Block.Properties.Values.ContainsKey("PreventTimer"))
{
StringParsers.TryParseBool(__instance.Properties.Values["PreventTimer"], out preventTimer);
}
if (_commandName == "take" && __instance.Properties.Contains("TakeDelay") && !preventTimer)
{
//Log.Out("BlockPatches-GetBlockActivationCommands takeDelay: " + takeDelay);
//Log.Out("BlockPatches-GetBlockActivationCommands replacementBlockName: " + replacementBlockName);
//Log.Out("BlockPatches-GetBlockActivationCommands numBlocks: " + numBlocks);
//Log.Out("BlockPatches-GetBlockActivationCommands isReplacementItem: " + isReplacementItem);
RebirthUtilities.TakeItemWithTimer(_world, _cIdx, _blockPos, _blockValue, _player, takeDelay, null, replacementBlockName, numBlocks, isReplacementItem);
}
}
}
}