.getAction?

Discussion in 'Plugin Development' started by Bernabeast, Jun 18, 2013.

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

    Bernabeast

    Im trying to get event.getAction() to work, but where do i get getAction from? Where do i import it from? Here is the code.

    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.entity.Snowball;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerInteractEvent.getAction;

    public class Snowstick extends JavaPlugin implements Listener {

    @EventHandler
    public void onPlayerEvent(PlayerEvent event){
    Player player = event.getPlayer();
    if(event.getAction == Action.RIGHT_CLICK_AIR)


    }
     
  2. Offline

    Shzylo

    First off, Action is undefined for PlayerEvent, use PlayerInteractEvent instead.
    It would be better if you made a new variable:

    Code:java
    1. public class Snowstick exends JavaPlugin implements Listener {
    2.  
    3. @EventHandler
    4. public void onPlayerEvent(PlayerInteractEvent event) {
    5. Player p = event.getPlayer();
    6. Action act = event.getAction();
    7.  
    8. if(act == Action.RIGHT_CLICK_AIR) {
    9. //do stuff
    10. }
    11. }

    And don't forget to register the event.
     
  3. Offline

    Bernabeast

    "getAction cannot be resolved or is not a field" is the error under .getAction

    Here is the new Code:

    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.entity.Snowball;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class Snowstick extends JavaPlugin implements Listener {

    @EventHandler
    public void onPlayerEvent(PlayerInteractEvent event){
    Player player = event.getPlayer();
    if(event.getAction == Action.RIGHT_CLICK_AIR)


    }



    }
     
  4. Offline

    bitWolfy

    Bernabeast You need to learn Java before writing plugins. getAction() is a method, not a variable, and should be accessed as such.
     
    Cirno and TheRealNovus like this.
  5. Offline

    Shzylo

    In your if statement: if(event.getAction == Action.RIGHT_CLICK_AIR)
    add parenthesis: event.getAction().
     
  6. Offline

    Bernabeast

    Now, upon using player.launchProjectile(Snowball)
    Its says (Snowball) "Snowball cannot be resolved to a variable"
    Why must it have a Variable, why not just shoot the freaking Snowball?
    Here is the new code:

    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.entity.Snowball;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class Snowstick extends JavaPlugin implements Listener {

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    Player p = (Player) sender;
    if (cmd.getName().equalsIgnoreCase("snowstick"));
    ItemStack item = new ItemStack(Material.STICK);
    PlayerInventory pi = p.getInventory();
    pi.addItem(item);
    return false;
    }

    @EventHandler
    public void onPlayerEvent(PlayerInteractEvent event){
    Player player = event.getPlayer();
    if(event.getAction() == Action.RIGHT_CLICK_AIR);
    if(event.getAction() == Action.RIGHT_CLICK_BLOCK);
    if (player.getItemInHand().getType() == Material.STICK);
    player.launchProjectile(Snowball);




    }



    }
     
  7. Offline

    Shzylo

    I posted the answer on your new thread about this.
     
  8. Offline

    ColaCraft

    Add this:
    Code:java
    1. public void onEnable(){
    2. this.pdfFile = this.getDescription();
    3. this.config = this.getConfig();
    4. log.info( logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " enabled!");
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6. }
    7. public void onDisable(){
    8. log.info( logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " disabled!");
    9. }


    It wont "listen" properly without the onEnable & onDisable.

    Also, in that first command, change the "return" to true- it'll save you a lot of time trying to figure out why the usage pops up.

    Change this:

    Code:java
    1. public void onPlayerEvent(PlayerInteractEvent event){
    2. Player player = event.getPlayer();
    3. if(event.getAction() == Action.RIGHT_CLICK_AIR);
    4. if(event.getAction() == Action.RIGHT_CLICK_BLOCK);
    5. if (player.getItemInHand().getType() == Material.STICK);
    6. player.launchProjectile(Snowball);
    7. }


    To this:

    Code:java
    1. public void onPlayerEvent(PlayerInteractEvent event){
    2. Player player = event.getPlayer();
    3. if(event.getAction() == Action.RIGHT_CLICK_AIR){
    4. if (player.getItemInHand().getType() == Material.STICK){
    5. player.launchProjectile(Snowball.class);
    6.  
    7. //Snowball should have a red squiggly, import it
    8. }
    9. }
    10. if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    11. if (player.getItemInHand().getType() == Material.STICK){
    12. player.launchProjectile(Snowball.class);
    13. }
    14. }
    15. }
    16.  


    That should work.
     
  9. Offline

    Rocoty

    Still helping this guy, are we? If he can't be arsed to learn proper Java and the Bukkit API, he doesn't deserve help
     
Thread Status:
Not open for further replies.

Share This Page