Uploaded image for project: 'Spigot'
  1. Spigot
  2. SPIGOT-7458

Exception when Entity CommandSender executes Vanilla command

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • None
    • None
    • None
    • >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
    • Yes

      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.

            Unassigned Unassigned
            WillKroboth Will Kroboth
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: