Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.outlook.kmounties.testplayerdata;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- 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.PlayerJoinEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.io.File;
- import java.io.IOException;
- import java.util.UUID;
- public class TestPlayerData extends JavaPlugin implements Listener {
- private File dataFolder;
- private File playersFolder;
- @Override
- public void onEnable() {
- // Register listener
- getServer().getPluginManager().registerEvents(this, this);
- // Check if players folder exists, create it if it doesn't
- playersFolder = new File(getDataFolder(), "players");
- if (!playersFolder.exists()) {
- playersFolder.mkdirs();
- }
- }
- @EventHandler
- public void onPlayerJoin(PlayerJoinEvent event) {
- Player player = event.getPlayer();
- // Check if player data file exists, create it if it doesn't
- File playerFile = new File(playersFolder, player.getUniqueId().toString() + ".yml");
- if (!playerFile.exists()) {
- try {
- playerFile.createNewFile();
- FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
- playerConfig.set("name", player.getName());
- playerConfig.set("uuid", player.getUniqueId().toString());
- playerConfig.save(playerFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (command.getName().equalsIgnoreCase("getname")) {
- if (args.length != 1) {
- sender.sendMessage(ChatColor.RED + "Usage: /getname <player>");
- return true;
- }
- // Get the player's UUID from their name, even if they're offline
- UUID uuid = Bukkit.getOfflinePlayer(args[0]).getUniqueId();
- // Get the player's .yml file
- File playerFile = new File(playersFolder, uuid.toString() + ".yml");
- if (!playerFile.exists()) {
- sender.sendMessage(ChatColor.RED + "Player data not found.");
- return true;
- }
- // Load the player's .yml file
- FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
- // Get the "name" value from the .yml file
- String name = playerConfig.getString("name");
- if (name == null) {
- sender.sendMessage(ChatColor.RED + "Player name not found in data.");
- return true;
- }
- // Send the player's name to the command sender
- sender.sendMessage(ChatColor.GREEN + "Player " + args[0] + " has UUID " + uuid.toString() + " and name: " + name);
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement