@Override public void run() { QueuedPlayer qp = queuedPlayers.poll(); if (qp != null) { platform.executeAsync(new PlayerCommandExecutor(qp, platform)); } }
public void run(boolean scheduleAgain) { if (platform.getApiClient() == null) { return; // no API client platform.log(Level.INFO, "Already fetching due player information!"); return; platform.log(Level.INFO, "Fetching all due players..."); do { try { information = platform.getApiClient().retrieveDueQueue(); if(information == null){ return; platform.log(Level.SEVERE, "Could not fetch due players queue", e); return; platform.log(Level.INFO, String.format("Fetched due players (%d found).", allDue.size())); platform.log(Level.INFO, "Executing commands that can be completed now..."); platform.executeAsync(new ImmediateCommandExecutor(platform)); inProgress.set(false); if (scheduleAgain) platform.executeAsyncLater(this, nextCheck, TimeUnit.SECONDS);
String finalCommand = platform.getPlaceholderManager().doReplace(command.getPlayer(), command.getCommand()); platform.log(Level.INFO, String.format("Dispatching command '%s' for player '%s'.", finalCommand, command.getPlayer().getName())); try { platform.dispatchCommand(finalCommand); completedCommandsTask.add(command.getCommand().getId()); } catch (Exception e) { platform.log(Level.SEVERE, String.format("Could not dispatch command '%s' for player '%s'. " + "This is typically a plugin error, not an issue with BuycraftX.", finalCommand, command.getPlayer().getName()), e); platform.log(Level.SEVERE, "Command execution took " + timeMs.toPlainString() + "ms to complete. " + "This likely indicates an issue with one of your server's plugins, which can cause lag."); } else { platform.log(Level.SEVERE, "Command execution took " + timeMs.toPlainString() + "ms to complete. " + "This likely indicates an issue with one of your server's plugins, which will slow command execution.");
@Override public void run() { if (platform.getApiClient() == null) { // no API client return; } try { listing.set(platform.getApiClient().retrieveListing()); } catch (IOException | ApiException e) { platform.log(Level.SEVERE, "Error whilst retrieving listing", e); return; } lastUpdate.set(new Date()); if (updateTask != null) { updateTask.run(); } }
private void processOnlinePlayers() { // Check for online players and execute their commands. List<QueuedPlayer> processNow = new ArrayList<>(); lock.lock(); try { for (Iterator<QueuedPlayer> it = due.values().iterator(); it.hasNext(); ) { QueuedPlayer qp = it.next(); if (platform.isPlayerOnline(qp)) { if (processNow.size() < MAXIMUM_ONLINE_PLAYERS_TO_EXECUTE) { processNow.add(qp); it.remove(); } } } } finally { lock.unlock(); } if (!processNow.isEmpty()) { if (verbose) { platform.log(Level.INFO, String.format("Executing commands for %d online players...", processNow.size())); } for (int i = 0; i < processNow.size(); i++) { QueuedPlayer qp = processNow.get(i); platform.executeAsyncLater(new PlayerCommandExecutor(qp, platform), DELAY_BETWEEN_PLAYERS * (i + 1), TimeUnit.MILLISECONDS); } } }
@Override public void run() { if (platform.getApiClient() == null) { return; // no API client } QueueInformation information; try { // Retrieve offline command queue. information = platform.getApiClient().retrieveOfflineQueue(); } catch (IOException | ApiException e) { platform.log(Level.SEVERE, "Could not fetch command queue", e); return; } // Queue commands for later. for (QueuedCommand command : information.getCommands()) { platform.getExecutor().queue(new ToRunQueuedCommand(command.getPlayer(), command, false)); } } }
@EventHandler public void onPostLogin(final PlayerJoinEvent event) { if (event.getPlayer().hasPermission("buycraft.admin") && !upToDate) { plugin.getPlatform().executeAsyncLater(new Runnable() { @Override public void run() { event.getPlayer().sendMessage(TextFormat.YELLOW + plugin.getI18n().get("update_available", lastKnownVersion.getVersion())); } }, 3, TimeUnit.SECONDS); } } }
writer.println("Server version: " + platform.getPlatformInformation().getType() + " / " + platform.getPlatformInformation().getVersion()); writer.println("Server IP and port: " + ip + " / " + port); writer.println("Online mode: " + serverOnlineMode); writer.println("Plugin version: " + platform.getPluginVersion()); writer.println("Platform: " + platform.getPlatformInformation().getType() + " / " + platform.getPlatformInformation().getVersion()); writer.println(); writer.println("Connected to Buycraft? " + (platform.getApiClient() != null)); ServerInformation information = platform.getServerInformation(); writer.println("Web store information found? " + (information != null)); if (information != null) {
public void initializeTasks() { try { serverInformation = platform.getApiClient().getServerInformation(); } catch (IOException | ApiException e) { throw new RuntimeException("Can't fetch account information", e); } executorService.schedule(playerFetcher = new DuePlayerFetcher(platform, verbose), 1, TimeUnit.SECONDS); }
@EventHandler public void onPlayerJoin(final PlayerJoinEvent event) { if (event.getPlayer().hasPermission("buycraft.admin") && !upToDate) { plugin.getPlatform().executeAsyncLater(new Runnable() { @Override public void run() { event.getPlayer().sendMessage(ChatColor.YELLOW + plugin.getI18n().get("update_available", lastKnownVersion.getVersion())); } }, 3, TimeUnit.SECONDS); } } }
@Override public void run() { QueueInformation information; try { information = platform.getApiClient().getPlayerQueue(player.getId()); } catch (IOException | ApiException e) { // TODO: Implement retry logic. platform.log(Level.SEVERE, "Could not fetch command queue for player", e); return; } platform.log(Level.INFO, String.format("Fetched %d commands for player '%s'.", information.getCommands().size(), player.getName())); // Queue commands for later. for (QueuedCommand command : information.getCommands()) { platform.getExecutor().queue(new ToRunQueuedCommand(player, command, true)); } } }
public void flush() { if (!completed.isEmpty()) { for (List<Integer> list : Lists.partition(new ArrayList<>(completed), MAXIMUM_COMMANDS_TO_POST)) { try { platform.getApiClient().deleteCommand(list); } catch (IOException | ApiException e) { platform.log(Level.SEVERE, "Unable to mark commands as completed", e); break; } } } } }
private void createCoupon(final CommandSender sender, String[] args) { String[] stripped = Arrays.copyOfRange(args, 1, args.length); final Coupon coupon; try { coupon = CouponUtil.parseArguments(stripped); } catch (Exception e) { sender.sendMessage(ChatColor.RED + plugin.getI18n().get("coupon_creation_arg_parse_failure", e.getMessage())); return; } plugin.getPlatform().executeAsync(new Runnable() { @Override public void run() { try { plugin.getApiClient().createCoupon(coupon); sender.sendMessage(ChatColor.GREEN + plugin.getI18n().get("coupon_creation_success", coupon.getCode())); } catch (ApiException | IOException e) { sender.sendMessage(ChatColor.RED + plugin.getI18n().get("generic_api_operation_error")); } } }); }
@EventHandler public void onPostLogin(final PostLoginEvent event) { if (event.getPlayer().hasPermission("buycraft.admin") && !upToDate) { plugin.getPlatform().executeAsyncLater(new Runnable() { @Override public void run() { event.getPlayer().sendMessage(ChatColor.YELLOW + plugin.getI18n().get("update_available", lastKnownVersion.getVersion())); } }, 3, TimeUnit.SECONDS); } } }
@Override public void run() { List<Integer> commandsToPost = new ArrayList<>(); while (commandsToPost.size() < MAXIMUM_COMMANDS_TO_POST) { Integer posted = completed.poll(); if (posted == null) break; commandsToPost.add(posted); } if (!commandsToPost.isEmpty()) { try { platform.getApiClient().deleteCommand(commandsToPost); } catch (IOException | ApiException e) { platform.log(Level.SEVERE, "Unable to mark commands as completed", e); // TODO: Retry? } } }
private void createCoupon(final CommandSender sender, String[] args) { String[] stripped = Arrays.copyOfRange(args, 1, args.length); final Coupon coupon; try { coupon = CouponUtil.parseArguments(stripped); } catch (Exception e) { sender.sendMessage(TextFormat.RED + plugin.getI18n().get("coupon_creation_arg_parse_failure", e.getMessage())); return; } plugin.getPlatform().executeAsync(new Runnable() { @Override public void run() { try { plugin.getApiClient().createCoupon(coupon); sender.sendMessage(TextFormat.GREEN + plugin.getI18n().get("coupon_creation_success", coupon.getCode())); } catch (ApiException | IOException e) { sender.sendMessage(TextFormat.RED + plugin.getI18n().get("generic_api_operation_error")); } } }); }
@Listener public void onPlayerJoinEvent(ClientConnectionEvent.Join event) { if (event.getTargetEntity().hasPermission("buycraft.admin") && !upToDate) { plugin.getPlatform().executeAsyncLater(() -> { try { event.getTargetEntity().sendMessage( Text.builder() .append(Text.of(plugin.getI18n().get("update_available", lastKnownVersion.getVersion()))) .onClick(TextActions.openUrl(new URL("https://server.buycraft.net"))) .color(TextColors.YELLOW) .build()); } catch (MalformedURLException e) { throw new AssertionError(e); // seriously? } }, 3, TimeUnit.SECONDS); } } }
private void createCoupon(final CommandSender sender, String[] args) { String[] stripped = Arrays.copyOfRange(args, 1, args.length); final Coupon coupon; try { coupon = CouponUtil.parseArguments(stripped); } catch (Exception e) { sender.sendMessage(e.getMessage()); sender.sendMessage(ChatColor.RED + plugin.getI18n().get("coupon_creation_arg_parse_failure", e.getMessage())); return; } plugin.getPlatform().executeAsync(new Runnable() { @Override public void run() { try { plugin.getApiClient().createCoupon(coupon); sender.sendMessage(ChatColor.GREEN + plugin.getI18n().get("coupon_creation_success", coupon.getCode())); } catch (ApiException | IOException e) { sender.sendMessage(ChatColor.RED + e.getMessage()); } } }); }
platform.executeAsyncLater(duePlayerFetcher = new DuePlayerFetcher(platform, configuration.isVerbose()), 1, TimeUnit.SECONDS); completedCommandsTask = new PostCompletedCommandsTask(platform); commandExecutor = new QueuedCommandExecutor(platform, completedCommandsTask);
private void deleteCoupon(final CommandSender sender, String[] args) { if (args.length != 2) { sender.sendMessage(ChatColor.RED + plugin.getI18n().get("no_coupon_specified")); return; } final String code = args[1]; plugin.getPlatform().executeAsync(new Runnable() { @Override public void run() { try { plugin.getApiClient().deleteCoupon(code); sender.sendMessage(ChatColor.GREEN + plugin.getI18n().get("coupon_deleted")); } catch (ApiException | IOException e) { sender.sendMessage(ChatColor.RED + e.getMessage()); return; } } }); }