[Gui InventoryClickEvent]

Discussion in 'Programming' started by rumfiske, Jul 10, 2015.

  1. So when people click the item it just cancel the action without takeing any money or giveing any item.

    Yes Vault is installed and a essentials econymy, also i did implement everything needed for Vault to work. i tested it with a PlayerJoinEvent.



    Code (Text):
      @EventHandler
            public void onInventoryClick(InventoryClickEvent e) {
               
               
                //K1
                 if (e.getCurrentItem().getItemMeta().getDisplayName().contains("10 Diamonds")) {
                     e.setCancelled(true);
                     ItemStack Diamond = new ItemStack(Material.DIAMOND, 10);
                     Player p = (Player) e.getWhoClicked();
                     EconomyResponse r = econ.withdrawPlayer(p, 100.0D);
                     if (r.transactionSuccess()) {
                          p.sendMessage(" " + ChatColor.AQUA + ChatColor.BOLD + "You Bught 10 Diamonds");
                         p.getInventory().addItem(Diamond);
                         p.updateInventory();
                         p.closeInventory();
                     }
                   
                   
                     else {
                         p.sendMessage(ChatColor.GREEN + "[SHOP]" + ChatColor.RED + "You dont have enough money !");
                     }
                   
                   
                   
                   
                 }
     
  2. Is the name of the item "10 diamonds"? Or do you mean if the player has 10 diamonds?
     
  3. Did you register the listeners?
     
  4. Code (Text):
    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("10 Diamonds")) {
    That's a really bad way of checking if a play has 10 diamonds.

    Try this:
    Code (Text):
    if(e.getCurrentItem() != null) {
         if(e.getCurrentItem().getType() == Material.diamond) {
               if(e.getCurrentItem().getAmount() == 10) {
                     //Code to be executed here
               }
           }
     }
    It's more lines but a much better way of doing it because you have no chance of a NPE if there is no item in their inventory and it will work with any type without having to change anything.
     
    • Funny Funny x 1
  5. Try and change the players name to a string. I've had this issue before that seemed to solve it.
    Code (Text):

            Player p = (Player) event.getWhoClicked();
            String name = p.getName();
     
     
  6. To all of you, did you never code before? Sorry but it crystal clear to see that this has nothing to do, with a check if the player has "10 Diamonds" its defined as 10 diamonds as a name in GUI. And yes the Listeners has been registered.

    Again the only problem is if you click on the item name 10 Diamonds, in the GUI it does nothing.

    But if you click something else that haven't anything to do with the economy all works just fine. Here is some more of the code, so some that a more experienced can tell me whats wrong except for those who think i try to check if the players has 10 diamonds.

    Code (Text):

    package me.Rumfiske.GUI;

    import java.util.Arrays;

    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;




    public class Menu implements Listener, CommandExecutor {
            private Inventory inv;
      private ItemStack K1, K2, K3;
      public static Economy econ = null;
            public Menu() {
             
                    inv = Bukkit.getServer().createInventory(null, 45, ChatColor.AQUA + "SHOP");

       
                 
                    K1 = ItemManager.createItem(Material.DIAMOND,ChatColor.AQUA + "10 Diamonds", Arrays.asList(
                            ChatColor.DARK_GRAY + "$100"));
                 
                 
                 
                    K2 = ItemManager.createItem(Material.OBSIDIAN,ChatColor.AQUA + "64 Obsidian", Arrays.asList(
                            ChatColor.DARK_GRAY + "$200"));
                 
              ItemStack glass = ItemManager.createItem(Material.THIN_GLASS,ChatColor.AQUA + "SHOP");

              for (int i = 0; i < 10; i++) {
               inv.setItem(i, glass);
              }

              for (int i = 35; i < 45; i++) {
               inv.setItem(i, glass);
              }
           
               inv.setItem(10, K1);
               inv.setItem(11, K2);
               inv.setItem(12, K3);

            }

         
         
         



            public void show(Player p) {
                    p.openInventory(inv);
            }
         
         
         
         
            [USER=17846]@EventHandler[/USER]
            public void PlayerJoinEvenst(PlayerJoinEvent e) {
                Player p = (Player) e.getPlayer();
                p.sendMessage("Welcome " + p.getName() );
                EconomyResponse r = econ.depositPlayer(p, 10.0D);
               if (r.transactionSuccess()) {
                  p.sendMessage(ChatColor.GREEN + "You just recviced $10");
               }
            }
         
         
         
         
       
                 
                 
               
               [USER=17846]@EventHandler[/USER]
               public void onInventoryClick(InventoryClickEvent e) {
                         
                         
                         
                         
                         
                         
                         
                            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("SHOP")) {
                                Player p = (Player) e.getWhoClicked();
                                p.sendMessage(" " + ChatColor.RED + ChatColor.BOLD + "You cant get this item!");
                                e.setCancelled(true);
                             
                             
                        }
                         
                         
                         
                         
                           //K1    EconomyResponse r = econ.depositPlayer(p, 10.0D);
                              if (e.getCurrentItem().getItemMeta().getDisplayName().contains("10 Diamonds")) {
                                    e.setCancelled(true);
                                    ItemStack TenDiamonds = new ItemStack(Material.DIAMOND, 10);
                                    Player p = (Player) e.getWhoClicked();
                                    EconomyResponse r = econ.depositPlayer(p, 100.0D);
                                   if  (r.transactionSuccess()) {
                                      p.getInventory().addItem(TenDiamonds);
                                        p.updateInventory();
                                        p.sendMessage(" " + ChatColor.AQUA + ChatColor.BOLD + "You Bught 10 Diamonds!");
                                        e.getWhoClicked().closeInventory();
                                   }
                                   else
                                      p.sendMessage(ChatColor.GREEN + "You dont have enough money!");
                            }
                         
                         
                         
                         
                            //K2
                            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("64 Obsidian")) {
                                e.setCancelled(true);
                                ItemStack Obsidian = new ItemStack(Material.OBSIDIAN, 64);
                                Player p = (Player) e.getWhoClicked();
                                p.getInventory().addItem(Obsidian);
                                p.updateInventory();
                                p.sendMessage(" " + ChatColor.AQUA + ChatColor.BOLD + "You Bught 64 Obsidians!");
                                e.getWhoClicked().closeInventory();
     
                        }
               
                    }


            [USER=32110]@Override[/USER]
            public boolean onCommand(CommandSender arg0, Command arg1, String arg2,
                    String[] arg3) {
                // TODO Auto-generated method stub
                return false;
            }
            }


     
     
    #6 rumfiske, Jul 14, 2015
    Last edited: Jul 14, 2015
  7. We don't have to help, if you are going to be cheeky we can go help someone more grateful
     
    • Like Like x 1
  8. You haven't even tried what I told you. I'm not going to spoon feed you code. We're not bukkit
     
    • Like Like x 1