Start a method from main class to another class

Discussion in 'Plugin Development' started by FuZioN720, Jul 25, 2013.

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

    FuZioN720

    Hello, i'm trying to start a method from the Main class but it's giving me an error. It saying there is an error when i try to call the method onEnable.

    Code:java
    1. @Override
    2. public void onEnable()
    3. {
    4. this.log = getLogger();
    5.  
    6.  
    7. File f = new File(getDataFolder(), "config.yml");
    8. if(!f.exists()){
    9. this.log.info("No configuration file found, generating a new one");
    10. saveDefaultConfig();
    11. this.log.info("Config file saved.");
    12. }
    13. loadData();
    14.  
    15. PluginManager pm = getServer().getPluginManager();
    16. pm.registerEvents(new MOTDmanager(), this);
    17. pm.registerEvents(new CountdownManager(), this);
    18. pm.registerEvents(new Kits(), this);
    19.  
    20. log.info("Survivor was enabled successfully");
    21.  
    22. cm.startCountdown();
    23. }


    The error is cm.startCountdown

    my CM/CountdownManager Class:
    Code:java
    1.  
    2. package plugins.xxfuzion360xx.survivor;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Sound;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.event.player.PlayerQuitEvent;
    12. import org.bukkit.plugin.Plugin;
    13.  
    14. public class CountdownManager implements Listener{
    15.  
    16. //Variables
    17. public static String prefix = ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "Survivor" + ChatColor.DARK_GRAY + "]" + ChatColor.RESET;
    18. static int Count = 900;
    19. public boolean sstart = false;
    20. static int playerCount = 0;
    21.  
    22. //MOTD States
    23. public static final String IN_GAME = prefix + ChatColor.GREEN + " Game Is In GAME";
    24. public static final String IN_LOBBY = prefix + ChatColor.YELLOW + " Game Is In LOBBY";
    25. public static final String POST_GAME = prefix + ChatColor.RED + " Game ENDING";
    26. public static final String GAME_RESETING = prefix + ChatColor.RED + " Game Is RESETING";
    27. public static final String PRE_GAME = prefix + ChatColor.AQUA + " Game Is STARTING";
    28.  
    29. //Class References
    30. public MOTDmanager mm;
    31. public Survivor plugin;
    32. public PlayerConfig pc;
    33. public CommandManager command;
    34. public Kits kits;
    35.  
    36. //Countdown Timer
    37. @SuppressWarnings("deprecation")
    38. public void countDown() {
    39. if (!this.sstart) {
    40. this.sstart = true;
    41. Bukkit.getScheduler().scheduleAsyncRepeatingTask((Plugin) this, new Runnable(){
    42. @Override
    43. public void run(){
    44. final int minutes = Count / 60;
    45. if (Count % 60 == 0 && Count > 60){
    46. Bukkit.broadcastMessage(prefix + ChatColor.GREEN + " THE GAME STARTS IN " + ChatColor.GOLD + ChatColor.BOLD + minutes + " MINUTES!");
    47. for (Player onlineplayer : Bukkit.getServer().getOnlinePlayers()) {
    48. onlineplayer.playSound(onlineplayer.getLocation(), Sound.ITEM_BREAK, 1, 1);
    49. }
    50. mm.setGameState(IN_LOBBY);
    51. }
    52. else if (Count == 60){
    53. Bukkit.broadcastMessage(prefix + ChatColor.GREEN + " THE GAME STARTS IN " + ChatColor.GOLD + ChatColor.BOLD + minutes + " MINUTE!");
    54. for (Player onlineplayer : Bukkit.getServer().getOnlinePlayers()) {
    55. onlineplayer.playSound(onlineplayer.getLocation(), Sound.ITEM_BREAK, 1, 1);
    56. }
    57. mm.setGameState(IN_LOBBY);
    58. }
    59. else if (Count == 30){
    60. Bukkit.broadcastMessage(prefix + ChatColor.GREEN + " THE GAME STARTS IN " + ChatColor.GOLD + ChatColor.BOLD + Count + " SECONDS!");
    61. for (Player onlineplayer : Bukkit.getServer().getOnlinePlayers()) {
    62. onlineplayer.playSound(onlineplayer.getLocation(), Sound.ITEM_BREAK, 1, 1);
    63. }
    64. mm.setGameState(IN_LOBBY);
    65. }
    66. else if (Count == 15){
    67. Bukkit.broadcastMessage(prefix + ChatColor.GREEN + " THE GAME STARTS IN " + ChatColor.GOLD + ChatColor.BOLD + Count + " SECONDS!");
    68. for (Player onlineplayer : Bukkit.getServer().getOnlinePlayers()) {
    69. onlineplayer.playSound(onlineplayer.getLocation(), Sound.ITEM_BREAK, 1, 1);
    70. }
    71. mm.setGameState(IN_LOBBY);
    72. }
    73. else if (Count < 11 && Count > 0){
    74. Bukkit.broadcastMessage(prefix + ChatColor.GREEN + " THE GAME STARTS IN " + ChatColor.GOLD + ChatColor.BOLD + Count + " SECONDS!");
    75. for (Player onlineplayer : Bukkit.getServer().getOnlinePlayers()) {
    76. onlineplayer.playSound(onlineplayer.getLocation(), Sound.ITEM_BREAK, 1, 1);
    77. }
    78. mm.setGameState(PRE_GAME);
    79. }
    80. else if (Count == 0){
    81. if (playerCount >= 1){
    82. mm.setGameState(IN_GAME);
    83. Bukkit.broadcastMessage(prefix + ChatColor.GREEN + " THE GAME IS STARTING! GOOD LUCK");
    84. } else {
    85. Bukkit.broadcastMessage(prefix + ChatColor.RED + " Not Enough Players On To Start The Game! Game Restarting");
    86. Count = 900;
    87. countDown();
    88. }
    89. }
    90. Count--;
    91. }
    92. },0L, 20L);
    93. }
    94. }
    95.  
    96. //Check if there is someone online
    97. @EventHandler
    98. public void onPlayerJoin(PlayerJoinEvent event) {
    99. playerCount ++;
    100. if (playerCount >= 4){
    101. Count = 65;
    102. }
    103. }
    104.  
    105. @EventHandler
    106. public void onPlayerQuit(PlayerQuitEvent event) {
    107. playerCount --;
    108. if (playerCount <=0){
    109. Count = 900;
    110. }
    111. }
    112.  
    113. public void startCountdown() {
    114. this.sstart = false;
    115. countDown();
    116. }
    117.  
    118. public void stopCountdown() {
    119. this.sstart = true;
    120. countDown();
    121. }
    122. }
    123.  


    i don't know why i have an error please help, thanks
     
  2. Offline

    xTrollxDudex

    FuZioN720
    (Plugin) this doesn't cut it, you need to pass an instance of the main class to the listener.
     
  3. Offline

    FuZioN720

    xTrollxDudex

    Code:java
    1.  
    2. public void Survivor(Survivor plugin) {
    3. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    4. }
    5.  


    i add this to all the listener classes but that didn't work
     
  4. Offline

    xTrollxDudex

    FuZioN720
    You create a constructor with a Survivor plugin parameter and pass the instance of Survivor when registering the listener, then save it as a field in your Listener.
     
  5. Offline

    FuZioN720

    xTrollxDudex
    Ok so now i have this in my CountDown class
    Code:java
    1.  
    2. public Survivor plugin = new Survivor();
    3. public MOTDmanager mm = new MOTDmanager(plugin);
    4. public PlayerConfig pc = new PlayerConfig(plugin);
    5. public CommandManager command = new CommandManager();
    6. public Kits kits = new Kits(plugin);
    7.  
    8. public CountdownManager(Survivor plugin){
    9. this.plugin = plugin;
    10. }
    11.  


    Then in my Main Class
    Code:java
    1.  
    2. public CountdownManager cm = new CountdownManager(this);
    3. public MOTDmanager mm = new MOTDmanager(this);
    4. public PlayerConfig pc = new PlayerConfig(this);
    5. public CommandManager command = new CommandManager();
    6. public Kits kits = new Kits(this);
    7.  
    8. @Override
    9. public void onEnable()
    10. {
    11. this.log = getLogger();
    12.  
    13.  
    14. File f = new File(getDataFolder(), "config.yml");
    15. if(!f.exists()){
    16. this.log.info("No configuration file found, generating a new one");
    17. saveDefaultConfig();
    18. this.log.info("Config file saved.");
    19. }
    20. loadData();
    21.  
    22. PluginManager pm = getServer().getPluginManager();
    23. pm.registerEvents(new MOTDmanager(this), this);
    24. pm.registerEvents(new CountdownManager(this), this);
    25. pm.registerEvents(new Kits(this), this);
    26.  
    27. log.info("Survivor was enabled successfully");
    28.  
    29. cm.startCountdown();
    30. }
    31.  


    but this is giving me and spammed error: which is saying there is an error on line 17 which is -
    public CountdownManager cm = new CountdownManager(this);
     
  6. Offline

    hexagon

    Buut you never said what error it is, post it if you want to get precise help.
     
  7. Offline

    xTrollxDudex

  8. Offline

    FuZioN720

Thread Status:
Not open for further replies.

Share This Page