1818import com .google .gson .JsonObject ;
1919import com .google .gson .JsonPrimitive ;
2020
21+ import net .minecraft .client .MinecraftClient ;
2122import net .minecraft .client .network .ClientPlayerEntity ;
23+ import net .minecraft .client .network .ClientPlayNetworkHandler ;
2224import net .minecraft .entity .Entity ;
25+ import net .minecraft .entity .EntityType ;
2326import net .minecraft .entity .SpawnGroup ;
2427import net .minecraft .entity .attribute .EntityAttributes ;
2528import net .minecraft .entity .player .PlayerInventory ;
2831import net .minecraft .item .MaceItem ;
2932import net .minecraft .item .TridentItem ;
3033import net .minecraft .registry .Registries ;
34+ import net .minecraft .registry .Registry ;
35+ import net .minecraft .registry .RegistryKeys ;
36+ import net .minecraft .registry .DynamicRegistryManager ;
3137import net .minecraft .registry .tag .ItemTags ;
3238import net .minecraft .util .Identifier ;
3339import net .wurstclient .WurstClient ;
@@ -44,7 +50,7 @@ public final class MobWeaponRuleSetting extends Setting
4450{
4551 private static final MobOption ANY_OPTION =
4652 new MobOption ("any" , "Any mob" , null );
47- private static final List <MobOption > MOB_OPTIONS = buildMobOptions () ;
53+ private static volatile List <MobOption > mobOptionsCache ;
4854
4955 private MobOption selectedMob = ANY_OPTION ;
5056 private WeaponCategory selectedWeapon = WeaponCategory .NONE ;
@@ -65,7 +71,7 @@ public Component getComponent()
6571
6672 public List <MobOption > getMobOptions ()
6773 {
68- return MOB_OPTIONS ;
74+ return getOrCreateMobOptions () ;
6975 }
7076
7177 public MobOption getSelectedMob ()
@@ -171,30 +177,88 @@ public Set<PossibleKeybind> getPossibleKeybinds(String featureName)
171177
172178 private static MobOption findMobOption (String id )
173179 {
174- for (MobOption option : MOB_OPTIONS )
180+ for (MobOption option : getOrCreateMobOptions () )
175181 if (option .id ().equals (id ))
176182 return option ;
177183
178184 return ANY_OPTION ;
179185 }
180186
187+ private static List <MobOption > getOrCreateMobOptions ()
188+ {
189+ List <MobOption > cached = mobOptionsCache ;
190+ int cachedSize = cached == null ? 0 : cached .size ();
191+ if (cached == null || cachedSize <= 1 )
192+ {
193+ List <MobOption > rebuilt = buildMobOptions ();
194+ if (rebuilt .size () > 1 )
195+ {
196+ mobOptionsCache = rebuilt ;
197+ return rebuilt ;
198+ }
199+
200+ return rebuilt ;
201+ }
202+
203+ return cached ;
204+ }
205+
181206 private static List <MobOption > buildMobOptions ()
182207 {
183208 List <MobOption > options = new ArrayList <>();
184209 options .add (ANY_OPTION );
185- Registries .ENTITY_TYPE .getIds ().stream ()
186- .map (Registries .ENTITY_TYPE ::get )
187- .filter (type -> type .getSpawnGroup () != SpawnGroup .MISC )
188- .map (type -> {
189- Identifier id = Registries .ENTITY_TYPE .getId (type );
190- String name = type .getName ().getString ();
191- return new MobOption (id .toString (), name , type );
192- }).sorted (Comparator .comparing (MobOption ::displayName ,
193- String .CASE_INSENSITIVE_ORDER ))
194- .forEach (options ::add );
210+
211+ Registry <EntityType <?>> registry = resolveEntityRegistry ();
212+ for (Identifier id : registry .getIds ())
213+ {
214+ EntityType <?> type = registry .get (id );
215+ if (type == null || type .getSpawnGroup () == SpawnGroup .MISC )
216+ continue ;
217+
218+ String name = type .getName ().getString ();
219+ options .add (new MobOption (id .toString (), name , type ));
220+ }
221+
222+ options .sort (Comparator .comparing (MobOption ::displayName ,
223+ String .CASE_INSENSITIVE_ORDER ));
195224 return Collections .unmodifiableList (options );
196225 }
197226
227+ private static Registry <EntityType <?>> resolveEntityRegistry ()
228+ {
229+ MinecraftClient mc = WurstClient .MC ;
230+ if (mc != null )
231+ {
232+ if (mc .world != null )
233+ {
234+ Registry <EntityType <?>> worldRegistry =
235+ getRegistryFromManager (mc .world .getRegistryManager ());
236+ if (worldRegistry != null )
237+ return worldRegistry ;
238+ }
239+
240+ ClientPlayNetworkHandler handler = mc .getNetworkHandler ();
241+ if (handler != null )
242+ {
243+ Registry <EntityType <?>> networkRegistry =
244+ getRegistryFromManager (handler .getRegistryManager ());
245+ if (networkRegistry != null )
246+ return networkRegistry ;
247+ }
248+ }
249+
250+ return Registries .ENTITY_TYPE ;
251+ }
252+
253+ private static Registry <EntityType <?>> getRegistryFromManager (
254+ DynamicRegistryManager manager )
255+ {
256+ if (manager == null )
257+ return null ;
258+
259+ return manager .getOptional (RegistryKeys .ENTITY_TYPE ).orElse (null );
260+ }
261+
198262 public record MobOption (String id , String displayName ,
199263 net .minecraft .entity .EntityType <?> type )
200264 {
0 commit comments