I have a plugin in which a player left-clicks with an emerald and an explosion occurs at the targeted block. Is there anyway I can add a cooldown until the effect can be used again?
Code:JAVA //First of all declare a HashMapHashMap<String, Long> countdown = new HashMap<String, Long>();//Then when the explosion is called in your plugin put the player in the Hashmap and the time he triggers it.countdown.put(player.getName(), System.currentTimeMillis());//So now the player is stored and the time when he triggers the explosion, too. Let's use it, to compare if he allready waited 10 secif(countdown.get(player.getName()) - System.currentTimeMillis() >= 10000){//do some thing because he allready waited 10sec (10000 milli-seconds)}else{//what a badassplayer.sendMessage("You are a badass!");} @fireblast709 is that what he asked?
lolz at the small note. Well it is a code description of what Tirelessly ment, so yea, this is the cooldown. Though you might want to change Code:java if(countdown.get(player.getName()) - System.currentTimeMillis() >= 10000){ into Code:java if(System.currentTimeMillis() - (countdown.contains(player.getName()) ? countdown.get(player.getName()) : 0) >= 10000){
Acer_Mortem I know the problem but for better helping purposes: could you please post your code snippet where the hashmap thing is now?
Here's the code with your suggestion: Code: import java.util.HashMap; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEvent; public class MyPlayerListener implements Listener { public static NoSpammer plugin; @EventHandler public void onPlayerInteract(PlayerInteractEvent event){ Player player = event.getPlayer(); int blockId = player.getItemInHand().getType().getId(); if(blockId == 388){ Block block = player.getTargetBlock(null, 10); Location location = block.getLocation(); World world = player.getWorld(); world.createExplosion(location, 5); HashMap<String, Long> countdown = new HashMap<String, Long>(); countdown.put(player.getName(), System.currentTimeMillis()); if(countdown.get(player.getName()) - System.currentTimeMillis() >= 10000){ }else{ player.sendMessage("You are a badass!"); } }else if(blockId == 369){ Block block = player.getTargetBlock(null, 10); Location location = block.getLocation(); World world = player.getWorld(); world.strikeLightning(location); } } } And here is the code with FireBlast709's suggestion: Code: import java.util.HashMap; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEvent; public class MyPlayerListener implements Listener { public static NoSpammer plugin; @EventHandler public void onPlayerInteract(PlayerInteractEvent event){ Player player = event.getPlayer(); int blockId = player.getItemInHand().getType().getId(); if(blockId == 388){ Block block = player.getTargetBlock(null, 10); Location location = block.getLocation(); World world = player.getWorld(); world.createExplosion(location, 5); HashMap<String, Long> countdown = new HashMap<String, Long>(); countdown.put(player.getName(), System.currentTimeMillis()); if(System.currentTimeMillis() - (countdown.contains(player.getName()) ? countdown.get(player.getName()) : 0) >= 10000){ }else{ player.sendMessage("You are a badass!"); } }else if(blockId == 369){ Block block = player.getTargetBlock(null, 10); Location location = block.getLocation(); World world = player.getWorld(); world.strikeLightning(location); } } } In FireBlast's code, there is an error saying "the method contains(string) is undefined..."
Code:JAVA public class MyPlayerListener implements Listener {public static NoSpammer plugin;//declare the HashMap there, because we need only one instance!private HashMap<String, Long> countdown = new HashMap<String, Long>(); @EventHandlerpublic void onPlayerInteract(PlayerInteractEvent event){Player player = event.getPlayer();int blockId = player.getItemInHand().getType().getId();if(blockId == 388){//if the player is not in the HashMap put him in there!if(!countdown.containsKey(player.getName())){countdown.put(player.getName(), System.currentTimeMillis());Block block = player.getTargetBlock(null, 10);Location location = block.getLocation();World world = player.getWorld();world.createExplosion(location, 5);//else if he is, compare if he is in there 10000 milli-sec}else{if(countdown.get(player.getName()) - System.currentTimeMillis() >= 10000){player.sendMessage("You waited ten seconds!");//if he is in there for 10000 milli-sec remove him, because we do not longer need him!countdown.remove(player.getName());Block block = player.getTargetBlock(null, 10);Location location = block.getLocation();World world = player.getWorld();world.createExplosion(location, 5);}else{//if not, he is still a badassplayer.sendMessage("You are a badass!");}}}else if(blockId == 369){Block block = player.getTargetBlock(null, 10);Location location = block.getLocation();World world = player.getWorld();world.strikeLightning(location);}}} Yaaargh Captain Longbeard says it is not tested, but it should work fine!
^ Hm, seems like the cooldown starts but never ends xD and every time I use the emerald again to trigger the explosive, it just keeps saying "You are a badass!" EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Code:JAVA import java.util.HashMap; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEvent; public class MyPlayerListener implements Listener { public static NoSpammer plugin; //declare the HashMap there, because we need only one instance! private HashMap<String, Long> countdown = new HashMap<String, Long>(); @EventHandler public void onPlayerInteract(PlayerInteractEvent event){ Player player = event.getPlayer(); int blockId = player.getItemInHand().getType().getId(); if(blockId == 388){ //if the player is not in the HashMap put him in there! if(!countdown.containsKey(player.getName())){ countdown.put(player.getName(), System.currentTimeMillis()); Block block = player.getTargetBlock(null, 10); Location location = block.getLocation(); World world = player.getWorld(); world.createExplosion(location, 5); //else if he is, compare if he is in there 10000 milli-sec }else{ if(countdown.get(System.currentTimeMillis() - countdown.get(player.getName())) > 10000){ player.sendMessage("You waited ten seconds!"); //if he is in there for 10000 milli-sec remove him, because we do not longer need him! countdown.remove(player.getName()); Block block = player.getTargetBlock(null, 10); Location location = block.getLocation(); World world = player.getWorld(); world.createExplosion(location, 5); }else if(System.currentTimeMillis() - countdown.get(player.getName()) < 10000){ //if not, he is still a badass player.sendMessage("You are a badass!"); } } } } } Sorry for some logical errors but i'm very tired btw if the plugin keeps saying you are a badass you should listen fireblast709 hardcorefixed!
Muxon your stored System.currentTimeMillis() - the actual System.currentTimeMillis() will always be negative Acer_Mortem use my code, with the change of .contains to .containsKey
fireblast709 Your code allows the explosion to occur. The only noticable thing is "You are a Badass!" Muxon Is there anyway you could re-enable the explosion to occur? Because then it would work.
I'm quite sure that both, fireblast and Muxon's code, should work now. I edited my code above so try it again
What about it? Syntax errors ;3 EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Muxon Same error? Any code that actually works? Btw, thanks for all the help guys =) It's really appreciated.
Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide Spoiler Code:JAVA import java.util.HashMap; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEvent; public class MyPlayerListener implements Listener { public static NoSpammer plugin; //declare the HashMap there, because we need only one instance! private HashMap<String, Long> countdown = new HashMap<String, Long>(); @EventHandler public void onPlayerInteract(PlayerInteractEvent event){ Player player = event.getPlayer(); int blockId = player.getItemInHand().getType().getId(); if(blockId == 388){ //if the player is not in the HashMap put him in there! if(!countdown.containsKey(player.getName())){ countdown.put(player.getName(), System.currentTimeMillis()); Block block = player.getTargetBlock(null, 10); Location location = block.getLocation(); World world = player.getWorld(); world.createExplosion(location, 5); //else if he is, compare if he is in there 10000 milli-sec }else{ if(countdown.get(System.currentTimeMillis() - countdown.get(player.getName())) > 10000){ player.sendMessage("You waited ten seconds!"); //if he is in there for 10000 milli-sec remove him, because we do not longer need him! countdown.remove(player.getName()); Block block = player.getTargetBlock(null, 10); Location location = block.getLocation(); World world = player.getWorld(); world.createExplosion(location, 5); }else if(System.currentTimeMillis() - countdown.get(player.getName()) < 10000){ //if not, he is still a badass player.sendMessage("You are a badass!"); } } } } } Finally this should work now
Acer_Mortem Code:java import java.util.HashMap; import org.bukkit.Location;import org.bukkit.World;import org.bukkit.block.Block;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerInteractEvent; public class MyPlayerListener implements Listener { public static NoSpammer plugin; HashMap<String, Long> countdown = new HashMap<String, Long>(); @EventHandler public void onPlayerInteract(PlayerInteractEvent event){ Player player = event.getPlayer(); int blockId = player.getItemInHand().getType().getId(); Block block = player.getTargetBlock(null, 10); if(block == null) return; if(blockId == 388){ if(System.currentTimeMillis() - (countdown.containsKey(player.getName()) ? countdown.get(player.getName()) : 0) >= 10000) { Location location = block.getLocation(); World world = player.getWorld(); world.createExplosion(location, 5); countdown.put(player.getName(), System.currentTimeMillis()); } else { player.sendMessage("You are a badass!"); } } else if(blockId == 369) { Location location = block.getLocation(); World world = player.getWorld(); world.strikeLightning(location); } }}
*sigh* Once again, doesn't work =( Explosive goes once, but the countdown doesn't end. Maybe is there a way to freeze the player so that he cannot use the item again? OMG! THIS ONE WORKS! THANK YOU SO MUCH! *BIG HUGS* EDIT by Moderator: merged posts, please use the edit button instead of double posting.
fireblast709 Spoiler (Move your mouse to the spoiler area to reveal the content) Show Spoiler Hide Spoiler