Files
src
build.gradle
Code
4 114
src/main/java/com/example/shardsscheduler/ShardsScheduler.java
package com.example.shardsscheduler;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.util.List;
public class ShardsScheduler extends JavaPlugin {
private BukkitTask task;
@Override
public void onEnable() {
saveDefaultConfig();
startScheduler();
getLogger().info("ShardsScheduler has been enabled!");
}
@Override
public void onDisable() {
if (task != null && !task.isCancelled()) {
task.cancel();
}
getLogger().info("ShardsScheduler has been disabled!");
}
private void startScheduler() {
FileConfiguration config = getConfig();
long intervalSeconds = config.getLong("scheduler.interval", 60);
long intervalTicks = intervalSeconds * 20L;
task = Bukkit.getScheduler().runTaskTimer(this, this::executeCommands, intervalTicks, intervalTicks);
}
private void executeCommands() {
FileConfiguration config = getConfig();
int amount = config.getInt("scheduler.amount", 10);
List<String> commands = config.getStringList("scheduler.commands");
if (commands.isEmpty()) return;
for (String cmd : commands) {
// Pre-process global placeholders
String processedCmd = cmd.replace("%amount%", String.valueOf(amount));
if (processedCmd.contains("%player%")) {
// Execute for each player
for (Player player : Bukkit.getOnlinePlayers()) {
String playerCmd = processedCmd.replace("%player%", player.getName());
dispatchCommand(playerCmd);
}
} else {
// Execute once as console
dispatchCommand(processedCmd);
}
}
}
private void dispatchCommand(String command) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}
}