Auto finishing targets names?

Discussion in 'Plugin Development' started by Vinceguy1, Feb 1, 2012.

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

    Vinceguy1

    I recently have been working a lot on my plugin "ChangeGameMode" and to change someone else's game mode you have to type out there full name, how do i make it more like essentials where you only have to type a bit?

    help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  2. Offline

    Father Of Time

    This is how I've handled it in the past, their most likely is a more efficient way.

    String partialname = ...source...
    Player player = Bukkit.getPlayer( partialname );
    String fullname = player.getName();

    the getPlayer function can accept partial names, so I simply use that. Hope this helps!

    p.s. A lot of people work mid day during the week, being patient and waiting more than 30 minutes for responses, otherwise you may begin to alienate the developers here at Bukkit.
     
  3. Offline

    Vinceguy1

    ty and what am i supposed to put in place for ...source...
     
  4. Offline

    Father Of Time

    What ever variable contains your partial player name, that depends on your plug-in... The "source" of the partial name is usually input by a player using a /command.
     
  5. Offline

    Vinceguy1

    Do you need the code? ill send it to you through PM but i need help please
     
  6. Offline

    Father Of Time

    I mean this respectfully as possible; I've literally given you the answer, I can't really assist you any further without physically doing it for you, which I'm sorry but I won't do (teach a man to fish, you know?).

    Give it your best and post your code if it fails to perform as expected, I will show you what is wrong once you've accomplished that much. I would imagine that this code will either reside inside a command handler or a custom handler, but regardless the names are coming from somewhere, where ever those names are being "gathered" from is where this code most likely will go.

    Good luck with your project, I wish you much luck! :D
     
  7. Offline

    Vinceguy1

    i sent you the code, i just want to know what to put there, sorry im fairly new at java
     
  8. Offline

    Father Of Time

    I'm sorry but I don't work via PM, it doesn't give back to the community; if you would like assistance please post your trouble code, that way future forum users may benefit from our work.

    Also, I don't really get your question... obviously you have something generating partial names, as far as I'm aware the only way to get a partial name is from user input. If that is the case you should be getting the partial name from the args[] variable in the onCommand function, but only you would know that...

    If this is the case (you want to get the names from players using a /command) then I would suggest searching bukkit forums for "passing arguments to a command".

    Learning to program requires a great deal of research and reading, the only thing the community can assist you with is with pointing you in the direction of the right resources.
     
  9. Offline

    Vinceguy1

    package me.Vinceguy1.pie;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;

    @SuppressWarnings("unused")
    public class pie extends JavaPlugin{
    Logger logger = Logger.getLogger("Minecraft");

    @Override
    public void onEnable() {
    PluginDescriptionFile pdffile = this.getDescription();
    this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + " has been enabled!");
    }

    @Override
    public void onDisable() {
    PluginDescriptionFile pdffile = this.getDescription();
    this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + " has been disabled!");
    }
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
    Server servar = sender.getServer();

    if (label.equalsIgnoreCase("gm"))
    {
    Player player = (Player) sender;
    Server server = player.getServer();
    if(args.length == 1){
    for(int i = 0; i < server.getOnlinePlayers().length; i++)
    {
    if(args[0].equalsIgnoreCase(server.getOnlinePlayers().getName()))
    {
    Player Terget = server.getOnlinePlayers();
    String partialname = Terget.getName();
    Player pizza = Bukkit.getPlayer( partialname );
    this.logger.info(pizza.toString());
    Player Target = pizza;

    if (player.hasPermission("gm.toggle.others")){
    this.logger.info(player.getDisplayName() + " used /gm " + Target.getName());
    if(Target.getGameMode() == GameMode.SURVIVAL)
    {
    Target.setGameMode(GameMode.CREATIVE);
    player.sendMessage(ChatColor.YELLOW + Target.getName() + "'s gamemode has been changed to Creative!");
    Target.sendMessage(ChatColor.YELLOW + player.getName() + " changed your gamemode to creative");
    }
    else if (Target.getGameMode() == GameMode.CREATIVE)
    {
    Target.setGameMode(GameMode.SURVIVAL);
    player.sendMessage(ChatColor.YELLOW + Target.getName() + "'s gamemode has been changed to Survival!");
    Target.sendMessage(ChatColor.YELLOW + player.getName() + " changed your gamemode to survival");
    }
    }
    else
    {
    this.logger.info(player.getDisplayName() + " tried to use used /gm " + Target.getName() + " but did not have access");
    player.sendMessage(ChatColor.RED + "Sorry, you don't have access to this command!");
    }
    }
    }

    }
    }
    return true;
    }
    }
    This is what i tried but it will only accept full names :(
     
  10. Offline

    Father Of Time

    Yes, this is definitely overly complicated. I see a BUNCH I would change, but for now lets start by simplifying... Lets just start by making a command that will accept a name as a parameter, and getting that name on the code side. So the command we will make is /find <playername>

    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
        {
            Player player = (player)sender;
            if( args.Length == 1 )
            {
                String partialname = args[0];
                Player player = Bukkit.getPlayer( partialname );
                String fullname = player.getName();
                Player.sendMessage("You found me!");
                return true;
            }
            return false;
        }
    This would make a command that if you wanted to send a message to player "Markus" and used the command "/find mark" it would end up sending the player Markus the message "You found me!", assuming no one with a more similar name exist. Remember, in order for this command to work you will need to add the command name to your configuration file, if you don't it will still compile but simply not function.

    This is a very stripped down version of what you are asking for, 100% of the answer to your question resides in the above code, you simply need to pick apart the sections you need and implement it into your project. My best advise to you from what I see is don't work SO far ahead. you have giant bodies of code that are completely incorrect and even when you solve this issue you will only receive a bunch of additional errors. Work in very small "snippets" of code, and compile as you work. Think of the very small parts of the bigger picture and program each part one at a time, testing as you go. for instance.

    If you want to give a player money:
    1) identify the player
    2) give the player money

    First make a function that locates the player, and sends them a message to know they you have found them properly, once that works then make a another function that can take a player object (which we know is working) and give them money. Once you know the money is transferring properly simply combine the two "test functions".

    If you do it this way you can identify what part of the process is broken as you go and fix it accordingly, if you code all at once and test at the end you wouldn't know if its the giving money aspect of the plug-in or the player retrieval that is broken, you will have to reverse engineer to resolve the issue.

    Anyways, I am rambling... I hope this helps, good luck with your project!
     
  11. Offline

    Vinceguy1

    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
        {
            Player player = (player)sender;
            if( args.Length == 1 )
            {
                String partialname = args[0];
                Player player = Bukkit.getPlayer( partialname );
                String fullname = player.getName();
                Player.sendMessage("You found me!");
                return true;
            }
            return false;
        }
    wouldn't that send You Found Me! to the player who sent the message?

    I ended up using this code
    CODE (open)

    Code:
    package me.Vinceguy1.pie;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("unused")
    public class pie extends JavaPlugin{
    Logger logger = Logger.getLogger("Minecraft");
     
    @Override
    public void onEnable() {
    PluginDescriptionFile pdffile = this.getDescription();
    this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + " has been enabled!");
    }
     
    @Override
    public void onDisable() {
    PluginDescriptionFile pdffile = this.getDescription();
    this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + " has been disabled!");
    }
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
    Server servar = sender.getServer();
     
    if (label.equalsIgnoreCase("gm"))
    {
    Player player = (Player) sender;
    Server server = player.getServer();
    if(args.length == 1){
    String partialname = args[0];
              Player pie = Bukkit.getPlayer( partialname );
              pie.sendMessage("You found me!");
              return true;
     
    }
    }
    return true;
    }
    }
    

    Anyways, TYSM for the help father of time, i will thank you in my changelog of my plugin

    EDIT: I found out you have to type in a name with the start of theres if there name is "Vinceguy1" and you type in "guy1" it won't work but hey, id rather have this then nothing at all, thank you

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  12. Offline

    Father Of Time

    Eek, you are right I apologize... I forgot two important things, the first is that I declare the command user (sender) as "player", then declare another variable as "player" when we are looking for the player... you would simple need to rename the second player variable to something like "searchplayer", then do searchplayer.sendMessage(...);

    I also forgot the check for what command was used, However you you managed to include on your own (good job):

    Code:
    if (label.equalsIgnoreCase("gm"))
    I am very happy to hear that you resolved your issues, and I am impressive with your ability to utilize the assistance of the public as a novice programmer, very well done!

    That is mighty kind of you, and appriciated. Good luck with the rest of your project, holler back if you find yourself in need of assistance yet again. :D
     
  13. Offline

    Vinceguy1

    Ok i understand, i thought you were telling me a fix for the starting with thingy anyways ty and im not really a novice programmer, just more at java, anyways if anyone has any way to fix the partial name thing where it doesn't need to start with the same letters post here so everyone can see, and also maybe send me a message so i can see too

    Here is the code to get rid of that null error

    Code (open)

    Code:
    package me.Vinceguy1.pie;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    @SuppressWarnings("unused")
    public class pie extends JavaPlugin{
    Logger logger = Logger.getLogger("Minecraft");
     
    @Override
    public void onEnable() {
    PluginDescriptionFile pdffile = this.getDescription();
    this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + " has been enabled!");
    }
     
    @Override
    public void onDisable() {
    PluginDescriptionFile pdffile = this.getDescription();
    this.logger.info(pdffile.getName() + " version " + pdffile.getVersion() + " has been disabled!");
    }
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
     
    if (label.equalsIgnoreCase("gm")) 
    {
    Player player = (Player) sender;
    Server server = player.getServer();
    if(args.length == 1){
    String partialname = args[0];
               Player pie = Bukkit.getPlayer( partialname );
               if (pie != null){
               this.logger.info(pie.toString());
               pie.sendMessage("You found me!");
               }
               else
               {
               this.logger.info(sender.getName() + " tried to use /gm " + partialname.toString() + " but, the command failed to execute because it could not find the player, this is usally because you need to put in what the name starts with, so guy1 for vinceguy1 wouldn't work but v or vince would");
               sender.sendMessage("You tried to use /gm " + partialname.toString() + " but, the command failed to execute    because it could not find the player, this is usally because     you need to put in what the name starts with, so guy1 for       vinceguy1 wouldn't work but v or vince would");
               }
               return true;
     
    }
    }
    return true;
    }
    }
    


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
Thread Status:
Not open for further replies.

Share This Page