diff --git a/src/com/massivecraft/massivecore/collections/ExceptionSet.java b/src/com/massivecraft/massivecore/collections/ExceptionSet.java index 1f98a3b5..856606d9 100644 --- a/src/com/massivecraft/massivecore/collections/ExceptionSet.java +++ b/src/com/massivecraft/massivecore/collections/ExceptionSet.java @@ -35,6 +35,16 @@ public class ExceptionSet this.exceptions.addAll(Arrays.asList(exceptions)); } + @SafeVarargs + public ExceptionSet(boolean standard, T... exceptions) + { + this.standard = standard; + for (T exception : exceptions) + { + this.exceptions.add(convert(exception)); + } + } + // -------------------------------------------- // // CONTAINS // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/command/type/TypeColor.java b/src/com/massivecraft/massivecore/command/type/TypeColor.java new file mode 100644 index 00000000..e0e12c65 --- /dev/null +++ b/src/com/massivecraft/massivecore/command/type/TypeColor.java @@ -0,0 +1,118 @@ +package com.massivecraft.massivecore.command.type; + +import java.util.Collection; + +import org.bukkit.Color; +import org.bukkit.DyeColor; +import org.bukkit.command.CommandSender; + +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.command.type.enumeration.TypeDyeColor; +import com.massivecraft.massivecore.command.type.primitive.TypeInteger; +import com.massivecraft.massivecore.util.Txt; + +public class TypeColor extends TypeAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static TypeColor i = new TypeColor(); + public static TypeColor get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Color read(String arg, CommandSender sender) throws MassiveException + { + Color ret; + + // Try RGB + ret = readInnerRgb(arg); + if (ret != null) return ret; + + // Try hex + ret = readInnerHex(arg); + if (ret != null) return ret; + + // Try chat color + ret = readInnerDyeColor(arg); + if (ret != null) return ret; + + throw new MassiveException().addMsg("No color matches \"%s\".", arg); + } + + public Color readInnerRgb(String arg) throws MassiveException + { + String[] rgb = Txt.PATTERN_WHITESPACE.split(arg); + if (rgb.length != 3) return null; + + int red = getRgb(rgb[0]); + int green = getRgb(rgb[1]); + int blue = getRgb(rgb[2]); + + return Color.fromRGB(red, green, blue); + } + + private int getRgb(String arg) throws MassiveException + { + int ret = TypeInteger.get().read(arg); + if (ret > 255 || ret < 0) throw new MassiveException().addMsg("RGB number must be between 0 and 255."); + return ret; + } + + public Color readInnerHex(String arg) throws MassiveException + { + boolean verbose = false; + + // Explicit verbose hex + if (arg.startsWith("#")) + { + arg = arg.substring(1); + verbose = true; + } + + // Length check + if (arg.length() != 6) + { + if (verbose) throw new MassiveException().addMsg("Hex must be 6 hexadecimals."); + return null; + } + + try + { + int red = Integer.parseInt(arg.substring(0, 2), 16); + int green = Integer.parseInt(arg.substring(2, 4), 16); + int blue = Integer.parseInt(arg.substring(4, 6), 16); + + return Color.fromRGB(red, green, blue); + } + catch (IllegalArgumentException e) + { + if (verbose) throw new MassiveException().addMsg("\"%s\" is not valid hexadecimal.", arg); + return null; + } + } + + public Color readInnerDyeColor(String arg) throws MassiveException + { + try + { + DyeColor color = TypeDyeColor.get().read(arg); + return color.getColor(); + } + catch (MassiveException e) + { + return null; + } + } + + @Override + public Collection getTabList(CommandSender sender, String arg) + { + return TypeDyeColor.get().getTabList(sender, arg); + } + +} diff --git a/src/com/massivecraft/massivecore/command/type/TypeItemStack.java b/src/com/massivecraft/massivecore/command/type/TypeItemStack.java index 9df4e072..d40233b5 100644 --- a/src/com/massivecraft/massivecore/command/type/TypeItemStack.java +++ b/src/com/massivecraft/massivecore/command/type/TypeItemStack.java @@ -2,24 +2,43 @@ package com.massivecraft.massivecore.command.type; import java.util.Collection; +import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.collections.ExceptionSet; import com.massivecraft.massivecore.mson.Mson; import com.massivecraft.massivecore.util.InventoryUtil; import com.massivecraft.massivecore.util.Txt; public class TypeItemStack extends TypeAbstract { + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final ExceptionSet materialsAllowed; + // -------------------------------------------- // // INSTANCE & CONSTRUCT // -------------------------------------------- // - private static TypeItemStack i = new TypeItemStack(); + private static TypeItemStack i = new TypeItemStack(new ExceptionSet(true)); public static TypeItemStack get() { return i; } + public static TypeItemStack get(Material... materialWhitelist) + { + ExceptionSet materialsAllowed = new ExceptionSet<>(false, materialWhitelist); + return new TypeItemStack(materialsAllowed); + } + + public TypeItemStack(ExceptionSet materialsAllowed) + { + this.materialsAllowed = materialsAllowed; + } + // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // @@ -57,6 +76,10 @@ public class TypeItemStack extends TypeAbstract ItemStack ret = player.getItemInHand(); if (InventoryUtil.isNothing(ret)) throw new MassiveException().addMsg("You must hold an item in your hand."); + + Material material = ret.getType(); + if ( ! this.materialsAllowed.contains(material)) throw new MassiveException().addMsg("%s is not allowed.", Txt.getNicedEnum(material)); + ret = new ItemStack(ret); return ret; } diff --git a/src/com/massivecraft/massivecore/command/type/enumeration/TypeDyeColor.java b/src/com/massivecraft/massivecore/command/type/enumeration/TypeDyeColor.java index e5dc9882..259eef37 100644 --- a/src/com/massivecraft/massivecore/command/type/enumeration/TypeDyeColor.java +++ b/src/com/massivecraft/massivecore/command/type/enumeration/TypeDyeColor.java @@ -24,10 +24,9 @@ public class TypeDyeColor extends TypeEnum // -------------------------------------------- // @Override - public String getVisualInner(DyeColor value, CommandSender sender) + public ChatColor getVisualColor(DyeColor value, CommandSender sender) { - ChatColor color = MUtil.getChatColor(value); - return color + super.getNameInner(value); + return MUtil.getChatColor(value); } }