Posted February 11, 20214 yr Hello! For some reason when adding multiple spawn eggs for my entities, only the last one registered renders in the inventory with its proper colors. All eggs registered before the last on appear white and I can't figure out why. Item register code: https://github.com/Phrille/Vanilla-Boom/blob/master/src/main/java/phrille/vanillaboom/init/ModItems.java My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 11, 20214 yr Author Bump My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 11, 20214 yr That's because the spawn egg entry being pushed into the map keep replacing the null key value. As such, only the last egg is ever considered. You need to reflect into the map to add your spawn eggs with the corresponding entity type during common setup.
February 11, 20214 yr Author Alright that makes sense. Is this thread safe to do or should I use event.enqueueWork? My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 12, 20214 yr Author Can't get this to work yet: public static void addSpawnEgg(IItemProvider item) { if (item instanceof EntitySpawnItem) { EntitySpawnItem egg = (EntitySpawnItem) item; try { Field field = ObfuscationReflectionHelper.findField(SpawnEggItem.class, "field_195987_b"); Map<EntityType<?>, SpawnEggItem> map = (Map<EntityType<?>, SpawnEggItem>) field.get(null); map.put(egg.getType(null), egg); } catch (IllegalAccessException | IllegalArgumentException e) { throw new RuntimeException("Could not add " + item.asItem().getRegistryName().toString() + " to spawn eggs map", e); } } } This is called from FMLCommonSetup: event.enqueueWork(() -> { Utils.addSpawnEgg(ModItems.PERCH_SPAWN_EGG); Utils.addSpawnEgg(ModItems.EEL_SPAWN_EGG); Utils.addSpawnEgg(ModItems.PIKE_SPAWN_EGG); Utils.addSpawnEgg(ModItems.TUNA_SPAWN_EGG); Utils.addSpawnEgg(ModItems.SWAMP_DWELLER_SPAWN_EGG); }); My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 12, 20214 yr 3 hours ago, Godis_apan said: Field field = ObfuscationReflectionHelper.findField(SpawnEggItem.class, "field_195987_b"); Map<EntityType<?>, SpawnEggItem> map = (Map<EntityType<?>, SpawnEggItem>) field.get(null); Just store the map as a map with one reflection, this is just unnecessary. Use breakpoints to determine whether or not the entries are being put in the map. Also, the null entry within the map should be removed afterwards.
February 12, 20214 yr Author Okay! Checked with breakpoints and as far as I could tell the map is modified but still no result. What am I doing wrong here? public static final Map<EntityType<?>, SpawnEggItem> EGG_MAP = Maps.newHashMap(); public static void addSpawnEggs() { try { Map<EntityType<?>, SpawnEggItem> map = (Map<EntityType<?>, SpawnEggItem>) ObfuscationReflectionHelper.findField(SpawnEggItem.class, "field_195987_b").get(null); map.keySet().removeIf(Objects::isNull); map.putAll(EGG_MAP); } catch (IllegalAccessException | IllegalArgumentException e) { throw new RuntimeException("Failed to spawn eggs to map", e); } } Edited February 12, 20214 yr by Godis_apan My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 12, 20214 yr You are not storing the map itself but now a copy. Store the private value of the map and put elements into that. Edited February 12, 20214 yr by ChampionAsh5357
February 12, 20214 yr Author I'm sorry to be such a dum-dum here but I can't see how I would do this. Wouldn't this be better to do with ATs? Or is that bad? Thanks for the help it's very much appreciated. My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 13, 20214 yr 8 hours ago, Godis_apan said: Wouldn't this be better to do with ATs? Not really, you are only using the value once so it'd be a waste to AT into the field. As I've mentioned, use ObfuscationReflectionHelper::getPrivateValue and store the map. Then, do the required operations on the map itself. Since we're changing the elements the map refers to, we can just store the overarching object.
February 13, 20214 yr Author Okay Im using that method now but how do I store the map? Do I need to use setPrivateValue afterwards? Map<EntityType<?>, SpawnEggItem> map = (Map<EntityType<?>, SpawnEggItem>) ObfuscationReflectionHelper.getPrivateValue(SpawnEggItem.class, null, "field_195987_b"); map.keySet().removeIf(Objects::isNull); map.putAll(EGG_MAP); My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 14, 20214 yr 12 hours ago, Godis_apan said: Okay Im using that method now but how do I store the map? Do I need to use setPrivateValue afterwards? It's a map... Modifying information within an object doesn't require you to replace it. Only if you're modifying the outer object itself.
February 14, 20214 yr Author Well it still isn't working, all the eggs except the last still appear white My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 16, 20214 yr Author I have, still no result. My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 16, 20214 yr Then I would put a breakpoint where ItemColors calls the egg list to add them and see if your eggs are present within there.
February 18, 20214 yr Author Okay so I debugged this and it seems that the ItemColors registers before I reflect into the map. When I added a breakpoint in the ItemColors only the last egg I added showed up in the for loop where SpawnEggItem#getEggs() gets called. But when I run the same for loop after my reflection has taken place my entities also shows up. So how do I reflect into the map before ItemColors are registered? https://github.com/Phrille/Vanilla-Boom/blob/master/src/main/java/phrille/vanillaboom/util/Utils.java Edited February 18, 20214 yr by Godis_apan My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 20, 20214 yr Author Bump Edited February 22, 20214 yr by Godis_apan My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
February 22, 20214 yr Author Bum again My mod for futher awesomeness: http://www.minecraftforum.net/topic/1714396-the-decopack-collection-v010-wip-made-a-signature-new-snapshot-1-screenshots-are-up-small-snapshot-1-is-out-for-147/#entry21250399
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.