How to assign a player input (from server chat) to a variable?

Discussion in 'Plugin Development' started by JustFrozen, Jan 30, 2019.

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

    JustFrozen

    Hey guys.
    I have a problem what I cant fix:
    I want to do a Time command where the player can use the shortcut /tset (For time set) and then Day, Night, morning..

    But if the player wants some special time in Ticks? (0 - 24000 in Minecraft)
    I want to put the input from the chat into a variable and so I can set the time in ticks to what the player typed in the chat.
    Any help appreciated!
    (sorry for my bad english)
     
  2. Online

    KarimAKL

    @JustFrozen You can get the time specified by usings 'args[0]' for the first argument. (/tset first second third etc)
    Then parse the string to an integer and then do what you want with it.
     
  3. Offline

    JustFrozen

    @KarimAKL

    upload_2019-1-31_16-29-55.png
    Thats what i want to do, i already tried the args[0] method
     
    Last edited: Jan 31, 2019
  4. Offline

    Tango_

    You need to check if the argument is an integer first, so convert the string to an int.
    Then do a check to see if the number is greater or less than 24001.
    Wrote this quickly so not sure if it works, but something like this:

    Code:
    } else if(Integer.valueOf(args[0]) != null) {
                    int time = Integer.valueOf(args[0]);
                    if(time > 0 && <= 24001) {
                        w.setTime(time);
                    }
                }
     
  5. Offline

    JustFrozen

    Thank you all a lot guys!
    You also know the method to save the current world in a String? :D
    (the name of the world where the player is right now f.e. 'world' or a map for a gamemode 'Survivalgames24'
     
    Last edited: Jan 31, 2019
  6. Offline

    Tango_

    You can get the world from a Player object, player.getLocation.getWorld.getName and that will return a String of the world name.
     
  7. Online

    KarimAKL

    @JustFrozen Another way to check if the argument is an integer:
    Code:Java
    1. try {
    2. int number = Integer.parseInt(args[0]);
    3. if (number > 0 && number < 24001) {
    4. //Do something
    5. } else {
    6. sender.sendMessage("Argument 1 needs to be between 0 and 24000");
    7. return true;
    8. }
    9. } catch (NumberFormatException e) {
    10. sender.sendMessage("Argument 1 needs to be a number");
    11. return true;
    12. }
     
  8. Offline

    JustFrozen

    upload_2019-2-1_14-1-25.png



    I also did the thing with the world name so you can see in wich world the time changes
    upload_2019-2-1_14-0-52.png

    It works great thank you all!
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page