2.5h TypeColor

This commit is contained in:
BuildTools 2016-03-19 09:36:24 +01:00 committed by Olof Larsson
parent 94bc49b7d3
commit 5082f535e4
4 changed files with 154 additions and 4 deletions

View File

@ -35,6 +35,16 @@ public class ExceptionSet<T>
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
// -------------------------------------------- //

View File

@ -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<Color>
{
// -------------------------------------------- //
// 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("<b>No color matches \"<h>%s<b>\".", 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("<b>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("<b>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("<b>\"<h>%s<b>\" 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<String> getTabList(CommandSender sender, String arg)
{
return TypeDyeColor.get().getTabList(sender, arg);
}
}

View File

@ -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<ItemStack>
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final ExceptionSet<Material> materialsAllowed;
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static TypeItemStack i = new TypeItemStack();
private static TypeItemStack i = new TypeItemStack(new ExceptionSet<Material>(true));
public static TypeItemStack get() { return i; }
public static TypeItemStack get(Material... materialWhitelist)
{
ExceptionSet<Material> materialsAllowed = new ExceptionSet<>(false, materialWhitelist);
return new TypeItemStack(materialsAllowed);
}
public TypeItemStack(ExceptionSet<Material> materialsAllowed)
{
this.materialsAllowed = materialsAllowed;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@ -57,6 +76,10 @@ public class TypeItemStack extends TypeAbstract<ItemStack>
ItemStack ret = player.getItemInHand();
if (InventoryUtil.isNothing(ret)) throw new MassiveException().addMsg("<b>You must hold an item in your hand.");
Material material = ret.getType();
if ( ! this.materialsAllowed.contains(material)) throw new MassiveException().addMsg("<h>%s <b>is not allowed.", Txt.getNicedEnum(material));
ret = new ItemStack(ret);
return ret;
}

View File

@ -24,10 +24,9 @@ public class TypeDyeColor extends TypeEnum<DyeColor>
// -------------------------------------------- //
@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);
}
}