Plugin methods aren't running, onCommand boolean is though

Discussion in 'Plugin Development' started by bling4525, Jul 18, 2015.

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

    bling4525

    Hi!

    I have encountered a rather peculiar error after fixing a plugin that I had created earlier this week - when I run the plugin, the onCommand boolean is running, meaning that you can use the commands in the plugin properly, but then when I try to use those items nothing happens except what usually would. Here is my code:

    Code:
    package me.droppedorphan.lhammer;
    
    import java.util.HashSet;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.LargeFireball;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.WitherSkull;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class hammer extends JavaPlugin{
    
        public final Logger logger = Logger.getLogger("Minecraft");
     
        public static hammer plugin; 
     
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + "version " + pdfFile.getVersion() + " has been disabled.");
        }
     
        @Override
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled.");
         
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
         
            if(commandLabel.equalsIgnoreCase("WBow")){
             
                ((Player)sender).getInventory().addItem(WBow(new ItemStack(Material.BOW), ChatColor.BOLD + "The Wither Bow"));
            }
            else if(commandLabel.equalsIgnoreCase("LHammer")){
                ((Player)sender).getInventory().addItem(LHammer(new ItemStack(Material.GOLD_AXE), ChatColor.BOLD + "The Lightning Hammer"));
            }
            else if(commandLabel.equalsIgnoreCase("GhAxe")){
                ((Player)sender).getInventory().addItem(GhAxe(new ItemStack(Material.DIAMOND_PICKAXE), ChatColor.BOLD + "The Ghast Tear Pickaxe"));
            }
             
        return true;
        }
    
        public ItemStack WBow(ItemStack is, String name) {
            ItemMeta im = is.getItemMeta();
         
            im.setDisplayName(name);
            is.setItemMeta(im);
         
            return is;
         
        }
        public ItemStack LHammer(ItemStack is2, String name2) {
            ItemMeta im2 = is2.getItemMeta();
         
            im2.setDisplayName(name2);
            is2.setItemMeta(im2);
         
            return is2;
     
        } 
        public ItemStack GhAxe(ItemStack is3, String name3){
            ItemMeta im3 = is3.getItemMeta();
         
            im3.setDisplayName(name3);
            is3.setItemMeta(im3);
         
            return is3;
     
        }
     
        @EventHandler
        public void WSkull(EntityShootBowEvent eventOne){
             
        Player player = (Player) eventOne.getEntity();
             
            if(player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.BOLD + "The Wither Bow")){ 
                                  
                eventOne.setCancelled(true);
    
                player.launchProjectile(WitherSkull.class).setVelocity(eventOne.getProjectile().getVelocity().multiply(0.3));
            }
        }
     
        @EventHandler
        public void hammerstrike(PlayerInteractEvent event){
    
            Player player = event.getPlayer();
         
            if(player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.BOLD + "The Lightning Hammer")){ 
             
                World world1 = player.getWorld();
                Block block1 = player.getTargetBlock((HashSet<Byte>)null, 100);
                Location location1 = block1.getLocation();
             
                world1.strikeLightning(location1);
            }
        }
     
        @EventHandler
        public void GhStrike(PlayerInteractEvent event1){
         
            Player player = event1.getPlayer();
         
            if(player.getInventory().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.BOLD + "The Ghast Tear Pickaxe"));
         
                event1.setCancelled(true);
             
                player.launchProjectile(LargeFireball.class);
        }
    
    }
    It's really strange that onCommand runs and none of the other methods do. Perhaps they are placed incorrectly?

    Help is appreciated! Thanks, Bling4525

    Edit: The onCommand is running, but anything that is tagged as an event is not. The @EventHandler tag is where the code stops. I don't know if it helps, but I tried implementing Listener as well. That didn't work... I am completely baffled.

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

    bling4525

    Is the reason nobody has responded is because this is a stupid post, or just because nobody knows?
     
  3. @bling4525 Ok there are quite a lot of issues in this plugin. I am suspecting you learnt of TheBCBroz. He is not a good person to learn off, he teaches bad practices and really doesn't teach well.

    1. Naming conventions - http://www.oracle.com/technetwork/java/codeconventions-135099.html
    2. Use Bukkit's logger not Java's
    3. Bukkit does onEnable and onDisable messages for you.
    4. cmd.getName not commandLabel
    5. Check before casting
    6. The events will not fire as they are not registered.
     
    bling4525 and mine-care like this.
  4. Offline

    bling4525

    You are right, I did learn off of the BCBroz
    (cue groaning)
    Thanks for the reply, I was going mad. Is there anywhere reliable where I can learn all of this stuff?

    How do I register events?
    How do I use the Bukkit Logger?
    - What is the Bukkit Logger?
    - Do I need to use it?
     
    Last edited: Jul 20, 2015
  5. Offline

    justin_393

    wiki.bukkit.org/Plugin_Tutorial
     
    bling4525 likes this.
  6. Offline

    bling4525

    thanks, didn't see that post from justin just now.

    @bwfcwalshy

    I looked at the official bukkit tutorial thingy and you are right, I have absolutely no idea what I am doing and neither do the BCbroz. Could I have some help with registering my 3 events? The Event API reference wasn't very clear on how to do so.

    thanks!

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

    mythbusterma

    @bling4525

    Make a new instance of the Listener, then do PluginManager#registerEvents(...) with your Listener and your plugin as arguments.
     
    bling4525 likes this.
Thread Status:
Not open for further replies.

Share This Page