Solved Developing a time plugin, need help.

Discussion in 'Plugin Development' started by Axanite, Aug 30, 2013.

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

    Axanite

    So as the title says, I am developing a time plugin (like to do /time to check time and then /time set day/night) and doing /time does at it should but when I go to set the time with /time set day (day is the only one implemented as its easier to fix one command rather than 2), it gives me an internal error saying "Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at com.omegagamers.rf2minecraft.SimpleTime.SimpleTime.onCommand(SimpleTi
    me.java:27)", I have no idea how to fix this as it is needed I believe. If not, oh well, easily fixed. If somebody can help me, it would be great.


    Source Code:
    Code:java
    1. package com.omegagamers.rf2minecraft.SimpleTime;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.World;
    8.  
    9. public final class SimpleTime extends JavaPlugin {
    10. public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
    11. if(sender instanceof Player) {
    12. if(cmd.getName().equalsIgnoreCase("time")) {
    13. if(args.length == 0) {
    14. Player player = (Player) sender; /* Gets the player from the sender */
    15. String world = player.getWorld().getName(); /* Gets the world the player is in & name of it */
    16. long time = player.getWorld().getTime(); /* Gets the time of the world at the time of sending command */
    17. int hours = (int)((time / 1000L + 6L) % 24L); /* Grabs the hours of the world time */
    18. int minutes = (int)(60L * (time % 1000L) / 1000L); /* Grabs the minutes of the world time */
    19. if(time < 6000) {
    20. sender.sendMessage(ChatColor.DARK_PURPLE + "The time in: " + ChatColor.LIGHT_PURPLE + "" + world + ChatColor.DARK_PURPLE + " is " + ChatColor.LIGHT_PURPLE + "" + time + ChatColor.DARK_PURPLE + " ticks / " + "" + hours+":"+minutes + "" + "AM");
    21. }
    22. if(time > 6000) {
    23. sender.sendMessage(ChatColor.DARK_PURPLE + "The time in: " + ChatColor.LIGHT_PURPLE + "" + world + ChatColor.DARK_PURPLE + " is " + ChatColor.LIGHT_PURPLE + "" + time + ChatColor.DARK_PURPLE + " ticks / " + "" + hours+":"+minutes + "" + "PM");
    24. }
    25. }
    26. if(args[1].equalsIgnoreCase("set")) {
    27. if(args[2] == null) {
    28. sender.sendMessage(ChatColor.DARK_PURPLE + "Please provide a time you wish to set the world to.");
    29. }
    30. if(args[2] != null) { /* If there is 2 arguments after the normal command */
    31. if(args[2].equalsIgnoreCase("day")) { /* If the 2nd argument is "day", do the following. */
    32. Player player = (Player) sender; /* Grabs sender's player */
    33. World world = player.getWorld(); /* Gets world that player is in */
    34. world.setFullTime(0); /* Sets world time to day. */
    35. sender.sendMessage(ChatColor.DARK_PURPLE + "Time set."); /* Sends player a message telling them time is set */
    36.  
    37. }
    38.  
    39. }
    40. }
    41. }
    42. }
    43. return true;
    44. }
    45. }
     
  2. Offline

    Chinwe

    You need to return true at the end of the args.length == 0 command, as when it has finished with that, it carries onto checking args[1] which doesn't exist and throws that exception :)

    And by the way, you should use 'else' instead of 'if (time > 6000)', as if time == 6000, neither of the ifs conditions will be satisfied :<
     
  3. Offline

    Axanite

    Thanks, I fixed it now. :)
     
Thread Status:
Not open for further replies.

Share This Page