mojira.dev
MC-197241

Players can change the color of a wolf's collar even if they're not its owner

The Bug:

Players can change the color of a wolf's collar even if they're not its owner.

It's important to note that this concept is exclusive to wolves and cannot be seen with cats. This issue isn't present with cats because the isOwnedBy() method is called appropriately with this entity, therefore preventing other players from being able to interact with them.

Steps to Reproduce:

  1. Obtain any color dye and summon a wolf that is not your owner by using the command provided below.

    /summon minecraft:wolf ~ ~ ~ {Owner:"Notch"}
  2. Attempt to change the color of the wolf's collar by using the dye.

  3. Take note as to whether or not players can change the color of a wolf's collar even if they're not its owner.

Observed Behavior:

Players can change the color of a wolf's collar even if they're not its owner.

Expected Behavior:

Players would not be able to change the color of a wolf's collar if they're not its owner.

Code Analysis:

Code analysis by @unknown can be found below.

The following is based on a decompiled version of Minecraft 1.19.2 using MCP-Reborn.

net.minecraft.world.entity.animal.Wolf.java

public class Wolf extends TamableAnimal implements NeutralMob {
   ...
   public InteractionResult mobInteract(Player player, InteractionHand interactionHand) {
      ItemStack itemstack = player.getItemInHand(interactionHand);
      Item item = itemstack.getItem();
      if (this.level.isClientSide) {
         ...
      } else {
         if (this.isTame()) {
            ...
            DyeColor dyecolor = ((DyeItem)item).getDyeColor();
            if (dyecolor != this.getCollarColor() && this.isOwnedBy(player)) {
               this.setCollarColor(dyecolor);
               if (!player.getAbilities().instabuild) {
                  itemstack.shrink(1);
               }
               return InteractionResult.SUCCESS;
            }
            ...

If we look at the above class, we can see that there are two checks that are carried out before allowing the color of a wolf's collar to be changed. One of these checks is to see if the wolf is tamed and the other is to see if the player is holding a dye that's not already the same color as the wolf's collar at the time of the interaction. If these two requirements are met, the color of the wolf's collar can be changed. The game doesn't check if the player carrying out the interaction is the wolf's owner before allowing the color of its collar to be changed, therefore resulting in this problem occurring.

Fix:

Simply altering the appropriate existing "if" statement within this piece of code to check if the player carrying out the interaction is the wolf's owner before allowing the color of its collar to be changed will resolve this problem.

Current "if" statement:

if (dyecolor != this.getCollarColor())

Fixed "if" statement:

if (dyecolor != this.getCollarColor() && this.isOwnedBy(player))

Linked issues

Attachments

Comments 13

I think the bug here is about tamed wolfs not the cats. Since you are not allowed to interact with with other peoples pets. Can you change the description of the report to reflect this.

Can confirm. You don’t even need to play multiplayer to test this. Just use this command:

/summon wolf ~ ~ ~ {Owner:[I;123,123,123,123]}

(If the dog is actually yours, which is very, very, very unlikey, just change one of the numbers in the command.)

Can confirm in 20w51a.

Can confirm in 21w06a.

3 more comments

Can confirm in 1.17.1.

I am able to confirm this in 21w41a. The expected behavior would be that only the owner of the dog would be able to change the collar color.

I can confirm this in 1.18.1 and 22w07a.

It's important to note that this concept is exclusive to wolves and cannot be seen with cats. This issue isn't present with cats because the isOwnedBy() method is called appropriately, therefore preventing other players from being able to interact with them.

Here's a code analysis along with a potential fix regarding this issue.

Code Analysis:

The following is based on a decompiled version of Minecraft 1.18.1 using MCP-Reborn.

net.minecraft.world.entity.animal.Wolf.java

public class Wolf extends TamableAnimal implements NeutralMob {
   ...
   public InteractionResult mobInteract(Player $p, InteractionHand $ih) {
      ItemStack itemstack = $p.getItemInHand($ih);
      Item item = itemstack.getItem();
      if (this.level.isClientSide) {
         ...
      } else {
         if (this.isTame()) {
            ...
            DyeColor dyecolor = ((DyeItem)item).getDyeColor();
            if (dyecolor != this.getCollarColor()) {
               this.setCollarColor(dyecolor);
               if (!$p.getAbilities().instabuild) {
                  itemstack.shrink(1);
               }

               return InteractionResult.SUCCESS;
            }
         ...

If we look at the above class, we can see that only two checks are carried out before the game allows the color of a wolf's collar to be changed. These two checks are as follows:

  • The wolf must be tamed.

  • The player must interact with the wolf whilst holding a dye that's not already the color of their collar.

If these two requirements are met, the setCollarColor() method is called, which changes the color of the wolf's collar. Since the game doesn't check whether or not the player interacting with the wolf is its owner, this results in all players being allowed to change the color of a tamed wolf's collar.

Potential Fix:

Simply altering the appropriate "if" statement or adding a new one altogether to check the call the isOwnedBy() method before allowing players to change the color of a wolf's collar, should resolve this problem. The following line of code could be used in order to fix this:

if (this.isOwnedBy($p))

Can confirm in 1.18.2 and 22w19a. You can use the following command in order to more easily reproduce this issue.

/summon minecraft:wolf ~ ~ ~ {Owner:"Notch"}

Following on from my code analysis, I've double-checked my proposed fix and I can confidently confirm that it's fully functioning and works as expected, so I've attached two screenshots to this report, one of which shows the current code and the other that shows the fixed code. I feel this information may be quite insightful hence my reasoning for providing it. 🙂

[media][media]

Tinay

Avoma

mattiasselin

Confirmed

Gameplay

Normal

Items, Mob behaviour

wolf

1.16.1, 1.16.3, 1.16.4, 20w46a, 20w48a, ..., 22w19a, 1.19, 1.19.2, 1.19.3, 1.19.4

23w12a

Retrieved