How to run code on command?

Discussion in 'Plugin Development' started by Cookies160, May 4, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Cookies160

    I've got some code I think will work, but it errors:
    Code:
    [FONT=Menlo][SIZE=11px]13:03:26 ERROR]: [Earthquake] Earthquake v1.0 attempted to register an invalid EventHandler method signature "public boolean com.cookies160.earthquake.earthquake.onCommand(org.bukkit.command.CommandSender,org.bukkit.command.Command,java.lang.String,java.lang.String[])" in class com.cookies160.earthquake.earthquake[/SIZE][/FONT]
    This is my code:
    Code:
    package com.cookies160.earthquake;
     
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.logging.Logger;
     
    import org.bukkit.Material;
    import org.bukkit.Server;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.FallingBlock;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
     
    public class earthquake extends JavaPlugin
      implements Listener
    {
      public void onEnable()
      {
        getLogger().info("Woot, it works!");
        getLogger().severe("No, you're crazy!");
        getServer().getPluginManager().registerEvents(this, this);
      }
      @EventHandler
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("earthquake")){
    for(Block b : event.blockList()){
    float x = 0.0f;
    float y = -1.0f;
    float z = 0.0f;
     
    FallingBlock fallingBlock = b.getWorld().spawnFallingBlock(b.getLocation(), b.getTypeId(), b.getData());
    fallingBlock.setDropItem(false);
    fallingBlock.setVelocity(new Vector(x, y, z));
     
    b.setType(Material.AIR);
    }
    return false;
     
    }}}
    
    If I'm right, I'm trying to run it like a command, and that won't work, correct?
    Either way, I'm trying to run the some code when "/earthquake" is executed. Any help?

    I feel like a huge noob...
     
  2. Offline

    joethebowl

    Cookies160 I am not completely sure, because the rest of the code DOES look correct, but I think the return must be true because the code thinks you are continuing the command in the the }}} of the ending brackets!
     
  3. Offline

    Cookies160

    I switched it to true, but it produces the same result. Weird...
    Also, it doesn't recognize the event in event.blockList
     
  4. Offline

    idontcare1025

    You don't need @EventHandler about onCommand()
     
    Cookies160 likes this.
  5. Offline

    Cookies160

    O.O thank you! it worked! But there's an internal error, an I'm not surprised. How would I go about setting a radius to this?
    Code:
    [13:33:56] [Server thread/INFO]: Cookies160 issued server command: /earthquake
    [13:33:56] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'earthquake' in plugin Earthquake v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:703) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:955) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    Caused by: java.lang.Error: Unresolved compilation problem: 
    event cannot be resolved
     
    at com.cookies160.earthquake.earthquake.onCommand(earthquake.java:39) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    ... 13 more
    
     
  6. Offline

    ZodiacTheories

    Cookies160

    Can I see your PluginCommand class?
     
  7. Offline

    Cookies160

    I posted my only class in the OP
     
  8. Offline

    ZodiacTheories

  9. Offline

    Cookies160

    Code:
    [FONT=Monaco][SIZE=11px]name: Earthquake[/SIZE][/FONT]
    [FONT=Monaco][SIZE=11px]main: com.cookies160.earthquake.earthquake[/SIZE][/FONT]
    [FONT=Monaco][SIZE=11px]version: 1.0[/SIZE][/FONT]
    [FONT=Monaco][SIZE=11px]description: >[/SIZE][/FONT]
    [FONT=Monaco][SIZE=11px]            Creates Earthquakes[/SIZE][/FONT]
    [FONT=Monaco][SIZE=11px]commands:[/SIZE][/FONT]
    [FONT=Monaco][SIZE=11px]  earthquake:[/SIZE][/FONT]
     
    [FONT=Monaco][SIZE=11px]    description: Makes Earthquake[/SIZE][/FONT]
    [FONT=Monaco][SIZE=11px]
    [/SIZE][/FONT]
     
  10. Offline

    ZodiacTheories

  11. Offline

    Cookies160

    Code:
    name: Earthquake
    main: com.cookies160.earthquake.earthquake
    version: 1.0
        description:
    Makes Earthquakes
    commands: earthquake
    description: Creates Earthquake
    
    Sorry, weird fonts...
     
  12. Offline

    ZodiacTheories

    Cookies160

    I think it should be:

    Code:
    name: Earthquake
    version: 1.0
    main: com.cookies160.earthquake.earthquake
    author: Cookies160
    description: Makes earthquakes
     
    commands:
        earthquake:
            usage: /<command>
            description: Creates Earthquake
    Some of it isn't mandatory, I just like it :3
     
  13. Offline

    ZekeMo

    @Cookies160
    Try commenting out this line from your onEnable()
    Code:
    getServer().getPluginManager().registerEvents(this, this);
    and this line just above your onCommand()
    Code:
    @EventHandler
     
  14. Offline

    Synapz

    In your code you are not implementing CommandExecutor. You are implementing Listener when there are no listeners.
     
  15. Offline

    NDUGAR

    ZodiacTheories, too many spaces, either 2 spaces or 2 then 4

    Code:
    name: Earthquake
    version: 1.0
    main: com.cookies160.earthquake.earthquake
    author: Cookies160
    description: Makes earthquakes
     
    commands:
      earthquake:
        usage: /<command>
        description: Creates Earthquake
     
  16. Offline

    ZodiacTheories

    NDUGAR

    Im not an expert :p

    Synapz

    Its his main class, you can't instantiate in your main class. He doesn't need CommandExecutor.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  17. Offline

    Rocoty

    So many weird suggestions around here. It is obvious that OP's latest error comes from trying to use a variable called event while there is none.
     
  18. Offline

    coasterman10

    Very few suggestions here actually attack the root of the problem.

    It appears you have in fact solved your issue with setting up onCommand properly, but now you're accessing a variable (event) that doesn't exist. Variables are not going to materialize out of thin air. Whatever event.blockList() is supposed to be needs to be defined or retrieved from other methods. It seems you're using it to iterate over blocks. I could further help with getting this variable if you could explain how you want to get these blocks.
     
  19. Offline

    NDUGAR

    xD neither am I, it's just YAML's syntax, it scuffs up if you have more than 4 or 2 so The order is either 2, 2, etc. or 2,4
     
  20. Offline

    Cookies160

    Everywhere I look people are using this method with nothing more than I. What am I missing, or has this method been removed or changed?
     
Thread Status:
Not open for further replies.

Share This Page