Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-168188

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • None
    • 1.15, 1.15.1, 1.15.1 Pre-release 1, 1.15.2 Pre-release 2, 1.15.2, 20w06a, 20w08a, 20w09a, 20w11a, 20w12a, 20w13b, 20w18a, 20w19a, 20w20b, 20w21a, 1.16 Pre-release 2, 1.16 Pre-release 5, 1.16 Pre-release 6, 1.16 Pre-release 7, 1.16 Release Candidate 1, 1.16, 1.16.1, 20w27a, 20w30a, 1.16.2 Pre-release 1, 1.16.2, 1.16.3 Release Candidate 1, 1.16.3, 1.16.4 Release Candidate 1, 1.16.4, 20w46a, 20w51a, 1.16.5, 21w05a, 21w05b, 21w06a, 21w07a, 21w11a, 1.17, 1.17.1 Pre-release 1, 1.18.2, 22w16b, 1.19.3
    • Confirmed
    • Commands, Mob behaviour
    • Low
    • Platform

      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 Thumpbacker

      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);
               }
            }
         }
      

            Unassigned Unassigned
            drownedzombie01 DrownedZombie
            Votes:
            10 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              CHK: