Skip to content

Commit 0b13524

Browse files
committed
- Added player difficulty and world difficulty
- Added update to reload tooltip for player and world in Config:OnChanged() Note: still not working, join singleplayer gets "Failed to select player and world"
1 parent 15bfcd6 commit 0b13524

12 files changed

Lines changed: 167 additions & 80 deletions

File tree

Common/Configs/Config.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using ModReloader.Common.Configs.ConfigElements.ModsConfigElements;
45
using ModReloader.Common.Configs.ConfigElements.PlayerAndWorld;
6+
using ModReloader.Core.Features.MainMenuFeatures;
7+
using ModReloader.Core.Features.Reload;
58
using Terraria.ModLoader.Config;
69

710
namespace ModReloader.Common.Configs
@@ -68,29 +71,41 @@ public enum WorldDifficulty { Normal, Expert, Master, Journey }
6871
[DefaultValue(true)]
6972
public bool LogDebugMessages;
7073

74+
75+
76+
7177
public override void OnChanged()
7278
{
7379
base.OnChanged();
74-
80+
UpdateMainMenuReloadTooltip();
7581
}
76-
}
7782

78-
public static class Conf
79-
{
80-
public static void Save()
83+
private void UpdateMainMenuReloadTooltip()
8184
{
82-
try
83-
{
84-
ConfigManager.Save(C);
85-
}
86-
catch
85+
// Update reload tooltip
86+
var mainMenuSys = ModContent.GetInstance<MainMenuSystem>();
87+
if (mainMenuSys == null)
8788
{
88-
Log.Error("An error occurred while manually saving ModConfig!.");
89+
Log.Info("main menu sys is null!");
90+
return;
8991
}
92+
var state = mainMenuSys.state;
93+
if (state == null) return;
94+
95+
Main.LoadPlayers();
96+
Main.LoadWorlds();
97+
98+
int p = Utilities.FindPlayerId(Conf.C.Player);
99+
int w = Utilities.FindWorldId(Conf.C.World);
100+
101+
state.UpdatePlayerIndex(p);
102+
state.UpdateWorldIndex(w);
90103
}
104+
}
91105

92-
// Instance of the Config class
93-
// Use it like 'Conf.C.YourConfigField' for easy access to the config values
106+
public static class Conf
107+
{
108+
/// <summary> Quick instance getter. Usage example: Conf.C.Field /// </summary>
94109
public static Config C => ModContent.GetInstance<Config>();
95110
}
96111
}

Common/Configs/ConfigElements/PlayerAndWorld/IntPathOptionElement.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using ModReloader.Core.Features.MainMenuFeatures;
23
using Terraria.ModLoader.Config.UI;
34
using Terraria.ModLoader.UI;
45

@@ -8,7 +9,10 @@ public abstract class IntPathOptionElement : RangeElement
89
{
910
public override int NumberTicks
1011
{
11-
get => GetCount();
12+
get
13+
{
14+
return GetCount();
15+
}
1216
}
1317

1418
public override float TickIncrement
@@ -52,14 +56,17 @@ public override void OnBind()
5256
TextDisplayFunction = () =>
5357
{
5458
string header = Label ?? MemberInfo.Name;
55-
//return header + ": " + ResolveName(ReadIndex());
56-
return header;
59+
string name = ResolveName(ReadIndex());
60+
string difficulty = ResolveDifficulty(ReadIndex());
61+
return header + ": " + name + " (" + difficulty + ")";
62+
//return header;
5763
};
5864
}
5965

6066
protected abstract int GetCount();
6167

6268
protected abstract string ResolveName(int index);
69+
protected abstract string ResolveDifficulty(int index);
6370

6471
protected abstract string IDToPath(int index);
6572

@@ -78,6 +85,11 @@ private void WriteIndex(int i)
7885
{
7986
if (!MemberInfo.CanWrite) return;
8087
MemberInfo.SetValue(Item, IDToPath(i));
88+
89+
//// instant UI sync (no need to wait for Apply)
90+
//ModContent.GetInstance<MainMenuSystem>()?.SyncIndicesFromConfig();
91+
92+
//Interface.modConfig.SetPendingChanges();
8193
}
8294
}
8395
}

Common/Configs/ConfigElements/PlayerAndWorld/PlayerIndexSliderElement.cs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ public class PlayerIndexSliderElement : IntPathOptionElement
99
{
1010
protected override int GetCount()
1111
{
12-
if (Main.PlayerList == null) return 0;
12+
if (Main.PlayerList == null)
13+
{
14+
return 0;
15+
}
16+
//Log.Info($"Found {Main.PlayerList.Count} players");
1317
return Main.PlayerList.Count;
1418
}
1519

@@ -26,6 +30,32 @@ protected override string ResolveName(int index)
2630
return "Player " + clamped;
2731
}
2832

33+
protected override string ResolveDifficulty(int index)
34+
{
35+
if (Main.PlayerList == null || Main.PlayerList.Count == 0)
36+
return "No players";
37+
38+
int clamped = index;
39+
if (clamped < 0) clamped = 0;
40+
if (clamped > Main.PlayerList.Count - 1) clamped = Main.PlayerList.Count - 1;
41+
42+
var file = Main.PlayerList[clamped];
43+
if (file == null)
44+
return "Unknown";
45+
46+
// map difficulty byte -> string
47+
string difficultyText = file.Player?.difficulty switch
48+
{
49+
0 => "Classic",
50+
1 => "Mediumcore",
51+
2 => "Hardcore",
52+
3 => "Journey",
53+
_ => "Unknown"
54+
};
55+
56+
return difficultyText;
57+
}
58+
2959
public override void Draw(SpriteBatch sb)
3060
{
3161
base.Draw(sb);
@@ -46,20 +76,18 @@ public override void Draw(SpriteBatch sb)
4676
var dims = GetDimensions();
4777
var rect = dims.ToRectangle();
4878
var pos = new Vector2(rect.X + dims.Width - 200, rect.Y + 12);
79+
4980
// Player
5081
var player = Main.PlayerList[playerIndex].Player;
51-
string name = player.name;
52-
53-
// Measure width
54-
var font = FontAssets.ItemStack.Value;
55-
var width = font.MeasureString(name).X;
56-
var namePos = new Vector2(rect.X + dims.Width - 200 - width, rect.Y + 7);
57-
58-
ChatManager.DrawColorCodedStringWithShadow(sb, font,
59-
name, namePos, Color.White, 0f, Vector2.Zero, Vector2.One);
82+
//string name = player.name;
83+
//var font = FontAssets.ItemStack.Value;
84+
//var width = font.MeasureString(name).X;
85+
//var namePos = new Vector2(rect.X + dims.Width - 200 - width, rect.Y + 7);
86+
//ChatManager.DrawColorCodedStringWithShadow(sb, font,
87+
//name, namePos, Color.White, 0f, Vector2.Zero, Vector2.One);
6088

6189
// Player head pos
62-
Vector2 headPos = new(namePos.X - 20, namePos.Y + 5);
90+
Vector2 headPos = new(rect.X +rect.Width-210, rect.Y + 13);
6391

6492
// Draw player head
6593
Main.MapPlayerRenderer.DrawPlayerHead(

Common/Configs/ConfigElements/PlayerAndWorld/WorldIndexSliderElement.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class WorldIndexSliderElement : IntPathOptionElement
1212
protected override int GetCount()
1313
{
1414
if (Main.WorldList == null) return 0;
15+
//Log.Info($"Found {Main.WorldList.Count} world");
1516
return Main.WorldList.Count;
1617
}
1718

@@ -26,6 +27,31 @@ protected override string ResolveName(int index)
2627
return "World " + clamped;
2728
}
2829

30+
protected override string ResolveDifficulty(int index)
31+
{
32+
if (Main.WorldList == null || Main.WorldList.Count == 0)
33+
return "No worlds";
34+
35+
int clamped = index;
36+
if (clamped < 0) clamped = 0;
37+
if (clamped > Main.WorldList.Count - 1) clamped = Main.WorldList.Count - 1;
38+
39+
var world = Main.WorldList[clamped];
40+
if (world == null)
41+
return "Unknown";
42+
43+
// map GameMode byte -> string
44+
string difficultyText = world.GameMode switch
45+
{
46+
0 => "Normal",
47+
1 => "Expert",
48+
2 => "Master",
49+
3 => "Journey",
50+
_ => "Unknown"
51+
};
52+
return difficultyText;
53+
}
54+
2955
public override void OnBind()
3056
{
3157
base.OnBind();
@@ -48,21 +74,16 @@ public override void Draw(SpriteBatch sb)
4874
return;
4975

5076
var world = Main.WorldList[worldIndex];
51-
string name = world.GetWorldName();
52-
53-
// Measure width for positioning text
54-
var font = FontAssets.ItemStack.Value;
55-
var width = font.MeasureString(name).X;
5677

5778
var dims = GetDimensions();
5879
var rect = dims.ToRectangle();
5980

6081
// Position of the world name
61-
var namePos = new Vector2(rect.X + dims.Width - 200 - width, rect.Y + 7);
62-
ChatManager.DrawColorCodedStringWithShadow(sb, font, name, namePos, Color.White, 0f, Vector2.Zero, Vector2.One);
82+
//var namePos = new Vector2(rect.X + dims.Width - 200 - width, rect.Y + 7);
83+
//ChatManager.DrawColorCodedStringWithShadow(sb, font, name, namePos, Color.White, 0f, Vector2.Zero, Vector2.One);
6384

6485
// Position for preview (to the left of the name)
65-
Vector2 previewPos = new(namePos.X - 36, rect.Y + 2);
86+
Vector2 previewPos = new(rect.X + rect.Width-220, rect.Y);
6687

6788
// Update the preview element with this world’s options
6889
byte difficulty = (byte)world.GameMode; // 0 normal, 1 expert, 2 master, 3 journey

Common/Helpers/Log.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void SlowInfo(object message, int seconds = 1, [CallerFilePath] st
5454

5555
public static void Info(object message, [CallerFilePath] string callerFilePath = "")
5656
{
57-
if (!Conf.C.LogDebugMessages)
57+
if (Conf.C == null || !Conf.C.LogDebugMessages)
5858
return; // Skip logging if the config is set to false
5959

6060
// Extract the class name from the caller's file path.

Core/Features/DebugTextInWorld/DebugText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override void LeftClick(UIMouseEvent evt)
4545
base.LeftClick(evt);
4646

4747
Conf.C.ShowDebugInfo = !Conf.C.ShowDebugInfo;
48-
Conf.Save();
48+
Conf.C.SaveChanges();
4949

5050
// Open client log
5151
// Log.OpenClientLog();

Core/Features/MainMenuFeatures/MainMenuActions.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ public override void ModifyWorldGenTasks(List<GenPass> tasks, ref double totalWe
2121

2222
// Passes known (or very likely) to crash on a 2 100 × 600 map
2323
string[] skipExact =
24-
{
25-
"Dungeon", "Dungeon Entrance", "Pyramids", "Temple", "Jungle Temple", "Bee Hives",
26-
"Oceans", "Ocean Caves", "Ocean Sand",
27-
"Lakes", "Oasis", "Dunes",
28-
"Mushroom Patches", "Mushroom Biomes",
29-
"Gem Caves", "Granite Caves", "Marble Caves",
30-
"Floating Islands", "Sky Lakes",
31-
"Waterfalls", "Water Features", "Rivers",
32-
"Jungle Chests", "Jungle Shrines",
33-
"Micro Biomes" // NEW
34-
};
24+
[
25+
"Dungeon", "Dungeon Entrance", "Pyramids", "Temple", "Jungle Temple", "Bee Hives",
26+
"Oceans", "Ocean Caves", "Ocean Sand",
27+
"Lakes", "Oasis", "Dunes",
28+
"Mushroom Patches", "Mushroom Biomes",
29+
"Gem Caves", "Granite Caves", "Marble Caves",
30+
"Floating Islands", "Sky Lakes",
31+
"Waterfalls", "Water Features", "Rivers",
32+
"Jungle Chests", "Jungle Shrines",
33+
"Micro Biomes"
34+
];
3535
var exact = new HashSet<string>(skipExact, StringComparer.OrdinalIgnoreCase);
3636

3737
// any pass name containing these substrings will also be skipped automatically

Core/Features/MainMenuFeatures/MainMenuState.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ private void AddOptionsSection(TooltipPanel tooltipPanel)
128128
mainMenuList.Add(spacer);
129129
}
130130

131+
private int playerIndex = -1;
132+
private int worldIndex = -1;
133+
134+
public void UpdatePlayerIndex(int index) => playerIndex = index;
135+
public void UpdateWorldIndex(int index) => worldIndex = index;
136+
131137
private void AddSingleplayerSection(TooltipPanel tooltipPanel)
132138
{
133139
// Load players and worlds for tooltips
@@ -144,22 +150,39 @@ private void AddSingleplayerSection(TooltipPanel tooltipPanel)
144150

145151
var singleplayerHeader = new HeaderMainMenuElement(Loc.Get("MainMenu.SingleplayerHeader"), () => Loc.Get("MainMenu.SingleplayerTooltip"), tooltipPanel);
146152
var joinSingleplayer = new ActionMainMenuElement(
147-
() =>
148-
{
149-
ClientDataJsonHelper.ClientMode = ClientMode.SinglePlayer;
150-
ClientDataJsonHelper.PlayerPath = null;
151-
ClientDataJsonHelper.WorldPath = null;
152-
AutoloadPlayerInWorldSystem.EnterSingleplayerWorld();
153-
},
154-
Loc.Get("MainMenu.JoinSingleplayerText"),
155-
() =>
156-
{
157-
if (string.IsNullOrEmpty(playerName) || string.IsNullOrEmpty(worldName))
158-
return Loc.Get("MainMenu.JoinSingleplayerTooltipNoData");
159-
return Loc.Get("MainMenu.JoinSingleplayerTooltip", $"[c/FFFF00:{playerName}]", $"[c/FFFF00:{worldName}]");
160-
},
161-
tooltipPanel
162-
);
153+
() =>
154+
{
155+
ClientDataJsonHelper.ClientMode = ClientMode.SinglePlayer;
156+
ClientDataJsonHelper.PlayerPath = null;
157+
ClientDataJsonHelper.WorldPath = null;
158+
AutoloadPlayerInWorldSystem.EnterSingleplayerWorld();
159+
},
160+
Loc.Get("MainMenu.JoinSingleplayerText"),
161+
() =>
162+
{
163+
Main.LoadPlayers();
164+
Main.LoadWorlds();
165+
166+
int playerIdx = Utilities.FindPlayerId(Conf.C.Player);
167+
int worldIdx = Utilities.FindWorldId(Conf.C.World);
168+
169+
string pName = (playerIdx >= 0 && playerIdx < Main.PlayerList.Count)
170+
? Main.PlayerList[playerIdx].Name
171+
: "";
172+
string wName = (worldIdx >= 0 && worldIdx < Main.WorldList.Count)
173+
? Main.WorldList[worldIdx].Name
174+
: "";
175+
176+
if (string.IsNullOrEmpty(pName) || string.IsNullOrEmpty(wName))
177+
return Loc.Get("MainMenu.JoinSingleplayerTooltipNoData");
178+
179+
return Loc.Get("MainMenu.JoinSingleplayerTooltip",
180+
$"[c/FFFF00:{pName}]",
181+
$"[c/FFFF00:{wName}]");
182+
},
183+
tooltipPanel
184+
);
185+
163186
var spacer = new SpacerMainMenuElement();
164187
mainMenuList.Add(singleplayerHeader);
165188
mainMenuList.Add(joinSingleplayer);
@@ -214,13 +237,6 @@ public override void Draw(SpriteBatch spriteBatch)
214237
//DrawActionRowsDebug(spriteBatch);
215238
}
216239

217-
private void PositionTooltip()
218-
{
219-
// tooltipPanel.Left.Set(15 + 3, 0f);
220-
// tooltipPanel.Top.Set(470f, 0f); // 6 px under the list
221-
// tooltipPanel.Recalculate();
222-
}
223-
224240
public override void Update(GameTime gameTime)
225241
{
226242
base.Update(gameTime);

Core/Features/MainMenuFeatures/MainMenuSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ModReloader.Core.Features.MainMenuFeatures;
66
internal sealed class MainMenuSystem : ModSystem
77
{
88
private UserInterface ui;
9-
private MainMenuState state;
9+
public MainMenuState state;
1010

1111
public override void PostSetupContent()
1212
{

0 commit comments

Comments
 (0)