How to limit command use.

Discussion in 'Plugin Development' started by tigerbon, May 8, 2020.

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

    tigerbon

    So i'm new to programming plugins and I've created a plugin that just gives you a golden shovel when you type the command "/claimland" (because you need a golden shovel to claim land)
    How would I put a timer on it to prevent people from spamming the command?
    I've tried using the Java timer however I just don't know how to implement it.



    package me.nick.GoldShovel;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

    @Override
    public void onEnable() {
    }

    @Override
    public void onDisable() {
    }

    public boolean onCommand(CommandSender sender, Command cmd, String str, String[] args) {
    if (str.equals("claimland")) {
    if (sender instanceof Player)
    {
    Player player = (Player) sender;
    player.sendMessage(ChatColor.GOLD + "Use this golden shovel to claim up to 1500 sq blocks!");
    player.sendMessage(ChatColor.GOLD + "Just right click two blocks to claim territory.");
    player.getInventory().addItem(new ItemStack(Material.GOLDEN_SHOVEL,1));
    }
    else
    {
    return true;
    }
    }
    return false;
    }
    }
     
  2. Offline

    Wick

    You can use System.currentTimeMillis() and store in it some Map<UUID, Long>, so that whenever they try to execute the command you retrieve the long value from the map and compare it against System.currentTimeMillis(), the difference between the values would be how many milliseconds have passed.
     
    Strahan likes this.
Thread Status:
Not open for further replies.

Share This Page