Bassebombecraft

Bassebombecraft

18.5k Downloads

SpawnWarPig2 failed with: java.lang.NullPointerException @ bassebombecraft.entity.EntityUtils.setAttribute

athrane opened this issue ยท 3 comments

commented

Observed on the 26/8/21 via analytics for 1.16.5-2.1:
Reproduced in client.
Stack trace:

[26Aug2021 16:50:32.397] [Server thread/ERROR] [bassebombecraft.BassebombeCraft/]: java.lang.NullPointerException
	at bassebombecraft.entity.EntityUtils.setAttribute(EntityUtils.java:456)
	at bassebombecraft.operator.entity.SpawnWarPig2.run(SpawnWarPig2.java:89)
	at bassebombecraft.operator.Operators2.run(Operators2.java:20)
	at bassebombecraft.operator.Operators2.run(Operators2.java:39)
	at bassebombecraft.operator.Sequence2.run(Sequence2.java:71)
	at bassebombecraft.operator.Operators2.run(Operators2.java:20)
	at bassebombecraft.item.action.inventory.ExecuteOperatorOnTarget2.applyEffect(ExecuteOperatorOnTarget2.java:58)
	at bassebombecraft.item.inventory.GenericInventoryItem.applyEffect(GenericInventoryItem.java:191)
	at bassebombecraft.item.inventory.GenericInventoryItem.inventoryTick(GenericInventoryItem.java:152)
	at net.minecraft.item.ItemStack.inventoryTick(ItemStack.java:499)
	at net.minecraft.entity.player.PlayerInventory.tick(PlayerInventory.java:265)
	at net.minecraft.entity.player.PlayerEntity.livingTick(PlayerEntity.java:514)
	at net.minecraft.entity.LivingEntity.tick(LivingEntity.java:2305)
	at net.minecraft.entity.player.PlayerEntity.tick(PlayerEntity.java:227)
	at net.minecraft.entity.player.ServerPlayerEntity.playerTick(ServerPlayerEntity.java:422)
	at net.minecraft.network.play.ServerPlayNetHandler.tick(ServerPlayNetHandler.java:212)
	at net.minecraft.network.NetworkManager.tick(NetworkManager.java:248)
	at net.minecraft.network.NetworkSystem.tick(NetworkSystem.java:151)
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:899)
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:821)
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:84)
	at net.minecraft.server.MinecraftServer.func_240802_v_(MinecraftServer.java:664)
	at net.minecraft.server.MinecraftServer.lambda$startServer$0(MinecraftServer.java:233)
	at java.lang.Thread.run(Thread.java:748)

commented

The SpawnWarPig2 operator attempts to set these attributes:

		setAttribute(entity, MOVEMENT_SPEED, movementSpeed);
		setAttribute(entity, ATTACK_DAMAGE, damage);

Only these are defined as mutable attributes in the Pig class:

   public static AttributeModifierMap.MutableAttribute func_234215_eI_() {
      return MobEntity.func_233666_p_().createMutableAttribute(Attributes.MAX_HEALTH, 10.0D).createMutableAttribute(Attributes.MOVEMENT_SPEED, 0.25D);
   }

commented

Solution:

Remove modification of immutable attribute ATTACK_DAMAGE.
Can either be implemented using:

  • AttackDamageEffect, see the STRENGTH potion in the Effects class.
  • Override the registerAttributes in the pig class (see the Bee class)
  • add attribute modifier

Attribute modified doesn't work because the pig entity doesn't ATTACK_DAMAGE as a registered attribute:

AttributeModifier am = new AttributeModifier("warpig increased attack", damage, AttributeModifier.Operation.ADDITION);
ModifiableAttributeInstance attributeInstance = entity.getAttribute(ATTACK_DAMAGE);
attributeInstance.applyPersistentModifier(am);

Final solution is to assign ATTACK_DAMAGE attribute to the PigEntity entity:

@Mod.EventBusSubscriber(modid = MODID, bus = MOD)
public class EntityAttributeEventHandler {

  @SubscribeEvent
  static public void handleEntityAttributeModificationEvent(EntityAttributeModificationEvent event) {
   ...		
    addAttackDamageAttributeToPigEntity(event);		
  }

  static void addAttackDamageAttributeToPigEntity(EntityAttributeModificationEvent event) {
    event.add(EntityType.PIG, ATTACK_DAMAGE);
   }	
}

For more info see: Attribute page on the wiki

commented

Resolved with commit: 28302d8