Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Biome and Goto commands #515

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Example mod/BiomeHandlerExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ private void Awake()

// Add the biome somewhere to the world
CoordinatedSpawnsHandler.RegisterCoordinatedSpawn(new SpawnInfo(volumePrefabInfo.ClassID, new Vector3(-1400, -80, 600), Quaternion.identity, new Vector3(50, 50, 50)));

// Add this biome to the "biome" command
ConsoleCommandsHandler.AddBiomeTeleportPosition("nautilusexamplebiome", new Vector3(-1400, -80, 600));
}
}
24 changes: 24 additions & 0 deletions Nautilus/Handlers/ConsoleCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using HarmonyLib;
using Nautilus.Commands;
using Nautilus.Patchers;
using UnityEngine;

namespace Nautilus.Handlers;

Expand Down Expand Up @@ -79,4 +80,27 @@ public static void RegisterConsoleCommands(Type type)
{
ConsoleCommandsPatcher.ParseCustomCommands(type);
}


/// <summary>
/// Adds a new teleport position to the "biome" command.
/// </summary>
/// <param name="biomeName">The name of the teleport. Case-insensitive, no spaces allowed.</param>
/// <param name="position">The world coordinates of the teleport.</param>
public static void AddBiomeTeleportPosition(string biomeName, Vector3 position)
{
ConsoleCommandsPatcher.BiomeTeleportPositionsToAdd.Add(new TeleportPosition{name = biomeName, position = position});
ConsoleCommandsPatcher.UpdateTeleportPositions();
}

/// <summary>
/// Adds a new teleport position to the "goto" command.
/// </summary>
/// <param name="locationName">The name of the teleport. Case-insensitive, no spaces allowed.</param>
/// <param name="position">The world coordinates of the teleport.</param>
public static void AddGotoTeleportPosition(string locationName, Vector3 position)
{
ConsoleCommandsPatcher.GotoTeleportPositionsToAdd.Add(new TeleportPosition{name = locationName, position = position});
ConsoleCommandsPatcher.UpdateTeleportPositions();
}
}
37 changes: 37 additions & 0 deletions Nautilus/Patchers/ConsoleCommandsPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ internal static class ConsoleCommandsPatcher
private static Color ModOriginColor = new(0, 1, 0);
private static Color ModConflictColor = new(0.75f, 0.75f, 0.75f);

internal static List<TeleportPosition> GotoTeleportPositionsToAdd = new List<TeleportPosition>();
internal static List<TeleportPosition> BiomeTeleportPositionsToAdd = new List<TeleportPosition>();

public static void Patch(Harmony harmony)
{
harmony.PatchAll(typeof(ConsoleCommandsPatcher));
Expand Down Expand Up @@ -281,4 +284,38 @@ public static string StripXML(this string source)
{
return xmlRegex.Replace(source, string.Empty);
}

[HarmonyPatch(typeof(GotoConsoleCommand), nameof(GotoConsoleCommand.Awake))]
[HarmonyPostfix]
private static void GotoConsoleCommandAwakePostfix(GotoConsoleCommand __instance)
{
UpdateTeleportPositions();
}

[HarmonyPatch(typeof(BiomeConsoleCommand), nameof(BiomeConsoleCommand.Awake))]
[HarmonyPostfix]
private static void BiomeConsoleCommandAwakePostfix(BiomeConsoleCommand __instance)
{
UpdateTeleportPositions();
}

internal static void UpdateTeleportPositions()
{
if (GotoConsoleCommand.main != null && GotoTeleportPositionsToAdd.Count > 0)
{
AddTeleportPositionsToCommandData(GotoConsoleCommand.main.data, GotoTeleportPositionsToAdd);
}
if (BiomeConsoleCommand.main != null && BiomeTeleportPositionsToAdd.Count > 0)
{
AddTeleportPositionsToCommandData(BiomeConsoleCommand.main.data, BiomeTeleportPositionsToAdd);
}
}

private static void AddTeleportPositionsToCommandData(TeleportCommandData commandData, List<TeleportPosition> positionsToAdd)
{
var list = new List<TeleportPosition>(commandData.locations);
list.AddRange(positionsToAdd);
commandData.locations = list.ToArray();
positionsToAdd.Clear();
}
}