Solved Player Location from unreachable String.

Discussion in 'Plugin Development' started by OTF Catastrophe, Jun 27, 2016.

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

    OTF Catastrophe

    I know the title might be confusing, I only say it is unreachable because I have no clue how to go about getting information from a string that I cant let be singled out.

    So the plugin is supposed to be setting a spot where the player is standing to become a warp and set all the player location data inside of the config. My issue isnt with getting the info and putting it in, but is more of less trying to get the information to check whether or not the player is in that specific location or not. My code:


    Code:
    package me.SirApathy.blockWarp;
    
    import java.util.ArrayList;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class $main extends JavaPlugin implements Listener{
       
        public $main plugin;
       
        public static ArrayList<String> warps = new ArrayList();
       
        public static ArrayList<Map> ploc = new ArrayList();
       
        static String[] instance;
       
        FileConfiguration config = this.getConfig();
       
        public void configCreator($main plugin)
        {
               
            this.plugin = plugin;
               
        }
     
        public static String[] getInstance()
        {
           
            return instance;
           
        }
       
        @Override
        public void onEnable()
        {
           
            System.out.print("BlockWarp has been enabled! Thank you for your suggestion!");
           
            Bukkit.getPluginManager().registerEvents(this, this);
           
        }
       
        @Override
        public void onDisable()
        {
           
            System.out.print("BlockWarp has been disabled!");
           
        }
       
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
        {
           
            Player p = (Player) sender;
           
            if (commandLabel.equalsIgnoreCase("blockwarp"))
            {
                   
                if (args.length <= 3)
                {
                       
                    return true;
                       
                }
               
                if (args.length > 3)
                {
                   
                    Location loc = p.getLocation();
                   
                    if (args[0].equalsIgnoreCase("set") && !args[1].isEmpty())
                    {
                       
                        int startArg = 0;
                        
                        if(args.length <= startArg)
                           
                            return false;
                        
                        StringBuilder str = new StringBuilder(args[startArg]);
                        
                        for(int i = startArg + 1; i < args.length; i++)
                        {
                           
                             str.append(' ').append(args[i]);
                            
                        }
                       
                        String message = str.toString();
                       
                        Location tmp = p.getLocation();
                       
                        if (loc.subtract(0.0D, 1.0D, 0.0D).getBlock().getType() != Material.AIR)
                        {
                           
                            warps.add(args[1]);
                           
                            config.set("Warps." + args[1] + ".Command", message);
                           
                            config.set("Warps." + tmp.getWorld().getName() + args[1] + ".blockLocation.x", Double.valueOf(tmp.getX()));
                            config.set("Warps." + tmp.getWorld().getName() + args[1] + ".blockLocation.y", Double.valueOf(tmp.getY()));
                            config.set("Warps." + tmp.getWorld().getName() + args[1] + ".blockLocation.z", Double.valueOf(tmp.getZ()));
                           
                        }
                       
                        else
                        {
                           
                            p.sendMessage("(BlockWarp) You must be standing on a block!");
                           
                        }
                       
                    }
                   
                }
                   
            }
            return false;
           
        }
       
        @EventHandler
        public void PlayerMoveEvent(PlayerMoveEvent pme)
        {
           
            Player p = pme.getPlayer();
           
            Location tmp = p.getLocation();
           
            if (!config.contains("Warps." + tmp.getWorld().getName()))
            {
               
                return;
               
            }
           
            else
            {
               
                if (Double.valueOf(tmp.getX()) != null && Double.valueOf(tmp.getY()) != null && Double.valueOf(tmp.getZ()) != null)
                {
                   
                    if ()
                    {
                       
                        //In this function I want it to check if the player is in the world, and is also on a corresponding x, y, and z as a String in config.
                       
                    }
                   
                }
               
            }
           
        }
       
    }
    
    So as you can see I have all the data getting all set up for it, and I believe it should all work. But the way I have it set up is when the player does the command to set the warp area, is goes in config like this:


    Code:
    Warps:
        exampleWorld:
            warpName:
                x:
                y:
                z:
    My issue is I'm only trying to look for if the players x, y, and z match up to ANY instance in the configs strings, I dont want to make it specifically use the warps name. I know its kind of hard to understand this, I actually have a really hard time trying to explain it myself, but hopefully someone understands. I just need a way of checking if the players x, y, and z match up to any instance in config.

    Thank you for taking the time to read!
     
  2. Offline

    Zombie_Striker

    You cannot work on bukkit without officially learning Java.
    All classes need to be capitalized, should not have symbols or numbers, and your main class needs to be the name of your plugin.

    Also, you are using plugin in a static method. First, you cannot reference non-static variables in static methods. Also, both the field and the methods are public. Only one of those things should be public (and it's illogical to have a private getter). Finally you never even set plugin equal to anything. It is useless.


    Package names need to be lower case.

    You cannot get the config before the plugin is even enabled. This will eventually cause an error.


    Do not log your own plugins. Bukkit does this for you.

    BTW: Don't use Sys.out for Bukkit. Use Logger if you want to display a message in the console.

    Because of the above, you can remove this whole method.

    Never blindly cast. There's a reason sender is not automatically a Player. What if a plugin sends a command? What if it's the console? Plugins and consoles are not players, so this will break your command.

    Use cmd.getName() instead of commandlabel.

    Return true only when something worked as it should have. If anything goes wrong, you should return false.

    You are converting a double to a double, and then checking if it's not null, even though numbers can never be null.

    Please, learn Java before working on bukkit. Doing so would prevent you from making simple mistakes. You can find a tutorial from the following link. Pick one, learn Java, and then come back to bukkit.
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/
     
    bwfcwalshy likes this.
  3. Offline

    OTF Catastrophe

    Okay so you took a lot of time to respond but didn't even try to answer my question. Please only comment if you have an answer to my problem instead of putting out random tab bits of what's wrong with my code. You pointed out minor things that don't matter at all. I know the basics of how to create a plugin and that's all I need to know. This forum is for help regarding the creation of bukkit plugins.

    Regarding your "help":
    1st issue: Capitalization isn't breaking anything and neither is using symbols. Useless knowledge.

    2nd issue: Capitalization of the package name doesn't matter. It's not breaking anything if I put a different type of capitalization on it or not. So this is useless knowledge.

    3rd Issue: Semi useful knowledge. But hasn't affected anything so far. Doubt it really ever will.

    4th Issue: If System.out.print works.... why not use it?

    5th Issue: The on disable is only there because I so wish to have a "good-bye message"

    6th Issue: None of my plugin is going to be accessible by console, along with the commands. The next best alternative is just making the Player.. the sender. If I was worried about console commands I'd check if it isn't an instanceof Player.

    7th Issue: It's literally the same thing with a different amount of characters.

    8th Issue: I have it to return true; because if the args are equal to 3 or less it'll return the Usage in the plugin.yml file.

    Final issue: The area under the Event is things I'm trying to get help on, so obviously it's going to be wrong. That's why I'm on here.. for help. Not to get replies from a guy telling me to change the way I write when almost all of your "helpful tips" were useless knowledge that doesn't make a difference.

    Buy anyways, still looking for a solution to any one else reading this ^-^
     
  4. Offline

    mythbusterma

    @OTF Catastrophe

    Why write a plugin if you're not interested in writing it correctly? Wouldn't knowing how to be a halfway decent Java programmer be a useful skill to have anyway? Many of the issues he's pointing out will generate stack traces and pre-empt you creating other threads asking for answers to stupid questions that have already been answered many times on this forum. So yes, they matter quite a bit.

    https://bukkit.org/threads/best-way-to-check-if-two-locations-are-the-same.28077/
    https://bukkit.org/threads/check-if-a-player-is-in-an-area.95772/

    Are some answers I found by quickly searching on Google, and if you actually took the time to learn to program in Java, you wouldn't be asking this question.

    The short answer is: get the location from the configuration file and compare it to the player's location. You'll have to do this separately for each of the three dimensions (or two, if you don't care about height).
     
    bwfcwalshy likes this.
  5. Offline

    OTF Catastrophe

    I knew I had to compare the two locations from config and the players location, the issue I'm trying to get fixed is I'd have to access the string from config. Which I cannot do simply because I don't know a different method other than:

    Code:
    Warps:
      exampleWorld:
        warpName:
          x:
          y:
          z:
    To get all the information I need. I wouldn't be able to get the config warps x, y, and z simply because in my previous code I'm setting warpName with args[1] from the set command. I need a way around looking for the warp names because I cannot look for args[1] in the Event.
     
  6. Offline

    mythbusterma

    @OTF Catastrophe

    So you need to iterate over every warp in your config? ConfigurationSection#getValues(false)
     
  7. Offline

    OTF Catastrophe

    So using ConfigurationSection would be able to get the string I need to match up the players x, y, and z? How so? (Not looking for you to give me code, just genuinely curious how it works.)
     
  8. Offline

    I Al Istannen

    @OTF Catastrophe
    I would make use of the fact that Location implements ConfigurationSerializable since 1.8. This means you can directly set it using the set method. So config#set(String key, Location value) will work flawlessly. Saves you from doing it yourself and also removes the need to sort them by worlds, as the world is a part of the Location.

    Then just loop through the getValues() of the configuration section "Warps", as mythbusterma said. It will return a map or an entryset (don't know right now and doesn't really matter. You can just call Map#entrySet()), where you can loop over. If you set the boolean deep to false, and have this config:
    Code:
    Warps:
      warpOne:
        <serialized location>
      warpTwo:
        <serialized location>
      warpThree:
        <serialized location>
    You will get warpOne with the corresponding Location as value, and the same for warpTwo and warpThree in your loop.
     
  9. Offline

    OTF Catastrophe

    Thank you a ton! I didnt even think to use ConfigurationSerializable, that makes it way easier to get all the information. Thank you again!
     
Thread Status:
Not open for further replies.

Share This Page