Cooldowns for kit event: Kit Taken

Discussion in 'Plugin Development' started by TCO_007, Apr 10, 2014.

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

    TCO_007

    Hello! I am new to bukkit coding and I do know a good amount of the Java language. I just need some help with my kit. It is basically a remake of the endermage kit from the well-known MCPVP server. This time its with an iron ingot but pretty much the same. Anyways, I need some help. I want to be able to set a 5 second cooldown on my kit. What I mean is the player can teleport and then has to wait 5 seconds to do it again. I would like for it to also count down like:
    p.sendMessage("Please wait 5 seconds for teleportation!");
    p.sendMessage("Please wait 4 seconds for teleportation!");
    and so on.
    I need some help and if anyone could provide anything that could help, that would be absolutely fantastic! Thank you!
    Kit Taken Event Class:
    Code:java
    1. @ EventHandler
    2. public void onTakenClick(PlayerInteractEvent event) {
    3. Player p = (Player) event.getPlayer();
    4. if (plugin.Taken.contains(p.getName())){
    5. if (event.getAction() == Action.RIGHT_CLICK_AIR && p.getItemInHand().getType() == Material.IRON_INGOT) {
    6. List<Entity> nearby = p.getNearbyEntities(3, 20000, 3);
    7. for (Entity tmp : nearby)
    8. if (tmp instanceof Player) {
    9. tmp.teleport(p.getLocation());
    10. p.teleport(p.getLocation());
    11. p.sendMessage(ChatColor.GOLD + "You have teleported " + ((Player) tmp).getName() + " to you!");
    12.  
    13. }
    14. }
    15. }
    16. }
     
  2. Offline

    XvBaseballkidvX

    This should work!

    Code:java
    1. HashMap<String, Integer> map = new HashMap<String, Integer>();
    2.  
    3. public void addCooldown(String name, int time){
    4. map.put(name, time);
    5. }
    6.  
    7. public boolean hasCooldown(String name){
    8. return map.containsKey(name);
    9. }
    10.  
    11. public void removeOneSecond(String name){
    12. map.put(name, map.get(name) - 1);
    13. }
    14.  
    15. public int getSeconds(String name){
    16. if(map.containsKey(name)){
    17. return map.get(name);
    18. }else{
    19. return 0;
    20. }
    21. }
    22.  
    23. //call this in your onEnable method
    24. public void startTask(){
    25. new BukkitRunnable(){
    26. public void run(){
    27. for(String entry : map.keySet()){
    28. removeOneSecond(entry);
    29. int amount = getSeconds(entry);
    30. if(amount <= 0){
    31. map.remove(entry);
    32. }
    33. }
    34. }
    35. }.runTaskTimer(Main.getInstance(), 0, 20); //Runs every second, also. Im not 100% sure but this might cause a concurrent modification exception
    36. }


    If you have any problems just tahg me! :D
     
  3. Offline

    Dahwn

    XvBaseballkidvX
    Don't give them the code, they need to start learning it on their own. Just give them some pseudo code or possible ways to finish it like: put the player into a list and after 5 seconds remove him with a scheduler.

    TCO_007
    You should look some videos about the java basics before you start to code real plugins. Hashmaps and ArrayLists are really the simplest things!
    PS: Tag the thread as solved please.

    -Dahwn
     
  4. Offline

    TCO_007

    Dahwn I know the basics of ArrayLists and Hasmaps and such but I just needed some sample code for help. Thats the way I learn best and I appreciate the help XvBaseballkidvX ! It will help a lot and Im sure! Thank you for taking the time to do it. Dahwn Thanks for trying to teach me and I will certainly look into it! Videos havent worked out real well for me in the past but they do help somewhat. Thanks so much guys!

    Although I do have one question though for the code given. How do I add the player to the cooldown after the Taken kits teleports a player? Its probably a really easy answer but I am new to coding. Thanks!
    Kit Taken Events Class:
    Code:java
    1. @EventHandler
    2. public void CavemanHit(EntityDamageByEntityEvent e){
    3. final Player p = (Player) e.getEntity();
    4. Player d = (Player) e.getDamager();
    5. if (plugin.Caveman.contains(d.getName())){
    6. if (e.getDamager() instanceof Player && e.getEntity() instanceof Player){
    7. if (d.getItemInHand().getType() == Material.STICK){
    8. if(Math.random() > 0.75D){
    9. p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 80, 10));
    10. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 80, 10));
    11.  
    12. }
    13. }
    14. }
    15. }
    16. }
    17.  
    18. HashMap<String, Integer> map = new HashMap<String, Integer>();
    19.  
    20. public void addCooldown(String name, int time){
    21. map.put(name, time);
    22. }
    23.  
    24. public boolean hasCooldown(String name){
    25. return map.containsKey(name);
    26. }
    27.  
    28. public void removeOneSecond(String name){
    29. map.put(name, map.get(name) - 1);
    30. }
    31.  
    32. public int getSeconds(String name){
    33. if(map.containsKey(name)){
    34. return map.get(name);
    35. }else{
    36. return 0;
    37. }
    38. }
    39. }
    40.  


    onEnable Method:
    Code:java
    1. public void startTask(){
    2. new BukkitRunnable(){
    3. public void run(){
    4. for(String entry : map.keySet()){
    5. removeOneSecond(entry);
    6. int amount = getSeconds(entry);
    7. if(amount <= 0){
    8. map.remove(entry);
    9. }
    10. }
    11. }
    12. }.runTaskTimer(Main.getInstance(), 0, 20); //Runs every second, also. Im not 100% sure but this might cause a concurrent modification exception
    13. }
    14. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  5. Offline

    coasterman10

    You should familiarize yourself with Maps, and how to add/remove/get data from it. Here's some example code that you should be able to use to figure out what to do:
    Code:java
    1. // Add a value to a Map:
    2. map.put(key, value);
    3.  
    4. // Remove a value from the Map:
    5. map.remove(key);
    6.  
    7. // Get the value by key from the Map:
    8. value = map.get(key);
     
  6. Offline

    TCO_007

    coasterman10 Where could I learn more about maps because, this probably sounds very silly but I have never used it before. Where could I learn more?
     
  7. Offline

    coasterman10

    TCO_007 A good place to look is the Javadoc: http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
    Map is an interface defining a data structure that maps keys to values and allows looking up the value for that key later. HashMap is the most common implementation of Map and will get the job done for you. So when you go to create a map, it would be defined like this:
    Code:java
    1. Map<K, V> map = new HashMap<K, V>();
     
  8. Offline

    TCO_007

    Thanks coaster! I will look at it now!
     
Thread Status:
Not open for further replies.

Share This Page