Solved Lightning sword doesn't work

Discussion in 'Plugin Development' started by Niv-Mizzet, Sep 21, 2019.

  1. Offline

    Niv-Mizzet

    Hi!
    I am just starting plugin development, so this may be a stupid beginner question.
    I am trying to make a sword that strikes down a storm of lightning near the target when they click on a block (I know how to do this, its just not in my code because I'm trying to make this work first). I also want this not to happen on a normal diamond sword. What I'm doing is making a listener class that checks for a block interaction, then checking the item in their main hand. The problem is that the only way for this to work is by getting the material. I've tried checking the Metadata, and even converting the Metadata to a string. This does not work, even though when I print the data, it is the exact same. This all happens in the listener class.
    Main Class:
    Code:
    package go.SuperCraft.me;
    
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
           @Override
            public void onEnable() {
               this.getCommand("Hi").setExecutor((CommandExecutor)new Command());
               PluginManager pm=getServer().getPluginManager();
               ListenerOne listener= new ListenerOne(this);
               pm.registerEvents(listener, this);
            }
           @Override
            public void onDisable() {
              
            }
    }
    
    Item Creation Class:
    Code:
    package go.SuperCraft.me;
    
    import java.util.ArrayList;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class ItemSmith {
    public ItemStack makeItem(Material m, String name, String desc, int amount){
        ItemStack item=new ItemStack(m, amount);
        ItemMeta im= item.getItemMeta();
        im.setDisplayName(name);
        ArrayList<String> lore= new ArrayList<String>();
        lore.add(desc);
        im.setLore(lore);
        im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
        im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
        item.setItemMeta(im);
        return item;
    }
    }
    
    Listener Class:
    Code:
    package go.SuperCraft.me;
    
    import java.util.Set;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import net.md_5.bungee.api.ChatColor;
    
    public class ListenerOne implements Listener {
        public ListenerOne(Main plugin){
           
        }
        public ItemStack hhelo=new ItemSmith().makeItem(Material.DIAMOND_SWORD, "Sword of Zeus", "May Lightning destroy your enemies", 1);
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event){
            Player player=(Player) event.getPlayer();
            player.getInventory().addItem(hhelo);
        }
        @EventHandler
        public void onPlayerInteractBlock(PlayerInteractEvent event) {
           
              Player player = event.getPlayer();
             
            if (String.valueOf(player.getInventory().getItemInMainHand().getItemMeta()) == String.valueOf(hhelo.getItemMeta())) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "hi2"));
                // Creates a bolt of lightning at a given location. In this case, that location is where the player is looking.
                // Can only create lightning up to 200 blocks away.
                player.getWorld().strikeLightning(player.getLocation().add(5, 0, 5));
            }
       
           
        }
    }
    
    Thanks in advance!
     
  2. Offline

    wand555

    You can use getType() to get the material from an itemstack.
     
  3. Offline

    Niv-Mizzet

    I know that, but that would make it so all diamond swords did this. I want only this one to do it.
    Thanks!
     
  4. Offline

    Tango_

    Perhaps trying:

    if (player.getInventory().getItemInMainHand().isSimilar(hhelo)) {
     
  5. Offline

    Niv-Mizzet

    You are amazing!
    Thanks so much!
     

Share This Page