how to make protection for not player spam a command (once a day)

Discussion in 'Plugin Development' started by OrnomaS, Nov 29, 2017.

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

    OrnomaS

    Hello,
    Today I have a problem for make a protection for not player spam command I want he runs it once a day,
    Thanks.
     
  2. Offline

    MightyOne

    what have you got yet? show some code where you want to implement that
     
  3. Offline

    OrnomaS

    I want to implement in the onCommand.

    Code:
    package fr.ornomas.plugin.commands;
    
    import java.util.Random;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandRtp implements CommandExecutor 
    {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) 
        {
            if (sender instanceof Player)
            {
                Player player = (Player)sender;
                World World = player.getWorld();
                Random random = new Random();
                Location ploc = player.getLocation();
                int x = (int) (ploc.getX() + random.nextInt(5000));
                int z = (int) (ploc.getZ() - random.nextInt(5000));
                Location rtp = new Location(player.getWorld(), x, 50, z, 0f, 0f);
                Location rtp1 = new Location(player.getWorld(), rtp.getX(), World.getHighestBlockYAt(rtp), rtp.getZ(), 0f, 0f);
                player.sendMessage(ChatColor.DARK_GRAY +  "Teleportation aleatoire...");
                player.teleport(rtp1);
            }
           
            else 
            {
                System.out.println(ChatColor.DARK_GRAY + "Seul les joueurs peuvent executer cette commande");
            }
            return false;
        }
       
    }
    >
     
    Last edited by a moderator: Nov 29, 2017
  4. Offline

    MightyOne

    (maybe check for the commands name before executing it. If you only have one well ok but its not a good practice)
    make a HashMap<UUID, Long> and store the players uuid and the current system's time in there. before executing the random tp check if the last use is already one day ago (upToDateTime - lastTime > 1 day)
     
  5. Offline

    OrnomaS

    it's too difficulte for me because i'm bingeener.
    you have an idea for the code.
     
  6. Offline

    timtower Administrator Administrator Moderator

    We don't spoonfeed here.
    Start by making a hashmap and putting the key to the one who runs the command and they value to the current system time in ms.
     
    OfficerDeveloper likes this.
  7. @OP

    Better to learn how to use a custom config file or otherwise, so that if your server resets/reloads the HashMap isn't cleared.

    Write names and local time in the file and everytime they run the command check if it's past 24 hours, or relevant time.
     
Thread Status:
Not open for further replies.

Share This Page