OnClickEvent

Discussion in 'Plugin Development' started by SkyLarkPvP, Jan 19, 2015.

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

    SkyLarkPvP

    Hello, I've made a plugin so when you click on an item in a custom inventory, you get it, for some reason when you click on it, the transaction and the message gets repeated twice.
    Enjoy Your Drink Sir!
    Code:
    Code:
    public class Core
    extends JavaPlugin implements Listener, CommandExecutor
    {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Core plugin;
    public static Economy econ = null;
    public static EconomyResponse r;
    Inventory drinks;
    
    public void onDisable() {
    PluginDescriptionFile pdfFile = getDescription();
    this.logger.info(pdfFile.getName() + " Has Been Disabled!"); }
    
    public void onEnable() {
    this.logger.info("sExtras Has Been Enabled!");
    getServer().getPluginManager().registerEvents(this, this);
    
    drinks = Bukkit.createInventory(null, 9, "§dMerchant's Selection of Drinks");
    drinks.setItem(0, createItem(Material.POTION, 1, (short) 0, "§3Root Beer", "§fPrice §a$200"));
    if (!setupEconomy() ) {
        getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
        getServer().getPluginManager().disablePlugin(this);
        return;
    }
    
    getLogger().info("Plugin has been enabled!");
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
    
    private boolean setupEconomy() {
    if (getServer().getPluginManager().getPlugin("Vault") == null) {
        return false;
    }
    RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    if (rsp == null) {
        return false;
    }
    econ = rsp.getProvider();
    return econ != null;
    }
    
    
    public ItemStack createItem(Material material, int amount, int shrt, String displayname, String lore) {
    ItemStack item = new ItemStack(material, amount, (short) shrt);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(displayname);
    meta.setLore(Arrays.asList(lore));
    item.setItemMeta(meta);
    return item; }
    
    @EventHandler
    public void onClick(InventoryClickEvent e) {
    Player p = (Player) e.getWhoClicked();
    ItemStack item = e.getCurrentItem();
    r = econ.withdrawPlayer(p.getName(), 1000.00);
    if (e.getInventory().getName().equals("§dMerchant's Selection of Drinks")) {
    e.setCancelled(true);
    if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equals("§3Root Beer") && r.transactionSuccess()) {
    p.sendMessage(ChatColor.GREEN + "Enjoy your drink sir!");
    p.getInventory().addItem(new ItemStack(Material.POTION, 1));
    } else {
    p.sendMessage(ChatColor.RED + "An error occured when paying out your money. Please contact a server admin.");
          
    }}}}
    Any help would be appreciated!
     
    GrandmaJam likes this.
  2. Offline

    ReadySetPawn

    What version are you using? Some of the 1.8 versions have problems with duplicating events.
     
  3. Offline

    SkyLarkPvP

  4. Offline

    87pen

    @SkyLarkPvP
    Please Indent your Code. Try cancelling the click event after you make the transactions and send the message. That might work.
     
  5. Offline

    SkyLarkPvP

    Done, still doesn't work though :(
    Code:
    Code:
    @EventHandler
    public void onClick(InventoryClickEvent e) {
    Player p = (Player) e.getWhoClicked();
    ItemStack item = e.getCurrentItem();
    r = econ.withdrawPlayer(p.getName(), 1000.00);
    if (e.getInventory().getName().equals("§dMerchant's Selection of Drinks")) {
    if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equals("§3Root Beer") && r.transactionSuccess()) {
    e.setCancelled(true);
    p.sendMessage(ChatColor.GREEN + "Enjoy your drink sir!");
    p.getInventory().addItem(applyLore(new ItemStack(Material.POTION), "§3Root Beer", null));
    } else {
    p.sendMessage(ChatColor.RED + "An error occured when paying out your money. Please contact a server admin.");
           
    }}}}
    @87pen
     
  6. Offline

    multikus

    Try to put the event cancelling at the end of the event, after the player receives the item.
     
  7. Offline

    Lolmewn

    It doesn't matter when the event is cancelled.
    You registered the event twice, of course it's gonna run twice.
     
    87pen likes this.
Thread Status:
Not open for further replies.

Share This Page