Cancelling bukkit runnable in other method

Discussion in 'Plugin Development' started by Dmrtje, Nov 21, 2014.

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

    Dmrtje

    Hello i want to cancel my bukkit runable in the updateInventory(final Player p) method in my inventoryCloseEvent this is my code:

    Code:java
    1. package net.voxelvoid.themepark.inventory;
    2.  
    3. import java.util.Arrays;
    4. import net.voxelvoid.themepark.ThemePark;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.inventory.InventoryClickEvent;
    12. import org.bukkit.event.inventory.InventoryCloseEvent;
    13. import org.bukkit.event.inventory.InventoryOpenEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.scheduler.BukkitRunnable;
    18.  
    19. /**
    20. *
    21. * @author Tim
    22. */
    23. public class InventoryShows implements Listener {
    24.  
    25. private static ThemePark instance;
    26. private static InventoryMain main;
    27.  
    28. public InventoryShows(ThemePark instance) {
    29. this.instance = instance;
    30. this.main = new InventoryMain(instance);
    31. }
    32.  
    33. private static final ItemStack pegasusWaterShowClosed = createItem(ChatColor.GOLD + "Pegasus", Material.STAINED_CLAY, 1, (short) 14, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Pegasus watershow!");
    34. private static final ItemStack pegasusWaterShowStarted = createItem(ChatColor.GOLD + "Pegasus", Material.STAINED_CLAY, 1, (short) 4, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Pegasus watershow!");
    35. private static final ItemStack pegasusWaterShowOpend = createItem(ChatColor.GOLD + "Pegasus", Material.STAINED_CLAY, 1, (short) 5, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Pegasus watershow!");
    36. private static final ItemStack VoxelVoidFireworkShowClosed = createItem(ChatColor.GOLD + "VoxelVoid firework", Material.STAINED_CLAY, 1, (short) 14, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Bonfire firework show!");
    37. private static final ItemStack VoxelVoidFireworkShowStarted = createItem(ChatColor.GOLD + "VoxelVoid firework", Material.STAINED_CLAY, 1, (short) 4, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Bonfire firework show!");
    38. private static final ItemStack VoxelVoidFireworkShowOpend = createItem(ChatColor.GOLD + "VoxelVoid firework", Material.STAINED_CLAY, 1, (short) 5, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Bonfire firework show!");
    39. private static final ItemStack magicCircleTheaterShowClosed = createItem(ChatColor.GOLD + "The magic circle", Material.STAINED_CLAY, 1, (short) 14, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing magic circle theatershow!");
    40. private static final ItemStack magicCircleTheaterShowStarted = createItem(ChatColor.GOLD + "The magic circle", Material.STAINED_CLAY, 4, (short) 1, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing magic circle theatershow!");
    41. private static final ItemStack magicCircleTheaterShowOpend = createItem(ChatColor.GOLD + "The magic circle", Material.STAINED_CLAY, 5, (short) 1, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing magic circle theatershow!");
    42. private static final ItemStack juegoCircusClosed = createItem(ChatColor.GOLD + "Circus Juego", Material.STAINED_CLAY, 1, (short) 14, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Juego circus!");
    43. private static final ItemStack juegoCircusStarted = createItem(ChatColor.GOLD + "Circus Juego", Material.STAINED_CLAY, 1, (short) 4, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Juego circus!");
    44. private static final ItemStack juegoCircusOpend = createItem(ChatColor.GOLD + "Circus Juego", Material.STAINED_CLAY, 1, (short) 5, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing Juego circus!");
    45. private static final ItemStack destinyConcertClosed = createItem(ChatColor.GOLD + "Concert Destiny", Material.STAINED_CLAY, 1, (short) 14, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing concert Destiny");
    46. private static final ItemStack destinyConcertStarted = createItem(ChatColor.GOLD + "Concert Destiny", Material.STAINED_CLAY, 1, (short) 4, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing concert Destiny");
    47. private static final ItemStack destinyConcertOpend = createItem(ChatColor.GOLD + "Concert Destiny", Material.STAINED_CLAY, 1, (short) 5, ChatColor.GRAY + "Click to go to the", ChatColor.GRAY + "amazing concert Destiny");
    48. private static final ItemStack backItem = createItem(ChatColor.GOLD + "Main menu", Material.ARROW, 1, (short) 0, ChatColor.GRAY + "Click to go back to the main menu.");
    49.  
    50. public static Inventory inventory_shows = Bukkit.createInventory(null, 9, ChatColor.BLACK + "" + ChatColor.BOLD + " Shows.");
    51.  
    52. private static ItemStack createItem(String name, Material mat, int amount, short data, String... lore) {
    53. ItemStack item = new ItemStack(mat, amount, data);
    54. ItemMeta meta = item.getItemMeta();
    55. meta.setDisplayName(name);
    56. meta.setLore(Arrays.asList(lore));
    57. item.setItemMeta(meta);
    58. return item;
    59. }
    60.  
    61. public void updateInventory(final Player p) {
    62. Bukkit.broadcastMessage("update");
    63. new BukkitRunnable() {
    64. @Override
    65. public void run() {
    66. Bukkit.broadcastMessage("update!");
    67. final Inventory inv = p.getOpenInventory().getTopInventory();
    68. for (ItemStack i : inv.getContents()) {
    69. if (i.hasItemMeta()) {
    70. if (i.getItemMeta().hasDisplayName()) {
    71. String s = i.getItemMeta().getDisplayName();
    72. if (s.contains(ChatColor.GOLD + "Pegasus")) {
    73. String a = instance.showsAndRides.getString("shows.pegasus");
    74. if (a.equals("opend")) {
    75. inv.setItem(0, pegasusWaterShowOpend);
    76. p.updateInventory();
    77. } else if (a.equals("started")) {
    78. inv.setItem(0, pegasusWaterShowStarted);
    79. p.updateInventory();
    80. } else if (a.equals("closed")) {
    81. inv.setItem(0, pegasusWaterShowClosed);
    82. p.updateInventory();
    83. } else {
    84. inv.setItem(0, pegasusWaterShowClosed);
    85. p.updateInventory();
    86. }
    87. } else if (s.contains(ChatColor.GOLD + "VoxelVoid firework")) {
    88. String a = instance.showsAndRides.getString("shows.voxelvoid firework");
    89. if (a.equals("opend")) {
    90. inv.setItem(1, VoxelVoidFireworkShowOpend);
    91. p.updateInventory();
    92. } else if (a.equals("started")) {
    93. inv.setItem(1, VoxelVoidFireworkShowStarted);
    94. p.updateInventory();
    95. } else if (a.equals("closed")) {
    96. inv.setItem(1, VoxelVoidFireworkShowClosed);
    97. p.updateInventory();
    98. } else {
    99. inv.setItem(1, VoxelVoidFireworkShowClosed);
    100. p.updateInventory();
    101. }
    102. } else if (s.contains(ChatColor.GOLD + "The magig circle")) {
    103. String a = instance.showsAndRides.getString("shows.magic circle");
    104. if (a.equals("opend")) {
    105. inv.setItem(2, magicCircleTheaterShowOpend);
    106. p.updateInventory();
    107. } else if (a.equals("started")) {
    108. inv.setItem(2, magicCircleTheaterShowStarted);
    109. p.updateInventory();
    110. } else if (a.equals("closed")) {
    111. inv.setItem(2, magicCircleTheaterShowClosed);
    112. p.updateInventory();
    113. } else {
    114. inv.setItem(2, magicCircleTheaterShowClosed);
    115. p.updateInventory();
    116. }
    117. } else if (s.contains(ChatColor.GOLD + "Circus Juego")) {
    118. String a = instance.showsAndRides.getString("shows.circus juego");
    119. if (a.equals("opend")) {
    120. inv.setItem(3, juegoCircusOpend);
    121. p.updateInventory();
    122. } else if (a.equals("started")) {
    123. inv.setItem(3, juegoCircusStarted);
    124. p.updateInventory();
    125. } else if (a.equals("closed")) {
    126. inv.setItem(3, juegoCircusClosed);
    127. p.updateInventory();
    128. } else {
    129. inv.setItem(3, juegoCircusClosed);
    130. p.updateInventory();
    131. }
    132. } else if (s.contains(ChatColor.GOLD + "Concert Destiny")) {
    133. String a = instance.showsAndRides.getString("shows.concert destiny");
    134. if (a.equals("opend")) {
    135. inv.setItem(4, destinyConcertOpend);
    136. p.updateInventory();
    137. } else if (a.equals("started")) {
    138. inv.setItem(4, destinyConcertStarted);
    139. p.updateInventory();
    140. } else if (a.equals("closed")) {
    141. inv.setItem(4, destinyConcertClosed);
    142. p.updateInventory();
    143. } else {
    144. inv.setItem(4, destinyConcertClosed);
    145. p.updateInventory();
    146. }
    147. }
    148. }
    149. }
    150. }
    151. }
    152. }.runTaskTimer(instance, 20L, 50L);
    153. }
    154.  
    155. @EventHandler
    156. public void onClose(InventoryCloseEvent e) {
    157.  
    158. if (e.getInventory().getName().equals(inventory_shows.getName())) {
    159. e.getInventory().clear();
    160. } else {
    161. return;
    162. }
    163. }
    164.  
    165. @EventHandler
    166. public void onOpen(InventoryOpenEvent e) {
    167. updateInventory((Player) e.getPlayer());
    168. }
    169.  
    170. public static void loadInventory(final Player p) {
    171. final Inventory inv = Bukkit.createInventory(null, 9, ChatColor.BLACK + "" + ChatColor.BOLD + " Shows.");
    172. p.openInventory(inv);
    173.  
    174. inv.setItem(8, pegasusWaterShowClosed);
    175.  
    176. new BukkitRunnable() {
    177. @Override
    178. public void run() {
    179. inv.setItem(7, pegasusWaterShowClosed);
    180. inv.setItem(8, VoxelVoidFireworkShowClosed);
    181. }
    182. }.runTaskLater(instance, 2L);
    183.  
    184. new BukkitRunnable() {
    185. @Override
    186. public void run() {
    187. inv.setItem(6, pegasusWaterShowClosed);
    188. inv.setItem(7, VoxelVoidFireworkShowClosed);
    189. inv.setItem(8, magicCircleTheaterShowClosed);
    190. }
    191. }.runTaskLater(instance, 4L);
    192.  
    193. new BukkitRunnable() {
    194. @Override
    195. public void run() {
    196. inv.setItem(5, pegasusWaterShowClosed);
    197. inv.setItem(6, VoxelVoidFireworkShowClosed);
    198. inv.setItem(7, magicCircleTheaterShowClosed);
    199. inv.setItem(8, juegoCircusClosed);
    200. }
    201. }.runTaskLater(instance, 6L);
    202.  
    203. new BukkitRunnable() {
    204. @Override
    205. public void run() {
    206. inv.setItem(4, pegasusWaterShowClosed);
    207. inv.setItem(5, VoxelVoidFireworkShowClosed);
    208. inv.setItem(6, magicCircleTheaterShowClosed);
    209. inv.setItem(7, juegoCircusClosed);
    210. inv.setItem(8, destinyConcertClosed);
    211. }
    212. }.runTaskLater(instance, 8L);
    213.  
    214. new BukkitRunnable() {
    215. @Override
    216. public void run() {
    217. inv.setItem(3, pegasusWaterShowClosed);
    218. inv.setItem(4, VoxelVoidFireworkShowClosed);
    219. inv.setItem(5, magicCircleTheaterShowClosed);
    220. inv.setItem(6, juegoCircusClosed);
    221. inv.setItem(7, destinyConcertClosed);
    222. inv.setItem(8, backItem);
    223. }
    224. }.runTaskLater(instance, 10L);
    225.  
    226. new BukkitRunnable() {
    227. @Override
    228. public void run() {
    229. inv.setItem(2, pegasusWaterShowClosed);
    230. inv.setItem(3, VoxelVoidFireworkShowClosed);
    231. inv.setItem(4, magicCircleTheaterShowClosed);
    232. inv.setItem(5, juegoCircusClosed);
    233. inv.setItem(7, null);
    234. inv.setItem(6, destinyConcertClosed);
    235. }
    236. }.runTaskLater(instance, 12L);
    237.  
    238. new BukkitRunnable() {
    239. @Override
    240. public void run() {
    241. inv.setItem(1, pegasusWaterShowClosed);
    242. inv.setItem(2, VoxelVoidFireworkShowClosed);
    243. inv.setItem(3, magicCircleTheaterShowClosed);
    244. inv.setItem(4, juegoCircusClosed);
    245. inv.setItem(6, null);
    246. inv.setItem(5, destinyConcertClosed);
    247. }
    248. }.runTaskLater(instance, 14L);
    249.  
    250. new BukkitRunnable() {
    251. @Override
    252. public void run() {
    253. inv.setItem(0, pegasusWaterShowClosed);
    254. inv.setItem(1, VoxelVoidFireworkShowClosed);
    255. inv.setItem(2, magicCircleTheaterShowClosed);
    256. inv.setItem(3, juegoCircusClosed);
    257. inv.setItem(5, null);
    258. inv.setItem(4, destinyConcertClosed);
    259. }
    260. }.runTaskLater(instance, 16L);
    261. }
    262.  
    263. @EventHandler
    264. public void onClick(InventoryClickEvent e) {
    265. Player p = (Player) e.getWhoClicked();
    266. if (e.getInventory().getName().equals(inventory_shows.getName())) {
    267. if (e.getCurrentItem() != null && !e.getCurrentItem().getType().equals(Material.AIR)) {
    268. if (e.getCurrentItem().hasItemMeta()) {
    269. if (e.getCurrentItem().getItemMeta().hasDisplayName()) {
    270. if (e.getCurrentItem().getItemMeta().getDisplayName().equals(backItem.getItemMeta().getDisplayName())) {
    271. e.setCancelled(true);
    272. p.closeInventory();
    273. main.loadInventory(p);
    274. } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(pegasusWaterShowClosed.getItemMeta().getDisplayName())) {
    275. e.setCancelled(true);
    276. p.closeInventory();
    277. //command
    278. } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(VoxelVoidFireworkShowClosed.getItemMeta().getDisplayName())) {
    279. e.setCancelled(true);
    280. p.closeInventory();
    281. //command
    282. } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(magicCircleTheaterShowClosed.getItemMeta().getDisplayName())) {
    283. e.setCancelled(true);
    284. p.closeInventory();
    285. //command
    286. } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(juegoCircusClosed.getItemMeta().getDisplayName())) {
    287. e.setCancelled(true);
    288. p.closeInventory();
    289. //command
    290. } else if (e.getCurrentItem().getItemMeta().getDisplayName().equals(destinyConcertClosed.getItemMeta().getDisplayName())) {
    291. e.setCancelled(true);
    292. p.closeInventory();
    293. //command
    294. } else {
    295. e.setCancelled(true);
    296. }
    297. } else {
    298. return;
    299. }
    300. } else {
    301. return;
    302. }
    303. } else {
    304. return;
    305. }
    306. } else {
    307. return;
    308. }
    309. }
    310. }
    311.  
     
  2. Offline

    mythbusterma

    Dmrtje

    Why is everything static? I legitimately flinched trying to read that code. Go back and learn what Object Oriented design means before you try and write in a language that is as object oriented as Java.

    As for your issue, you have to obtain an instance of the runnable and invoke cancel(), or obtain it's task id when you schedule it and ask the scheduler to cancel it with cancel(int).
     
    adam753 likes this.
  3. Offline

    Skionz

    Dmrtje Use constructors to pass the runnable to your other class. Also do what mythbusterma said because that is a lot of static.
     
  4. Offline

    teej107

    DrPyroCupcake and Skionz like this.
  5. Offline

    Dmrtje

    teej107 Skionz mythbusterma I know where static is for it is for getting something without a instance and this is just a preview later ill change the most from static to not static. And how do i obtain an instance from the runnable i'm kinda new to runnables?
     
  6. Offline

    mythbusterma

    Dmrtje

    The same way you obtain the instance of any other class.
     
  7. Offline

    Dmrtje

    mythbusterma But a runnalbe is not a method itself its a method inside of a method.

    I know how to obtain an instance from a class but i don't get how i obtain the instance of the runnable.
     
  8. Offline

    teej107

    No, the Runnable is the object. run() is the method inside the Runnable. When you create a straight up Runnable, you are doing what you would call an anonymous class. There are no such thing as methods inside other methods.
     
    Rocoty likes this.
  9. Offline

    Dmrtje

    teej107 okay thanks for the information but i still don't get how to cancel the runnable
     
  10. Offline

    Skionz

    Dmrtje Bukkit.getScheduler().cancelTask(int)? Use a constructor to pass it to another class.
     
  11. Offline

    Dmrtje

    Skionz i want it in the same class but the problem is for everyone that opens the GUI a new schedular starts since everyone has a custom GUI but i only want to cancel the schedular for the menu of that player
     
  12. Offline

    Skionz

  13. Offline

    Dmrtje

    Skionz ehm i don't get it i mean i need a taskID to cancel it outside the run method right? but how do i obtain the taskID since it is different for everyone
     
  14. Offline

    Skionz

    Dmrtje Use a Map that saves the Player and the id (Integer)
     
  15. Offline

    Dmrtje

    Skionz thanks gonna try it!
    do you know how to see if a player has my custom inventory opend cause player.getOpendInventory does not return my custom inventory
     
  16. Offline

    Skionz

    Dmrtje It should. Try comparing the names.
     
  17. Offline

    Dmrtje

    Skionz i already did but it gives me something weird like container.crafting

    Skionz do you know why?

    <Edit by mrCookieSlime: Merged Posts. Please dont double post. There is an Edit-Button right next to the Date.>
     
  18. Offline

    Skionz

    Dmrtje That is probably the type. Use Inventory#getName()
     
  19. Offline

    Dmrtje

    there is only inventory.getTitle() or i need to do inventory.getTopInventory.getName() but that does return container.crafting too

    EDIT:
    forgot to tahg you Skionz
     
  20. Offline

    Skionz

  21. Offline

    Dmrtje

    if i do e.getInventory from the InventoryCloseEvent its an inventory but if i do p.getOpendInventory its an inventoryView

    EDIT:
    Forgot to tahg you again (facepalm) Skionz
     
  22. Offline

    Dmrtje

    Skionz do you know why r somebody that knows it?
     
Thread Status:
Not open for further replies.

Share This Page