package me.guireward.commands;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.PluginClassLoader;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class Reward implements CommandExecutor {
private final Map<UUID, Long> cooldowns = new HashMap<>();
private final long cooldownTime = 86400000L; // 24 hours in milliseconds
private final File cooldownFile;
private final YamlConfiguration cooldownConfig;
private final YamlConfiguration itemsConfig;
public Reward(File dataFolder) {
cooldownFile = new File(dataFolder, "cooldowns.yml");
cooldownConfig = YamlConfiguration.loadConfiguration(cooldownFile);
File itemsFile = new File(dataFolder, "items.yml");
itemsConfig = YamlConfiguration.loadConfiguration(itemsFile);
// Load saved cooldowns from file
for (String uuidStr : cooldownConfig.getKeys(false)) {
UUID uuid = UUID.fromString(uuidStr);
long cooldownEnd = cooldownConfig.getLong(uuidStr);
if (cooldownEnd > System.currentTimeMillis()) {
cooldowns.put(uuid, cooldownEnd);
}
}
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
Gui main = Gui.gui()
.title(Component.text("§4§n§lDayliReward"))
.rows(3)
.create();
GuiItem take = ItemBuilder.from(Material.GOLD_INGOT).name(Component.text("§3§lReward")).lore(Component.text("Get your Dayli Reward!")).asGuiItem(inventoryClickEvent ->{
if (command.getName().equalsIgnoreCase("reward")) {
Player player = (Player) sender;
UUID uuid = player.getUniqueId();
if (!player.isOp() && cooldowns.containsKey(uuid))
//if (!player.hasPermission("no.cooldown") && cooldowns.containsKey(uuid))
if (cooldowns.containsKey(uuid)) {
long timeRemaining = cooldowns.get(uuid) - System.currentTimeMillis();
if (timeRemaining > 0) {
long hours = (timeRemaining / (1000 * 60 * 60)) % 24;
long minutes = (timeRemaining / (1000 * 60)) % 60;
long seconds = (timeRemaining / 1000) % 60;
player.sendMessage(String.format("You must wait %d hours, %d minutes, and %d seconds before using that command again.", hours, minutes, seconds));
return;
}
}
List<String> rewardItems = itemsConfig.getStringList("reward_items");
if (!rewardItems.isEmpty()) {
int randomIndex = ThreadLocalRandom.current().nextInt(rewardItems.size());
String selectedMaterial = rewardItems.get(randomIndex);
ItemStack selectedActualItem = new ItemStack(Material.valueOf(selectedMaterial));
int amount = ThreadLocalRandom.current().nextInt(1, 11);
selectedActualItem.setAmount(amount);
PlayerInventory inventory = player.getInventory();
int freeSlots = 0;
for (ItemStack item : inventory.getStorageContents()) {
if (item == null || item.getType() == Material.AIR) {
freeSlots++;
} else if (item.getType() == selectedActualItem.getType() && item.getAmount() < item.getMaxStackSize()) {
freeSlots += item.getMaxStackSize() - item.getAmount();
}
}
if (freeSlots < 1) {
player.sendMessage("You don't have enough space in your inventory.");
return;
}
//Adds the selected item
inventory.addItem(selectedActualItem);
player.sendMessage("You received " + amount + " " + selectedMaterial);
cooldowns.put(uuid, System.currentTimeMillis() + cooldownTime);
// Save cooldown data to file
cooldownConfig.set(uuid.toString(), cooldowns.get(uuid));
try {
cooldownConfig.save(cooldownFile);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Handle the case where the items list is empty
System.out.println("The list is empty? Idk why");
}
}
});
main.disableAllInteractions();
main.setItem(2,3,take);
if (sender instanceof Player){
Player player = (Player) sender;
main.open(player);
} else {
sender.sendMessage("§4§nThis command can only be run by a player.");
}
return false;
}
}