Schedule sync repeating task

Discussion in 'Plugin Development' started by hi_guy_5, Jan 22, 2014.

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

    hi_guy_5

    I'm trying to make the smoke effect repeat on a player but "Schedule sync repeating task"
    keeps getting an error. i know this error is because its not my main class but how do i fix it? (please just give me the code instead of telling me what to do I'm new)

    and why is the PlayerEffect crossed out?
    remember I'm new :) cut me slack
    MY LISTENER/MENU CLASS
    Code:java
    1. package mc.masterkraft.plugin;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Color;
    8. import org.bukkit.DyeColor;
    9. import org.bukkit.Effect;
    10. import org.bukkit.FireworkEffect;
    11. import org.bukkit.FireworkEffect.Type;
    12. import org.bukkit.Location;
    13. import org.bukkit.entity.Firework;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.inventory.InventoryClickEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.meta.FireworkMeta;
    21. import org.bukkit.inventory.meta.ItemMeta;
    22. import org.bukkit.material.Wool;
    23. import org.bukkit.plugin.Plugin;
    24. import org.bukkit.potion.PotionEffect;
    25. import org.bukkit.potion.PotionEffectType;
    26.  
    27. public class Menu implements Listener{
    28.  
    29. private Inventory inv;
    30. private ItemStack f, sj, st, ma, pa, hg;
    31.  
    32. public Menu(Plugin p) {
    33. inv = Bukkit.getServer().createInventory(null, 9,ChatColor.DARK_RED + "Master" +ChatColor.GREEN +"Kraft"+ChatColor.BLUE +" DonatorMenu" );
    34.  
    35.  
    36. f = createItem(DyeColor.BLUE, ChatColor.BLUE + "Launch Firework");
    37. sj = createItem(DyeColor.RED, ChatColor.RED + "SuperJump");
    38. st = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "SmokeTrail");
    39. ma = createItem(DyeColor.ORANGE, ChatColor.GOLD + "MobArena");
    40. pa = createItem(DyeColor.PURPLE, ChatColor.DARK_PURPLE + "Parkour");
    41. hg = createItem(DyeColor.GREEN, ChatColor.GREEN + "HungerGames");
    42. inv.setItem(2, f);
    43. inv.setItem(3, sj);
    44. inv.setItem(4, st);
    45. inv.setItem(7, ma);
    46. inv.setItem(8, pa);
    47. inv.setItem(1, hg);
    48. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    49. }
    50.  
    51.  
    52. private ItemStack createItem(DyeColor dc, String name){
    53. ItemStack i = new Wool(dc).toItemStack(1);
    54. ItemMeta im = i.getItemMeta();
    55. im.setDisplayName(name);
    56. im.setLore(Arrays.asList( ChatColor.AQUA + "Click To Do This"));
    57. i.setItemMeta(im);
    58. return i;
    59. }
    60.  
    61. public void show(Player p){
    62. p.openInventory(inv);
    63.  
    64.  
    65. }
    66.  
    67.  
    68. @EventHandler
    69. public void onInventoryClick(InventoryClickEvent e){
    70.  
    71. final Player p = (Player) e.getWhoClicked();
    72.  
    73. if (!(e.getInventory().getName().equalsIgnoreCase(inv.getName()))) return;
    74. if(e.getCurrentItem() == null) return;
    75.  
    76.  
    77. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Launch Firework")){//FIREWORK!
    78. e.setCancelled(true);
    79. p.closeInventory();
    80. p.sendMessage(ChatColor.AQUA +"[!Attention!]This Plugin Has A Spam Detection! Remember Not To Spam The Firework");
    81.  
    82.  
    83. Firework f = (Firework) e.getWhoClicked().getWorld().spawn(e.getWhoClicked().getLocation(), Firework.class);
    84. FireworkMeta fm = f.getFireworkMeta();
    85. fm.addEffect(FireworkEffect.builder()
    86. .flicker(false)
    87. .trail(true)
    88. .with(Type.BALL_LARGE)
    89. .withColor(Color.RED)
    90. .withFade(Color.BLUE)
    91. .build());
    92. fm.setPower(1);
    93. f.setFireworkMeta(fm);
    94.  
    95. }
    96.  
    97.  
    98. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("SuperJump")){
    99. e.setCancelled(true);
    100. p.closeInventory();
    101. e.getWhoClicked().addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, 20 ));
    102. }
    103. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("SmokeTrail")){
    104. e.setCancelled(true);
    105. p.closeInventory();
    106. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    107. public void run() {
    108. p.playEffect(p.getLocation(), Effect.SMOKE, 1);
    109. }
    110.  
    111. }, 5, 5);
    112.  
    113. }
    114. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("MobArena")){
    115. e.setCancelled(true);
    116. ((Player)e.getWhoClicked()).teleport(new Location(Bukkit.getServer().getWorld("mobarena"), 658, 4, 936));
    117. }
    118. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Parkour")){
    119. e.setCancelled(true);
    120. ((Player)e.getWhoClicked()).teleport(new Location(Bukkit.getServer().getWorld("parkour"), -175, 5, -305));
    121. }
    122. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("HungerGames")){
    123. e.setCancelled(true);
    124. ((Player)e.getWhoClicked()).teleport(new Location(Bukkit.getServer().getWorld("HungerGames"), 1144, 4, -1637));
    125.  
    126. }
    127. }
    128.  
    129. }
    130.  



    MY MAIN CLASS
    Code:java
    1. public class MenuInv extends JavaPlugin implements Listener {
    2.  
    3. private Menu menu;
    4.  
    5. public void onEnable(){
    6. menu = new Menu(this);
    7. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    8. }
    9.  
    10. @EventHandler
    11. public void onPlayerInteract(PlayerInteractEvent e){
    12. if(!(e.getMaterial() == Material.COMPASS)) return;
    13. menu.show(e.getPlayer());
    14. }
    15.  
    16. }
    17.  
     
  2. Offline

    xTrollxDudex

    hi_guy_5
    We don't code for you, whether or not you are new (which you aren't, you have 75 posts). A legitimate coder should learn how to turn words into code and vice versa, and if you don't, go back to the basics.

    You need to store your plugin parameter in a field and access it using that field instead of using "this", your listener doesn't extend JavaPlugin.
     
    Jake6177 likes this.
  3. Offline

    hi_guy_5

    shouldn't i be able to use p because i did that in line 32?
    but it dosnt work
     
  4. Offline

    xTrollxDudex

    hi_guy_5
    No, that's out of scope. As I said before, you need to store it in a field.
     
  5. Offline

    hi_guy_5

    ...is there somewhere i can read about how to do that? (sorry) :D



    @xTrollxDudex
    Thank you :) i figured it out... it turned out to be simple and i punched myself in the face when i saw what i was missing! but the player.playEffect is crossed out and it suggest i add suppress warnings to run().... should i?

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

    Drew1080

    The playerEffect method is crossed out because if is deprecated.
     
  7. Offline

    hi_guy_5

    how would i go about fixing that
     
  8. Offline

    Wizehh

    Code:java
    1. // Main.getInstance()
    2. static Main instance = new Main();
    3.  
    4. public static Main getInstance() {
    5. return instance;
    6. }
     
  9. Offline

    hi_guy_5

    did i do this correctly? still talking about the plugin part on line 70, because it showed up as an error....
    Code:java
    1. ublic class Menu implements Listener{
    2.  
    3. private Inventory inv;
    4. private ItemStack f, sj, st, ma, pa, hg;
    5.  
    6. public Menu(Plugin p) {
    7. inv = Bukkit.getServer().createInventory(null, 9,ChatColor.DARK_RED + "Master" +ChatColor.GREEN +"Kraft"+ChatColor.BLUE +" DonatorMenu" );
    8.  
    9.  
    10. f = createItem(DyeColor.BLUE, ChatColor.BLUE + "Launch Firework");
    11. sj = createItem(DyeColor.RED, ChatColor.RED + "SuperJump");
    12. st = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "SmokeTrail");
    13. ma = createItem(DyeColor.ORANGE, ChatColor.GOLD + "MobArena");
    14. pa = createItem(DyeColor.PURPLE, ChatColor.DARK_PURPLE + "Parkour");
    15. hg = createItem(DyeColor.GREEN, ChatColor.GREEN + "HungerGames");
    16. inv.setItem(2, f);
    17. inv.setItem(3, sj);
    18. inv.setItem(4, st);
    19. inv.setItem(7, ma);
    20. inv.setItem(8, pa);
    21. inv.setItem(1, hg);
    22. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    23. }
    24.  
    25.  
    26. private ItemStack createItem(DyeColor dc, String name){
    27. ItemStack i = new Wool(dc).toItemStack(1);
    28. ItemMeta im = i.getItemMeta();
    29. im.setDisplayName(name);
    30. im.setLore(Arrays.asList( ChatColor.AQUA + "Click To Do This"));
    31. i.setItemMeta(im);
    32. return i;
    33. }
    34.  
    35. public void show(Player p) {
    36. p.openInventory(inv);
    37.  
    38.  
    39. }
    40.  
    41.  
    42. @EventHandler
    43. public void onInventoryClick(InventoryClickEvent e){
    44. public Plugin (Plugin p) {
    45. final Player player = (Player) e.getWhoClicked();
    46.  
    47. if (!(e.getInventory().getName().equalsIgnoreCase(inv.getName()))) return;
    48. if(e.getCurrentItem() == null) return;
    49.  
    50.  
    51. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Launch Firework")){//FIREWORK!
    52. e.setCancelled(true);
    53. player.closeInventory();
    54. player.sendMessage(ChatColor.AQUA +"[!Attention!]This Plugin Has A Spam Detection! Remember Not To Spam The Firework");
    55.  
    56.  
    57. Firework f = (Firework) e.getWhoClicked().getWorld().spawn(e.getWhoClicked().getLocation(), Firework.class);
    58. FireworkMeta fm = f.getFireworkMeta();
    59. fm.addEffect(FireworkEffect.builder()
    60. .flicker(false)
    61. .trail(true)
    62. .with(Type.BALL_LARGE)
    63. .withColor(Color.RED)
    64. .withFade(Color.BLUE)
    65. .build());
    66. fm.setPower(1);
    67. f.setFireworkMeta(fm);
    68.  
    69. }
    70.  
    71.  
    72. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("SuperJump")){
    73. e.setCancelled(true);
    74. player.closeInventory();
    75. e.getWhoClicked().addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, 20 ));
    76. }
    77. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("SmokeTrail")){
    78. e.setCancelled(true);
    79. player.closeInventory();
    80. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(p, new Runnable() {
    81. public void run() {
    82. player.playEffect(player.getLocation(), Effect.SMOKE, 1);
    83.  
    84. }
    85. }, 5, 5);
    86.  
    87. }
    88. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("MobArena")){
    89. e.setCancelled(true);
    90. ((Player)e.getWhoClicked()).teleport(new Location(Bukkit.getServer().getWorld("mobarena"), 658, 4, 936));
    91. }
    92. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Parkour")){
    93. e.setCancelled(true);
    94. ((Player)e.getWhoClicked()).teleport(new Location(Bukkit.getServer().getWorld("parkour"), -175, 5, -305));
    95. }
    96. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("HungerGames")){
    97. e.setCancelled(true);
    98. ((Player)e.getWhoClicked()).teleport(new Location(Bukkit.getServer().getWorld("HungerGames"), 1144, 4, -1637));
    99.  
    100. }
    101. }
    102. }
    103. }
    104.  
     
  10. Offline

    Wizehh

    hi_guy_5 Put the code I gave you in your main class; then, change "p" (in your scheduler to "Main.getInstance()".
     
  11. Offline

    xTrollxDudex

    Wizehh
    That's really bad code design, and you did it wrong anyways.

    hi_guy_5
    You can't use deprecated methods. Well, I mean you can use them, but if you have to use a workaround in order to avoid the error. Use playEffect in the World class at the player's location instead.
     
  12. Offline

    Wizehh

    It works for me.
     
  13. Offline

    xTrollxDudex

    Wizehh
    PHP:
    new Main()
    That's what's really scaring me. JavaPlugin should not be instantiated twice.
     
  14. Offline

    Wizehh

    Ah. Do you have a better way to do it?
     
  15. Offline

    xTrollxDudex

    Wizehh
    Assign it to "this" in the onEnable?
     
  16. Offline

    hi_guy_5

    @Wizehh
    @xTrollxDudex

    is there a way that i could get around this? maybe by changing what is in what class or making a new class file?
     
Thread Status:
Not open for further replies.

Share This Page