Why won't my gun work???

Discussion in 'Plugin Development' started by camakeson, Feb 24, 2013.

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

    camakeson

    Ive been working hard on making this but it wont work :/

    Code:
    package me.cameron.CallofDuty;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class Guns {
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
           
            if (p.getItemInHand().equals(Material.WOOD_AXE)) {
                if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                    p.throwSnowball();
               
                }
            }
       
        }
     
  2. Offline

    RainoBoy97

    public class Guns implements Listener {

    And register in onEnable
     
  3. Offline

    gamerzap

    Also, I think that Player.throwSnowball() no longer works. You have to use Player.launchProjectile(Snowball.class);
     
  4. Offline

    camakeson

    still doesn't work. Also Player.launchProjectile(Snowball.class); Doesnt exist. Maybe you are thinking about Mods?
    This is what i have now
    Code:
    package me.cameron.CallofDuty;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class Guns implements Listener {
       
        public void onEnable(){
        }
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
           
            if (p.getItemInHand().equals(Material.WOOD_AXE)) {
                if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                    p.throwSnowball();
               
                }
            }
       
        }
    }
    Thanks for your help!
     
  5. Offline

    Technius

    1. Is that your main class? If so, you need to have it extend JavaPlugin.
    2. You need to register events in onEnable in your main class.
    3. You need to have a valid plugin.yml
    4. Implement Listener for the events to be caught
     
  6. Offline

    camakeson

    ok how would I register the event in Main Class... I think i should just that class in the main plugin, but i felt smarter making another class but i guess maybe that will fix it :p
     
  7. And ItemStack can never equal a Material.
    Code:
    p.getItemInHand().equals(Material.WOOD_AXE)
    Use
    Code:
    p.getItemInHand().getType() == Material.WOOD_AXE
    instead.

    You can do the same for getAction() although that works just fine as is, but it would be faster to just use == for enums :p
     
  8. Offline

    camakeson

    ok, now the plugin wont export because there is an error with this...
    Code:
    if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) 
    I researched this and couldn't find anything.. is this even the right code :/

    Ok I researched even more and i tried this code
    Code:
    package me.cameron.CallofDuty;
     
    import java.awt.Desktop.Action;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class CallofDuty extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
     
        public void onEnable(){
            logger.info("[CoD] has been enabled!");
        }
       
        public void onDisable(){
            logger.info("[CoD has been disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (commandLabel.equalsIgnoreCase("CoD Leave")) {
                player.sendMessage(ChatColor.RED + "You have exited the game!");
            }
           
            else if (commandLabel.equalsIgnoreCase("CoD Join")) {
                player.sendMessage(ChatColor.RED + "You have joined the game!");
            }
            return false;
        }
                @SuppressWarnings("deprecation")
                @EventHandler
                public void onPlayerInteract(PlayerInteractEvent e) {
                    Player p = e.getPlayer();
                    if (p.getItemInHand().getType() == Material.WOOD_AXE) {
                        if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
                            p.throwSnowball();
                       
                        }
                    }
               
            return;
           
           
        }
       
    }
    
    The problem is still on
    Code:
    if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
    I was thinking... maybe its not working because I forgot to put something above it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  9. Offline

    CookCreeperz

    Check your }'s
     
  10. Offline

    camakeson

    No Ik my brackets are fine, the only error is on the action.LEFT_CLICK ones....
     
  11. Maybe you included the wrong Action class ? It must be org.bukkit not java.awt...
     
  12. Offline

    chasechocolate

    camakeson Yeah, you imported the wrong action class. Change "import java.awt.Desktop.Action;" to "org.bukkit.block.Action;".
     
  13. Offline

    microgeek

    Geez. Learn basic Java syntax before trying program with it.
    >.<
     
  14. Offline

    Deckerz

    I have post working gun code in resources that you can look at
     
  15. Offline

    camakeson

    I changed the
    Code:
    import java.awt.Desktop.Action;
    to
    Code:
    [COLOR=#921e68][SIZE=11px][FONT=Monaco]import[/FONT][/SIZE][/COLOR][U][SIZE=11px][FONT=Monaco]org.bukkit.block.Action[/FONT][/SIZE][/U][SIZE=11px][FONT=Monaco];
    [/FONT][/SIZE]
     
  16. Offline

    Wolftic

    Code:
    if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK){
    If you use an == in a if statement and use || in it you'll have to give both ( )

    Code:
    if((e.getAction() == Action.LEFT_CLICK_AIR) || (e.getAction() == Action.LEFT_CLICK_BLOCK)){
    As simple as that..
     
  17. Wolftic
    No you don't need those in that case.

    You only need them to be very specific on multiple diferent conditions:
    Code:
    if( ( cond && ( cond || cond || opt ) ) || ( cond && cond ) )
     
  18. Offline

    camakeson

    Wolftic
    It doesn't work....
    Ok, so I don't know what your saying digi.... I am really new to this. I am starting to understand slowly but I don't get what your saying. Do I have to make new strings for the cond && cond right???
     
  19. Offline

    ZachBora

  20. Offline

    camakeson

    ok i changed the thing to
    Code:
    p.launchProjectile(SnowBall.class);
    But it still is giving an error...
     
  21. Offline

    ZachBora

    I shouldn't need to ask this, you should have already provided it. What is the error?
     
  22. Offline

    camakeson

    ZachBora
    I actually was going to give you the error but I guess I forgot..
    When I went to see it I accidentally fixed it -_-
    But now could you fix it while you are still helping me :p
    Code:
    if(e.getAction() == Action.LEFT_CLICK_AIR) || (e.getAction() == Action.LEFT_CLICK_BLOCK){
    The error is the OR thing ( || <-that thingy ) it says the syntax error on token "||" invalid OnlySynchronized
     
  23. You closed the condition before the OR... replace ") || (" with "||"
     
Thread Status:
Not open for further replies.

Share This Page