package net.knarcraft.playerpayouts; import com.earth2me.essentials.IEssentials; import net.knarcraft.playerpayouts.command.ReloadCommand; import net.knarcraft.playerpayouts.command.SetGroupPaymentCommand; import net.knarcraft.playerpayouts.command.SetGroupPaymentTabCompleter; import net.knarcraft.playerpayouts.command.SetPlayerPaymentCommand; import net.knarcraft.playerpayouts.command.SetPlayerPaymentTabCompleter; import net.knarcraft.playerpayouts.config.Configuration; import net.knarcraft.playerpayouts.listener.PlayerJoinListener; import net.knarcraft.playerpayouts.manager.EconomyManager; import net.knarcraft.playerpayouts.manager.PermissionManager; import net.knarcraft.playerpayouts.manager.PlayerTracker; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.Statistic; import org.bukkit.command.CommandExecutor; import org.bukkit.command.PluginCommand; import org.bukkit.command.TabCompleter; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.logging.Level; /** * Time is money's main class */ @SuppressWarnings("unused") public final class PlayerPayouts extends JavaPlugin { private Configuration configuration; private IEssentials essentials = null; private static PlayerPayouts playerPayouts; @Override public void onEnable() { playerPayouts = this; this.saveDefaultConfig(); this.reloadConfig(); this.saveConfig(); FileConfiguration fileConfiguration = this.getConfig(); fileConfiguration.options().copyDefaults(true); this.saveConfig(); this.configuration = new Configuration(fileConfiguration); // Plugin startup logic if (Bukkit.getPluginManager().getPlugin("Vault") != null) { if (!setupVault()) { return; } } else { this.getLogger().log(Level.SEVERE, "Could not find Vault. Plugin disabled."); this.setEnabled(false); return; } if (Bukkit.getPluginManager().isPluginEnabled("Essentials")) { essentials = (com.earth2me.essentials.IEssentials) Bukkit.getServer().getPluginManager().getPlugin("Essentials"); } else { this.getLogger().log(Level.WARNING, "Could not find Essentials. AFK tracking is disabled."); } ReloadCommand reloadCommand = new ReloadCommand(); registerCommand(getCommand("reload"), reloadCommand, reloadCommand); registerCommand(getCommand("setGroupPayout"), new SetGroupPaymentCommand(configuration), new SetGroupPaymentTabCompleter()); registerCommand(getCommand("setPlayerPayout"), new SetPlayerPaymentCommand(configuration), new SetPlayerPaymentTabCompleter()); Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this); Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::payPlayers, 20, 20); } /** * Registers a new command * * @param command

The command to register a command executor for

* @param executor

The executor to register to the command

* @param tabCompleter

The tab completer to register to the command, or null

*/ private void registerCommand(@Nullable PluginCommand command, @NotNull CommandExecutor executor, @Nullable TabCompleter tabCompleter) { if (command != null) { command.setExecutor(executor); if (tabCompleter != null) { command.setTabCompleter(tabCompleter); } } } /** * Gets an instance of this plugin * * @return

An instance of this plugin

*/ public static PlayerPayouts getInstance() { return playerPayouts; } /** * Reloads the configuration from disk */ public static void reload() { playerPayouts.reloadConfig(); playerPayouts.saveConfig(); playerPayouts.configuration = new Configuration(playerPayouts.getConfig()); } /** * Pay all players that have been on the server long enough */ private void payPlayers() { if (!EconomyManager.isInitialized()) { return; } // Deposit money to all players for (Player player : Bukkit.getOnlinePlayers()) { Long lastPaid = PlayerTracker.getPaymentTime(player); if (lastPaid == null) { continue; } if ((System.currentTimeMillis() - lastPaid) / 60000 < configuration.getPaymentDelay()) { continue; } // Don't pay, or just pay a percentage to AFK players double afkPercentage = configuration.getAfkPercentage(); boolean isAFK = essentials != null && essentials.getUser(player).isAfk(); if (isAFK && afkPercentage <= 0) { continue; } double payment = configuration.getBasePay(player) * getPayoutMultiplier(player); if (isAFK) { payment *= afkPercentage; } EconomyManager.deposit(player, payment); if (configuration.displayPaymentMessage()) { player.sendMessage(this.getDescription().getPrefix() + "You got a paycheck of " + EconomyManager.format(payment)); } PlayerTracker.trackPlayer(player); } } /** * Gets a payout multiplier based on how many hours the player has played on the server * * @param player

The player to check

* @return

The player's payout multiplier

*/ private double getPayoutMultiplier(@NotNull Player player) { if (configuration.getHoursUntilBonus() < 0) { return 1; } double playtimeHours = getPlayTimeHours(player); if (playtimeHours < configuration.getHoursUntilBonus()) { return 1; } else { return Math.max((playtimeHours / configuration.getHoursUntilBonus()) * configuration.getBonusMultiplier(), 1); } } /** * Gets the number of hours a player has played * * @param player

The player to check playtime for

* @return

The player's playtime

*/ private double getPlayTimeHours(@NotNull Player player) { return player.getStatistic(Statistic.PLAY_ONE_MINUTE) / 20.0 / 3600.0; } /** * Sets up Vault by getting plugins from their providers */ private boolean setupVault() { ServicesManager servicesManager = this.getServer().getServicesManager(); RegisteredServiceProvider permissionProvider = servicesManager.getRegistration(Permission.class); RegisteredServiceProvider economyProvider = servicesManager.getRegistration(Economy.class); if (permissionProvider != null) { PermissionManager.initialize(permissionProvider.getProvider()); } else { getLogger().log(Level.WARNING, "No Vault permission provider found. Per-group payouts cannot be used!"); } if (economyProvider != null) { EconomyManager.initialize(economyProvider.getProvider()); } else { getLogger().log(Level.SEVERE, "No Vault economy provider found. PlayerPayouts disabled!"); this.onDisable(); return false; } return true; } }