Files
src
build.gradle
settings.gradle
Code
8 864
src/main/java/dev/kodari/essentials/KodariEssentialsPlugin.java
package dev.kodari.essentials;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
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.PlayerTeleportEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
public final class KodariEssentialsPlugin extends JavaPlugin implements Listener {
private final Map<UUID, Location> lastLocations = new ConcurrentHashMap<>();
private final Map<UUID, TeleportRequest> teleportRequests = new ConcurrentHashMap<>();
private final Map<UUID, UUID> lastMessagers = new ConcurrentHashMap<>();
private File dataFile;
private FileConfiguration data;
private MenuService menuService;
private EssentialsCommand essentialsCommand;
@Override
public void onEnable() {
saveDefaultConfig();
dataFile = new File(getDataFolder(), "data.yml");
if (!dataFile.exists()) {
saveResource("data.yml", false);
}
data = YamlConfiguration.loadConfiguration(dataFile);
menuService = new MenuService(this);
essentialsCommand = new EssentialsCommand(this);
for (String name : List.of("essentials", "spawn", "setspawn", "home", "sethome", "delhome", "homes",
"warp", "warps", "setwarp", "delwarp", "tpa", "tpahere", "tpaccept", "tpdeny", "back",
"msg", "reply", "heal", "feed", "fly", "enderchest", "workbench")) {
PluginCommand command = getCommand(name);
if (command != null) {
command.setExecutor(essentialsCommand);
command.setTabCompleter(essentialsCommand);
}
}
getServer().getPluginManager().registerEvents(this, this);
getLogger().info("KodariEssentials enabled. Open /essentials for the command dashboard.");
}
@EventHandler
public void onPlayerTeleport(PlayerTeleportEvent event) {
if (event.getFrom().getWorld() != null) {
lastLocations.put(event.getPlayer().getUniqueId(), event.getFrom().clone());
}
}
public FileConfiguration data() {
return data;
}
public MenuService menus() {
return menuService;
}
public Map<UUID, TeleportRequest> teleportRequests() {
return teleportRequests;
}
public Map<UUID, UUID> lastMessagers() {
return lastMessagers;
}
public Location lastLocation(Player player) {
Location location = lastLocations.get(player.getUniqueId());
return location == null ? null : location.clone();
}
public List<String> homes(Player player) {
ConfigurationSection section = data.getConfigurationSection("homes." + player.getUniqueId());
return section == null ? new ArrayList<>() : new ArrayList<>(section.getKeys(false));
}
public List<String> warps() {
ConfigurationSection section = data.getConfigurationSection("warps");
return section == null ? new ArrayList<>() : new ArrayList<>(section.getKeys(false));
}
public Location readLocation(String path) {
String worldName = data.getString(path + ".world");
World world = worldName == null ? null : Bukkit.getWorld(worldName);
if (world == null || !data.contains(path + ".x")) {
return null;
}
return new Location(world,
data.getDouble(path + ".x"),
data.getDouble(path + ".y"),
data.getDouble(path + ".z"),
(float) data.getDouble(path + ".yaw"),
(float) data.getDouble(path + ".pitch"));
}
public void writeLocation(String path, Location location) {
data.set(path + ".world", location.getWorld().getName());
data.set(path + ".x", location.getX());
data.set(path + ".y", location.getY());
data.set(path + ".z", location.getZ());
data.set(path + ".yaw", location.getYaw());
data.set(path + ".pitch", location.getPitch());
}
public void saveData() {
try {
data.save(dataFile);
} catch (IOException exception) {
getLogger().severe("Could not save data.yml: " + exception.getMessage());
}
}
public int teleportTimeoutSeconds() {
return Math.max(5, getConfig().getInt("tpa-timeout-seconds", 60));
}
public static String normalizedName(String name) {
return name.toLowerCase(Locale.ROOT);
}
public record TeleportRequest(UUID requester, boolean teleportRequesterHere, long expiresAt) {
}
}