简体   繁体   中英

How to click an inventory item only once

I am trying to manipulate a user GUI in minecraft with Bukkit using a chest inventory so my server staff members can work with punishments better. I am fairly new to Java because I was tired of the existing plugins because they were not the features I needed.

I have hooked the GUI with the InventoryClickEvent so whenever a player clicks on a specific item, a specific command will be execute from the player. However, even if I cancel the event immediately to prevent a /mute command (example) more than once, the event will not cancel immediately as a result the target will be muted 2-3 times (because the staff member is clicking the item).

Is there any way to execute that command only one time even if the event is not cancelled immediately?

Here is the major part of the code:

/**
 * Applies the punishment when the player clicks on an item.
 * 
 * @param e
 */
@EventHandler
public void ApplyPunishment(InventoryClickEvent e)
{
    if (!e.getInventory().getName().equalsIgnoreCase(gui.getName())) return;
    if (e.getCurrentItem().getItemMeta() == null) return;

    this._player = (Player) e.getWhoClicked();

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Modified/Hacked Client"))
    {
        e.setCancelled(true);
        this._player.closeInventory();
        PunishModifiedClient();
    }

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Chat Spam/Advertisement"))
    {
        e.setCancelled(true);
        this._player.closeInventory();
        PunishChatSpam();
    }

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Bug Exploitation/Glitch"))
    {
        e.setCancelled(true);
        this._player.closeInventory();
        PunishBugExploit();
    }

    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("General/Other Offense"))
    {
        e.setCancelled(true);
        this._player.closeInventory();
        PunishGeneralOffense();
    }

    e.setCancelled(true);
}

/**
 * Modified Client Punishment
 * 
 * Temporary Ban.
 */
public void PunishModifiedClient()
{
    Bukkit.dispatchCommand((CommandSender) _player, "sudo " + _player.getName() + " tempban "+ _target.getName() + " 86400");
    this._player.playSound(this._player.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
}

/**
 * Chat Spam/Advertising
 * 
 * 1 Hour Mute.
 */
public void PunishChatSpam()
{
    Bukkit.dispatchCommand((CommandSender) _player, "mute " + _target.getName() + " 3600");
    this._player.playSound(this._player.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
}

/**
 * Bug Exploitation/Glitch
 * 
 * 3 Hours Ban.
 */
public void PunishBugExploit()
{
    Bukkit.dispatchCommand((CommandSender) _player, "tempban " + _target.getName() + " 10800");
    this._player.playSound(this._player.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
}

/**
 * General/Other offense
 * 
 * 2 Hours Ban.
 */
public void PunishGeneralOffense()
{
    Bukkit.dispatchCommand((CommandSender) _player, "tempban " + _target.getName() + " 7200");
    this._player.playSound(this._player.getLocation(), Sound.NOTE_PLING, 1.0F, 1.0F);
}

You could add a cooldown to the punish commands that only lets a player run a punishment once every second. That should reduce most of the spam. Look into Maps and keep a history of the last time a player did a punishment. If it hasn't been X seconds since they have run a punishment, don't let them run the punishment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM