Carpet

Carpet

2M Downloads

Scarpet: Add entity event `on_transformed` or `on_converted` for when an entity is converted to another entity.

James103 opened this issue ยท 10 comments

commented

Currently, when an entity converts into another entity, there is no clean way of copying e.g. scoreboard or other data from the old entity to the new entity without hardcoding all cases where an entity may convert to another.

This could be resolved with a new entity event on_transformed (alternative name: on_converted), which would be called whenever an entity converts to another with two parameters: source_entity for the entity to be converted and target_entity for the entity that the source_entity is converting into.

For example, when a Zombie Villager converts to a Villager, the on_transformed event would be called with the Zombie Villager right before conversion as source_entity and the Villager about to be spawned as target_entity. The event handler could then migrate scoreboard data from the source_entity to the target_entity, allowing various villager-specific statistics to be preserved through conversion.

There are two possible use cases I can think of for this entity event:

  1. Fix MC-88967, in part or in full.
  2. Make it easier to migrate additional data (provided by, for example, mob-levelling apps) between entities as they are converted (without this, a bunch of additional Scarpet code would need to be written to account for as many conversion situations from the below list as possible).

The following cases would need to be handled, as listed in the linked bug report above:

  • Zombie turning into Drowned when underwater
  • Husk turning into Zombie when underwater
  • Mushroom turning into Cow when sheared
  • Slime and Magma Cube splitting into smaller variants of themselves
  • Villager being zombified (creating a Zombie Villager)
  • Zombie Villager being cured (creating a Villager)
  • Piglin, Piglin Brute, and Hoglin zombifying when in the Overworld (creating Zombified Piglin or Zoglin)
  • Villager turning into Witch when struck by lightning
  • Skeleton turning into Stray when freezing
  • Tadpole growing up into a Frog
commented

I will work on this and see if I can make it, and it's any good

commented

Ok I have a solid version of it here:
https://github.com/Ghoulboy78/fabric-carpet/tree/transform-event

A bunch of them were rly easy cos they use the Mob#convertTo method, so I just had to inject there.
The rest I'll have to do with individual injections, cos Im not 100% sure which ones need them.

Here are all the ones from the list it can do so far:

  • Zombie -> Drowned
  • Husk -> Zombie
  • Mooshroom -> Cow
  • Slime / Lava slime splitting (Not sure about this one ngl)
  • Villager -> Zombie Villager
  • Zombie Villager -> Villager
  • Piglin /Piglin Brute -> Pigman
  • Hoglin -> Zoglin
  • Villager -> Witch
  • Skeleton -> Stray
  • Tadpole -> Frog
  • Pig -> Pigman
commented

Does that Mob#convertTo method (for which the event is being fired from) also handle Hoglin -> Zoglin?

commented

ooh, don't know

Edit: I think so, cos it uses the same method as piglins -> pigmen
I'll test to be safe tho

commented

Just remembered Pigs -> Pigmen via lightning, it appears even the Mojira bug author forgot

commented

Ok I have added the remaining events, I just need to test them

commented

Ok all of them work, except for some reason mooshroom shearing triggers once for the actual conversion, and then like 5 other times for all the mushroom items that get dropped.
OH wait I realised why im dumb

commented

ok @James103 it's all done

commented

Well in the case of Slimes and Magma cubes, it's less so a conversion and more so a splitting, it's not 1 entity becoming another entity, it's one entity becoming many entities, so it makes sense that they get treated differently. But for the rest I agree.

commented

@Ghoulboy78 Are you still willing to PR the feature you developed? A Scarpet app I'm developing requires the feature to facilitate easier transfer of scoreboard objectives and other data during entity transformations, including custom health, level, and other stats.

There are no conflicts with the base branch as far as I can tell. If you can create the PR, I will be able to test the feature with a sample Scarpet app.

Edit: Clicked wrong button causing an accidental close/reopen.