@Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (!sender.hasPermission(GoldRushPerms.SCHEDULE)) { deny(sender); return true; } int taskID = Departure.getTaskID(); BukkitScheduler s = Bukkit.getScheduler(); if (args[0].equalsIgnoreCase("Start") && taskID == -1) { sender.sendMessage("Starting Train Cycle..."); Departure.setTaskID( Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Departure(plugin), 100, 400)); return true; } else if (taskID != 0) { sender.sendMessage("Train Cycle already started!"); return true; } else if (args[0].equalsIgnoreCase("Stop") && (s.isCurrentlyRunning(taskID) || s.isQueued(taskID))) { sender.sendMessage("Stopping Train Cycle..."); Bukkit.getScheduler().cancelTask(taskID); Departure.resetTaskID(); return true; } else if (taskID == -1) { sender.sendMessage("Train Cycle is not running."); return true; } return false; }
public void run() { try { if (!lock.compareAndSet(false, true)) return; BukkitScheduler bs = plugin.getServer().getScheduler(); if (bs.isQueued(AutoUpdate.this.pid) || bs.isCurrentlyRunning(AutoUpdate.this.pid)) bs.cancelTask(AutoUpdate.this.pid); if (restart) AutoUpdate.this.pid = bs.scheduleAsyncRepeatingTask(plugin, AutoUpdate.this, 5L, delay); else AutoUpdate.this.pid = -1; lock.set(false); bs.cancelTask(pid); } catch (Throwable t) { printStackTraceSync(t, false); } }
/** Handle the command */ @Override public void handle(final CallInfo call) { if (call.getArgs().length <= 1) { call.getSender() .sendMessage(Prism.messenger.playerError("Invalid command. Use /pr ? for help")); return; } boolean recorderActive = false; if (plugin.recordingTask != null) { final int taskId = plugin.recordingTask.getTaskId(); final BukkitScheduler scheduler = Bukkit.getScheduler(); if (scheduler.isCurrentlyRunning(taskId) || scheduler.isQueued(taskId)) { recorderActive = true; } } // Allow for canceling recorders if (call.getArg(1).equals("cancel")) { if (recorderActive) { plugin.recordingTask.cancel(); plugin.recordingTask = null; call.getSender() .sendMessage(Prism.messenger.playerMsg("Current recording task has been canceled.")); call.getSender() .sendMessage( Prism.messenger.playerError( "WARNING: Actions will collect until queue until recorder restarted manually.")); } else { call.getSender() .sendMessage(Prism.messenger.playerError("No recording task is currently running.")); } return; } // Allow for force-restarting recorders if (call.getArg(1).equals("start")) { if (recorderActive) { call.getSender() .sendMessage( Prism.messenger.playerError( "Recording tasks are currently running. Cannot start.")); } else { // Run db tests... call.getSender() .sendMessage(Prism.messenger.playerMsg("Validating database connections...")); // Attempt to get db Connection conn = null; try { conn = Prism.dbc(); if (conn == null || conn.isClosed()) { call.getSender() .sendMessage( Prism.messenger.playerError( "Valid database connection could not be found. Check the db/console and try again.")); return; } call.getSender() .sendMessage(Prism.messenger.playerSuccess("Valid connection found. Yay!")); call.getSender() .sendMessage(Prism.messenger.playerMsg("Restarting recordering tasks...")); plugin.actionRecorderTask(); } catch (final Exception e) { e.printStackTrace(); } finally { if (conn != null) try { conn.close(); } catch (final Exception ignored) { } ; } } return; } }