Best way to solve this?

Discussion in 'Plugin Development' started by 22vortex22, Nov 27, 2013.

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

    22vortex22



    You will hear some background noise and yes my name is Harris. Please don't focus on that though.
     
  2. Offline

    sgavster

    22vortex22 Can you post just the link? I can't see the video. It's just a white square.
     
  3. Offline

    HyrulesLegend

    Code please? Stacktrace?
     
  4. Offline

    22vortex22


    Code:java
    1. package com.gmail.codervortex;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.entity.EntityDamageEvent;
    12. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.event.player.PlayerMoveEvent;
    16.  
    17. public class Events implements Listener{
    18. public Main plugin;
    19. public Events(Main instance){
    20. plugin = instance;
    21. }
    22.  
    23.  
    24. @EventHandler(priority = EventPriority.HIGH)
    25. public void onFallDamage(EntityDamageEvent event){
    26. if(event.getEntity() instanceof Player && event.getCause() == DamageCause.FALL)
    27. event.setCancelled(true);
    28. }
    29.  
    30.  
    31.  
    32.  
    33. @EventHandler
    34. public void OnPlayerRocket(PlayerInteractEvent event){
    35. Player player = event.getPlayer();
    36. if(player.getExp() == 1f){
    37. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() ==Action.RIGHT_CLICK_BLOCK) && player.getItemInHand().getType() == Material.FIREWORK){
    38. player.setVelocity(player.getLocation().getDirection()
    39. .multiply(1.05).setY(1.15));
    40. player.setExp(0.50F);
    41. plugin.RocketCooldown2.add(player.getName());
    42. }
    43. }
    44. else if(plugin.RocketCooldown2.contains(player.getName()) && player.getExp() == 0.5f){
    45. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() ==Action.RIGHT_CLICK_BLOCK) && player.getItemInHand().getType() == Material.FIREWORK){
    46. player.setVelocity(player.getLocation().getDirection()
    47. .multiply(1.05).setY(1.15));
    48. player.setExp(0);
    49. }
    50. }
    51.  
    52. }
    53. int time;
    54. @EventHandler
    55. public void OnGround(PlayerMoveEvent e){
    56. final Player p = e.getPlayer();
    57. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable()
    58. {
    59. @Override
    60. public void run(){
    61. if(p.getLocation().add(0,-1,0).getBlock().getType() != Material.AIR){
    62. p.setExp(1);
    63. }
    64. }
    65. }
    66. ,3L);
    67.  
    68.  
    69. }
    70.  
    71. @EventHandler
    72. public void OnPlayerJoin(PlayerJoinEvent event){
    73. Player player = event.getPlayer();
    74. event.setJoinMessage(ChatColor.GOLD + "Hello " + player.getName() + ". Welcome to Minecraft Smash!");
    75. }
    76.  
    77.  
    78.  
    79.  
    80.  
    81.  
    82.  
    83.  
    84.  
    85. }
    86.  


    Cant believe I forgot about that.


    Code:
    http://www.youtube.com/watch?v=F9uNDQqjhnw&feature=youtu.be
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  5. Offline

    sgavster

    22vortex22 You're probably being added to the arraylist more than one time, or not removed correctly.
     
  6. Offline

    22vortex22

    That shouldn't happen... Im pretty sure it has something do with when they are on the ground but I really need help on this.
     
  7. Offline

    sgavster

    22vortex22 Code?


    Also, [offtopic] are you trying to remake the thing from your smash video? :3 [/offtopic]
     
  8. Offline

    22vortex22

    Yes.
     
  9. Offline

    sgavster

    22vortex22 Are you interested in getting help? looks like fun (pm me if yes)

    And is there code so that I can help fix this? :p
     
  10. Offline

    22vortex22


    Code:java
    1. @EventHandler
    2. public void OnPlayerRocket(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. if(player.getExp() == 1f){
    5. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() ==Action.RIGHT_CLICK_BLOCK) && player.getItemInHand().getType() == Material.FIREWORK){
    6. player.setVelocity(player.getLocation().getDirection()
    7. .multiply(1.05).setY(1.15));
    8. player.setExp(0.50F);
    9. plugin.RocketCooldown2.add(player.getName());
    10. }
    11. }
    12. else if(plugin.RocketCooldown2.contains(player.getName()) && player.getExp() == 0.5f){
    13. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() ==Action.RIGHT_CLICK_BLOCK) && player.getItemInHand().getType() == Material.FIREWORK){
    14. player.setVelocity(player.getLocation().getDirection()
    15. .multiply(1.05).setY(1.15));
    16. player.setExp(0);
    17. }
    18. }
    19.  
    20. }
    21. int time;
    22. @EventHandler
    23. public void OnGround(PlayerMoveEvent e){
    24. final Player p = e.getPlayer();
    25. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable()
    26. {
    27. @Override
    28. public void run(){
    29. if(p.getLocation().add(0,-1,0).getBlock().getType() != Material.AIR){
    30. p.setExp(1);
    31. }
    32. }
    33. }
    34. ,3L);
    35.  
    36.  
    37. }


    Bump

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

    NathanWolf

    I think the problem here is a favorite rant of mine.

    Implementing cooldowns with timers is the wrong way to implement cooldowns. Rather than re-rant, I'll just link:

    http://forums.bukkit.org/threads/simple-question-cooldown-for-commands.192037/#post-197551

    The short answer is using timers for cooldowns is inefficient and broken, and could likely cause the weird behavior you're seeing. The end effect is that the actual cooldown time ends up being random, ranging from nearly 0 to whatever cooldown time you're actually trying to set.

    A simple and effective way to properly implement cooldowns is to store the last time each player used the command. Then just check to see if that timestamp is older than X seconds ago, if so allow the command and update the timestamp. No need for schedulers, timers- and it actually works right :)

    You may have other problems there, but I'm thinking the cooldowns are going to be an issue.
     
  12. Offline

    22vortex22



    I agree I think the cooldown is the problem. You're explanation is great but how would I apply it to this? I'm trying to make it so when they land on the ground it gives them the xp bar back. There is no exact cooldown. Would your code you gave for that post work for this?

    NathanWolf
     
  13. Offline

    NathanWolf

    Hm- ok, sorry, taking a closer look I think that rant was unnecessary :)

    I'm still not really clear, but I think I may see a couple of problems:

    1. You never remove a player from the cooldown map- I assumed this was happening in the scheduled task when I made my rant, but I should've looked closer. Did you mean to remove them in the "else if" section? Otherwise it seems like the first part will only ever happen once.

    2. Scheduling a task every single time every player moves seems crazy :) Is there an issue with just giving them the XP directly in the move event? Or, perhaps a better approach, schedule a repeating task and loop through all players, giving them XP if they're on the ground? (Unless you mean it to only happen when they move- but keep in mind you get a LOT of move events!)

    3. I can imagine that the 3-tick delay on the move timer will interfere with what the onInteract is doing- let's say:
    - you're moving, it's going to set your XP to 1 in 3 ticks
    - you rocket, which tries to set your XP to 0
    - Depending on timing the two above things might be racing each other.

    I think svgaster has a better idea of what you're trying to do than me :)
     
  14. Offline

    22vortex22



    You seem to know alot about what you are talking about but I really have no idea do to any of the stuff you said. I just want it to be when they hit the ground they get their xp back so they can take off again. However I think when they launch it gives it back to them because it doesnt give them enough time to get off the ground. NathanWolf sgavster

    Bump

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

    user_90854156

    Looks like you have an arraylist, but you never actually remove the player from it when he hits the ground?
    That probably gives you extra jumps :s
     
Thread Status:
Not open for further replies.

Share This Page