Detect when a player right clicks with an item called Magical Wand

Discussion in 'Plugin Development' started by TRIPL3_CATS, Feb 20, 2015.

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

    TRIPL3_CATS

    I want to make a magical wand plugin, when someone right clicks with a stick called Magical Wand, it will put out a fireball. So far I made it so that when they hold a stick and right click it shoots the fireball, I want it to be only if that is their name. so far I have this:
    public void interact(PlayerInteractEvent e){

    Action eventAction = e.getAction();

    Player player = e.getPlayer();

    if (eventAction == Action.RIGHT_CLICK_AIR || eventAction == Action.RIGHT_CLICK_BLOCK) {

    if (player.getItemInHand().getType().equals(Material.STICK)) {

    player.launchProjectile(SmallFireball.class).setVelocity(player.getLocation().getDirection().multiply(0.5));
     
  2. Offline

    ProMCKingz

    @TRIPL3_CATS
    First of all, please use proper code formatting.
    You can simply check this with an if statement like:
    Code:
    if(getItemInHand().getItemMeta().getDisplayName() == "Your Display Name"){
     
  3. Offline

    Konato_K

    @ProMCKingz Then the player clicks with no item in hand and you have a pretty NullPointerException :DDDDD

    Besides, you should check if it the item has meta before getting it, this prevents the server creating a meta for an item that is not going to be used.

    Edit: Also, you apparently don't know how to compare strings in java...
     
    ProMCKingz likes this.
  4. Offline

    ProMCKingz

    @Konato_K
    True, did this quickly. Just checked my previous plugins which had null checkers.
     
  5. Offline

    TRIPL3_CATS

    It doesn't work: nothing happens in game, I have this:
    public void interact(PlayerInteractEvent e){

    Action eventAction = e.getAction();

    Player player = e.getPlayer();

    if (player.getItemInHand().getItemMeta().getDisplayName() == ChatColor.BLUE + "" + ChatColor.BOLD + "Magical Wand") {

    if (eventAction == Action.RIGHT_CLICK_AIR || eventAction == Action.RIGHT_CLICK_BLOCK) {

    player.launchProjectile(Fireball.class).setVelocity(player.getLocation().getDirection().multiply(0.5));
     
  6. Offline

    candyfloss20

    @TRIPL3_CATS
    Try this :
    Code:
        @EventHandler
        public void onClick(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Action a = e.getAction();
            ItemStack i = p.getItemInHand();
    
            if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
                if (i.getType() == Material.TheItemYouWant) {
                    if (i.getItemMeta().hasDisplayName()) {
                        if (i.getItemMeta().getDisplayName().equalsIgnoreCase("TheNameOfYourItem")) {
                            p.launchProjectile(SmallFireball.class).setVelocity(p.getLocation().getDirection().multiply(0.5));
                            p.sendMessage("§a§lcandyfloss20 is bae!");
                        }
                    }
                }
            }
        }
     
  7. Offline

    TRIPL3_CATS

    This still Doesn't work! D: No errors in console, neither!

    Guys this is my entire class:
    Code:
    package me.tripl3_cats.magicwand;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.SmallFireball;
    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.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MagicWand extends JavaPlugin {
    
        public void onEnable() {
            wandrecipe();
        }
     
        private void wandrecipe() {
            ItemStack wand = new ItemStack(Material.BONE, 1);
            wand.addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, 10);
            ItemMeta meta = wand.getItemMeta();
            meta.setDisplayName("Magical Wand");
            meta.setLore(Arrays.asList("Powered by redstone!"));
            wand.setItemMeta(meta);
         
            ShapedRecipe wandrecipe = new ShapedRecipe(wand);
            wandrecipe.shape(
                    "@#$",
                    "%!%",
                    "$#@");
            wandrecipe.setIngredient('!', Material.BONE);
            wandrecipe.setIngredient('@', Material.MAGMA_CREAM);
            wandrecipe.setIngredient('#', Material.DIAMOND);
            wandrecipe.setIngredient('$', Material.EMERALD_BLOCK);
            wandrecipe.setIngredient('%', Material.GLOWSTONE_DUST);
            Bukkit.getServer().addRecipe(wandrecipe);
        }
     
        @EventHandler
        public void onClick(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        Action a = e.getAction();
        ItemStack i = p.getItemInHand();
        if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
        if (i.getType() == Material.BONE) {
        if (i.getItemMeta().hasDisplayName()) {
        if (i.getItemMeta().getDisplayName().equalsIgnoreCase("Magical Wand")) {
        p.launchProjectile(SmallFireball.class).setVelocity(p.getLocation().getDirection().multiply(0.5));
        p.sendMessage("Hello!");
                        }
                    }
                }
            }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  8. Offline

    candyfloss20

    @TRIPL3_CATS
    You forgot to register your events :mad:

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

    TRIPL3_CATS

    Yay! It works! But how do I make it explode when it hits a block but not do any block damage, right now, it goes through all blocks.
     
  10. Offline

    candyfloss20

Thread Status:
Not open for further replies.

Share This Page