44import net .minecraft .core .Registry ;
55import net .minecraft .core .registries .BuiltInRegistries ;
66import net .minecraft .core .registries .Registries ;
7+ import net .minecraft .network .syncher .SynchedEntityData ;
78import net .minecraft .resources .Identifier ;
89import net .minecraft .resources .ResourceKey ;
910import net .minecraft .util .Util ;
1920import org .jetbrains .annotations .NotNull ;
2021import pl .olafcio .avoid .net .entity .Entity ;
2122import pl .olafcio .avoid .net .entity .custom .Merchant ;
23+ import pl .olafcio .avoid .net .entity .sync .NetworkSync ;
2224import pl .olafcio .avoid_impl .net .entity .custom_internal .AvoidEntity ;
2325import pl .olafcio .avoid_impl .net .entity .custom_internal .AvoidLivingEntity ;
2426import pl .olafcio .avoid_impl .net .entity .custom_internal .AvoidMerchant ;
2527import pl .olafcio .avoid_impl .net .entity .custom_internal .EntityConstructor ;
2628import pl .olafcio .avoid .net .entity_type .properties .*;
2729import pl .olafcio .avoid .net .entity_type .properties .attachment .AttachmentOperation ;
2830import pl .olafcio .avoid .net .entity_type .properties .attribute .AttributeBase ;
31+ import pl .olafcio .avoid_impl .net .entity_type .sync .Controller ;
32+ import pl .olafcio .avoid_impl .net .entity_type .sync .Controllers ;
2933import pl .olafcio .avoid_impl .net .entity_type .values .AttachmentTypeNative ;
3034import pl .olafcio .avoid .net .entity_type .values .Category ;
3135import pl .olafcio .avoid_impl .net .entity_type .values .CategoryNative ;
3236import pl .olafcio .avoid .net .id .Identification ;
3337import pl .olafcio .avoid_impl .net .id .IdentificationNative ;
3438
39+ import java .lang .reflect .Field ;
3540import java .util .Arrays ;
3641import java .util .HashMap ;
3742import java .util .Optional ;
@@ -106,6 +111,7 @@ public static <T extends pl.olafcio.avoid.net.entity.Entity> void register(Ident
106111 var Type = createType (
107112 klass ,
108113 id ,
114+ AvoidMerchant .class ,
109115 (EntityType <@ NotNull Villager > type , Level level )
110116 -> new AvoidMerchant (type , level , (EntityConstructor <Merchant >) constructor ),
111117 category ,
@@ -121,6 +127,7 @@ public static <T extends pl.olafcio.avoid.net.entity.Entity> void register(Ident
121127 var Type = createType (
122128 klass ,
123129 id ,
130+ AvoidLivingEntity .class ,
124131 (EntityType <@ NotNull LivingEntity > type , Level level )
125132 -> new AvoidLivingEntity (type , level , (EntityConstructor <pl .olafcio .avoid .net .entity .custom .Entity >) constructor ),
126133 category ,
@@ -133,6 +140,7 @@ public static <T extends pl.olafcio.avoid.net.entity.Entity> void register(Ident
133140 createType (
134141 klass ,
135142 id ,
143+ AvoidEntity .class ,
136144 (EntityType <net .minecraft .world .entity .@ NotNull Entity > type , Level level )
137145 -> new AvoidEntity (type , level , (EntityConstructor <pl .olafcio .avoid .net .entity .custom .Entity >) constructor ),
138146 category ,
@@ -143,18 +151,52 @@ public static <T extends pl.olafcio.avoid.net.entity.Entity> void register(Ident
143151 }
144152 }
145153
154+ private static final HashMap <Class <?>, Integer > lastIDs
155+ = new HashMap <>();
156+
146157 private static <T extends net .minecraft .world .entity .Entity > @ NotNull EntityType <T > createType (
147158 Class <? extends pl .olafcio .avoid .net .entity .Entity > klass ,
148159 Identifier id ,
160+ Class <?> destination ,
149161 EntityType .EntityFactory <T > factory ,
150162 Category category ,
151163 EntityDimensions dimensions ,
152164 EntityAttachments .Builder attachments
153165 ) {
166+ var fields = klass .getDeclaredFields ();
167+ var synchs = new HashMap <Controller <?, ?, ?>, Field >();
168+
169+ // Minecraft tf is ts class shit
170+ int lastID = lastIDs .computeIfAbsent (destination , SynchedEntityData .ID_REGISTRY ::getLastIdFor );
171+
172+ for (var field : fields ) {
173+ if (field .isAnnotationPresent (NetworkSync .class )) {
174+ var serializer = Controllers .convert (field .getType ());
175+ int valueID = ++lastID ;
176+
177+ if (valueID > 254 ) {
178+ throw new IllegalArgumentException ("Data value id is too big with " + valueID + "! (Max is 254)" );
179+ }
180+
181+ var accessor = serializer .serializer .createAccessor (valueID );
182+ serializer .accessor = accessor ;
183+
184+ synchs .put (serializer , field );
185+ }
186+ }
187+
188+ // FIXME Doing this 2 times because otherwise it didn't work
189+ for (int i = 0 ; i < 2 ; i ++)
190+ SynchedEntityData .ID_REGISTRY .classToLastIdCache .put (
191+ destination ,
192+ Math .max (SynchedEntityData .ID_REGISTRY .classToLastIdCache .getOrDefault (destination , -1 ), lastID )
193+ );
194+
154195 EntityType <T > Type ;
155196
156197 // TODO: Should I use the builder? Guess we'll see when there are issues in cross-version compatibility ;d
157- Registry .register (BuiltInRegistries .ENTITY_TYPE , id , Type = new EntityType <T >(
198+ Registry .register (BuiltInRegistries .ENTITY_TYPE , id , Type = new AvoidEntityType <T >(
199+ synchs ,
158200 factory ,
159201 CategoryNative .convertFrom (category ),
160202 !klass .isAnnotationPresent (_noSave .class ),
0 commit comments