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) }
First off, Action is undefined for PlayerEvent, use PlayerInteractEvent instead. It would be better if you made a new variable: Code:java public class Snowstick exends JavaPlugin implements Listener { @EventHandlerpublic void onPlayerEvent(PlayerInteractEvent event) { Player p = event.getPlayer(); Action act = event.getAction(); if(act == Action.RIGHT_CLICK_AIR) { //do stuff }} And don't forget to register the event.
"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) } }
Bernabeast You need to learn Java before writing plugins. getAction() is a method, not a variable, and should be accessed as such.
In your if statement: if(event.getAction == Action.RIGHT_CLICK_AIR) add parenthesis: event.getAction().
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); } }
Add this: Code:java public void onEnable(){ this.pdfFile = this.getDescription(); this.config = this.getConfig(); log.info( logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " enabled!"); Bukkit.getServer().getPluginManager().registerEvents(this, this); } public void onDisable(){ log.info( logTag + pdfFile.getName() + " v" + pdfFile.getVersion() + " disabled!"); } 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 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);} To this: Code:java public void onPlayerEvent(PlayerInteractEvent event){Player player = event.getPlayer();if(event.getAction() == Action.RIGHT_CLICK_AIR){if (player.getItemInHand().getType() == Material.STICK){player.launchProjectile(Snowball.class); //Snowball should have a red squiggly, import it }}if(event.getAction() == Action.RIGHT_CLICK_BLOCK){if (player.getItemInHand().getType() == Material.STICK){player.launchProjectile(Snowball.class); } }} That should work.
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