Solved why am i getting this error?

Discussion in 'Plugin Development' started by lrdemolition, Aug 18, 2014.

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

    lrdemolition

    i have a class to create a world and teleport the players in an array to it:



    Code:
    package me.lrdemolition.UHCT;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.entity.Player;
     
    public class gamestarter {
        public void initialize(){
          Bukkit.getServer().broadcastMessage("gamestart initialized");
         
         
          Bukkit.getServer().createWorld(new WorldCreator("arena").environment(World.Environment.NORMAL));
        this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            World arena = Bukkit.getWorld("arena");
            Location playp = new Location(arena, 50, 70, 50);
             
             
              public void run() {
                  for (Player b : MyListeners.bluetp) {
                      b.teleport(playp);
                      }
                 
                 
              }
              }, 200L);
    }
     
    }
     
    
    but on this line:

    "this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {"

    it says under getServer():"The method getServer() is undefined for the type gamestarter"

    please help, thanks
     
  2. Offline

    Gater12

    lrdemolition likes this.
  3. Offline

    lrdemolition

    Gater12

    i think ive tried that but ill try it again and checkout what happens
     
  4. Offline

    Lactem

    You cannot do this.getServer() if your class does not inherit from JavaPlugin. If this is your main class, then you should add extends JavaPlugin at the top. Otherwise, just use Bukkit.getServer() instead of this.getServer().
     
    lrdemolition likes this.
  5. Offline

    lrdemolition

    ok i used:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {

    but now under :

    scheduleSyncDelayedTask


    it gives an error saying:"The method scheduleSyncDelayedTask(Plugin, Runnable, long) in the type BukkitScheduler is not applicable for the arguments (gamestarter, new Runnable(){}, long)"

    and has 3 quickfixes saying:
    1.cast this to plugin
    2.nvm
    3.nvm

    i think i might have fixed it by pressing cast this to plugin
    Gater12
     
  6. Offline

    Gater12

    lrdemolition
    gamestarter is not a subclass of Plugin so therefore it will throw a ClassCastException during runtime.

    What you need to do is to pass the instance of your plugin's main class to gamestarter to the scheduler parameters.
     
    lrdemolition likes this.
  7. Offline

    Lactem

    Not quite. Casting it will just give you a ClassCastException at runtime. You need to put a plugin parameter there instead of gamestarter. It wants a parameter that extends from JavaPlugin.
     
    lrdemolition likes this.
  8. Offline

    lrdemolition

    ok so i did this:
    Code:
    public class gamestarter implements Plugin {
        public void initialize(){
          Bukkit.getServer().broadcastMessage("gamestart initialized");
         
         
          Bukkit.getServer().createWorld(new WorldCreator("arena").environment(World.Environment.NORMAL));
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            World arena = Bukkit.getWorld("arena");
            Location playp = new Location(arena, 50, 70, 50);
             
             
              public void run() {
                  for (Player b : MyListeners.bluetp) {
                      b.teleport(playp);
                      }
                 
                 
              }
              }, 200L);
    }
     
    }
    
    but now under gamestarter it says "The type gamestarter must implement the inherited abstract method Plugin.isEnabled()"
    and gives the option to make type gamestarter abstract or to add all unimplemented methods

    thanks for the help so far guys

    Lactem Gater12
     
  9. Offline

    Lactem

    Change "implements Plugin" to "extends JavaPlugin."
     
  10. Offline

    lrdemolition

    Code:
    package me.lrdemolition.UHCT;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
     
    public class gamestarter extends Plugin {
        public void initialize(){
          Bukkit.getServer().broadcastMessage("gamestart initialized");
         
         
          Bukkit.getServer().createWorld(new WorldCreator("arena").environment(World.Environment.NORMAL));
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            World arena = Bukkit.getWorld("arena");
            Location playp = new Location(arena, 50, 70, 50);
             
             
              public void run() {
                  for (Player b : MyListeners.bluetp) {
                      b.teleport(playp);
                      }
                 
                 
              }
              }, 200L);
    }
     
    }
     
    


    under "Plugin" on:
    public class gamestarter extends Plugin {

    it says"The type Plugin cannot be the superclass of gamestarter; a superclass must be a class"

    then under

    scheduleSyncDelayedTask

    it says"The method scheduleSyncDelayedTask(Plugin, Runnable, long) in the type BukkitScheduler is not applicable for the arguments (gamestarter, new Runnable(){}, long)"
     
  11. Offline

    Rocoty

    lrdemolition Lactem No! No, no, no, no, no! Do not extend JavaPlugin in any other class than the main class for your plugin! Please, just save yourself the trouble and LEARN Java! Learn how it works. Learn the mechanics about it! You need this knowledge before trying to create Bukkit plugins.
     
  12. Offline

    lrdemolition

    Rocoty

    help but dont say learn java.
     
    ProMCKingz likes this.
  13. Offline

    Lactem

    Rocoty I think we may have said that... "If this is your main class, then you should add extends JavaPlugin at the top. Otherwise..."
     
  14. Offline

    lrdemolition

    Rocoty
    and its not extending java plugin, its extending "plugin" from the main class

    Lactem

    it wants to change gamestarter to an interface?

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

    Lactem

    No. It wants an variable that extends JavaPlugin. Don't you have a main class?
     
  16. Offline

    lrdemolition

    or change extends to implements,
    and both of those suggestions are wrong
     
  17. Offline

    teej107

    Learn how to pass variables through parameters.
     
  18. Offline

    Lactem

    Code:
    private JavaPlugin plugin;
     
    public gamestarter(JavaPlugin plugin) {
      this.plugin = plugin;
    }
    Add the above to your gamestarter class and the following to your main class in onEnable():
    Code:
    gamestarter starter = new gamestarter(this);
     
    lrdemolition likes this.
  19. Offline

    lrdemolition

    Code:
    package me.lrdemolition.UHCT;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class main extends JavaPlugin{
        @SuppressWarnings("unused")
        private static Plugin plugin;
        @Override
        public void onEnable(){
            getLogger().info("+_+UHCT ENABLED+_+");
            plugin = this;//this is where we register events/commands
            registerEvents(this, new MyListeners());
        }
        @Override
        public void onDisable(){
            plugin = null;//stops memory leaking
        }
        @SuppressWarnings("unused")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            //if(cmd.getName().equalsIgnoreCase("") && sender instanceof Player){
                //Player player = (Player) sender;
                //Location loc = new Location(player.getWorld(), 0, 70, 0);
                //player.teleport(loc);
        //    }
            if(cmd.getName().equalsIgnoreCase("lobby") && sender instanceof Player){
                Player player = (Player) sender;
               
               
               
            }
            return false;
        }
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
            Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
            }
       
    }
    
    Code:
    package me.lrdemolition.UHCT;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
     
    public class gamestarter extends Plugin {
        public void initialize(){
          Bukkit.getServer().broadcastMessage("gamestart initialized");
         
         
          Bukkit.getServer().createWorld(new WorldCreator("arena").environment(World.Environment.NORMAL));
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            World arena = Bukkit.getWorld("arena");
            Location playp = new Location(arena, 50, 70, 50);
             
             
              public void run() {
                  for (Player b : MyListeners.bluetp) {
                      b.teleport(playp);
                      }
                 
                 
              }
              }, 200L);
    }
     
    }
     
    


    and i have another class containing listeners, arrays(bluetp is one of them) and i also want that class to be able to call this initialize() method (which i know just realized it needs to be static)

    Lactem

    using these"<>"?

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

    teej107

    It does not need to be static. Learn how to pass variables through parameters.

    EDIT: Parameters are these "()"

    Code:java
    1. public void parametersExample() <--- That is a parameter
    2.  
    3. public void passObject(Object o) <-- Passing an object through parameter
    4.  
    5.  


    You can pass multiple variables through parameters as well.
     
  21. Offline

    lrdemolition

    ok so i need to do something like
    public void initialize(plugin plugin)
    or like
    plugin plugin = main.plugin;
     
  22. Offline

    Lactem

    Remove your extends Plugin in gamestarter. Your onEnable should look like this:
    Code:java
    1. public void onEnable(){
    2. getLogger().info("+_+UHCT ENABLED+_+");
    3. plugin = this;//this is where we register events/commands
    4. registerEvents(this, new MyListeners());
    5. gamestarter starter = new gamestarter(this);
    6. starter.initialize();
    7. }
    That will only work if you add what I posted before to your gamestarter class.
     
  23. Offline

    teej107

    lrdemolition Pass the instance of your object through the parameters like this:

    ....(Object variable)

    Also please read the Java Naming Conventions.
    And don't make a static instance of your plugin.

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

    lrdemolition

    Code:
    private JavaPlugin plugin;
     
    public gamestarter(JavaPlugin plugin) {
      this.plugin = plugin;
        public void initialize(){
    is all underlined in red, does it need to be:
    Code:
    private JavaPlugin plugin;
     
    public class gamestarter(JavaPlugin plugin) {
      this.plugin = plugin;
        public void initialize(){
     
  25. Offline

    Lactem

    It's not public class gamestarter(JavaPlugin plugin). It's just public gamestarter(JavaPlugin plugin) because this is a constructor.
     
  26. Offline

    mythbusterma

    teej107

    There's no inherent problem with making your plugin a singleton, but it should be used sparingly.

    As for lrdemolition you shouldn't implement plugin when, in fact, the class you're creating isn't a Plugin.

    lrdemolition upon further thought, you should really read a Java tutorial before you try and write Bukkit plugins, you shouldn't be doing both at once, or not even bothering with Java.
     
  27. Offline

    Rocoty

  28. Offline

    lrdemolition

    ok so i have to change this:
    Code:
    public class gamestarter{
        public void initialize(Plugin plugin){
          Bukkit.getServer().broadcastMessage("gamestart initialized");
         
         
          Bukkit.getServer().createWorld(new WorldCreator("arena").environment(World.Environment.NORMAL));
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            World arena = Bukkit.getWorld("arena");
            Location playp = new Location(arena, 50, 70, 50);
             
             
              public void run() {
                  for (Player b : MyListeners.bluetp) {
                      b.teleport(playp);
                      }
                 
                 
              }
              }, 200L);
    }
     
    }
    to this:
    Code:
    private JavaPlugin plugin;
     
    public gamestarter(JavaPlugin plugin) {
      this.plugin = plugin;
        public void initialize(){
    public class gamestarter{
        public void initialize(Plugin plugin){
          Bukkit.getServer().broadcastMessage("gamestart initialized");
         
         
          Bukkit.getServer().createWorld(new WorldCreator("arena").environment(World.Environment.NORMAL));
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            World arena = Bukkit.getWorld("arena");
            Location playp = new Location(arena, 50, 70, 50);
             
             
              public void run() {
                  for (Player b : MyListeners.bluetp) {
                      b.teleport(playp);
                      }
                 
                 
              }
              }, 200L);
    }
     
    }
     
    
    im 13 and im trying to do that

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

    Necrodoom

    mythbusterma, lrdemolition and Rocoty like this.
  30. Offline

    teej107

    There isn't when you nullify the plugin at the disabling of the plugin which the OP did. I just don't like it since I think of it as bad OOP practice.
     
Thread Status:
Not open for further replies.

Share This Page