Solved Reloading Animation using durability?

Discussion in 'Plugin Development' started by MayoDwarf, May 3, 2014.

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

    MayoDwarf

    I am trying to make a reload animation as the durability of an item going from 0 back to it's max. How would I do that? Example of what I mean by durability going up and down:
    [​IMG]
    Thanks - Jared(MayoDwarf)
     
  2. Offline

    tommycake50

    Animation?
     
  3. Offline

    MayoDwarf

    tommycake50 Like making the durability go up each second giving by a number of seconds. For instance the cool down for the gun is 5 second. item.getType().getMaxDurability()/5 gets run every second out of 5 seconds. Get what I mean? You would see the durability bar increase each second to until it is full which meant it is reloaded.
     
  4. Offline

    tommycake50

    Well just create a SyncRepeatingTask set the interval to the interval (seconds) * 20 and use a BukkitRunnable so that when it has repeated a sufficient amount of times you can cancel() it and inside the run method you just do the repairing code.
     
  5. Offline

    MayoDwarf

    tommycake50 However you cannot divide a short which is what I need to do. I have already done the other stuff. I want to know how I can do: item.setDurability(item.getType().getDurability()/counter);
     
  6. Offline

    tommycake50

    Cast the short to an int and Cast back.
     
  7. Offline

    MayoDwarf

    tommycake50 int j = i.getType().getMaxDurability()/count;
    i.setDurability((short) j);
    ??? Good?
     
  8. Offline

    tommycake50

    More like.
    Code:
     
    short j = (short)(((int)i.getType().getMaxDurability())/count);
    i.setDurability(j);
    
     
  9. Offline

    MayoDwarf

    tommycake50 Still nothing... :( Code:
    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent evt) {
    3. final Player p = evt.getPlayer();
    4. final ItemStack i = p.getItemInHand();
    5. if(evt.getAction() == Action.RIGHT_CLICK_BLOCK) {
    6. if(i.getType() == Material.NETHER_STAR) {
    7. summonGolem(p, evt.getClickedBlock().getLocation(), GolemType.ATTACK);
    8. }
    9. }
    10. if(i.getType() == Material.WOOD_HOE) {
    11. if(evt.getAction() == Action.RIGHT_CLICK_AIR) {
    12. if(ChatColor.stripColor(i.getItemMeta().getDisplayName()).equals("XO-16 Chaingun")) {
    13. if(!cooldown.contains(p.getDisplayName())) {
    14. p.launchProjectile(Snowball.class);
    15. if(shots.containsKey(p.getDisplayName())) {
    16. shots.put(p.getDisplayName(), shots.get(p.getDisplayName())+1);
    17. } else {
    18. shots.put(p.getDisplayName(), 1);
    19. }
    20. if(shots.get(p.getDisplayName()) == 30) {
    21. cooldown.add(p.getDisplayName());
    22. p.sendMessage(ChatColor.DARK_PURPLE+""+ChatColor.BOLD+"<<RELOADING>>");
    23. final int task1 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable() {
    24. int count = 4;
    25. @Override
    26. public void run() {
    27. count--;
    28. if(count >= 1) {
    29. short j = (short)(((int)i.getType().getMaxDurability())/count);
    30. i.setDurability(j);
    31. } else {
    32. cooldown.remove(p.getDisplayName());
    33. shots.remove(p.getDisplayName());
    34. i.setDurability(i.getType().getMaxDurability());
    35. }
    36. }
    37. }, 0, 20);
    38. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    39. @Override
    40. public void run() {
    41. Bukkit.getScheduler().cancelTask(task1);
    42. }
    43. }, 20*6);
    44. }
    45. } else {
    46. p.sendMessage(ChatColor.RED+"You can't fire when you are reloading!");
    47. }
    48. }
    49. }
    50. } else
    51. if(i.getType() == Material.WOOD_AXE) {
    52. if(evt.getAction() == Action.RIGHT_CLICK_AIR) {
    53. if(ChatColor.stripColor(i.getItemMeta().getDisplayName()).equals("Rocket Salvo")) {
    54. if(!rocketCool.contains(ChatColor.stripColor(p.getDisplayName()))) {
    55. p.launchProjectile(SmallFireball.class);
    56. p.launchProjectile(SmallFireball.class);
    57. p.launchProjectile(SmallFireball.class);
    58. p.launchProjectile(SmallFireball.class);
    59. p.launchProjectile(SmallFireball.class);
    60. rocketCool.add(ChatColor.stripColor(p.getDisplayName()));
    61. final int task2 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable() {
    62. int count = 8;
    63. public void run() {
    64. count--;
    65. if(count >= 1) {
    66. short j = (short)(((int)i.getType().getMaxDurability())/count);
    67. i.setDurability(j);
    68. } else {
    69. i.setDurability(i.getType().getMaxDurability());
    70. }
    71. }
    72. }, 0, 20);
    73. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    74. @Override
    75. public void run() {
    76. rocketCool.remove(p.getDisplayName());
    77. Bukkit.getScheduler().cancelTask(task2);
    78. }
    79. }, 20*8);
    80. }
    81. }
    82. }
    83. }
    84. }


    tommycake50

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

    tommycake50

    Send a debug message with the value of j.

    BTW: Put an if statement if(count == 0) then run cancel() and use a BukkitRunnable instead of a Runnable.
     
  11. Offline

    MayoDwarf

    Console:
    Code:java
    1. [12:20:58 INFO]: 8
    2. [12:20:59 INFO]: 9
    3. [12:21:00 INFO]: 11
    4. [12:21:01 INFO]: 14
    5. [12:21:02 INFO]: 19
    6. [12:21:03 INFO]: 29
    7. [12:21:04 INFO]: 59
    8.  

    CODE:
    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent evt) {
    3. final Player p = evt.getPlayer();
    4. final ItemStack i = p.getItemInHand();
    5. if(evt.getAction() == Action.RIGHT_CLICK_BLOCK) {
    6. if(i.getType() == Material.NETHER_STAR) {
    7. summonGolem(p, evt.getClickedBlock().getLocation(), GolemType.ATTACK);
    8. }
    9. }
    10. if(i.getType() == Material.WOOD_HOE) {
    11. if(evt.getAction() == Action.RIGHT_CLICK_AIR) {
    12. if(ChatColor.stripColor(i.getItemMeta().getDisplayName()).equals("XO-16 Chaingun")) {
    13. if(!cooldown.contains(p.getDisplayName())) {
    14. p.launchProjectile(Snowball.class);
    15. if(shots.containsKey(p.getDisplayName())) {
    16. shots.put(p.getDisplayName(), shots.get(p.getDisplayName())+1);
    17. } else {
    18. shots.put(p.getDisplayName(), 1);
    19. }
    20. if(shots.get(p.getDisplayName()) == 30) {
    21. cooldown.add(p.getDisplayName());
    22. p.sendMessage(ChatColor.DARK_PURPLE+""+ChatColor.BOLD+"<<RELOADING>>");
    23. final int task1 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main, new Runnable() {
    24. int count = 4;
    25. @Override
    26. public void run() {
    27. count--;
    28. if(count >= 1) {
    29. short j = (short)(((int)i.getType().getMaxDurability())/count);
    30. i.setDurability(j);
    31. p.updateInventory();
    32. } else {
    33. cooldown.remove(p.getDisplayName());
    34. shots.remove(p.getDisplayName());
    35. i.setDurability(i.getType().getMaxDurability());
    36. p.updateInventory();
    37. }
    38. }
    39. }, 0, 20);
    40. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    41. @Override
    42. public void run() {
    43. Bukkit.getScheduler().cancelTask(task1);
    44. }
    45. }, 20*6);
    46. }
    47. } else {
    48. p.sendMessage(ChatColor.RED+"You can't fire when you are reloading!");
    49. }
    50. }
    51. }
    52. } else
    53. if(i.getType() == Material.WOOD_AXE) {
    54. if(evt.getAction() == Action.RIGHT_CLICK_AIR) {
    55. if(ChatColor.stripColor(i.getItemMeta().getDisplayName()).equals("Rocket Salvo")) {
    56. if(!rocketCool.contains(ChatColor.stripColor(p.getDisplayName()))) {
    57. p.launchProjectile(SmallFireball.class);
    58. p.launchProjectile(SmallFireball.class);
    59. p.launchProjectile(SmallFireball.class);
    60. p.launchProjectile(SmallFireball.class);
    61. p.launchProjectile(SmallFireball.class);
    62. rocketCool.add(ChatColor.stripColor(p.getDisplayName()));
    63. final int task2 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(main, new BukkitRunnable() {
    64. int count = 8;
    65. public void run() {
    66. count--;
    67. if(count >= 1) {
    68. short j = (short)(((int)i.getType().getMaxDurability())/count);
    69. i.setDurability(j);
    70. p.updateInventory();
    71. System.out.print(j);
    72. } else {
    73. i.setDurability(i.getType().getMaxDurability());
    74. p.updateInventory();
    75. }
    76. }
    77. }, 0, 20);
    78. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new BukkitRunnable() {
    79. @Override
    80. public void run() {
    81. rocketCool.remove(p.getDisplayName());
    82. Bukkit.getScheduler().cancelTask(task2);
    83. }
    84. }, 20*8);
    85. }
    86. }
    87. }
    88. }
    89. }


    tommycake50

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

    tommycake50

    As I thought, max durability is when it is depleted.
    eg 8 is almost full 59 is almost empty.
    I can't see why it doesn't update though.
     
  13. Offline

    MayoDwarf

  14. Offline

    tommycake50

    Well you could try deleting it then adding it as a new ItemStack with the new durability in the same slot before updating the inventory.
     
  15. Offline

    MayoDwarf

  16. Offline

    tommycake50

    It should apart from the fact that you are doing the opposite of repairing it.
     
Thread Status:
Not open for further replies.

Share This Page