Files
src
build.gradle
settings.gradle
Code
5 303
src/main/java/com/kodari/waffleafk/WaffleAFKPlugin.java
package com.kodari.waffleafk;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
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.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
public final class WaffleAFKPlugin extends JavaPlugin implements CommandExecutor, Listener {
private static final long REWARD_INTERVAL_SECONDS = 60;
private static final double WAFFLE_REWARD = 1.0;
private final Map<UUID, AfkSession> sessions = new HashMap<>();
private final Map<UUID, Long> lifetimeWaffles = new HashMap<>();
private Economy economy;
private YamlConfiguration playerData;
private File playerDataFile;
@Override
public void onEnable() {
saveDefaultConfig();
if (!hookEconomy()) {
getLogger().severe("Vault and a Vault-compatible economy provider are required.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
loadPlayerData();
getCommand("afk").setExecutor(this);
getCommand("setafk").setExecutor(this);
Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getScheduler().runTaskTimer(this, this::tickSessions, 20L, 20L);
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new WafflePlaceholders(this).register();
}
}
private boolean hookEconomy() {
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
if (registration == null) {
return false;
}
economy = registration.getProvider();
return economy != null;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("setafk")) {
if (!sender.hasPermission("waffleafk.admin")) {
sender.sendMessage(color("&cYou don't have permission to do that."));
return true;
}
if (!(sender instanceof Player player)) {
sender.sendMessage("Only a player can set the AFK location.");
return true;
}
saveAfkLocation(player.getLocation());
player.sendMessage(color("&aWaffle AFK location set."));
return true;
}
if (!(sender instanceof Player player)) {
sender.sendMessage("Only players can use /afk.");
return true;
}
UUID playerId = player.getUniqueId();
AfkSession current = sessions.get(playerId);
if (current != null) {
sessions.remove(playerId);
if (current.returnLocation() != null && current.returnLocation().getWorld() != null) {
player.teleport(current.returnLocation());
}
player.sendMessage(color("&eYou left the AFK area."));
return true;
}
Location afkLocation = getAfkLocation();
if (afkLocation == null) {
player.sendMessage(color("&cThe AFK location hasn't been set. Ask an admin to use /setafk."));
return true;
}
Location returnLocation = player.getLocation().clone();
if (!player.teleport(afkLocation)) {
player.sendMessage(color("&cTeleporting to the AFK area failed."));
return true;
}
sessions.put(playerId, new AfkSession(returnLocation, 0));
player.sendMessage(color("&aYou are now AFK. Earn 1 waffle every 60 seconds; use /afk to leave."));
return true;
}
private void tickSessions() {
double radius = Math.max(0.0, getConfig().getDouble("radius", 5.0));
double radiusSquared = radius * radius;
Location afkLocation = getAfkLocation();
for (Map.Entry<UUID, AfkSession> entry : new HashMap<>(sessions).entrySet()) {
Player player = Bukkit.getPlayer(entry.getKey());
if (player == null) {
sessions.remove(entry.getKey());
continue;
}
if (afkLocation == null || !player.getWorld().equals(afkLocation.getWorld())
|| player.getLocation().distanceSquared(afkLocation) > radiusSquared) {
sessions.remove(entry.getKey());
player.sendMessage(color("&cYou left the AFK area and stopped earning waffles."));
continue;
}
int elapsed = entry.getValue().elapsedSeconds() + 1;
if (elapsed >= REWARD_INTERVAL_SECONDS) {
EconomyResponse response = economy.depositPlayer(player, WAFFLE_REWARD);
if (response.transactionSuccess()) {
long total = lifetimeWaffles.getOrDefault(player.getUniqueId(), 0L) + 1L;
lifetimeWaffles.put(player.getUniqueId(), total);
playerData.set(player.getUniqueId().toString(), total);
savePlayerData();
player.sendMessage(color("&aYou earned 1 waffle!"));
} else {
getLogger().warning("Could not pay waffle reward to " + player.getName() + ": " + response.errorMessage);
}
elapsed = 0;
}
sessions.put(entry.getKey(), new AfkSession(entry.getValue().returnLocation(), elapsed));
}
}
private Location getAfkLocation() {
String worldName = getConfig().getString("afk-location.world", "");
if (worldName == null || worldName.isBlank()) {
return null;
}
org.bukkit.World world = Bukkit.getWorld(worldName);
if (world == null) {
return null;
}
return new Location(world,
getConfig().getDouble("afk-location.x"),
getConfig().getDouble("afk-location.y"),
getConfig().getDouble("afk-location.z"),
(float) getConfig().getDouble("afk-location.yaw"),
(float) getConfig().getDouble("afk-location.pitch"));
}
private void saveAfkLocation(Location location) {
getConfig().set("afk-location.world", location.getWorld().getName());
getConfig().set("afk-location.x", location.getX());
getConfig().set("afk-location.y", location.getY());
getConfig().set("afk-location.z", location.getZ());
getConfig().set("afk-location.yaw", location.getYaw());
getConfig().set("afk-location.pitch", location.getPitch());
saveConfig();
}
private void loadPlayerData() {
playerDataFile = new File(getDataFolder(), "players.yml");
playerData = YamlConfiguration.loadConfiguration(playerDataFile);
for (String key : playerData.getKeys(false)) {
try {
lifetimeWaffles.put(UUID.fromString(key), playerData.getLong(key));
} catch (IllegalArgumentException ignored) {
getLogger().warning("Ignoring invalid player UUID in players.yml: " + key);
}
}
}
private void savePlayerData() {
try {
playerData.save(playerDataFile);
} catch (IOException exception) {
getLogger().severe("Could not save waffle player data: " + exception.getMessage());
}
}
@EventHandler