10
10
import net .minecraftforge .client .event .ClientPlayerNetworkEvent ;
11
11
import net .minecraftforge .common .MinecraftForge ;
12
12
import net .minecraftforge .fml .network .NetworkEvent ;
13
- import org .jetbrains .annotations .Nullable ;
14
13
14
+ import javax .annotation .Nullable ;
15
15
import java .io .IOException ;
16
+ import java .util .HashMap ;
16
17
import java .util .UUID ;
18
+ import java .util .concurrent .atomic .AtomicInteger ;
17
19
18
20
public class ClientNetworkInstance extends AbstractNetworkInstance {
19
21
20
22
boolean isRemotePresent = false ;
23
+ private final AtomicInteger connectState = new AtomicInteger (0 );
21
24
22
25
public static ClientNetworkInstance networkInstance = new ClientNetworkInstance ();
23
26
@@ -30,11 +33,16 @@ public void init(){
30
33
}
31
34
32
35
private void connectServerCallback (ClientPlayerNetworkEvent .LoggedInEvent event ){
33
- this .isRemotePresent = false ;
36
+ if (connectState .incrementAndGet () == 2 ) {
37
+ connectState .set (0 );
38
+ this .sendConfigCallback ();
39
+ }
34
40
}
35
41
36
42
private void disconnectEvent (ClientPlayerNetworkEvent .LoggedOutEvent event ){
37
43
this .disconnect ();
44
+ this .isRemotePresent = false ;
45
+ this .connectState .set (0 );
38
46
}
39
47
40
48
private void receiveJunk (NetworkEvent .ServerCustomPayloadEvent event ){
@@ -44,18 +52,31 @@ private void receiveJunk(NetworkEvent.ServerCustomPayloadEvent event){
44
52
45
53
private void registerServerSide (NetworkEvent .ChannelRegistrationChangeEvent event ){
46
54
this .isRemotePresent = event .getRegistrationChangeType () == NetworkEvent .RegistrationChangeType .REGISTER ;
47
- this .sendConfigCallback ();
55
+ if (isRemotePresent && connectState .incrementAndGet () == 2 ) {
56
+ connectState .set (0 );
57
+ this .sendConfigCallback ();
58
+ }
48
59
}
49
60
50
61
void receiveMessage (FriendlyByteBuf buf ){
51
- if (buf .isDirect ()){ //If the received ByteBuf is direct i have to copy that onto the heap
62
+ if (buf .isDirect ()){
52
63
byte [] bytes = new byte [buf .readableBytes ()];
53
64
buf .getBytes (buf .readerIndex (), bytes );
54
65
receiveMessage (bytes );
55
66
}
56
67
else {
57
- receiveMessage (buf .array ()); //if heap, I can just use it's byte-array
68
+ receiveMessage (buf .array ());
69
+ }
70
+ }
71
+
72
+ @ Override
73
+ public HashMap <Byte , Byte > getVersions () {
74
+ if (disableNBS ){
75
+ HashMap <Byte , Byte > map = new HashMap <>();
76
+ map .put ((byte )3 , (byte ) 0 );
77
+ return map ;
58
78
}
79
+ return null ;
59
80
}
60
81
61
82
@ Override
@@ -71,10 +92,7 @@ public boolean isActive() {
71
92
}
72
93
73
94
public static ServerboundCustomPayloadPacket newC2SEmotePacket (NetData data ) throws IOException {
74
- ServerboundCustomPayloadPacket packet = new ServerboundCustomPayloadPacket ();
75
- packet .setName (ServerNetwork .channelID );
76
- packet .setData (new FriendlyByteBuf (Unpooled .wrappedBuffer (new EmotePacket .Builder (data ).build ().write ().array ())));
77
- return packet ;
95
+ return new ServerboundCustomPayloadPacket (ServerNetwork .channelID , new FriendlyByteBuf (Unpooled .wrappedBuffer (new EmotePacket .Builder (data ).build ().write ().array ())));
78
96
}
79
97
80
98
@ Override
@@ -83,6 +101,6 @@ public void sendMessage(EmotePacket.Builder builder, @Nullable UUID target) thro
83
101
builder .configureTarget (target );
84
102
}
85
103
if (Minecraft .getInstance ().getConnection () != null )
86
- Minecraft .getInstance ().getConnection ().send (newC2SEmotePacket (builder .copyAndGetData ()));
104
+ Minecraft .getInstance ().getConnection ().send (newC2SEmotePacket (builder .copyAndGetData ()));
87
105
}
88
106
}
0 commit comments