Skip to content

Commit

Permalink
feat: Add support for Biome and Goto commands (#515)
Browse files Browse the repository at this point in the history
* Allow adding teleports w/ console command handler

* Add example biome teleport to example mod

* Add proper documentation
  • Loading branch information
LeeTwentyThree committed Jan 1, 2024
1 parent 09bf155 commit 782ed96
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
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();
}
}

0 comments on commit 782ed96

Please sign in to comment.