A few questions...

Discussion in 'Plugin Development' started by Roritoro, Apr 15, 2015.

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

    Roritoro

    Can you do Bukkit scheduling (scheduleSyncRepeatingTask) inside of classes that are not the main class of your plugin?

    For instance, I am currently coding a plugin and in a class called "Chests," I am trying to setup a repeating task that will spawn a chest every 5 minutes if there isn't a chest at the desired location. I just can't seem to get my repeating task to work at all.
     
  2. Offline

    ColonelHedgehog

    1. Of course.
    2. Can you show us your code?
     
  3. Offline

    FisheyLP

    Yes you can, but you need an instance of your main class (extends JavaPlugin) to get it working.
     
  4. Offline

    Roritoro

    Currently, this is just to test if the block will spawn but it doesn't.

    I added the broadcasting message to see if it even enters the task, and it doesn't even do that. So I have no idea where I'm fucking up.

    Code:
    public class Chests extends JavaPlugin implements Listener {
        public static Main plugin;
    
        public Chests(Main m) {
            plugin = m;
        }
    
        public void onEnable(BlockPlaceEvent e) {
            final Player p = e.getPlayer();
            final World world = p.getWorld();
            Bukkit.getServer().getScheduler()
                    .scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            Bukkit.getServer().broadcastMessage("Testing...");
                           
                            world.getBlockAt(671, 6, -236).setType(Material.CHEST);
                        }
                    }, 1L, 20L);
        }
     
  5. Offline

    FisheyLP

    The method onEnable(BlockPlaceEvent e) never gets called.
    1. Don't name events onEnable
    2. Write @EventHandler over the event
    3. Register the events with Bukkit.getPluginManager().registerEvents(ListenerClass_instance, MainClass_instance);
    4. DO NOT EVER PUT A CONSTRUCTOR IN THE MAIN CLASS. IT WILL CAUSE ERRORS IF YOU CALL THE CONSTRUCTOR
    5. You can only have one class which extends JavaPlugin (the main class)
     
  6. Offline

    Konato_K

    @FisheyLP Actually as far I know, you can have a constructor as long as it contains no arguments, of course, it actually isn't needed for anything in a bukkit plugin.
     
  7. Offline

    Roritoro

    Fishey, I understand what you're saying, but then I can't use schedulers inside of my Chest.class if I don't use the extends JavaPlugin and I know that is breaking my entire plugin because if I put the scheduling code inside of my Main.class and comment out my Chests.class then it works fine.

    But I would rather not have 2,000 lines of code of what I plan on doing inside of my Main.class because that just looks terrible.
     
  8. Offline

    nverdier

    And
    and
    you don't use OOP. Which could complicate things quite a bit. Maybe not in this situation, but using multiple classes (and Objects) is necessary in many situations.
     
  9. Offline

    Roritoro

    So just go ahead and deal with putting it all inside of my Main.class then?
     
  10. Offline

    ColonelHedgehog

    No. ;_;
     
  11. Offline

    Roritoro

    Well, would someone like to help with my issue or keep giving me vague replies?
     
  12. Offline

    timtower Moderator Moderator

  13. Offline

    Roritoro

    I have no idea how to get it to work in my Chests.class, so I've begun working on it within my Main.class, because that's the only new insight to my issue that I have been given. So as of current; I haven nothing new inside of my Chests.class because it wasn't working and I was told it wouldn't.

    But within my Main.class, this is my code that I have that works the way I want it to, I would just rather it not be in my Main.class for obvious reasons.

    Code:
            Bukkit.getServer().getScheduler()
                        .scheduleSyncRepeatingTask(this, new Runnable() {
                            @EventHandler
                            public void run() {
                                final World world = Bukkit.getServer().getWorld("default");
                                Bukkit.getServer().broadcastMessage("Testing...");
                            
                            
                                ItemStack npcnull = new ItemStack(Material.IRON_FENCE);
                                ItemStack[] test = (new ItemStack[]{ npcnull });
                                world.getBlockAt(671, 6, -236).setType(Material.CHEST);
                            
                                Block block = world.getBlockAt(671, 6, -236);
                                Chest chest = (Chest) block.getState();
                                Inventory T1Chest = chest.getInventory();
                                T1Chest.addItem(test);
                            }
                        }, 1L, 20L);
     
    Last edited: Apr 15, 2015
  14. Offline

    timtower Moderator Moderator

    @Roritoro Try a constructor for the second class and make sure that it DOESN'T extend JavaPlugin
     
  15. Offline

    Roritoro

    @timtower

    Here is my current Chests.class, of which the .scheduleSyncRepeatingTask is giving me the error because I can't use extends JavaPlugin.


    Code:
    public class Chests implements Listener {
       public static Main plugin;
    
       public Chests(Main m) {
         plugin = m;
       }
    
       public void onEnable() {
         // T1 Chest Test
         Bukkit.getServer().getScheduler()
             .scheduleSyncRepeatingTask(this, new Runnable() {
               @EventHandler
               public void run() {
                 final World world = Bukkit.getServer().getWorld(
                     "default");
                 Bukkit.getServer().broadcastMessage("Testing...");
    
                 ItemStack npcnull = new ItemStack(Material.IRON_FENCE);
                 ItemStack[] test = (new ItemStack[] { npcnull });
                 world.getBlockAt(671, 6, -236).setType(Material.CHEST);
    
                 Block block = world.getBlockAt(671, 6, -236);
                 Chest chest = (Chest) block.getState();
                 Inventory T1Chest = chest.getInventory();
                 T1Chest.addItem(test);
               }
             }, 1L, 20L);
       }
    }
    
    How would I go about getting the "extends JavaPlugin" to work within my Chests.class, without actually putting it within the class?
     
  16. Offline

    timtower Moderator Moderator

    @Roritoro You don't need to extend it twice.
    1. Please remove the static part from the plugin field.
    2. Where you schedule the task it asks for a plugin, you have a plugin field, try it.
     
  17. Offline

    Roritoro

    @timtower

    If you're talking about going about it like this, it still doesn't work.

    The error is gone from the code, but it just doesn't run the broadcasted message or anything.

    Code:
    public class Chests implements Listener {
       public Main plugin;
    
       public Chests(Main m) {
         plugin = m;
       }
    
       public void onEnable() {
         // T1 Chest Test
         Bukkit.getServer().getScheduler()
             .scheduleSyncRepeatingTask(plugin, new Runnable() {
               @EventHandler
               public void run() {
                 final World world = Bukkit.getServer().getWorld(
                     "default");
                 Bukkit.getServer().broadcastMessage("Testing...");
    
                 ItemStack npcnull = new ItemStack(Material.IRON_FENCE);
                 ItemStack[] test = (new ItemStack[] { npcnull });
                 world.getBlockAt(671, 6, -236).setType(Material.CHEST);
    
                 Block block = world.getBlockAt(671, 6, -236);
                 Chest chest = (Chest) block.getState();
                 Inventory T1Chest = chest.getInventory();
                 T1Chest.addItem(test);
               }
             }, 1L, 20L);
       }
    }
    
     
  18. Offline

    timtower Moderator Moderator

    @Roritoro Why do you have an onEnable in a different class? It won't be called ever.
    Nor will the EventHandler do anything on a run method.
     
  19. Offline

    Roritoro

    @timtower

    Alright, I see what you're saying I had the onEnable because I've never used scheduling and the scheduling tutorial, has it. First time using scheduling so that's why I need help. I've been stuck on this problem for like 3 days...

    But instead of using the onEnable, how would I get the method to be activated to even start the scheduled task?

    I changed the method name to
    Code:
    public void chestSpawning() {
    But what should I use to start the scheduling upon server startup?
     
  20. Offline

    timtower Moderator Moderator

    @Roritoro You make an instance of Chests, and you call that method.
     
  21. Offline

    mythbusterma

    @Roritoro

    Invoke a method that schedules the task, alternatively if the class itself extends BukkitRunnable then just schedule it in onEnable().

    That being said, you really need to learn Java if you're going to keep trying to write Bukkit plugins. It is not something you can learn at the same time, and it's not something you can just "skip over."

    Here: The Java™ Tutorials
     
  22. Offline

    Roritoro

    @mythbusterma

    I'm doing this as a side project for fun to make a server with friends, and I've been doing fine so far. I asked for a little help on code, not on how I should practice Java.
     
  23. Offline

    nverdier

    @Roritoro Well Bukkit isn't 'code'. Bukkit is a tool you use to make Bukkit plugins. But you are writing in Java. If you don't want to learn Java, then you can't write Bukkit plugins. It's like trying to write a book in a language you don't know.
     
  24. Offline

    Roritoro

    @nverdier

    I'm learning Java but I'm doing Bukkit as I learn it just for fun to make a server. I'm in college for it so I understand basic Java as of current.

    I'm not sure why this thread has turned into a "let me tell you how to run your life" instead of just helping me with a simple code issue?
     
  25. Offline

    mythbusterma

    @Roritoro

    Because these forums are not for help with Java, if you need help with Java (which is indeed the case), head over to the Java ranch, stack overflow, or other related sites.

    If you have questions about the Bukkit API or how it works, ask them here. Your question is one of simple OOP, methods and class layouts, which in no way relate to the Bukkit API, save the Bukkit API being written in Java.
     
Thread Status:
Not open for further replies.

Share This Page