Minecraft plugin to turn Spawners on and off at certain time during the day

minecraft-java-edition

Im a bit of a noob when it comes to making plugins, but i'm trying to make a plugin that will toggle spawners on and off throughout the course of the day. Spawners are a big part of our economy and we want to balance them/get rid of lag.

Any ideas on how to get this working? Here's what I have so far that isn't working:

Event.java:

import org.bukkit.event.entity.SpawnerSpawnEvent;
import org.bukkit.event.Listener;


public class Main{
 public static void main(String args[]) {
   ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(2);    
   stpe.scheduleAtFixedRate(new YourJob(), 0, 5, TimeUnit.HOURS);
 }
}

class YourJob implements Runnable {
    public class Event implements Listener
    {
        @EventHandler
        public void onSpawn(final SpawnerSpawnEvent event) {
            event.setCancelled(true);
            if (event.setCancelled() == true) {
                event.setCancelled(false);
            }
            else {
            event.setCancelled(true);
            }
            }
    }
}

Main.java:

import org.bukkit.plugin.Plugin;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin
{
    public void onEnable() {
        this.getLogger().info("Successfully loaded!");
        this.getServer().getPluginManager().registerEvents((Listener)new Event(), (Plugin)this);
    }

    public void onDisable() {
        this.getLogger().info("Successfully unloaded!");
    }
}

Best Answer

Starting with Main.java. It is a good practice to include the @Override annotation above any methods you are overriding so I suggest you do that for the onEnable() and onDisable() methods. Also, there is no need to cast Event to Listener or this to Plugin as both classes are of the appropriate type.

In Event.java, you don't need the main method because your plugin's entry point is the onEnable() method is Main.java.

I'm not sure what you are trying to do in the event handler but use the event.isCancelled() method to determine if the event is canceled.

Here's how I would do it:

public class SpawnerSwitch extends JavaPlugin implements Listener {

    private Logger logger = Bukkit.getLogger();
    private long allowedTimeMin = 0;
    private long allowedTimeMax = 10000;

    @Override
    public void onEnable() {
        logger.info(getDescription().getName() + " enabled");
        this.getServer().getPluginManager().registerEvents(this, this);
    }

    @Override
    public void onDisable() {
        logger.info(getDescription().getName() + " enabled");
    }

    @EventHandler
    public void onSpawn (SpawnerSpawnEvent event){
        long worldTime = event.getSpawner().getWorld().getTime();
        if (!(worldTime >= allowedTimeMin && worldTime <= allowedTimeMax)){
            event.setCancelled(true);
        }
    }
}

the onSpawn() event handler will allow spawners to spawn entities from /time set 0 to /time set 10000.