001 package org.bukkit.command.defaults; 002 003 import java.util.List; 004 005 import org.bukkit.command.Command; 006 import org.bukkit.command.CommandSender; 007 008 public abstract class VanillaCommand extends Command { 009 static final int MAX_COORD = 30000000; 010 static final int MIN_COORD_MINUS_ONE = -30000001; 011 static final int MIN_COORD = -30000000; 012 013 protected VanillaCommand(String name) { 014 super(name); 015 } 016 017 protected VanillaCommand(String name, String description, String usageMessage, List<String> aliases) { 018 super(name, description, usageMessage, aliases); 019 } 020 021 public boolean matches(String input) { 022 return input.equalsIgnoreCase(this.getName()); 023 } 024 025 protected int getInteger(CommandSender sender, String value, int min) { 026 return getInteger(sender, value, min, Integer.MAX_VALUE); 027 } 028 029 int getInteger(CommandSender sender, String value, int min, int max) { 030 return getInteger(sender, value, min, max, false); 031 } 032 033 int getInteger(CommandSender sender, String value, int min, int max, boolean Throws) { 034 int i = min; 035 036 try { 037 i = Integer.valueOf(value); 038 } catch (NumberFormatException ex) { 039 if (Throws) { 040 throw new NumberFormatException(String.format("%s is not a valid number", value)); 041 } 042 } 043 044 if (i < min) { 045 i = min; 046 } else if (i > max) { 047 i = max; 048 } 049 050 return i; 051 } 052 053 Integer getInteger(String value) { 054 try { 055 return Integer.valueOf(value); 056 } catch (NumberFormatException ex) { 057 return null; 058 } 059 } 060 061 public static double getRelativeDouble(double original, CommandSender sender, String input) { 062 if (input.startsWith("~")) { 063 double value = getDouble(sender, input.substring(1)); 064 if (value == MIN_COORD_MINUS_ONE) { 065 return MIN_COORD_MINUS_ONE; 066 } 067 return original + value; 068 } else { 069 return getDouble(sender, input); 070 } 071 } 072 073 public static double getDouble(CommandSender sender, String input) { 074 try { 075 return Double.parseDouble(input); 076 } catch (NumberFormatException ex) { 077 return MIN_COORD_MINUS_ONE; 078 } 079 } 080 081 public static double getDouble(CommandSender sender, String input, double min, double max) { 082 double result = getDouble(sender, input); 083 084 // TODO: This should throw an exception instead. 085 if (result < min) { 086 result = min; 087 } else if (result > max) { 088 result = max; 089 } 090 091 return result; 092 } 093 094 String createString(String[] args, int start) { 095 return createString(args, start, " "); 096 } 097 098 String createString(String[] args, int start, String glue) { 099 StringBuilder string = new StringBuilder(); 100 101 for (int x = start; x < args.length; x++) { 102 string.append(args[x]); 103 if (x != args.length - 1) { 104 string.append(glue); 105 } 106 } 107 108 return string.toString(); 109 } 110 }