Advertisement
MasterKyle08

player files java

Mar 20th, 2023 (edited)
1,580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. package com.outlook.kmounties.testplayerdata;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.command.Command;
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.configuration.file.FileConfiguration;
  8. import org.bukkit.configuration.file.YamlConfiguration;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.player.PlayerJoinEvent;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14.  
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.UUID;
  18.  
  19. public class TestPlayerData extends JavaPlugin implements Listener {
  20.  
  21.     private File dataFolder;
  22.     private File playersFolder;
  23.  
  24.     @Override
  25.     public void onEnable() {
  26.         // Register listener
  27.         getServer().getPluginManager().registerEvents(this, this);
  28.  
  29.         // Check if players folder exists, create it if it doesn't
  30.         playersFolder = new File(getDataFolder(), "players");
  31.         if (!playersFolder.exists()) {
  32.             playersFolder.mkdirs();
  33.         }
  34.     }
  35.  
  36.     @EventHandler
  37.     public void onPlayerJoin(PlayerJoinEvent event) {
  38.         Player player = event.getPlayer();
  39.  
  40.         // Check if player data file exists, create it if it doesn't
  41.         File playerFile = new File(playersFolder, player.getUniqueId().toString() + ".yml");
  42.         if (!playerFile.exists()) {
  43.             try {
  44.                 playerFile.createNewFile();
  45.                 FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
  46.                 playerConfig.set("name", player.getName());
  47.                 playerConfig.set("uuid", player.getUniqueId().toString());
  48.                 playerConfig.save(playerFile);
  49.             } catch (IOException e) {
  50.                 e.printStackTrace();
  51.             }
  52.         }
  53.     }
  54.  
  55.     @Override
  56.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  57.         if (command.getName().equalsIgnoreCase("getname")) {
  58.             if (args.length != 1) {
  59.                 sender.sendMessage(ChatColor.RED + "Usage: /getname <player>");
  60.                 return true;
  61.             }
  62.  
  63.             // Get the player's UUID from their name, even if they're offline
  64.             UUID uuid = Bukkit.getOfflinePlayer(args[0]).getUniqueId();
  65.  
  66.             // Get the player's .yml file
  67.             File playerFile = new File(playersFolder, uuid.toString() + ".yml");
  68.             if (!playerFile.exists()) {
  69.                 sender.sendMessage(ChatColor.RED + "Player data not found.");
  70.                 return true;
  71.             }
  72.  
  73.             // Load the player's .yml file
  74.             FileConfiguration playerConfig = YamlConfiguration.loadConfiguration(playerFile);
  75.  
  76.             // Get the "name" value from the .yml file
  77.             String name = playerConfig.getString("name");
  78.             if (name == null) {
  79.                 sender.sendMessage(ChatColor.RED + "Player name not found in data.");
  80.                 return true;
  81.             }
  82.  
  83.             // Send the player's name to the command sender
  84.             sender.sendMessage(ChatColor.GREEN + "Player " + args[0] + " has UUID " + uuid.toString() + " and name: " + name);
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89.  
  90.  
  91.  
  92. }
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement