Untitled

mail@pastecode.io avatar
unknown
java
a month ago
4.4 kB
0
Indexable
Never
package me.nashplugz.mtnp;

import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class MoneyTime extends JavaPlugin {
    private Economy economy;
    private PlayerPlaytimeManager playtimeManager;
    private Map<String, Rank> ranks = new HashMap<>();

    @Override
    public void onEnable() {
        getLogger().info("MoneyTime plugin enabled!");

        // Initialize Vault and Economy
        if (!setupEconomy()) {
            getLogger().warning("Vault or an economy plugin not found. Disabling plugin.");
            getServer().getPluginManager().disablePlugin(this);
            return;
        }

        // Initialize PlayerPlaytimeManager
        playtimeManager = new PlayerPlaytimeManager(new File(getDataFolder(), "playtimes"), this);

        // Load config
        loadConfig();

        // Schedule a task to reward players based on config
        new BukkitRunnable() {
            @Override
            public void run() {
                for (Player player : getServer().getOnlinePlayers()) {
                    // Load player playtime asynchronously
                    playtimeManager.loadPlaytimeAsync(player);

                    // Get total play time in minutes for player
                    int playTimeMinutes = playtimeManager.getPlaytime(player);

                    // Determine player's rank and reward accordingly
                    Rank playerRank = getPlayerRank(player);
                    if (playerRank != null && playTimeMinutes >= playerRank.interval) {
                        // Reward player with money
                        economy.depositPlayer(player, playerRank.amount);

                        // Send message to player
                        player.sendMessage("You have played for " + playTimeMinutes + " minutes. Here is " + playerRank.amount + " money.");

                        getLogger().info(player.getName() + " rewarded " + playerRank.amount + " for playing " + playerRank.interval + " minutes.");

                        // Reset playtime for the next interval
                        playtimeManager.setPlaytime(player, playTimeMinutes - playerRank.interval);
                    }
                }
            }
        }.runTaskTimer(this, 0, 20 * 60); // Check every minute (20 ticks per second * 60 seconds)
    }

    @Override
    public void onDisable() {
        getLogger().info("MoneyTime plugin disabled!");
    }

    private void loadConfig() {
        saveDefaultConfig();
        FileConfiguration config = getConfig();

        for (String key : config.getConfigurationSection("ranks").getKeys(false)) {
            int interval = config.getInt("ranks." + key + ".interval");
            int amount = config.getInt("ranks." + key + ".amount");
            ranks.put(key, new Rank(interval, amount));
        }
    }

    private boolean setupEconomy() {
        Logger logger = getLogger();
        if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
            logger.severe("Vault plugin not found! Economy features will not be available.");
            return false;
        }

        RegisteredServiceProvider<Economy> rsp = Bukkit.getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
            logger.severe("Economy plugin not found! Economy features will not be available.");
            return false;
        }

        economy = rsp.getProvider();
        logger.info("Economy setup successful.");
        return economy != null;
    }

    private Rank getPlayerRank(Player player) {
        for (Map.Entry<String, Rank> entry : ranks.entrySet()) {
            String permission = "moneytime.rank." + entry.getKey();
            if (player.hasPermission(permission)) {
                return entry.getValue();
            }
        }
        return ranks.get("default"); // Default rank if no specific rank is found
    }

    private static class Rank {
        final int interval;
        final int amount;

        Rank(int interval, int amount) {
            this.interval = interval;
            this.amount = amount;
        }
    }
}
Leave a Comment