mojira.dev
MC-168188

Trader llama 'DespawnDelay' tag is active, even when spawned with a spawn egg/commands

The bug

The trader llama's DespawnDelay tag is active, even when spawned with commands or with their own spawn egg. After spawning a trader llama with commands or spawn egg, the tag starts at 47999 and begins to go down to 0 instead of the tag being default to 0 similar to wandering traders spawned by commands and their own spawn egg (and also makes them not despawn).

How to reproduce

  1. Spawn trader llama using a spawn egg (untamed)

  2. Run

    /data get entity @e[type=trader_llama,limit=1] DespawnDelay

    → ❌ The trader llama's DespawnDelay tag is active as it is not default to 0.

  3. Spawn a wandering trader with a spawn egg

  4. Use the /data get again
    → ❌ It's DespawnDelay is default to 0

Code Analysis

Code Analysis done by @unknown

This issue is that there is no check if the despawn delay is greater then zero allowing the counter to go down. Along with this the default value of the despawnDelay is always 47999 by default. This should be set when the trader llama spawns via wandering trader event

Current Code

net/minecraft/world/entity/animal/horse/TraderLlama.java

private void maybeDespawn() {
      if (this.canDespawn()) {
         this.despawnDelay = this.isLeashedToWanderingTrader() ? ((WanderingTrader)this.getLeashHolder()).getDespawnDelay() - 1 : this.despawnDelay - 1;

         if (this.despawnDelay <= 0) {
            this.dropLeash(true, false);
            this.discard();
         }

      }
   }

net/minecraft/world/entity/npc/WanderingTraderSpawner.java

private void tryToSpawnLlamaFor(ServerLevel p_35918_, WanderingTrader p_35919_, int p_35920_) {
      BlockPos blockpos = this.findSpawnPositionNear(p_35918_, p_35919_.blockPosition(), p_35920_);
      if (blockpos != null) {
         TraderLlama traderllama = EntityType.TRADER_LLAMA.spawn(p_35918_, (CompoundTag)null, (Component)null, (Player)null, blockpos, MobSpawnType.EVENT, false, false);
         if (traderllama != null) {
            traderllama.setLeashedTo(p_35919_, true);
         }
      }
   }

Fixed Code

net/minecraft/world/entity/animal/horse/TraderLlama.java

//Setting this despawnDelay to nothing allows for it to be zero by default helping fix MC-168188
   private int despawnDelay;
   ...
   private void maybeDespawn() {
      if (this.canDespawn()) {
         //Removing this line stops the constat updates to the despawn delay helping fix MC-168188 & MC-210224
         //this.despawnDelay = this.isLeashedToWanderingTrader() ? ((WanderingTrader)this.getLeashHolder()).getDespawnDelay() - 1 : this.despawnDelay - 1;

         //Doing a check for if the despawnDelay is greater then zero and then subtracting it helps fix MC-168188 & MC-210224
         if (this.despawnDelay > 0 && --this.despawnDelay == 0) {
            this.dropLeash(true, false);
            this.discard();
         }

      }
   }

net/minecraft/world/entity/npc/WanderingTraderSpawner.java

private void tryToSpawnLlamaFor(ServerLevel p_35918_, WanderingTrader p_35919_, int p_35920_) {
      BlockPos blockpos = this.findSpawnPositionNear(p_35918_, p_35919_.blockPosition(), p_35920_);
      if (blockpos != null) {
         TraderLlama traderllama = EntityType.TRADER_LLAMA.spawn(p_35918_, (CompoundTag)null, (Component)null, (Player)null, blockpos, MobSpawnType.EVENT, false, false);
         if (traderllama != null) {
            //Setting the trader llama despawn delay here still allows it to despawn when spawned by wander trader so MC-168188 & MC-210224 fix doesn't affect it
            traderllama.setDespawnDelay(47999);
            traderllama.setLeashedTo(p_35919_, true);
         }
      }
   }

Linked issues

Attachments

Comments 3

This is technically a suggestion, but a possible fix might be using something like the endermite's PlayerSpawned: NBT tag.

Can confirm, the trader llama's despawndelay is active even when it's not supposed to be. This occurs with eggs too (and it shouldn't because it's an egg). Setting the delay to 0 causes instant despawn instead of the permanent spawn like it does for the trader. This bug occurs in 1.16 pre-2.

In 1.18.2 and 22w16b.

DrownedZombie

(Unassigned)

Confirmed

Platform

Low

Commands, Mob behaviour

mob-behavior, trader_llama

1.15, 1.15.1, 1.15.1 Pre-release 1, 1.15.2 Pre-release 2, 1.15.2, ..., 1.17, 1.17.1 Pre-release 1, 1.18.2, 22w16b, 1.19.3

Retrieved