Private
Public Access
1
0
Files
OrekiWoofsBeehives/OrekiWoofsBeehives/ChatCommands.cs
2026-03-11 02:01:27 +01:00

148 lines
5.9 KiB
C#

using OrekiWoofsBees.Common.Configs;
using OrekiWoofsBees.Common;
using Vintagestory.API.Config;
using Vintagestory.API.Client;
using Vintagestory.API.Server;
using Vintagestory.API.Common;
namespace OrekiWoofsBeehives;
public partial class OrekiWoofsBeehivesModSystem
{
private const string lang_domain = "orekiwoofsbeehives";
public bool DebugUnloadEnabled { get; private set; }
private const string meta_config_filename = "OrekiWoofsBeehivesMeta.json";
public void SetupClientCommands(ICoreClientAPI api)
{
var rootCommand = api.ChatCommands.Create("beehives");
ConfigCommands.Register(rootCommand, api.ChatCommands, lang_domain, serverSide: false, () => Config.Instance, SaveClientConfig);
rootCommand
.BeginSubCommand("tester")
.HandleWith(HandleTesterCommand)
.EndSubCommand()
.BeginSubCommand("version")
.HandleWith(HandleVersionCommand)
.EndSubCommand();
}
public void SetupServerCommands(ICoreServerAPI api)
{
var rootCommand = api.ChatCommands.Create("beehives");
ConfigCommands.Register(rootCommand, api.ChatCommands, lang_domain, serverSide: true, () => Config.Instance, SaveServerConfig);
rootCommand
.BeginSubCommand("PlantReg")
.BeginSubCommand("BlocksPerTick")
.WithDescription(Lang.Get($"{lang_domain}:plantreg-blockpertick-desc"))
.WithArgs(api.ChatCommands.Parsers.OptionalIntRange("value", 0, 1000))
.HandleWith(HandlePlantRegBlockPerTickCommand)
.EndSubCommand()
.EndSubCommand()
.BeginSubCommand("setPopulation")
.WithDescription(Lang.Get($"{lang_domain}:setpopulation-desc"))
.WithArgs(api.ChatCommands.Parsers.IntRange("value", 0, Config.Instance.MaxBeePopulation))
.HandleWith(HandleSetPopulationCommand)
.EndSubCommand()
.BeginSubCommand("debugUnload")
.WithDescription(Lang.Get($"{lang_domain}:debugunload-desc"))
.WithArgs(api.ChatCommands.Parsers.Bool("value"))
.HandleWith(HandleDebugUnloadCommand)
.EndSubCommand();
}
public void BroadcastUnloadDebug(string message)
{
if (!DebugUnloadEnabled || serverApi == null)
return;
var fullMessage = $"[Beehives CatchUp] {message}";
serverApi.BroadcastMessageToAllGroups(fullMessage, EnumChatType.Notification);
}
private void SaveClientConfig(bool _)
{
var temp = api?.LoadModConfig<Config>(config_filename) ?? new Config();
temp.InformationVerbosity = Config.Instance.InformationVerbosity;
temp.DisableServerRecommended = Config.Instance.DisableServerRecommended;
temp.BeehiveAmbientVolume = Config.Instance.BeehiveAmbientVolume;
api?.StoreModConfig(temp, config_filename);
}
private void SaveServerConfig(bool _)
{
api?.StoreModConfig(Config.Instance, config_filename);
serverApi?.Network.GetChannel(CONFIG_CHANNEL_NAME).BroadcastPacket(Config.Instance);
}
private TextCommandResult HandlePlantRegBlockPerTickCommand(TextCommandCallingArgs args)
{
var registry = api?.ModLoader.GetModSystem<PlantPositionRegistryModSystem2>();
if (registry == null)
return TextCommandResult.Error(Lang.Get($"{lang_domain}:plantreg-unavailable"));
if (args.Parsers[0].IsMissing)
return TextCommandResult.Success($"PlantReg BlocksPerTick={registry.BlocksPerTick}");
if (args.Parsers[0].GetValue() is not int value)
return TextCommandResult.Error(Lang.Get($"{lang_domain}:plantreg-blockpertick-parse-error"));
registry.BlocksPerTick = value;
return TextCommandResult.Success($"PlantReg BlocksPerTick={registry.BlocksPerTick}");
}
private TextCommandResult HandleSetPopulationCommand(TextCommandCallingArgs args)
{
if (args.Caller.Player is not IServerPlayer serverPlayer)
return TextCommandResult.Error("This command can only be used by a player.");
if (args.Parsers[0].GetValue() is not int value)
return TextCommandResult.Error("Couldn't parse population value.");
var blockSel = serverPlayer.CurrentBlockSelection;
if (blockSel == null)
return TextCommandResult.Error("Look at a beehive block first.");
var beehive = api?.World.BlockAccessor.GetBlockEntity(blockSel.Position) as BlockEntities.BlockEntityReusableBeehive;
if (beehive == null)
return TextCommandResult.Error("The targeted block is not a reusable beehive.");
beehive.BeePopulation = value;
beehive.MarkDirty(true);
return TextCommandResult.Success($"Bee population set to {value}.");
}
private TextCommandResult HandleDebugUnloadCommand(TextCommandCallingArgs args)
{
if (args.Parsers[0].GetValue() is not bool value)
return TextCommandResult.Error("Couldn't parse. Use true or false.");
DebugUnloadEnabled = value;
return TextCommandResult.Success($"debugUnload={DebugUnloadEnabled}");
}
private TextCommandResult HandleTesterCommand(TextCommandCallingArgs args)
{
metaConfig.IsTester = !metaConfig.IsTester;
api?.StoreModConfig(metaConfig, meta_config_filename);
return TextCommandResult.Success(metaConfig.IsTester
? "Dev version warning disabled."
: "Dev version warning enabled.");
}
private TextCommandResult HandleVersionCommand(TextCommandCallingArgs args)
{
var result = $"{Mod.Info.ModID}@{Mod.Info.Version}";
var roamingBees = api?.ModLoader.GetMod("roamingbees");
if (roamingBees != null)
result += $"\n{roamingBees.Info.ModID}@{roamingBees.Info.Version}";
return TextCommandResult.Success(result);
}
}