P3pp3rF1y / Reliquary

The source code of Reliquary! That mod, with the magical swag, and a weapon, and stuff.
Other
44 stars 33 forks source link

[1.20.1-2.0.36.1199] NPE Crash in AI with Twilight Cloak and Hoglins #640

Closed TigerWalts closed 5 months ago

TigerWalts commented 1 year ago

Having the Twilight Cloak near Hoglins may result in a crash. This has been observed in ATM9 0.0.45. It can also be reproduced with just Reliquary.

  1. Create a world with cheats on
  2. Change your gamemode to creative and give yourself the Twilight Cloak
  3. Go somewhere dark enough for the cloak to give you the invisibility effect
  4. Give yourself a Hoglin spawn egg and use it to spawn a hoglin
  5. Change the gamemode to survival
  6. Drop the cloak and pick it up Repeat until the game crashes
  7. Reloading into the game will likely immediately crash if your saved state is to be near the hoglin in survival mode with the cloak
java.lang.NullPointerException: Ticking entity
    at java.util.Objects.requireNonNull(Objects.java:208) ~[?:?] {}
    at java.util.Optional.of(Optional.java:113) ~[?:?] {}
    at net.minecraft.world.entity.ai.behavior.declarative.MemoryAccessor.m_257512_(MemoryAccessor.java:30) ~[client-1.20.1-20230612.114412-srg.jar%23158!/:?] {re:classloading}
    at net.minecraft.world.entity.ai.behavior.StartAttacking.m_257271_(StartAttacking.java:37) ~[client-1.20.1-20230612.114412-srg.jar%23158!/:?] {re:classloading}
    at net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder$1.m_257808_(BehaviorBuilder.java:53) ~[client-1.20.1-20230612.114412-srg.jar%23158!/:?] {re:classloading}
    at net.minecraft.world.entity.ai.behavior.OneShot.m_22554_(OneShot.java:20) ~[client-1.20.1-20230612.114412-srg.jar%23158!/:?] {re:classloading}
    at net.minecraft.world.entity.ai.Brain.m_21957_(Brain.java:533) ~[client-1.20.1-20230612.114412-srg.jar%23158!/:?] {re:classloading}
    at net.minecraft.world.entity.ai.Brain.m_21865_(Brain.java:492) ~[client-1.20.1-20230612.114412-srg.jar%23158!/:?] {re:classloading}
    at net.minecraft.world.entity.monster.hoglin.Hoglin.m_8024_(Hoglin.java:124) ~[client-1.20.1-20230612.114412-srg.jar%23158!/:?] {re:classloading}
        ...
    at net.minecraft.world.level.entity.EntityTickList.m_156910_(EntityTickList.java:54) ~[client-1.20.1-20230612.114412-srg.jar%23158!/:?] {re:classloading}

I checked the hoglin's save data between a crashing state and a normal state and their 'brain` tags are the same, containing an empty memories collection.

Because it didn't crash each time the invisibility effect was applied there may be a race involved.

The MobHelper.resetTarget() call may need to be run as a task at another point in the tick cycle, even though I can't see why it could cause this crash as it doesn't appear to remove any memories and sets them to Empty.

https://github.com/P3pp3rF1y/Reliquary/blob/32deb62f8982941faa6f0837391e42314a48bc45/src/main/java/reliquary/util/MobHelper.java#L19-L39

grundyboy34 commented 1 year ago

This affects the twilight cloak and the mob charm items. The problem is that both use event.setNewTarget(null); in the LivingChangeTargetEventwhen they should be simply cancelling the event via event.setCanceled(true);

TigerWalts commented 9 months ago

For those running into this problem and need a fix, you can disable the item in the player's Curios inventory as follows:

  1. Open the player's save data in an NBT editor:
  2. Locate the Cloak or Charm in the Curios inventory
    • The tag path to the inventory is: ForgeCaps>curios:inventory>Curios
    • Inside each Curios inventory you'll find the items in the tag path StacksHandler>Stacks>Items
  3. Once you have found the item, open the tag tag and set the enabled tag inside it to 0

CloakFix

If the item is in the main Inventory tag and is passively enabled, you may have to resort to deleting it.