Private
Public Access
1
0
Files
OrekiWoofsBeehives/OrekiWoofsBeehives/OrekiWoofsBeehivesModSystem.cs
2026-03-21 01:43:43 +01:00

139 lines
4.8 KiB
C#

using HarmonyLib;
using OrekiWoofsBeehives.Behaviors;
using OrekiWoofsBeehives.BlockEntities;
using OrekiWoofsBeehives.Blocks;
using OrekiWoofsBeehives.Utilities;
using System;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Server;
namespace OrekiWoofsBeehives;
public partial class OrekiWoofsBeehivesModSystem : ModSystem
{
private Harmony? _harmony;
private ICoreServerAPI? serverApi;
private ICoreAPI? api;
public static bool IsSteadyGreenhousesLoaded { get; private set; }
private MetaConfig metaConfig = new();
public BeehiveRegistry BeehiveRegistry { get; } = new();
public VanillaSkepRegistry VanillaSkepRegistry { get; } = new();
// Called on server and client
// Useful for registering block/entity classes on both sides
public override void Start(ICoreAPI api)
{
this.api = api;
IsSteadyGreenhousesLoaded = api.ModLoader.IsModEnabled("steadygreenhouses");
api.RegisterBlockBehaviorClass(nameof(BlockBehaviorBeehiveAffected), typeof(BlockBehaviorBeehiveAffected));
api.RegisterBlockEntityBehaviorClass(nameof(BlockEntityBehaviorBeehiveYieldMultiplier), typeof(BlockEntityBehaviorBeehiveYieldMultiplier));
api.RegisterBlockEntityBehaviorClass(nameof(BlockEntityBehaviorVanillaSkepSwarmTarget), typeof(BlockEntityBehaviorVanillaSkepSwarmTarget));
api.RegisterBlockClass(nameof(BlockBeeSwarm), typeof(BlockBeeSwarm));
api.RegisterBlockClass(nameof(BlockBeehiveFrame), typeof(BlockBeehiveFrame));
api.RegisterBlockClass(nameof(BlockReusableBeehive), typeof(BlockReusableBeehive));
api.RegisterBlockEntityClass(nameof(BlockEntityReusableBeehive), typeof(BlockEntityReusableBeehive));
api.RegisterBlockEntityClass(nameof(BlockEntityBeeSwarm), typeof(BlockEntityBeeSwarm));
api.Network.RegisterChannel(CONFIG_CHANNEL_NAME).RegisterMessageType<Config>();
_harmony = new Harmony("com.orekiwoof.beehives");
_harmony.PatchAllUncategorized();
if (api.ModLoader.GetMod("game").Info.Version.StartsWith("1.22"))
_harmony.PatchCategory("1.22");
if (api.ModLoader.GetMod("game").Info.Version.StartsWith("1.21"))
_harmony.PatchCategory("1.21");
}
public override double ExecuteOrder() => 0.12;
public override void Dispose()
{
_harmony?.UnpatchAll("com.orekiwoof.beehives");
if (serverApi != null)
serverApi.Event.PlayerJoin -= OnPlayerJoin;
if (api?.ModLoader.IsModSystemEnabled("ConfigLib.ConfigLibModSystem") == true)
UnsubscribeFromConfigChange();
base.Dispose();
}
public override void StartServerSide(ICoreServerAPI api)
{
serverApi = api;
InitializeServerConfig(api);
SetupServerCommands(api);
try
{
if (api.ModLoader.IsModSystemEnabled("ConfigLib.ConfigLibModSystem"))
SubscribeToConfigChange(api);
}
catch (Exception)
{
Mod.Logger.VerboseDebug("Failed to subscribe to config change");
}
}
public override void StartClientSide(ICoreClientAPI api)
{
InitializeClientConfig(api);
SetupClientCommands(api);
api.Network.GetChannel(CONFIG_CHANNEL_NAME)?.SetMessageHandler<Config>(OnConfigReceivedFromServer);
try
{
if (api.ModLoader.IsModSystemEnabled("ConfigLib.ConfigLibModSystem"))
SubscribeToConfigChange(api);
}
catch (Exception)
{
Mod.Logger.VerboseDebug("Failed to subscribe to config change");
}
LoadMetaConfig(api);
ShowDevVersionWarningIfNeeded(api);
}
private void LoadMetaConfig(ICoreClientAPI api)
{
try
{
metaConfig = api.LoadModConfig<MetaConfig>(meta_config_filename) ?? new MetaConfig();
}
catch (Exception e)
{
Mod.Logger.Warning("Could not load meta config: {0}", e.Message);
metaConfig = new MetaConfig();
}
if (metaConfig.ModVersion != Mod.Info.Version)
{
metaConfig.ModVersion = Mod.Info.Version;
api.StoreModConfig(metaConfig, meta_config_filename);
}
}
private void ShowDevVersionWarningIfNeeded(ICoreClientAPI api)
{
if (!Mod.Info.Version.Contains("-dev"))
return;
if (metaConfig.IsTester)
return;
api.Event.LevelFinalize += () =>
{
api.ShowChatMessage("This is a development version of OrekiWoof's Beehives. It can break and crash. When reporting issues, mention the exact version of this mod (and Roaming Bees mod if installed).");
api.ShowChatMessage("To disable this warning, use \".beehives tester\"");
api.ShowChatMessage("To check mod versions, use \".beehives version\"");
};
}
}