Development Assistance Cooldown for my plugin

Discussion in 'Plugin Help/Development/Requests' started by TheMrJezza, Feb 21, 2015.

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

    TheMrJezza

    I am not a coder, I don't no java but 3 weeks ago I was looking at some tutorials and I came up with a plugin that randomly teleports a player when they do /respawn. I had a 24 hour cooldown but when players looged off the cooldown would reset, now its set to 20 seconds because of this. Can someone tell me how to add a working 24 hour cooldown?

    here is my code
    Code:
    package me.TheMrJezza;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TMJrespawn extends JavaPlugin {
    
        private List<Player> cantDoCommand = new ArrayList<Player>();
    
        Countdown d = new Countdown();
    
        public boolean onCommand(CommandSender sender, Command command,
                String commandLable, String[] args) {
            if (commandLable.equalsIgnoreCase("respawn")) {
                Player player = (Player) sender;
                if (cantDoCommand.contains(player)) {
                    player.sendMessage("§aThis command is cooling:");
                }
                if (!cantDoCommand.contains(player)) {
                    player.sendMessage("§aYou can use §2/respawn §aagain in 20 seconds");
                    cantDoCommand.add(player);
                    d.setList(cantDoCommand);
                    d.setPlayer(player);
                    new Thread(d).start();
    
                    Random random = new Random();
    
                    Location teleportLocation = null;
    
                    int x = random.nextInt(10000) + 1;
                    int y = 150;
                    int z = random.nextInt(10000) + 1;
    
                    boolean isOnLand = false;
    
                    while (isOnLand == false) {
    
                        teleportLocation = new Location(player.getWorld(), x, y, z);
    
                        if (teleportLocation.getBlock().getType() != Material.AIR) {
                            isOnLand = true;
                        } else
                            y--;
                        if (teleportLocation.getBlock().getType() != Material.WATER) {
                            isOnLand = true;
                        } else
                            y--;
                        if (teleportLocation.getBlock().getType() != Material.LAVA) {
                            isOnLand = true;
                        } else
                            y--;
    
    
                    }
    
                    player.teleport(new Location(player.getWorld(),
                            teleportLocation.getX(), teleportLocation.getY() + 1,
                            teleportLocation.getZ()));
    
                    return true;
                }
            }
            return false;
    
        }
    
        public class Countdown implements Runnable {
    
            public Player player1 = null;
            public List<Player> cantDoCommand1 = new ArrayList<Player>();
    
            public void setPlayer(Player player) {
                player1 = player;
    
            }
    
            public void setList(List<Player> list) {
                cantDoCommand1 = list;
            }
    
            public List<Player> getList() {
                return cantDoCommand1;
            }
    
            public void run() {
                try {
                    Thread.sleep(20000);
                    cantDoCommand1.remove(player1);
    
                } catch (Exception Ignored) {
    
                }
            }
        }
    }
     
  2. Offline

    the133448

    Just coded this really quickly without ide and just c+p from what I would use.

    Code:
    HashMap<String,Long> cooldowns =new HashMap<String,Long>();
    
    
    Finalint seconds =20;
    
    
    publicboolean hasCooldown(Player player){
    
    if(cooldowns.get(player.getName())<(System.getCurrentTimeMillis()- seconds*1000))
    
    returnfalse;
    
    else
    
    returntrue;
    
    }
    publicvoid activateCooldown(Player player){
    
    cooldowns.put(player.getName(), System.getCurrentTimeMillis());
    
    }
    public boolean onCommand(CommandSender sender, Command command,
    
                String commandLable, String[] args) {
    
            if (commandLable.equalsIgnoreCase("respawn")) {
    
                Player player = (Player) sender;
    
                if (hasCooldown(player)) {
    
                    player.sendMessage("§aThis command is cooling:");
    
                }
    
                if (!cantDoCommand.contains(player)) {
    
                    player.sendMessage("§aYou can use §2/respawn §aagain in 20 seconds");
    
                    activateCooldown(player);                            
    
                    Random random = new Random();
    
    
    
                    Location teleportLocation = null;
    
    
    
                    int x = random.nextInt(10000) + 1;
    
                    int y = 150;
    
                    int z = random.nextInt(10000) + 1;
    
    
    
                    boolean isOnLand = false;
    
    
    
                    while (isOnLand == false) {
    
    
    
                        teleportLocation = new Location(player.getWorld(), x, y, z);
    
    
    
                        if (teleportLocation.getBlock().getType() != Material.AIR) {
    
                            isOnLand = true;
    
                        } else
    
                            y--;
    
                        if (teleportLocation.getBlock().getType() != Material.WATER) {
    
                            isOnLand = true;
    
                        } else
    
                            y--;
    
                        if (teleportLocation.getBlock().getType() != Material.LAVA) {
    
                            isOnLand = true;
    
                        } else
    
                            y--;
    
    
    
    
    
                    }
    
    
    
                    player.teleport(new Location(player.getWorld(),
    
                            teleportLocation.getX(), teleportLocation.getY() + 1,
    
                            teleportLocation.getZ()));
    
    
    
                    return true;
    
                }
    
            }
    
            return false;
    [/code:java]
     
  3. Offline

    TheMrJezza

    Thank you, but it didn't work, when I used /respawn in game it said "An internal error has occurred" or something like that.
     
  4. Offline

    Sw_aG

    I'll try to help you when I get home.
    But i want to say that 24 hours timer might cause some lag.
     
  5. Offline

    TheMrJezza

    Thank you. But no matter how much lag, I kinda need the 24 hour cooldown so people don't crash the server from using /respawn every time they spawn somewhere they don't like.


    I'm removing this part of the code so its what it was when I first made the plugin:
    Code:
                 if (teleportLocation.getBlock().getType() != Material.WATER) {
                     isOnLand = true;
                 } else
                     y--;
                 if (teleportLocation.getBlock().getType() != Material.LAVA) {
                     isOnLand = true;
                 } else
                     y--;
    I added that to stop people spawning in oceans and drowning, now I'm removing it.
    It made people spawn really high above the ground and fall to their death.

    Add 24 hour cooldown to this code (This is the 100% original code, everything apart from cooldown works)
    Code:
    package me.TheMrJezza;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TMJrespawn extends JavaPlugin {
    
        private List<Player> cantDoCommand = new ArrayList<Player>();
    
        Countdown d = new Countdown();
    
        public boolean onCommand(CommandSender sender, Command command,
                String commandLable, String[] args) {
            if (commandLable.equalsIgnoreCase("respawn")) {
                Player player = (Player) sender;
                if (cantDoCommand.contains(player)) {
                    player.sendMessage("§aThis command is cooling:");
                }
                if (!cantDoCommand.contains(player)) {
                    player.sendMessage("§aYou can use §2/respawn §aagain in 20 seconds");
                    cantDoCommand.add(player);
                    d.setList(cantDoCommand);
                    d.setPlayer(player);
                    new Thread(d).start();
    
                    Random random = new Random();
    
                    Location teleportLocation = null;
    
                    int x = random.nextInt(10000) + 1;
                    int y = 150;
                    int z = random.nextInt(10000) + 1;
    
                    boolean isOnLand = false;
    
                    while (isOnLand == false) {
    
                        teleportLocation = new Location(player.getWorld(), x, y, z);
    
                        if (teleportLocation.getBlock().getType() != Material.AIR) {
                            isOnLand = true;
                        } else
                            y--;
    
                    }
    
                    player.teleport(new Location(player.getWorld(),
                            teleportLocation.getX(), teleportLocation.getY() + 1,
                            teleportLocation.getZ()));
    
                    return true;
                }
            }
            return false;
    
        }
    
        public class Countdown implements Runnable {
    
            public Player player1 = null;
            public List<Player> cantDoCommand1 = new ArrayList<Player>();
    
            public void setPlayer(Player player) {
                player1 = player;
    
            }
    
            public void setList(List<Player> list) {
                cantDoCommand1 = list;
            }
    
            public List<Player> getList() {
                return cantDoCommand1;
            }
    
            public void run() {
                try {
                    Thread.sleep(20000);
                    cantDoCommand1.remove(player1);
    
                } catch (Exception Ignored) {
    
                }
            }
        }
    }
     
    Last edited: Feb 21, 2015
Thread Status:
Not open for further replies.

Share This Page