Skip to content

Commit 2a689fd

Browse files
author
hamstar0
committed
v1.1.4 - Added proper player sync (untested)
* Added proper player sync (untested) * Updated to MH v5.9.2
1 parent d7d04f9 commit 2a689fd

File tree

7 files changed

+141
-38
lines changed

7 files changed

+141
-38
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
4+
using Terraria;
5+
using HamstarHelpers.Helpers.Debug;
6+
7+
8+
namespace MountedMagicMirrors.DataStructures {
9+
class DiscoveredMirrors : Dictionary<int, ISet<int>> {
10+
public DiscoveredMirrors() : base() { }
11+
public DiscoveredMirrors( int capacity ) : base( capacity ) { }
12+
public DiscoveredMirrors( IEqualityComparer<int> comparer )
13+
: base( comparer ) { }
14+
public DiscoveredMirrors( IDictionary<int, ISet<int>> dictionary )
15+
: base( dictionary ) { }
16+
public DiscoveredMirrors( int capacity, IEqualityComparer<int> comparer )
17+
: base( comparer ) { }
18+
public DiscoveredMirrors( IDictionary<int, ISet<int>> dictionary, IEqualityComparer<int> comparer )
19+
: base( dictionary, comparer ) { }
20+
protected DiscoveredMirrors( SerializationInfo info, StreamingContext context )
21+
: base( info, context ) { }
22+
}
23+
}

MountedMagicMirrors/MountedMagicMirrors.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</Target>
1313
<ItemGroup>
1414
<Reference Include="HamstarHelpers">
15-
<HintPath>..\..\..\Mod Helpers\v5 (tML11)\HamstarHelpers\bin\Release\net452\HamstarHelpers.dll</HintPath>
15+
<HintPath>..\..\..\Mod Helpers\Project\HamstarHelpers\bin\Release\net452\HamstarHelpers.dll</HintPath>
1616
</Reference>
1717
</ItemGroup>
1818
</Project>

MountedMagicMirrors/MyConfig.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using HamstarHelpers.Services.Configs;
2-
using System;
1+
using System;
32
using System.ComponentModel;
3+
using Terraria.ModLoader;
44
using Terraria.ModLoader.Config;
55

66

77
namespace MountedMagicMirrors {
8-
public class MMMConfig : StackableModConfig {
9-
public static MMMConfig Instance => ModConfigStack.GetMergedConfigs<MMMConfig>();
8+
public class MMMConfig : ModConfig {
9+
public static MMMConfig Instance => ModContent.GetInstance<MMMConfig>();
1010

1111

1212

MountedMagicMirrors/MyPlayer.cs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77
using HamstarHelpers.Helpers.DotNET.Extensions;
88
using HamstarHelpers.Helpers.World;
99
using HamstarHelpers.Helpers.TModLoader;
10+
using MountedMagicMirrors.Net;
11+
using MountedMagicMirrors.DataStructures;
1012

1113

1214
namespace MountedMagicMirrors {
13-
class DiscoveredMirrors : Dictionary<int, ISet<int>> { }
14-
15-
16-
17-
1815
partial class MMMPlayer : ModPlayer {
1916
internal static readonly object MyCurrentMirrorsLock = new object();
2017

2118

2219

2320
////////////////
2421

25-
private IDictionary<string, DiscoveredMirrors> DiscoveredMirrorTiles = new Dictionary<string, DiscoveredMirrors>();
22+
private IDictionary<string, DiscoveredMirrors> DiscoveredMirrorTilesPerWorld = new Dictionary<string, DiscoveredMirrors>();
2623

2724
////
2825

@@ -33,9 +30,10 @@ public DiscoveredMirrors CurrentWorldDiscoveredMirrorTiles {
3330
if( this._CurrentWorldDiscoveredMirrorTiles == null ) {
3431
string worldUid = WorldHelpers.GetUniqueIdForCurrentWorld( true );
3532

36-
if( !this.DiscoveredMirrorTiles.TryGetValue(worldUid, out this._CurrentWorldDiscoveredMirrorTiles)
37-
&& !this.DiscoveredMirrorTiles.TryGetValue("_", out this._CurrentWorldDiscoveredMirrorTiles) ) {
38-
this._CurrentWorldDiscoveredMirrorTiles = null;
33+
if( !this.DiscoveredMirrorTilesPerWorld.TryGetValue(worldUid, out this._CurrentWorldDiscoveredMirrorTiles) ) {
34+
if( !this.DiscoveredMirrorTilesPerWorld.TryGetValue("_", out this._CurrentWorldDiscoveredMirrorTiles) ) {
35+
this._CurrentWorldDiscoveredMirrorTiles = null;
36+
}
3937
}
4038
}
4139
return this._CurrentWorldDiscoveredMirrorTiles;
@@ -64,11 +62,11 @@ public override void clientClone( ModPlayer clientClone ) {
6462
myclone._CurrentWorldDiscoveredMirrorTiles = null;
6563

6664
lock( MMMPlayer.MyCurrentMirrorsLock ) {
67-
foreach( (string worldUid, DiscoveredMirrors mirrors) in this.DiscoveredMirrorTiles ) {
68-
myclone.DiscoveredMirrorTiles[worldUid] = new DiscoveredMirrors();
65+
foreach( (string worldUid, DiscoveredMirrors mirrors) in this.DiscoveredMirrorTilesPerWorld ) {
66+
myclone.DiscoveredMirrorTilesPerWorld[worldUid] = new DiscoveredMirrors();
6967

7068
foreach( (int tileX, ISet<int> tileYs) in mirrors ) {
71-
myclone.DiscoveredMirrorTiles[worldUid][tileX] = new HashSet<int>( tileYs );
69+
myclone.DiscoveredMirrorTilesPerWorld[worldUid][tileX] = new HashSet<int>( tileYs );
7270
}
7371
}
7472
}
@@ -77,25 +75,22 @@ public override void clientClone( ModPlayer clientClone ) {
7775

7876
////////////////
7977

80-
/*public override void SyncPlayer( int toWho, int fromWho, bool newPlayer ) {
81-
if( Main.netMode == 1 ) {
82-
if( toWho == -1 && fromWho == -1 && newPlayer ) {
83-
this.Logic.OnCurrentClientConnect();
84-
}
85-
}
86-
if( Main.netMode == 2 ) {
87-
if( toWho == -1 && fromWho == this.player.whoAmI ) {
88-
this.Logic.OnServerConnect( Main.player[fromWho] );
78+
public override void SyncPlayer( int toWho, int fromWho, bool newPlayer ) {
79+
lock( MMMPlayer.MyCurrentMirrorsLock ) {
80+
if( Main.netMode == 1 ) {
81+
PlayerDataProtocol.SendToServer( this.DiscoveredMirrorTilesPerWorld );
82+
} else {
83+
PlayerDataProtocol.SendToClients( toWho, fromWho, this.DiscoveredMirrorTilesPerWorld );
8984
}
9085
}
91-
}*/
86+
}
9287

9388

9489
////////////////
9590

9691
public override void Load( TagCompound tag ) {
9792
lock( MMMPlayer.MyCurrentMirrorsLock ) {
98-
this.DiscoveredMirrorTiles.Clear();
93+
this.DiscoveredMirrorTilesPerWorld.Clear();
9994

10095
int count = 0;
10196

@@ -114,13 +109,13 @@ public override void Load( TagCompound tag ) {
114109
private int LoadOld( TagCompound tag ) {
115110
int count = tag.GetInt( "discovery_count" );
116111

117-
this.DiscoveredMirrorTiles[ "_" ] = new DiscoveredMirrors();
112+
this.DiscoveredMirrorTilesPerWorld[ "_" ] = new DiscoveredMirrors();
118113

119114
for( int i = 0; i < count; i++ ) {
120115
int tileX = tag.GetInt( "discovery_x_" + i );
121116
int tileY = tag.GetInt( "discovery_y_" + i );
122117

123-
this.DiscoveredMirrorTiles["_"].Set2D( tileX, tileY );
118+
this.DiscoveredMirrorTilesPerWorld["_"].Set2D( tileX, tileY );
124119

125120
if( MMMConfig.Instance.DebugModeInfo ) {
126121
LogHelpers.Log( "(Old) Loaded mirror at " + tileX + ", " + tileY );
@@ -138,13 +133,13 @@ private int LoadNew( TagCompound tag ) {
138133
string worldUid = tag.GetString( "world_uid_"+i );
139134
int mirrorCount = tag.GetInt( "discovery_count_for_"+i );
140135

141-
this.DiscoveredMirrorTiles[worldUid] = new DiscoveredMirrors();
136+
this.DiscoveredMirrorTilesPerWorld[worldUid] = new DiscoveredMirrors();
142137

143138
for( int j=0; j<mirrorCount; j++ ) {
144139
int tileX = tag.GetInt( "discovery_x_"+i+"_"+j );
145140
int tileY = tag.GetInt( "discovery_y_"+i+"_"+j );
146141

147-
this.DiscoveredMirrorTiles[worldUid].Set2D( tileX, tileY );
142+
this.DiscoveredMirrorTilesPerWorld[worldUid].Set2D( tileX, tileY );
148143

149144
if( MMMConfig.Instance.DebugModeInfo ) {
150145
LogHelpers.Log( "Loaded mirror at " + tileX + ", " + tileY );
@@ -163,12 +158,12 @@ private int LoadNew( TagCompound tag ) {
163158
public override TagCompound Save() {
164159
lock( MMMPlayer.MyCurrentMirrorsLock ) {
165160
var tag = new TagCompound {
166-
{ "world_count", this.DiscoveredMirrorTiles.Count }
161+
{ "world_count", this.DiscoveredMirrorTilesPerWorld.Count }
167162
};
168163

169164
int i = 0;
170-
foreach( string worldUid in this.DiscoveredMirrorTiles.Keys ) {
171-
IDictionary<int, ISet<int>> mirrors = this.DiscoveredMirrorTiles[ worldUid ];
165+
foreach( string worldUid in this.DiscoveredMirrorTilesPerWorld.Keys ) {
166+
IDictionary<int, ISet<int>> mirrors = this.DiscoveredMirrorTilesPerWorld[ worldUid ];
172167
int count = mirrors.Count2D();
173168

174169
string myWorldUid = worldUid;
@@ -224,7 +219,7 @@ private void PreUpdateLocal() {
224219
if( this.CurrentWorldDiscoveredMirrorTiles == null ) {
225220
if( LoadHelpers.IsWorldBeingPlayed() ) {
226221
string currWorldUid = WorldHelpers.GetUniqueIdForCurrentWorld( true );
227-
this.DiscoveredMirrorTiles[currWorldUid] = new DiscoveredMirrors();
222+
this.DiscoveredMirrorTilesPerWorld[currWorldUid] = new DiscoveredMirrors();
228223
}
229224
}
230225

MountedMagicMirrors/MyPlayer_Mirrors.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using HamstarHelpers.Helpers.Players;
1010
using HamstarHelpers.Helpers.Tiles;
1111
using MountedMagicMirrors.Tiles;
12+
using MountedMagicMirrors.DataStructures;
1213

1314

1415
namespace MountedMagicMirrors {
@@ -49,6 +50,14 @@ partial class MMMPlayer : ModPlayer {
4950
}
5051
}
5152

53+
////
54+
55+
internal void SetDiscoveredMirrorsFromNetwork( IDictionary<string, DiscoveredMirrors> mirrors ) {
56+
this.DiscoveredMirrorTilesPerWorld = mirrors;
57+
}
58+
59+
////
60+
5261
public void ClearInvalidMirrorDiscoveries() {
5362
if( this.CurrentWorldDiscoveredMirrorTiles == null ) {
5463
return;

MountedMagicMirrors/Net/PlayerData.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Linq;
3+
using System.Collections.Generic;
4+
using Terraria;
5+
using HamstarHelpers.Services.Network.NetIO;
6+
using HamstarHelpers.Services.Network.NetIO.PayloadTypes;
7+
using MountedMagicMirrors.DataStructures;
8+
9+
10+
namespace MountedMagicMirrors.Net {
11+
class PlayerDataProtocol : NetIOBidirectionalPayload {
12+
public static void SendToServer( IDictionary<string, DiscoveredMirrors> mirrors ) {
13+
var protocol = new PlayerDataProtocol( Main.myPlayer, mirrors );
14+
NetIO.SendToServer( protocol );
15+
}
16+
17+
public static void SendToClients(
18+
int toWho,
19+
int fromWho,
20+
IDictionary<string, DiscoveredMirrors> mirrors ) {
21+
var protocol = new PlayerDataProtocol( fromWho, mirrors );
22+
NetIO.SendToClients( protocol, toWho, fromWho );
23+
}
24+
25+
26+
27+
////////////////
28+
29+
public int PlayerWho;
30+
public Dictionary<string, Dictionary<int, HashSet<int>>> DiscoveredMirrorTilesPerWorld;
31+
32+
33+
34+
////////////////
35+
36+
private PlayerDataProtocol() { }
37+
38+
private PlayerDataProtocol( int playerWho, IDictionary<string, DiscoveredMirrors> mirrors ) {
39+
this.PlayerWho = playerWho;
40+
this.DiscoveredMirrorTilesPerWorld = mirrors.ToDictionary(
41+
(kv) => kv.Key,
42+
(kv) => kv.Value.ToDictionary(
43+
kv2 => kv2.Key,
44+
kv2 => kv2.Value as HashSet<int>
45+
)
46+
);
47+
}
48+
49+
50+
////////////////
51+
52+
public override void ReceiveOnClient() {
53+
this.Receive();
54+
}
55+
56+
public override void ReceiveOnServer( int fromWho ) {
57+
}
58+
59+
////
60+
61+
private void Receive() {
62+
Player plr = Main.player[this.PlayerWho];
63+
var myplayer = plr.GetModPlayer<MMMPlayer>();
64+
65+
IDictionary<string, DiscoveredMirrors> mirrors = this.DiscoveredMirrorTilesPerWorld.ToDictionary(
66+
( kv ) => kv.Key,
67+
( kv ) => new DiscoveredMirrors( kv.Value.ToDictionary(
68+
kv2 => kv2.Key,
69+
kv2 => kv2.Value as ISet<int>
70+
) )
71+
);
72+
73+
myplayer.SetDiscoveredMirrorsFromNetwork( mirrors );
74+
}
75+
}
76+
}

MountedMagicMirrors/build.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
displayName = Mounted Magic Mirrors
22
author = hamstar
3-
version = 1.1.3.6
4-
modReferences = HamstarHelpers@5.6.1.1
3+
version = 1.1.4
4+
modReferences = HamstarHelpers@5.9.2
55
buildIgnore = *.csproj, *.user, *.bat, obj\*, bin\*, .vs\*, .git\*
66
homepage = https://forums.terraria.org/index.php?threads/mounted-magic-mirrors-fast-travel.83296/

0 commit comments

Comments
 (0)