forked from aedenthorn/ValheimMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBepInExPlugin.cs
138 lines (126 loc) · 7.04 KB
/
BepInExPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System.Reflection;
using UnityEngine;
namespace BuildRestrictionTweaks
{
[BepInPlugin("aedenthorn.BuildRestrictionTweaks", "Build Restriction Tweaks", "0.6.0")]
public partial class BepInExPlugin : BaseUnityPlugin
{
private enum PlacementStatus
{
Valid,
Invalid,
BlockedbyPlayer,
NoBuildZone,
PrivateZone,
MoreSpace,
NoTeleportArea,
ExtensionMissingStation,
WrongBiome,
NeedCultivated,
NeedDirt,
NotInDungeon
}
private static readonly bool isDebug = true;
public static ConfigEntry<string> modKey;
public static ConfigEntry<bool> modEnabled;
public static ConfigEntry<int> nexusID;
public static ConfigEntry<bool> alwaysValid;
public static ConfigEntry<bool> ignoreInvalid;
public static ConfigEntry<bool> ignoreBlockedbyPlayer;
public static ConfigEntry<bool> ignoreBuildZone;
public static ConfigEntry<bool> ignoreSpaceRestrictions;
public static ConfigEntry<bool> ignoreTeleportAreaRestrictions;
public static ConfigEntry<bool> ignoreMissingStation;
public static ConfigEntry<bool> ignoreMissingStationExtension;
public static ConfigEntry<bool> ignoreBiomeRestrictions;
public static ConfigEntry<bool> ignoreCultivationRestrictions;
public static ConfigEntry<bool> ignoreDirtRestrictions;
public static ConfigEntry<bool> ignoreDungeonRestrictions;
private static BepInExPlugin context;
public static GameObject craftingStationObject;
public static void Dbgl(string str = "", bool pref = true)
{
if (isDebug)
Debug.Log((pref ? typeof(BepInExPlugin).Namespace + " " : "") + str);
}
private void Awake()
{
context = this;
modEnabled = Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
nexusID = Config.Bind<int>("General", "NexusID", 1606, "Nexus mod ID for updates");
alwaysValid = Config.Bind<bool>("Options", "AlwaysValid", false, "Remove all build restrictions.");
ignoreBlockedbyPlayer = Config.Bind<bool>("Options", "ignoreBlockedbyPlayer", false, "Ignore player blocking build.");
ignoreInvalid = Config.Bind<bool>("Options", "IgnoreInvalid", false, "Prevent misc build restrictions.");
ignoreBuildZone = Config.Bind<bool>("Options", "ignoreBuildZone", false, "Ignore zone restrictions.");
ignoreSpaceRestrictions = Config.Bind<bool>("Options", "ignoreSpaceRestrictions", false, "Ignore space restrictions.");
ignoreTeleportAreaRestrictions = Config.Bind<bool>("Options", "ignoreTeleportAreaRestrictions", false, "Ignore teleport area restrictions.");
ignoreMissingStationExtension = Config.Bind<bool>("Options", "ignoreMissingStationExtension", false, "Ignore missing station extension.");
ignoreMissingStation = Config.Bind<bool>("Options", "ignoreMissingStation", false, "Ignore missing station.");
ignoreBiomeRestrictions = Config.Bind<bool>("Options", "ignoreBiomeRestrictions", false, "Ignore biome restrictions.");
ignoreCultivationRestrictions = Config.Bind<bool>("Options", "ignoreCultivationRestrictions", false, "Ignore need for cultivated ground.");
ignoreDirtRestrictions = Config.Bind<bool>("Options", "ignoreDirtRestrictions", false, "Ignore need for dirt.");
ignoreDungeonRestrictions = Config.Bind<bool>("Options", "ignoreDungeonRestrictions", false, "Ignore indoor restrictions.");
nexusID.Value = 1606;
if (!modEnabled.Value)
return;
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), MetadataHelper.GetMetadata(this).GUID );
}
[HarmonyPatch(typeof(Location), nameof(Location.IsInsideNoBuildLocation))]
static class Location_IsInsideNoBuildLocation_Patch
{
static void Postfix(ref bool __result)
{
if (!modEnabled.Value || (!ignoreBuildZone.Value && !alwaysValid.Value))
return;
__result = false;
}
}
[HarmonyPatch(typeof(CraftingStation), "HaveBuildStationInRange")]
static class CraftingStation_HaveBuildStationInRange_Patch
{
static void Postfix(ref CraftingStation __result, string name)
{
if (!modEnabled.Value || (!ignoreMissingStation.Value && !alwaysValid.Value) || __result != null)
return;
if (craftingStationObject)
__result = craftingStationObject.GetComponent<CraftingStation>();
else
{
craftingStationObject = new GameObject();
DontDestroyOnLoad(craftingStationObject);
__result = craftingStationObject.AddComponent<CraftingStation>();
}
}
}
[HarmonyPatch(typeof(Player), "UpdatePlacementGhost")]
static class Player_UpdatePlacementGhost_Patch
{
static void Postfix(Player __instance, GameObject ___m_placementGhost)
{
if (!modEnabled.Value || ___m_placementGhost == null)
return;
PlacementStatus placementStatus = (PlacementStatus)(int)AccessTools.Field(typeof(Player), "m_placementStatus").GetValue(__instance);
if (
(placementStatus != PlacementStatus.Valid && placementStatus != PlacementStatus.PrivateZone && alwaysValid.Value)
|| (placementStatus == PlacementStatus.Invalid && ignoreInvalid.Value)
|| (placementStatus == PlacementStatus.BlockedbyPlayer && ignoreBlockedbyPlayer.Value)
|| (placementStatus == PlacementStatus.NoBuildZone && ignoreBuildZone.Value)
|| (placementStatus == PlacementStatus.MoreSpace && ignoreSpaceRestrictions.Value)
|| (placementStatus == PlacementStatus.NoTeleportArea && ignoreTeleportAreaRestrictions.Value)
|| (placementStatus == PlacementStatus.ExtensionMissingStation && ignoreMissingStationExtension.Value)
|| (placementStatus == PlacementStatus.WrongBiome && ignoreBiomeRestrictions.Value)
|| (placementStatus == PlacementStatus.NeedCultivated && ignoreCultivationRestrictions.Value)
|| (placementStatus == PlacementStatus.NeedDirt && ignoreDirtRestrictions.Value)
|| (placementStatus == PlacementStatus.NotInDungeon && ignoreDungeonRestrictions.Value)
)
{
AccessTools.Field(typeof(Player), "m_placementStatus").SetValue(__instance, (int)PlacementStatus.Valid);
AccessTools.Method(typeof(Player), "SetPlacementGhostValid").Invoke(__instance, new object[] { true });
}
}
}
}
}