001 package org.bukkit.command.defaults; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import org.apache.commons.lang.Validate; 007 import org.bukkit.Bukkit; 008 import org.bukkit.ChatColor; 009 import org.bukkit.World; 010 import org.bukkit.command.Command; 011 import org.bukkit.command.CommandSender; 012 import org.bukkit.util.StringUtil; 013 014 import com.google.common.collect.ImmutableList; 015 016 public class TimeCommand extends VanillaCommand { 017 private static final List<String> TABCOMPLETE_ADD_SET = ImmutableList.of("add", "set"); 018 private static final List<String> TABCOMPLETE_DAY_NIGHT = ImmutableList.of("day", "night"); 019 020 public TimeCommand() { 021 super("time"); 022 this.description = "Changes the time on each world"; 023 this.usageMessage = "/time set <value>\n/time add <value>"; 024 this.setPermission("bukkit.command.time.add;bukkit.command.time.set"); 025 } 026 027 @Override 028 public boolean execute(CommandSender sender, String currentAlias, String[] args) { 029 if (args.length < 2) { 030 sender.sendMessage(ChatColor.RED + "Incorrect usage. Correct usage:\n" + usageMessage); 031 return false; 032 } 033 034 int value; 035 036 if (args[0].equals("set")) { 037 if (!sender.hasPermission("bukkit.command.time.set")) { 038 sender.sendMessage(ChatColor.RED + "You don't have permission to set the time"); 039 return true; 040 } 041 042 if (args[1].equals("day")) { 043 value = 0; 044 } else if (args[1].equals("night")) { 045 value = 12500; 046 } else { 047 value = getInteger(sender, args[1], 0); 048 } 049 050 for (World world : Bukkit.getWorlds()) { 051 world.setTime(value); 052 } 053 054 Command.broadcastCommandMessage(sender, "Set time to " + value); 055 } else if (args[0].equals("add")) { 056 if (!sender.hasPermission("bukkit.command.time.add")) { 057 sender.sendMessage(ChatColor.RED + "You don't have permission to set the time"); 058 return true; 059 } 060 061 value = getInteger(sender, args[1], 0); 062 063 for (World world : Bukkit.getWorlds()) { 064 world.setFullTime(world.getFullTime() + value); 065 } 066 067 Command.broadcastCommandMessage(sender, "Added " + value + " to time"); 068 } else { 069 sender.sendMessage("Unknown method. Usage: " + usageMessage); 070 } 071 072 return true; 073 } 074 075 @Override 076 public List<String> tabComplete(CommandSender sender, String alias, String[] args) { 077 Validate.notNull(sender, "Sender cannot be null"); 078 Validate.notNull(args, "Arguments cannot be null"); 079 Validate.notNull(alias, "Alias cannot be null"); 080 081 if (args.length == 1) { 082 return StringUtil.copyPartialMatches(args[0], TABCOMPLETE_ADD_SET, new ArrayList<String>(TABCOMPLETE_ADD_SET.size())); 083 } else if (args.length == 2 && args[0].equalsIgnoreCase("set")) { 084 return StringUtil.copyPartialMatches(args[1], TABCOMPLETE_DAY_NIGHT, new ArrayList<String>(TABCOMPLETE_DAY_NIGHT.size())); 085 } 086 return ImmutableList.of(); 087 } 088 }