[SPIGOT-7458] Exception when Entity CommandSender executes Vanilla command Created: 11/Aug/23  Updated: 25/Dec/24  Resolved: 12/Aug/23

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Will Kroboth Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None

Attachments: Java Archive File BukkitTestPlugin-1.0-SNAPSHOT.jar    
Version: >version [16:02:42] [Server thread/INFO]: This server is running CraftBukkit version 3854-Spigot-3bce793-46c7fc3 (MC: 1.20.1) (Implementing API version 1.20.1-R0.1-SNAPSHOT) [16:02:42] [Server thread/INFO]: Checking version, please wait... [16:02:42] [Th
Guidelines Read: Yes

 Description   

I used this test plugin to reproduce the issue:

public final class BukkitTestPlugin extends JavaPlugin implements Listener {
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onEntityDamage(EntityDamageEvent event) {
        getLogger().info("Entity damaged");

        Entity entity = event.getEntity();
        entity.addAttachment(this, "minecraft.command.say", true);

        Bukkit.dispatchCommand(entity, "say Ouch!");
    }
} 

The EntityDamageEvent is not important, it is just the first way I could think of to get an Entity. This issue seems to apply any time an Entity object is given as the sender to Bukkit#dispatchCommand, where the command in question is provided by Vanilla Minecraft,

Triggering the problem by damaging an entity (I punched a sheep) gives something like the following stack trace:

Caused by: java.lang.IllegalArgumentException: Cannot make CraftSheep a vanilla command listener
        at org.bukkit.craftbukkit.v1_20_R1.command.VanillaCommandWrapper.getListener(VanillaCommandWrapper.java:86) ~[spigot-1.20.1-R0.1-SNAPSHOT.jar:3854-Spigot-3bce793-46c7fc3]
        at org.bukkit.craftbukkit.v1_20_R1.command.VanillaCommandWrapper.execute(VanillaCommandWrapper.java:44) ~[spigot-1.20.1-R0.1-SNAPSHOT.jar:3854-Spigot-3bce793-46c7fc3]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-api-1.20.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.craftbukkit.v1_20_R1.CraftServer.dispatchCommand(CraftServer.java:875) ~[spigot-1.20.1-R0.1-SNAPSHOT.jar:3854-Spigot-3bce793-46c7fc3]
        at org.bukkit.Bukkit.dispatchCommand(Bukkit.java:884) ~[spigot-api-1.20.1-R0.1-SNAPSHOT.jar:?]
        at me.willkroboth.bukkittestplugin.BukkitTestPlugin.onEntityDamage(BukkitTestPlugin.java:23) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-api-1.20.1-R0.1-SNAPSHOT.jar:?]
        ... 33 more 

This is the full server log, including the full stack trace.

 

This seems to happen because VanillaCommandWrapper#getListener does not take into account that CommandSender may be instanceof Entity. To fix this issue, the following branch probably needs to be added to that method:

if (sender instanceof Entity) {
    return ((CraftEntity) sender).getHandle().createCommandSourceStack();
} 

Note that the Player and CommandMinecart are also instanceof Entity, so this check should probably be located after both of those to avoid interfering with their logic, or the cases could be merged.

Alternatively, if the VanillaCommandWrapper#getListener method was refactored to not use this if-instanceof pattern and instead rely on subclass method overloading, this issue would also be fixed, and it would probably be easier to notice similar issues in the future.



 Comments   
Comment by Will Kroboth [ 12/Aug/23 ]

Fixed by https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/commits/e795d7490310a2cbc8813d6fa72162f531683aa5

Generated at Fri Apr 11 16:22:16 UTC 2025 using Jira 10.3.3#10030003-sha1:d220e3fefc8dfc6d47f522d3b9a20c1455e12b7b.