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

178 lines
6.2 KiB
C#

namespace Harmony.WorldRebirth
{
public class WorldPatches
{
/*[HarmonyPatch(typeof(World))]
[HarmonyPatch("unloadEntity")]
public class unloadEntityPatch
{
public static bool Prefix(World __instance, Entity _e, EnumRemoveEntityReason _reason)
{
Log.Out("StackTrace: '{0}'", Environment.StackTrace);
return true;
}
}*/
[HarmonyPatch(typeof(World))]
[HarmonyPatch("CanPlaceBlockAt")]
public class CanPlaceBlockAtPatch
{
public static bool Prefix(World __instance, ref bool __result, Vector3i blockPos, PersistentPlayerData lpRelative, bool traderAllowed = false)
{
if (__instance == null || lpRelative == null)
{
return true;
}
EntityPlayer player = (EntityPlayer)__instance.GetEntity(lpRelative.EntityId);
if (player != null)
{
bool optionProtectTrader = RebirthVariables.customProtectTraderArea;
bool isWithinTraderArea = ((World)__instance).GetTraderAreaAt(blockPos) != null;
string blockName = player.inventory.holdingItem.GetItemName();
bool isAllowedBlock = false;
bool cannotPlace = isWithinTraderArea && optionProtectTrader;
//Log.Out("WorldPatches-CanPlaceBlockAt blockName: " + blockName);
//Log.Out("WorldPatches-CanPlaceBlockAt optionProtectTrader: " + optionProtectTrader);
//Log.Out("WorldPatches-CanPlaceBlockAt isWithinTraderArea: " + isWithinTraderArea);
//Log.Out("WorldPatches-CanPlaceBlockAt cannotPlace: " + cannotPlace);
if (cannotPlace && RebirthUtilities.IsBlockTraderAllowed(blockName))
{
isAllowedBlock = true;
}
//Log.Out("WorldPatches-CanPlaceBlockAt isAllowedBlock: " + isAllowedBlock);
//Log.Out("WorldPatches-CanPlaceBlockAt isAllowedBlock: " + isAllowedBlock);
if (isAllowedBlock)
{
__result = true;
return false;
}
}
return true;
}
}
[HarmonyPatch(typeof(World))]
[HarmonyPatch("CanPickupBlockAt")]
public class CanPickupBlockAtPatch
{
public static bool Prefix(World __instance, ref bool __result, Vector3i blockPos, PersistentPlayerData lpRelative)
{
if (__instance == null || lpRelative == null)
{
return true;
}
EntityPlayer player = (EntityPlayer)__instance.GetEntity(lpRelative.EntityId);
if (player != null)
{
bool optionProtectTrader = RebirthVariables.customProtectTraderArea;
bool isWithinTraderArea = ((World)__instance).GetTraderAreaAt(blockPos) != null;
BlockValue block = __instance.GetBlock(blockPos);
string blockName = block.Block.GetBlockName();
bool isAllowedBlock = RebirthUtilities.IsBlockTraderAllowed(blockName);
bool cannotPickUp = isWithinTraderArea && optionProtectTrader;
//Log.Out("WorldPatches-CanPickupBlockAt blockName: " + blockName);
//Log.Out("WorldPatches-CanPickupBlockAt isAllowedBlock: " + isAllowedBlock);
if (isAllowedBlock)
{
__result = true;
return false;
}
}
return true;
}
}
[HarmonyPatch(typeof(World))]
[HarmonyPatch("UnloadWorld")]
public class WorldUnloadWorld
{
static void Postfix()
{
if (RebirthManager.HasInstance)
{
RebirthManager.Cleanup();
}
/*if (RebirthManager.HasInstance)
{
RebirthManager.Cleanup();
}*/
}
}
[HarmonyPatch(typeof(World))]
[HarmonyPatch("IsWithinTraderArea")]
[HarmonyPatch(new Type[] { typeof(Vector3i) })]
public class IsWithinTraderAreaPatch
{
private static bool Prefix(World __instance, ref bool __result, Vector3i _worldBlockPos)
{
bool optionProtectTrader = RebirthVariables.customProtectTraderArea;
if (!optionProtectTrader)
{
__result = false;
return false;
}
bool isWithinTraderArea = ((World)__instance).GetTraderAreaAt(_worldBlockPos) != null;
BlockValue block = __instance.GetBlock(_worldBlockPos);
string blockName = block.Block.GetBlockName();
bool canDamageBlock = RebirthUtilities.IsBlockTraderAllowed(blockName);
if (canDamageBlock)
{
__result = false;
return false;
}
return true;
}
}
[HarmonyPatch(typeof(World))]
[HarmonyPatch("GetTileEntity")]
[HarmonyPatch(new Type[] { typeof(int) })]
public class GetTileEntityPatch
{
private static bool Prefix(World __instance, ref TileEntity __result, int _entityId)
{
Entity entity = __instance.GetEntity(_entityId);
if (entity == null)
{
__result = null;
return false;
}
if (entity is EntityAnimalChickenRebirth && entity.IsAlive())
{
//Log.Out("WorldPatches-GetTileEntity EntityAnimalChickenRebirth");
__result = ((EntityAnimalChickenRebirth)entity).tileEntityAnimalChickenRebirth;
return false;
}
return true;
}
}
}
}