whats wrong with my code?

Discussion in 'Plugin Development' started by coolmonkeyguy, Jun 4, 2013.

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

    coolmonkeyguy

    Right clicking with the defined items does not seem to engage the effects as my code says its supposed to.

    any ideas why?
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Contraband extends JavaPlugin {
     
     
       
        public final Logger logger = Logger.getLogger("Minecraft");
        public void onEnable() {   
        }
        {
        logger.info("[BBDrugs] has been enabled!");
        }
      public void onDisable() {
          logger.info("[BBDrugs] has been disabled!");
        }
     
     
         
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
       
            if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                    if (e.getPlayer().getItemInHand().getType().equals(Material.SUGAR)) {
                        e.getPlayer().sendMessage(ChatColor.BLUE + "[BBContraband] Enjoy Those Drugs" );
                            e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED ,20,1));
                    }
            }
            if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK) || e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                    if (e.getPlayer().getItemInHand().getType().equals(new ItemStack(Material.INK_SACK, 2, (short)1))){
                    e.getPlayer().sendMessage(ChatColor.BLUE + "[BBContraband] Enjoy Those Drugs" );
                        e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.HUNGER ,20,1));
                        e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SLOW ,20,1));
                }
            }
            if (e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
                if (e.getPlayer().getItemInHand().getType().equals(Material.BROWN_MUSHROOM)) {
                    e.getPlayer().sendMessage(ChatColor.BLUE + "[BBContraband] Enjoy Those Drugs" );
                        e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION ,20,1));
                }
            }
    }
           
    }
     
  2. Offline

    Technius

    coolmonkeyguy
    You have to register your events in onEnable.

    Open the spoiler if you don't know how to register events.
    Code (open)

    Code:
    public void onEnable()
    {
         getServer().getPluginManager().registerEvents(this, this);
    }
    
     
  3. Offline

    Rockett8855

    I've had problems with using e.getAction().equals() Instead I use if(e.getAction == Action.RIGHT_CLICK_AIR) that seems to work better for me.
     
  4. Offline

    coolmonkeyguy

    @Technius
    so would i register them like this:
    public final Logger logger = Logger.getLogger("Minecraft");
    public void onEnable() {

    onPlayerInteract
    }
    {
    logger.info("[BBDrugs] has been enabled!");
    }
    public void onDisable() {
    logger.info("[BBDrugs] has been disabled!");
    }
     
    SnipsRevival likes this.
  5. Offline

    Technius

    coolmonkeyguy
    Make your class implement Listener and make onEnable look like this:
    Code:
    public void onEnable()
    {
         getServer().getPluginManager.registerEvents(this, this);
    }
    
     
Thread Status:
Not open for further replies.

Share This Page