Solved Needing help with a ItemTP Plugin. Not what you think!

Discussion in 'Plugin Development' started by wiredbrother, Sep 1, 2016.

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

    ShaneCraftDev

    @wiredbrother You are trying to fetch a value from a config with the created key stored in the 'worldname' variable and trying to get a world with that name, however the config did not find a key with the generated name so it returned null and gives you a NPE when using it in Bukkit#getWorld. Add a null check before accessing the world object using the worldname through Bukkit#getWorld.

    Your code will crash on terminals immediately, in the beginning of the overriden method you cast the CommandSender object to a Player, however CommandSender can also be an instance of Console (I think this is the correct class name). Remove all checks for if the sender is of an instance of Player and add one at the top of the method. If the sender is not an instance of Player, return false immediately.

    Some of your code is redundant, you add checks within already checked code.
    Show Spoiler

    Example of something redundant in your code (see comments):
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("itemtp") && sender instanceof Player) {
    3.  
    4. if (args.length == 1) {
    5.  
    6. if (config.contains(args[0])) {
    7.  
    8. String find = args[0];
    9. int x = getConfig().getInt(sender.getName() + "." + find + ".x");
    10. int y = getConfig().getInt(sender.getName() + "." + find + ".y");
    11. int z = getConfig().getInt(sender.getName() + "." + find + ".z");
    12. String worldname = getConfig().getString(sender.getName() + "." + find + ".world");
    13.  
    14. // needs NULL Check!
    15. Location hi = new Location(Bukkit.getWorld(worldname), x, y, z);
    16.  
    17. if (sender instanceof Player) { // you've already checked if the sender is an instance of Player
    18. player.teleport(hi);
    19. player.sendMessage(ChatColor.GOLD + "You have been teleported to the item!");
    20.  
    21. } else player.sendMessage(ChatColor.RED + "You are a console! You do not have a home!"); // unreachable code, sender is always an instance of Player in this case
    22.  
    23. } else player.sendMessage(ChatColor.RED + "Item Not Found! Make sure to use capital if you used caps!");
    24.  
    25.  
    26. } else player.sendMessage(ChatColor.RED + "Invalid Entry! Need More Then That!");
    27.  
    28.  
    29. return true;
    30. }
    31.  
    32.  

     
  2. Offline

    wiredbrother

  3. Offline

    Zombie_Striker

    This error is thrown when a name provided is null. Most likely, its this line:
    worldname is most likely null. Male sure the config contains the world name. To fix this: Try re-saving the location to the config. Most likely, the world was not saved before. Resaving it should remove the error.
     
  4. Offline

    wiredbrother

    @Zombie_Striker Nope still throws the same error. You guys have the code instead of me keep going back and forth maybe you guys could try it and maybe fix it that way as my code has a lot of problems. lol
     
  5. @wiredbrother Ask as much as you want, we won't spoon feed. You copy fixed code and then come back the next day with another simple issue. Why don't we help you fix an issue yourself so you can fix more in the future. You won't have us here forever.
     
  6. Offline

    wiredbrother

    @bwfcwalshy True. Very true. Thank you I shall try and learn and fix the problem. :)
     
  7. Declare int x = 0; then throw a try catch block setting that number to Integer.parseInt(string); and if it catches the exception, simply send a message saying
     
    Last edited by a moderator: Sep 9, 2016
  8. Offline

    wiredbrother

    @TheEnderCrafter9 @Zombie_Striker @ZP18 @bwfcwalshy @ShaneCraftDev @AlvinB
    To Everyone in this thread!
    I thank you! You guys basically were helping a stupid kid who didn't want to think. (at all)
    So I finally decided (With a extra push from some people on this thread. Thank you by the way!) that I needed to fix it and make it work myself. So I did! I basically re did all of the code and made it new. Along with the ability to make, remove and TP to any location you make in the shop it can also reload the config and check for errors in the config. I thank you guys! I really do without you guys pushing me to figure it out and not giving me the code directly I wouldn't be here where I am now. I learned more about code and self push! I'm sorry about all that! Thank you and have a awesome weekend!

    Thank you!
    -wiredbrother
    :)


    @ZP18 Cool I shall check it out!

    @AlvinB I Will thank you!
     
    Last edited: Sep 10, 2016
  9. @wiredbrother
    Glad you got it working! If you feel you don't need anymore assistance, would you mind marking this thread as solved?
     
  10. Offline

    ZP18

  11. @ZP18 I am never going to recommend not enough args and to much args messages. They are simply annoying and not user friendly. Instead, when the syntax is wrong, just throw a syntax message.
     
  12. Offline

    ZP18

    I've made it so the messages are configurable in the config. Also having two separate messages for the not enough and invalid args makes it easier for the user to know what's wrong


    Sent from my iPhone using Tapatalk
     
  13. Offline

    JanTuck

    Well...
    int num = Integer.parseInt(args[0]);
    Why are you trying to parse string to int?
     
  14. Offline

    timtower Administrator Administrator Moderator

    To get user input as an number?
     
  15. Offline

    ZP18

    Don't worry guys the OP has solved his problem/will solve on his own. So this thread is needed to be marked as solved now.



    Sent from my iPhone using Tapatalk
     
  16. Offline

    JanTuck

    He had posted a post where he tried to /itemtp lol <-- so he parsed "lol" to int this wont work thats why i said it. But the post looks like its deleted now.
     
  17. I'd disagree. I will know that I need to use less args, but will have no clue what these args do unless I look at help. If I use a syntax message, I know how many args, and watch each arg represents. It also looks cleaner.
     
  18. Offline

    ZP18

    What do you mean by Syntax message? Like the usage?


    Sent from my iPhone using Tapatalk
     
  19. @ZP18 yes. Such as

    "§3Syntax: §b/NICK {USER} [NICK] OR /NICK [NICK]"
     
  20. Offline

    ZP18

    Ohhh. I'm going to do both messages in the next update


    Sent from my iPhone using Tapatalk
     
  21. Offline

    wiredbrother

Thread Status:
Not open for further replies.

Share This Page