How to get the player name in a method?

Discussion in 'Spigot Plugin Development' started by ShishKaBobz, May 11, 2017.

  1. Hello! So I want to reach the players name In a class where I cannot use e.getPlayer or player = sender;
    I cannot find anything around it, This is my code:
    Code (Text):
    package com.ionmc.core.Manager;

    import java.io.File;
    import java.io.IOException;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    import net.minecraft.server.v1_11_R1.PacketPlayOutPlayerInfo.PlayerInfoData;

    public class Files {
       
        static File userdata = new File(Bukkit.getServer().getPluginManager().getPlugin("Icore").getDataFolder(), File.separator + "PlayerDatabase");
        static File f = new File(userdata, File.separator + "prefixes" + ".yml");
        static FileConfiguration playerData = YamlConfiguration.loadConfiguration(f);
        String name = Bukkit.getName();
        public static void registerPrefixes(){

            if (!f.exists()) {
                try {
                    playerData.createSection("Prefixes");
                    playerData.set(, arg1);
                    playerData.save(f);
                } catch (IOException exception) {

                    exception.printStackTrace();
                }
            }
        }
    }
     
    So before
    "playerData.set(, arg1);" the arg1 I want to reach the players name, Any help Will be much appreciated! Thanks!
     
  2. You could pass the player's object instance in the parameters of the method? If that's what you want to do.
     
  3. What Object would that be?
     
  4. WAS

    WAS

    Why not use static methods provided?

    Code (Java):
    String name = Bukkit.getServer().getPlayer(UUID).getName();
    Code (Java):
    String name = Bukkit.getServer().getPlayer("Name").getDisplayName();
     
  5. Unless the above answer is what you needed, please explain more about what you're trying to do.
     
  6. I believe he is trying to do the same as I am.

    I am currently trying to make a placeholder parser, in a separate class of course. Though I just want to have one Object. The message string.

    I have a few thoughts, that's to pass an instance of the command via a "senderwrapper" (so the player's name and the object gets parsed), or the other one is to just extend the class and implement a constructor method.
     
  7. WAS

    WAS

    Seems like he may be building files based on users online. In which case you'd need to listen to the PlayerJoinEvent to pass some info to this script to "create" a player file. If not as iCodings said if it's via commands, a public method or constructor to pass the sender and optional args to the class.
     
  8. He could do #getOnlinePlayers(), have that run async as well. Though I would recommend going with WAS post for a PlayerJoinEvent.
     
  9. Why are you files and configs static... Just make a singleton.