My Own Plugin: Problem

Discussion in 'Spigot Plugin Help' started by ProtomMC, Jul 2, 2015.

  1. Hey guys,

    There is something wrong with my coding, when I click a lapis block I want it to say the kit names in chat, but doesn't work. Please someone take a look at the coding below:

    Gyazo View: http://gyazo.com/d2e32761c9a076453284838f2aef7661

    Code (Text):
    package beaconclick.ftp;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Material;
    import org.bukkit.block.Block;
    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.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
       
           @EventHandler
           public void onClick(PlayerInteractEvent event) {
            Block block = event.getClickedBlock();
            Player player = event.getPlayer();
            Action action = event.getAction();
            if (action != Action.RIGHT_CLICK_BLOCK) {
             return ;
            }
            if (block.getType() != Material.LAPIS_BLOCK) {
                event.setCancelled(true);
             return;
            }
            player.sendMessage(ChatColor.RED + "Choose a kit...");
         player.performCommand("kits");
           }
    }
    Thanks guys.
     
    • Agree Agree x 1
  2. Use block.getType().equals(Material) or !block.getType().equals(Material) instead of == or !=. Same for the action check.
     
  3. @Absentee23 Could you copy the code from above please and paste it on how it's supposed to look like, thank you very much.
     
    • Optimistic Optimistic x 1
  4. I'm not going to spoonfeed.

    It's simple, like I said replace the places you use the == or != operators, with block.getType().equals(Material.MATERIAL) or !block.getType().equals(Material.MATERIAL) respectively. Also do this with the action, but I'll leave that up to you to figure out.

    == or !=
    should mainly be used for comparing numbers, equals() should be used to compare two objects (in this case the block.getType() material with a specific material).

    Here's some decent info about equals() and other comparators (from a quick google search): http://perso.ensta-paristech.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html
     
    #5 Absentee23, Jul 2, 2015
    Last edited: Jul 2, 2015
  5. Hmm, A plugin that extends JavaPlugin, But doesn't implement Listener or, register a listener. Not sure how any of the previous comments will help. Hopefully someone has mentioned that you're missing this stuff in your code. Then again perhaps this is just pseudo code and, you've left that code out.
     
    • Funny Funny x 1
  6. Ah I didn't even catch that! Good point. Implement and register your listener.

    == and != are not reliable ways to compare objects, and definitely not reliable for comparing Strings (thats unrelated here though), so using equals() is a good habit to get into.
     
    • Funny Funny x 1