Skip to content

Commit

Permalink
Changed how per-player data is stored to work in multiplayer (I hope).
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeStrout committed Jan 29, 2023
1 parent 88b4e85 commit 5a48187
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Farmtronics/M1/M1API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,11 +2072,11 @@ public override string ToString(TAC.Machine vm) {
return content.ToString().Replace("UnityEngine.", "");
}

public override int Hash(int recursionDepth=16) {
public override int Hash() {
return content.GetHashCode();
}

public override double Equality(Value rhs, int recursionDepth=16) {
public override double Equality(Value rhs) {
return rhs is ValWrapper && ((ValWrapper)rhs).content == content ? 1 : 0;
}
}
Expand Down
30 changes: 0 additions & 30 deletions Farmtronics/M1/ModData.cs

This file was deleted.

32 changes: 32 additions & 0 deletions Farmtronics/M1/PerPlayerData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This module defines per-player data which is saved in the game save file.
// References:
// https://stardewcommunitywiki.com/Modding:Modder_Guide/APIs/Data
// https://stardewvalleywiki.com/Modding:Migrate_to_Stardew_Valley_1.5#Custom_mod_data
//
// We keep around a singleton instance of this (instance), whiche any code that
// has the need can read or update and the changes will get saved with the game.

using System;
using StardewValley;

namespace Farmtronics {
public static class PerPlayerData {

static string HomeComputerNameKey {
get { return $"{ModEntry.instance.ModManifest.UniqueID}/HomeComputerName"; }
}

public static string HomeComputerName {
get {
string result;
if (Game1.player.modData.TryGetValue(HomeComputerNameKey, out result)) {
return result;
}
return "Home Computer";
}
set {
Game1.player.modData[HomeComputerNameKey] = value;
}
}
}
}
14 changes: 7 additions & 7 deletions Farmtronics/M1/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public string name {
get { return bot == null ? _name : bot.name; }
set {
_name = value;
if (bot == null) ModData.instance.HomeComputerName = value;
if (bot == null) PerPlayerData.HomeComputerName = value;
else bot.Name = bot.DisplayName = value;
}
}
Expand Down Expand Up @@ -360,24 +360,24 @@ public void Exit() {
}
}

public void PrintLine(string line) {
public void PrintLine(string line, bool lineBreak=true) {
TextDisplay disp = console.display;
disp.Print(line);
disp.Print(disp.delimiter);
if (lineBreak) disp.Print(disp.delimiter);
}

public void PrintErrLine(string line) {
public void PrintErrLine(string line, bool lineBreak=true) {
if (interpreter.vm != null) {
stackAtLastErr = M1API.StackList(interpreter.vm);
interpreter.vm.globalContext.variables.SetElem(M1API._stackAtBreak, stackAtLastErr);
} else {
stackAtLastErr = new ValList(); // empty list signifies error without a VM, e.g. at compile time.
}
PrintLine(line);
PrintLine(line, lineBreak);
}

void PrintLineWithTaskCheck(string line) {
PrintLine(line);
void PrintLineWithTaskCheck(string line, bool lineBreak=true) {
PrintLine(line, lineBreak);
ToDoManager.NotePrintOutput(line);
}
}
Expand Down
7 changes: 3 additions & 4 deletions Farmtronics/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void OnMenuChanged(object sender, MenuChangedEventArgs e) {
var dlog = e.NewMenu as DialogueBox;
if (dlog == null) return;
if (!dlog.isQuestion || dlog.responses[0].responseKey != "Weather") return;
// Only allow players to use the home computer at their own cabin
// Only allow players to use the home computer at their own s
if (Game1.player.currentLocation.NameOrUniqueName != Game1.player.homeLocation.Value) return;

// TV menu: insert a new option for the Home Computer
Expand All @@ -193,7 +193,6 @@ public void OnMenuChanged(object sender, MenuChangedEventArgs e) {

public void OnSaving(object sender, SavingEventArgs args) {
Monitor.Log($"OnSaving");
ModData.Save();
// Host can't save without this
BotManager.ConvertBotsToChests(true);
BotManager.ClearAll();
Expand All @@ -213,7 +212,7 @@ public void OnSaveLoaded(object sender, SaveLoadedEventArgs args) {
MultiplayerManager.hostID = Game1.player.UniqueMultiplayerID;
}
BotManager.ConvertChestsToBots();
if (shell != null) shell.name = ModData.instance.HomeComputerName;
if (shell != null) shell.name = PerPlayerData.HomeComputerName;
}

public void OnDayStarted(object sender, DayStartedEventArgs args) {
Expand Down Expand Up @@ -245,7 +244,7 @@ private void OnDayEnding(object sender, DayEndingEventArgs e) {
private void InitComputerShell() {
if (shell == null) {
shell = new Shell();
shell.name = ModData.instance.HomeComputerName;
shell.name = PerPlayerData.HomeComputerName;
shell.Init(Game1.player.UniqueMultiplayerID);
}
}
Expand Down

0 comments on commit 5a48187

Please sign in to comment.