Needing help with error

Discussion in 'Plugin Development' started by militiaspack, Dec 11, 2013.

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

    militiaspack

    So trying to make a plugin that when you do /lightningrod then it give you a custom stick with the name lightning rod that shoots lightning upon right clicking but i seem to be having trouble.
    I am getting a error on eclipse here
    if(player.getItemInHand().equals(is) == Material.STICK)




    package io.github.militiaspack.lightningrod;

    import java.awt.Event;
    import java.util.ArrayList;
    import java.util.HashMap;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEvent;
    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;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.Server;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentWrapper;
    import org.bukkit.entity.Item;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;


    public final class Main extends JavaPlugin implements Listener {

    private HashMap<Integer, ItemStack> addItem;



    @Override
    public void onEnable(){
    Bukkit.getPluginManager().registerEvents(this, this);
    getLogger().info("LightningRod has been enabled");
    getLogger().info("Created by Militiaspack!");

    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("lightningrod")){
    ItemStack is = new ItemStack(Material.STICK, 1);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(ChatColor.DARK_BLUE + "Lightning Rod");
    is.setItemMeta(im);




    }
    return false;
    }
    {

    }

    public void onRodClick(PlayerInteractEvent e) {
    Player player = e.getPlayer();
    if(player.getItemInHand().equals(is) == Material.STICK) {
    //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.getTargetBlock(null, 200).getLocation());

    }
    }










    // TODO Insert logic to be performed when the plugin is enabled



    public void onDisable() {
    getLogger().info("LightningRod has been disabled");
    }



    {
    ItemStack is = new ItemStack(Material.STICK, 1);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(ChatColor.DARK_BLUE + "Lightning Rod");
    is.setItemMeta(im);
    }



    {
    }


    }

    Anyone?

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

    zeeveener

    First off, please use (without quotes) ["syntax"=java][/syntax] around your code. It makes it somewhat readable.

    Second, please post a paste bin of the error you are getting.
     
  3. Offline

    militiaspack


    In eclipse it says is can not be resolved in a variable for if(player.getItemInHand().equals(is) == Material.STICK)
     
  4. Offline

    zeeveener

    Well, that isn't a Boolean within the if statement.

    You have it like this.
    Code:java
    1. if(BOOLEAN == INTEGER)


    Remove the .equals(is) and try again.
     
  5. Offline

    L33m4n123

    Yes. look closely on what you do there..

    player.getItemInHand().equals(is) == Material.STICK


    Why I did it that bold and red.. Because that is something so stupiditly easy that you should see it when you look at it
     
  6. Offline

    militiaspack


    I did that and now it says imcompatabile aprehand types ItemStack and Material
     
  7. Offline

    zeeveener


    Code:java
    1. player.getItemInHand().getType()
    I think? If not, try:
    Code:java
    1. player.getItemInHand().getTypeId()
     
  8. Offline

    L33m4n123


    His issue is that he does

    Code:java
    1. player.getItemInHand().equals(is) //** lets forget what is behind there for now **/


    while he has the variable 'is' not defined anywhere in his code. At the same time he then checks if that is '==' Material.Stick
     
  9. Offline

    militiaspack


    Cannot convert from material to boolean error now


    (is) is defined but it is defined somewhere else in my code with ItemStack is = new ItemStack(Material.STICK, 1);

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

    The_Doctor_123

    L33m4n123 likes this.
  11. Offline

    L33m4n123

    Ok I thought because of this
    You had it nowhere.. Ok. Then you have it defined somewhere. But obviously in a whay that you cannot acces it in that portion of the code. I strongly suggest on reading up on Java since that is something of the basics of the basics of the basics of every programming language (that are OOP based at least) where to define your variables if you want to acces it in more than one method
     
  12. Offline

    zeeveener

    L33m4n123


    That's not the issue I was referencing. I already gave him the strops steps to fix the .equals(is) thing. I was going off of that in changing the Itemstack to something comparable to Material.

    militiaspack

    You need to compare Material.Stick to the item in their hand. By adding the .getType(), you fix the casting issue that the compiler gave you.
     
  13. Offline

    L33m4n123

    Well you can go ahaed and do
    Code:
    if (player.getItemInHand().getType() == Material.Stick)


    However he is/was still stuck at the .equals(is) and that you gave him steps to fix it, sry. Overread that then
     
  14. Offline

    zeeveener

    L33m4n123

    That's what I was telling him to do lol
     
  15. Offline

    militiaspack


    Well now when i do /lightningrod it just repeats the command?

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("lightningrod")){
    ItemStack is = new ItemStack(Material.STICK, 1);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(ChatColor.DARK_BLUE + "Lightning Rod");
    is.setItemMeta(im);
     
  16. Offline

    MarinD99

    You need to type
    Code:java
    1. Player player = (Player) sender;
    2. // because you're giving the lightning rod to
    3. // the player, not cmd.


    then, you need to type this at the bottom:
    Code:java
    1. player.getInventory().addItem(is);
    2.  
    3. // adds the is(ItemStack) to the players // inventory



    and here's my Listener suggestion:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. World w = p.getWorld();
    5. if(!(e.getItem().getType == Material.BLAZE_ROD)) return;
    6. if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    7. Block b = p.getTargetBlock(null, 100);
    8. Location l = b.getLocation();
    9. w.strikeLightning(location);
    10. w.createExplosion(location, 2);


    Hope it helped!
     
  17. Offline

    The_Doctor_123

    Not really.. You're letting this guy get away without him learning anything. Oh well, we'll just have to wait for the next thread he makes.
     
  18. Offline

    militiaspack



    Well everything went well except the listener. Not sure if i put it in the right location or not but here this is my code now

    package io.github.militiaspack.lightningrod;

    import java.awt.Desktop.Action;
    import java.awt.Event;
    import java.util.ArrayList;
    import java.util.HashMap;

    import javax.tools.JavaFileManager.Location;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEvent;
    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;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.Server;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.enchantments.EnchantmentWrapper;
    import org.bukkit.entity.Item;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.event.inventory.PrepareItemCraftEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;


    public final class Main extends JavaPlugin implements Listener {

    private HashMap<Integer, ItemStack> addItem;



    @Override
    public void onEnable(){
    Bukkit.getPluginManager().registerEvents(this, this);
    getLogger().info("LightningRod has been enabled");
    getLogger().info("Created by Militiaspack!");

    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("lightningrod")){
    Player player = (Player) sender;
    ItemStack is = new ItemStack(Material.STICK, 1);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(ChatColor.DARK_BLUE + "Lightning Rod");
    is.setItemMeta(im);
    player.getInventory().addItem(is);



    }
    return false;
    }
    {
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    World w = p.getWorld();
    if(!(e.getItem().getType == Material.STICK)) return;
    if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    Block b = p.getTargetBlock(null, 100);
    Location l = b.getLocation();
    w.strikeLightning(location);
    w.createExplosion(location, 2);
    }

    public void onRodClick(PlayerInteractEvent e) {
    Player player = e.getPlayer();
    if (player.getItemInHand().getType() == Material.STICK) {
    //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.getTargetBlock(null, 200).getLocation());

    }
    }










    // TODO Insert logic to be performed when the plugin is enabled



    public void onDisable() {
    getLogger().info("LightningRod has been disabled");
    }



    {
    ItemStack is = new ItemStack(Material.STICK, 1);
    ItemMeta im = is.getItemMeta();
    im.setDisplayName(ChatColor.DARK_BLUE + "Lightning Rod");
    is.setItemMeta(im);
    }



    {
    }


    }
     
  19. Offline

    zeeveener

    militiaspack

    Seriously. Learn how to use the syntax formatters. My first reply was about this.
     
    MarinD99 and L33m4n123 like this.
  20. Offline

    MarinD99

    militiaspack my listener is working fine for me. Wait, why do you have the ItemStack paragraph twice written? When it is written it doesn't have to written again. Also, you @Override public void onDisable() { is wrongly put. It needs to go beneath the public void onEnable() {
     
  21. Offline

    The_Doctor_123

    Haha, I chuckled a bit when I saw this:
    Code:
    import javax.tools.JavaFileManager.Location;
    And that's all I've read up to.

    [edit] Oh man.. I just read the rest.. [/edit]

    Don't ignore us, or we're not going to help. Learn Java.
     
    MarinD99 and L33m4n123 like this.
  22. Offline

    militiaspack


    Alright but do you have any good online tutorials that i could use? I can't buy any java books atm because of not having the money but any help would be great :D
     
  23. Offline

    L33m4n123

  24. Offline

    zeeveener

  25. Offline

    The_Doctor_123

  26. Offline

    militiaspack


    I fixed both but getting a error with the code you give me

    getType can not be resolved so I am not sure if i should change it to getType() and RIGHT_CLICK_BLOCK can not be resolved. So its not working for me

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    World w = p.getWorld();
    if(!(e.getItem().getType == Material.BLAZE_ROD)) return;
    if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    Block b = p.getTargetBlock(null, 100);
    Location l = b.getLocation();
    w.strikeLightning(location);
    w.createExplosion(location, 2);
     
  27. Offline

    MarinD99

    Like L33m4n123 and zeeveener said, learn Java first, we all did. This way it will be much easier for you to understand your mistakes. Trust me, once you do, you'll see how silly they are ;) and it'll be easier for you to code Java and Bukkit plugins. Best of luck!

    The Code's working fine for me? Oh, did you deprecate the getTargetBlock? From some point, it needs to be deprecated.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    The_Doctor_123 and L33m4n123 like this.
  28. Offline

    militiaspack


    How would i deprecate the getTargetblock? I think i might of already
     
  29. Offline

    MarinD99

    You hover the mouse over the 'getTargetBlock' and when in the section below you see a set as 'deprecated', you click on that.
     
  30. Offline

    militiaspack


    If i hover over it then it says add @suppresswarnings decepration to b and add @suppresswarnings decepration to onPlayerInteract()
     
Thread Status:
Not open for further replies.

Share This Page