Help with adding potion effects on right click

Discussion in 'Plugin Development' started by CheifKeef, May 5, 2015.

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

    CheifKeef

    Basically I'm making a drug plugin and I've got it down to /drugmeup gives the player the starting drugs, that works fine but i want to make it so if they have wheat in their hand and they right click with it, it will give them confusion,, i don't know whats wrong can someone look over the code?

    Code:
    package me.RalphLauren.Narcotics;
    
    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.block.Action;
    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;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Narcotics extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Narcotics plugin;
       
        @Override
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has been Enabled!");
        }
       
        @Override
        public void onDisable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has been Disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String args[]) {
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("Drugmeup"));
            ItemStack Wheat = new ItemStack(Material.SEEDS, 5);
            ItemMeta meta = Wheat.getItemMeta();
            meta.setDisplayName(ChatColor.GRAY + "[" + ChatColor.RED + "Kush" + ChatColor.GRAY + "]");
            Wheat.setItemMeta(meta);
            player.getInventory().addItem(Wheat);
            ItemStack Sugar = new ItemStack(Material.SUGAR, 5);
            ItemMeta meta1 = Sugar.getItemMeta();
            meta1.setDisplayName(ChatColor.GRAY + "[" + ChatColor.RED + "Cocaine" + ChatColor.GRAY + "]");
            Sugar.setItemMeta(meta1);
            player.getInventory().addItem(Sugar);
            ItemStack Cocoa = new ItemStack(Material.COCOA, 5);
            ItemMeta meta2 = Cocoa.getItemMeta();
            meta.setDisplayName(ChatColor.GRAY + "[" + ChatColor.RED + "Heroin" + ChatColor.GRAY + "]");
            Cocoa.setItemMeta(meta2);
            player.getInventory().addItem(Cocoa);
            ItemStack Mushroom = new ItemStack(Material.RED_MUSHROOM, 5);
            ItemMeta meta3 = Cocoa.getItemMeta();
            meta.setDisplayName(ChatColor.GRAY + "[" + ChatColor.RED + "Shrooms" + ChatColor.GRAY + "]");
            Mushroom.setItemMeta(meta3);
            player.getInventory().addItem(Mushroom);
            ItemStack Paper = new ItemStack(Material.PAPER, 5);
            ItemMeta meta4 = Paper.getItemMeta();
            meta.setDisplayName(ChatColor.GRAY + "[" + ChatColor.RED + "LSD" + ChatColor.GRAY + "]");
            Paper.setItemMeta(meta4);
            player.getInventory().addItem(Paper);
            ItemStack Netherwart = new ItemStack(Material.NETHER_STALK, 5);
            ItemMeta meta5 = Netherwart.getItemMeta();
            meta.setDisplayName(ChatColor.GRAY + "[" + ChatColor.RED + "Ecstasy" + ChatColor.GRAY + "]");
            Netherwart.setItemMeta(meta5);
            player.getInventory().addItem(Netherwart);
            return false;
                }
        @EventHandler
        public void onRight(PlayerInteractEvent ie) {
            Player player = ie.getPlayer();
            if(player.getInventory().contains(Material.WHEAT));
            ie.getAction().equals(Action.RIGHT_CLICK_AIR);
            player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 180, 1));
        }
        }
    
     
  2. Moved to Plugin Development.
     
  3. Offline

    mine-care

    Ok few things : don't steal minecraft logger! Use getLogger()
    Don't cast without checking see my signature
    You don't need enable or disable messages because bukkit does it for you
    Line 78-79 err why is there a ; next to your if? Why on line 79 is a line doing nothing?
    Also keep your itemstacks loaded on enable instead of creating them each time the command runs.
     
  4. Offline

    CoolGamerXD

    @CheifKeef
    Umm.. Have you checked that which item should be affected after right clicking it.. It will be like something.

    Code:
     if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
            if (!(e.getItem().getType() == Material.WHEAT)) return;
    player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 180, 1)); 
     
    Last edited: May 6, 2015
  5. Take a better look at the Event API, 1) your class doesn't implement the Listener interface and 2) You're not registering the events for the class. Also a random note: You're not setting the plugin field
     
Thread Status:
Not open for further replies.

Share This Page