Solved Why doesn't it give the effect?

Discussion in 'Spigot Plugin Development' started by Dartanman, May 31, 2016.

Thread Status:
Not open for further replies.
  1. #1 Dartanman, May 31, 2016
    Last edited: May 31, 2016
  2. Don't compare using ==, that checls if the instances are identical (the same new ClassName(...)). For most cases, use .equals(), but in case of ItemStacks use .isSimilar()
  3. Or you get the type of items ;)
  4. Neither of those worked. Updated with Broadcasts throughout (PRE IF, PRE FISH, PRE POTION). Only PRE IF shows, which means there is something wrong with the first if(statement). Thanks!
  5. show code.

    EDIT: nvm

    Check the type, like @MultiworldLP said
  6. Exactly, it doesn't work. Underlines in red.
    Says it doesn't work for ItemStacks.
  7. @Dartanman then you did something wrong. "It doesn't work" isn't very descriptive
  8. XD true.
    If I do

    if(inv.getHelmet().getType() == dHelmet){
    // Code here

    then inv.getHelmet().getType() == dHelmet
    is underlined in red saying some error (I forgot, I'm on my phone right now)
  9. @Dartanman that's because a Material is not an ItemStack .-.. Just check if the type is equal to Material.X
  10. Changed
    It checks if the player has a diamond helmet, but it won't check the lore, the broadcasts only show PRE IF and PRE FISH but they never say PRE POTION, meaning the if statement checking for the lore is messed up.
  11. What Spigot version are you using?
  12. As far as I know, 1.8.8 is greatly unsupported. However the following code works for me on a 1.9.4 build of Spigot:

    Code (Java):
       public void onMove(PlayerMoveEvent e){
         Player p = e.getPlayer();
         if(p.getInventory().getHelmet().getItemMeta().getLore().contains(ChatColor.RED + "Fish I") && p.getInventory().getHelmet().getType().equals(Material.DIAMOND_HELMET)){
           p.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 100,10));
  13. Code (Text):

            Player player = event.getPlayer();
            if (player.getInventory().getArmorContents() != null) {
                ItemStack[] armor = player.getInventory().getArmorContents();
                if (armor[3].getType() == Material.DIAMOND_HELMET) { //Whatever you want to check for here, as in the item
    Why don't you get the armor contents and check if the armor slot is occupied, if it is compare the two objects?
  14. @Dartanman don't cast ItemStack to ItemMeta, use ItemStack#getItemMeta() instead
  15. Just to let you know (Unrelated to the issue)

    Although it still works, you've imported md_5 chatcolor not bukkit.chatcolor
  16. Something like this should do the trick I believe
    Code (Text):
     public void onMove(PlayerMoveEvent event){
                                            Player player = event.getPlayer();
                                            Bukkit.broadcastMessage("PRE IF");
                                           if(player.getInventory().getArmorContents() != null){
                                            ItemStack[] armor = player.getInventory().getArmorContents();
                                            if(armor[3] == Material.DIAMOND_HELMET){
                                               ItemMeta helmetMeta = armor[3].getItemMeta();
                                                Bukkit.broadcastMessage("PRE FISH");
                                                if(helmetMeta.getLore().contains(ChatColor.RED + "Fish I")){
                                                    Bukkit.broadcastMessage("PRE POTION");
                                                    player.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 100, 10));
    Edit: Sorry for the indentation :p I just wanted to show an example but it didn't like that in the <code> tags D:
  17. Problem. With that code, it underlines in red "(armor[3] == Material.DIAMOND_HELMET)" with this error: "Incompatible operand types ItemStack and Material"

    I tried adding this line before "if(armor[3] == Material.DIAMOND_HELMET){"

    "ItemStack dHelmet = new ItemStack(Material.DIAMOND_HELMET);"

    it got rid of the errors, but never sent any broadcasts other than "PRE IF"
  18. It just imports that by default, and I'm too lazy to change it XD
  19. XD I was forgetting .equals
    (I suck lel)
Thread Status:
Not open for further replies.