Need some help on my event

Discussion in 'Plugin Development' started by PerezHD, Jan 16, 2015.

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

    PerezHD

    Hey guys, I need some help with some issues, my event doesn't get no issues in console, it is registered in onEnable in main, but for some odd reason it just doesn't want to do what I coded it to do.

    Code:
    package me.perezhd.flex.listeners;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import me.perezhd.flex.Main;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class CoinRightClick
      implements Listener
    {
      protected static Main plugin;
     
      public CoinRightClick(Main pl)
      {
        plugin = pl;
      }
     
      @EventHandler
      public void onPlayerInteract(PlayerInteractEvent e)
      {
          Player p = e.getPlayer();
          ItemStack ecoin = new ItemStack(Material.MAGMA_CREAM);
          ItemMeta meta = ecoin.getItemMeta();
          meta.setDisplayName("§eEnchantment Coin §7(Right Click)");
          List<String> ecoinlore = new ArrayList();
          ecoinlore.add(ChatColor.GRAY + "Right click them and use them at /enchants.");
          meta.setLore(ecoinlore);
          ecoin.setItemMeta(meta);
         
        ItemStack air = new ItemStack(Material.AIR);
        if ((e.getAction() != Action.RIGHT_CLICK_BLOCK) && (e.getAction() != Action.RIGHT_CLICK_AIR)) {
          return;
        }
        if (e.getItem() == null) {
          return;
        }
        if (e.getItem().equals(ecoin))
        {
            plugin.addCoins(p, p.getItemInHand().getAmount());
            p.sendMessage("§a§l+§a§l" + p.getItemInHand().getAmount() + " §a§lEnchantment Coin(s)!");
            p.setItemInHand(air);
            p.updateInventory();
        }
      }
    }
    
    I know that it doesn't want to do what I coded it to do because when I go in-game with the same exact ItemStack in-game (eCoin), it doesn't disappear on right click, nor does it add the amount of in hand into the players ecoin balance. It doesn't even display the message.

    my addCoins method in main:
    Code:
      public void addCoins(Player p, int newCoins) {
          int oldCoins = getCoins(p);
          int newpoint = oldCoins + newCoins;
          getUsers().set("Users." + p.getUniqueId(), newpoint);
          saveUsers();
          p.sendMessage(ChatColor.GOLD + "You now have " + ChatColor.AQUA + getCoins(p) + ChatColor.GOLD + " Coins");
      }
    NOTE: I AM NOT GETTING AN ERROR IN CONSOLE!
     
  2. Offline

    Skionz

    @PerezHD
    1. Debug
    2. Compare display names and Materials, not ItemStacks
     
  3. Offline

    PerezHD

    Something tells me there is an issue on line 50 of my code,
    Code:
            p.sendMessage("§a§l+§a§l" + p.getItemInHand().getAmount() + " §a§lEnchantment Coin(s)!");
    Since that lined popped up in an a null pointer exception the last time, it doesn't show know. Hopefully you can find that issue for me in line 50, to me it looks like nothing wrong.
     
  4. Offline

    Skionz

    @PerezHD If it is a logic error, the issue lies with a control statement. Debugging will tell you which.
     
  5. Offline

    PerezHD

    May you please give me a link to about "debugging" I really would like to understand that so I can actually get rid of these non senseless errors
     
  6. Offline

    reider45

    Debugging as in add lines to your code that print out to the console to check which statements are working and which aren't.
     
    PerezHD likes this.
  7. Offline

    Skionz

    http://google.com
     
    PerezHD likes this.
  8. Offline

    PerezHD

    k, I am done, you all are making me feel like a retard, I know how to "google" just can't seem to find a website which would suit me, also I know about adding lines, throwing exceptions, and print outs.
     
  9. Offline

    Skionz

    Example:
    Code:
    if(true) {
        System.out.println(1);
        if(true == true) {
            System.out.println(2);
            if(true == true && true == true) {
                System.out.println(3);
                if(false) {
                    System.out.println(4);
                }
            }
        }
    }
    Output:
    Code:
    1
    2
    3
    Notice 4 is missing
     
    reider45 likes this.
  10. Offline

    reider45

    -snip-

    I've been ninja'd
     
  11. Offline

    PerezHD

    Well, I see, but its the way it is setup, how exactly would I add all those print ins, into that event. That's my question. Sorry for that statement also, its just I am going through some shit with a non listening owner.
     
    Last edited: Jan 16, 2015
  12. Offline

    nverdier

    @PerezHD What's the problem? You can just do
    Code:
    System.out.println("1");
    Right?
     
  13. Offline

    sirrus86

    You can add those lines anywhere. Personally I place them either before or after conditionals. You can also have them print variables to make sure their values are what you think they should be *hint hint*.

    For example, you have:
    Code:
    if (e.getItem().equals(ecoin))
    You can create some debug messages before that line using those same variables:
    Code:
    System.out.println("e.getItem() = " + e.getItem().toString());
    System.out.println("ecoin = " + ecoin.toString());
    This will either confirm that you have the same item, or will show you what's different.
     
    PerezHD likes this.
  14. Offline

    PerezHD

    I was doing that, everything I would interact with something it will display the msg 1.

    Thank you for this, I found the issue now, basically if I had more than 1 ecoin, it wouldn't want to do the event. Thank you so much, hopefully I will practice more on debugging.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page