Issues with getPlayer(id)

Discussion in 'Plugin Development' started by ogretrolls, Aug 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ogretrolls

    Hi, so I'm having issues with Bukkit.getPlayer(id). I'd also would like to point out that I'm quite new to coding plugins and java. My entire goal with this plugin is to create a special type of chat for certain people, that even excludes OPs. My idea of doing this is to send the message to every name of the person I wish to receive the message. I first decided to try and use Bukkit.getPlayer("name of player"), it still works, but it's depreciated. So now I'm trying to figure out how to use the (id) part. I have the UUID of a player I want to use. However when i try
    Code:
    Bukkit.getPlayer(977d03c1-de8e-4367-8abd-92f5fe405467);
    It doesn't seem to work. Am I just using the (id) part wrong? If so how do I fix this?
    Any help will be greatly appreciated.

    And the full part of code I'm using is
    Code:
    Player temp = Bukkit.getPlayer(977d03c1-de8e-4367-8abd-92f5fe405467);
                            temp.sendMessage(player.getName() + " " + msg);
     
  2. Offline

    mine-care

    Instead use getOfflinePlayer and then turn it to player
     
  3. Offline

    Necrodoom

    mine-care what..

    ogretrolls you need a UUID object..
    Also, getting player by name works fine, its deprecated to rise awareness that you should not save name to file, due to the fact players can change names soon, so you should save UUID.
     
  4. Offline

    xize

    ogretrolls
    Aslong you use getPlayer(playername) when they are online you are fine the deprecation is just because uuids are needed these days.
    there is not really a need to use a uuid there, uuids are more used for stored player configs and data not a regular online player call ;-)
     
  5. Offline

    mine-care

    Necrodoom i was pointing out that to get player buy uuid he needs getOfflinePlayer. Has it been changed?
     
  6. Offline

    Necrodoom

    mine-care It has never been the case. Read player javadocs.
     
  7. Offline

    ogretrolls

    Necrodoom xize
    I'm wanting to use UUID's because I'm not sure if these certain players will ever change their names. And I tried doing this with UUID which still seems to have messed up xD
    Here's the code that I'm currently working with.
    Code:
    UUID ogretrollsID = UUID.fromString("977d03c1-de8e-4367-8abd-92f5fe405467");
    Player ogretrolls = Bukkit.getServer().getPlayer(ogretrollsID);
    ogretrolls.sendMessage("hi");
     
  8. Offline

    Necrodoom

    ogretrolls Why do you need the UUID of a single player only? What exactly do you want to do?

    Also, paste full class.
     
  9. Offline

    ogretrolls

    Necrodoom It's going to have more then 1 person for it, however this is my way of knowing that it won't work for ops either. What I'm wanting to do is create a special chat. If you have some way to give people a permission node, and it won't work for anybody, even ops, unless they have that permission node, then that will work just as well. Give me a lil bit to shorten all the code down.

    Ok, here's the code. What I expected this to do was when somebody with the permissionnode short.tc does /tc <msg> it would then send the person with that UUID a message.
    Code:
    package me.ogretrolls.Tyrone;
     
    import java.util.UUID;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Short extends JavaPlugin{
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
       
            Player player = (Player) sender;
       
            if(player.hasPermission("short.tc")){
                if(cmd.getName().equalsIgnoreCase("tc")){
                    if(args.length > 0){
                        String msg = "";
                        for(int i = 0; i < args.length; i++){
                            String arg = args[i] + " ";
                            msg = msg + arg;
                        }
                        UUID ogretrollsID = UUID.fromString("977d03c1-de8e-4367-8abd-92f5fe405467");
                        Player ogretrolls = Bukkit.getServer().getPlayer(ogretrollsID);
                        ogretrolls.sendMessage("[" + player.getName() + "]" + msg);
                    }
                }
            }
       
            return false;
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  10. Offline

    xize

    ogretrolls

    I would make it as dynamic as possible by using their name + uuid and storing in a sort config what presents like a sort of whitelist of course that means you still need to update the name if it doesn't match but the uuid is.

    you can choose if you want a wrapper or just a global List you obtained for example from getStringList() and then use the e.getRecipements() from the chat events where you can remove other players from the chat so you can make it more privated.

    edit, just ignore this I thought you where talking about chat events, typed this while you posted the code:p
     
  11. Offline

    Necrodoom

    ogretrolls It seems like you want to simply create a chat channel, in which case you would use the chat event, and remove recipients that dont have permission.

    Also, your code would NPE if you werent online.
     
  12. Offline

    ogretrolls

    Ok, well, thanks for everybody's assistance. I'll try to learn more about chat events and what not. If I still can't seem to find out how to do it I'll just use their names, I guess no problem. But yet again, thanks :D
     
  13. Offline

    mine-care

    Necrodoom did read them and just realized. I was using it this way because with the stored uuids I was not sure if player is online at the time. Sorry, my bad :/
     
Thread Status:
Not open for further replies.

Share This Page