Solved How to disable entity attack with command?

Discussion in 'Spigot Plugin Development' started by MrDefault, Feb 5, 2021.

  1. Here is my error code.
    Code (Text):
    package entity.cancel;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {

        public EntityTargetLivingEntityEvent ent;

        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this,this);
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if(command.getName().equalsIgnoreCase("monsterignore")) {
                    ent.setCancelled(true);
                }
            }


            return false;
        }

        @EventHandler
        public void entCancel(EntityTargetLivingEntityEvent e) {
            ent = e;
            // e.setCancelled(true);
        }
    }
     
    and it's an error like this when /monsterignore execute
    Code (Text):
    [14:50:23] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'zombieig' in plugin zombiecancel v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:763) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.PlayerConnection.handleCommand(PlayerConnection.java:1788) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:1631) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1584) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:942) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:935) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:919) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:851) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_281]
    Caused by: java.lang.NullPointerException
            at entity.cancel.cancelCommand.onCommand(cancelCommand.java:21) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            ... 19 more
     
    • Useful Useful x 1
  2. Thank you. I'll try it out
     
  3. It still not work. Can you give me an example?
     
  4. You can't cancel events that way. You should have a boolean field in your listener class that can be toggled from command, like this way:
    Code (Java):
    // Main
    class Main extends JavaPlugin {

        private MyListener listener;

        public void onEnable() {
            myListener = new MyListener();
            // register listener
        }
     
        // ...
     
        public boolean onCommand(...) {
            // ...
            listener.toggleEntityTarget();
            // ...
        }
    }
    Then the listener will be something like this
    Code (Java):
    class MyListener implements Listener {
     
        private boolean cantTargetEntities;
     
        @EventHandler
        public void onEntityTarget(EntityTargetLivingEntityEvent event) {
            event.setCancelled(cantTargetEntities);
        }
     
        public void toggleEntityTarget() {
            cantTargetEntities = !cantTargetEntities;
        }
    }
     
    #5 TheSniper99, Feb 5, 2021
    Last edited: Feb 5, 2021
    • Useful Useful x 1
  5. Thank you for your explanation, makes me understand. :)
     
    • Friendly Friendly x 1