11
11
import net .bettercombat .api .WeaponAttributesHelper ;
12
12
import net .bettercombat .api .component .BetterCombatDataComponents ;
13
13
import net .bettercombat .network .Packets ;
14
+ import net .bettercombat .utils .CompressionHelper ;
14
15
import net .minecraft .item .Item ;
15
16
import net .minecraft .item .ItemStack ;
16
17
import net .minecraft .registry .Registries ;
@@ -137,7 +138,8 @@ public static void resolveAndRegisterAttributes(Identifier itemId, AttributesCon
137
138
138
139
// NETWORK SYNC
139
140
140
- private static Packets .WeaponRegistrySync encodedRegistrations = new Packets .WeaponRegistrySync (List .of ());
141
+ private static Encoded encodedRegistrations = new Encoded (true , List .of ());
142
+ public record Encoded (boolean compressed , List <String > chunks ) {}
141
143
private static final int CHUNK_SIZE = 10000 ;
142
144
private static final Gson gson = new GsonBuilder ().create ();
143
145
public static class SyncFormat {
@@ -146,8 +148,8 @@ public static class SyncFormat {
146
148
}
147
149
148
150
public static void encodeRegistry () {
151
+ var compressed = BetterCombatMod .config .weapon_registry_compression ;
149
152
List <String > chunks = new ArrayList <>();
150
-
151
153
var syncContent = new SyncFormat ();
152
154
containers .forEach ((key , value ) -> {
153
155
syncContent .attributes .put (key .toString (), value );
@@ -157,31 +159,38 @@ public static void encodeRegistry() {
157
159
});
158
160
159
161
var json = gson .toJson (syncContent );
162
+ if (compressed ) {
163
+ json = CompressionHelper .gzipCompress (json );
164
+ }
160
165
if (BetterCombatMod .config .weapon_registry_logging ) {
161
166
LOGGER .info ("Weapon Attribute assignments loaded: " + json );
162
167
}
163
168
for (int i = 0 ; i < json .length (); i += CHUNK_SIZE ) {
164
169
chunks .add (json .substring (i , Math .min (json .length (), i + CHUNK_SIZE )));
165
170
}
166
171
167
- encodedRegistrations = new Packets .WeaponRegistrySync (chunks );
172
+ encodedRegistrations = new Encoded (compressed , chunks );
173
+
174
+ var referencePacket = new Packets .WeaponRegistrySync (compressed , chunks );
168
175
var buffer = Platform .createByteBuffer ();
169
- encodedRegistrations .write (buffer );
176
+ referencePacket .write (buffer );
170
177
LOGGER .info ("Encoded Weapon Attribute registry size (with package overhead): " + buffer .readableBytes ()
171
178
+ " bytes (in " + chunks .size () + " string chunks with the size of " + CHUNK_SIZE + ")" );
172
179
}
173
180
174
181
public static void decodeRegistry (Packets .WeaponRegistrySync syncPacket ) {
182
+ var compressed = syncPacket .compressed ();
175
183
String json = "" ;
176
184
for (var chunk : syncPacket .chunks ()) {
177
185
json = json .concat (chunk );
178
186
}
187
+ if (compressed ) {
188
+ json = CompressionHelper .gzipDecompress (json );
189
+ }
179
190
LOGGER .info ("Decoded Weapon Attribute registry in " + syncPacket .chunks ().size () + " string chunks" );
180
191
if (BetterCombatMod .config .weapon_registry_logging ) {
181
192
LOGGER .info ("Weapon Attribute registry received: " + json );
182
193
}
183
- LOGGER .info ("Weapon Attribute registry received (pretty printed): " );
184
- LOGGER .info (json );
185
194
186
195
SyncFormat sync = gson .fromJson (json , SyncFormat .class );
187
196
containers .clear ();
@@ -194,7 +203,7 @@ public static void decodeRegistry(Packets.WeaponRegistrySync syncPacket) {
194
203
});
195
204
}
196
205
197
- public static Packets . WeaponRegistrySync getEncodedRegistry () {
206
+ public static Encoded getEncodedRegistry () {
198
207
return encodedRegistrations ;
199
208
}
200
209
}
0 commit comments