use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.
the class CArray method sort.
public void sort(final SortType sort) {
if (this.associative_mode) {
array = new ArrayList(associative_array.values());
this.associative_array.clear();
this.associative_array = null;
this.associative_mode = false;
CHLog.GetLogger().Log(CHLog.Tags.GENERAL, LogLevel.VERBOSE, "Attempting to sort an associative array; key values will be lost.", this.getTarget());
}
array.sort(new Comparator<Construct>() {
@Override
public int compare(Construct o1, Construct o2) {
// o1 > o2 -> 1
for (int i = 0; i < 2; i++) {
Construct c;
if (i == 0) {
c = o1;
} else {
c = o2;
}
if (c instanceof CArray) {
throw new CRECastException("Cannot sort an array of arrays.", CArray.this.getTarget());
}
if (!(c instanceof CBoolean || c instanceof CString || c instanceof CInt || c instanceof CDouble || c instanceof CNull)) {
throw new CREFormatException("Unsupported type being sorted: " + c.getCType(), CArray.this.getTarget());
}
}
if (o1 instanceof CNull || o2 instanceof CNull) {
if (o1 instanceof CNull && o2 instanceof CNull) {
return 0;
} else if (o1 instanceof CNull) {
return "".compareTo(o2.getValue());
} else {
return o1.val().compareTo("");
}
}
if (o1 instanceof CBoolean || o2 instanceof CBoolean) {
if (Static.getBoolean(o1, Target.UNKNOWN) == Static.getBoolean(o2, Target.UNKNOWN)) {
return 0;
} else {
int oo1 = Static.getBoolean(o1, Target.UNKNOWN) ? 1 : 0;
int oo2 = Static.getBoolean(o2, Target.UNKNOWN) ? 1 : 0;
return (oo1 < oo2) ? -1 : 1;
}
}
// At this point, things will either be numbers or strings
switch(sort) {
case REGULAR:
return compareRegular(o1, o2);
case NUMERIC:
return compareNumeric(o1, o2);
case STRING:
return compareString(o1.val(), o2.val());
case STRING_IC:
return compareString(o1.val().toLowerCase(), o2.val().toLowerCase());
}
throw ConfigRuntimeException.CreateUncatchableException("Missing implementation for " + sort.name(), Target.UNKNOWN);
}
public int compareRegular(Construct o1, Construct o2) {
if (Static.getBoolean(new DataHandling.is_numeric().exec(Target.UNKNOWN, null, o1), Target.UNKNOWN) && Static.getBoolean(new DataHandling.is_numeric().exec(Target.UNKNOWN, null, o2), Target.UNKNOWN)) {
return compareNumeric(o1, o2);
} else if (Static.getBoolean(new DataHandling.is_numeric().exec(Target.UNKNOWN, null, o1), Target.UNKNOWN)) {
// The first is a number, the second is a string
return -1;
} else if (Static.getBoolean(new DataHandling.is_numeric().exec(Target.UNKNOWN, null, o2), Target.UNKNOWN)) {
// The second is a number, the first is a string
return 1;
} else {
// They are both strings
return compareString(o1.val(), o2.val());
}
}
public int compareNumeric(Construct o1, Construct o2) {
double d1 = Static.getNumber(o1, o1.getTarget());
double d2 = Static.getNumber(o2, o2.getTarget());
return Double.compare(d1, d2);
}
public int compareString(String o1, String o2) {
return o1.compareTo(o2);
}
});
this.setDirty();
}
use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.
the class AbstractEvent method execute.
/**
* This function is run when the actual event occurs.
*
* @param tree The compiled parse tree
* @param b The bound event
* @param env The operating environment
* @param activeEvent The active event being executed
*/
@Override
public final void execute(ParseTree tree, BoundEvent b, Environment env, BoundEvent.ActiveEvent activeEvent) throws ConfigRuntimeException {
preExecution(env, activeEvent);
// Various events have a player to put into the env.
// Do this after preExcecution() in case the particular event needs to inject the player first.
Construct c = activeEvent.getParsedEvent().get("player");
if (c != null) {
try {
MCPlayer p = Static.GetPlayer(c, Target.UNKNOWN);
env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
} catch (CREPlayerOfflineException e) {
// Set env CommandSender to prevent incorrect inherited player from being used in a player event.
if (env.getEnv(CommandHelperEnvironment.class).GetPlayer() != null) {
env.getEnv(CommandHelperEnvironment.class).SetCommandSender(Static.getServer().getConsole());
}
}
}
ProfilePoint event = null;
if (env.getEnv(GlobalEnv.class).GetProfiler() != null) {
event = env.getEnv(GlobalEnv.class).GetProfiler().start("Event " + b.getEventName() + " (defined at " + b.getTarget().toString() + ")", LogLevel.ERROR);
}
try {
try {
// Get the label from the bind time environment, and put it in the current environment.
String label = b.getEnvironment().getEnv(GlobalEnv.class).GetLabel();
if (label == null) {
// Set the permission to global if it's null, since that means
// it wasn't set, and so we aren't in a secured environment anyways.
label = Static.GLOBAL_PERMISSION;
}
env.getEnv(GlobalEnv.class).SetLabel(label);
MethodScriptCompiler.execute(tree, env, null, null);
} catch (CancelCommandException ex) {
if (ex.getMessage() != null && !ex.getMessage().isEmpty()) {
StreamUtils.GetSystemOut().println(ex.getMessage());
}
} catch (FunctionReturnException ex) {
// We simply allow this to end the event execution
} catch (ProgramFlowManipulationException ex) {
ConfigRuntimeException.HandleUncaughtException(new CREFormatException("Unexpected control flow operation used.", ex.getTarget()), env);
}
} finally {
if (event != null) {
event.stop();
}
// Finally, among other things, we need to clean-up injected players and entities
postExecution(env, activeEvent);
}
}
use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.
the class BukkitMCWorld method spawnMob.
@Override
public CArray spawnMob(MCMobs name, String subClass, int qty, MCLocation l, Target t) {
Class mobType = null;
CArray ids = new CArray(t);
Location location = (Location) l.getHandle();
MCVersion version = Static.getServer().getMinecraftVersion();
String[] subTypes = subClass.toUpperCase().split("-");
try {
switch(name) {
case BAT:
mobType = Bat.class;
break;
case BLAZE:
mobType = Blaze.class;
break;
case CAVESPIDER:
mobType = CaveSpider.class;
break;
case CHICKEN:
mobType = Chicken.class;
break;
case COW:
mobType = Cow.class;
break;
case CREEPER:
mobType = Creeper.class;
break;
case ELDERGUARDIAN:
mobType = ElderGuardian.class;
break;
case ENDERDRAGON:
mobType = EnderDragon.class;
break;
case ENDERMAN:
mobType = Enderman.class;
break;
case ENDERMITE:
mobType = Endermite.class;
break;
case EVOKER:
mobType = Evoker.class;
break;
case GHAST:
mobType = Ghast.class;
break;
case GUARDIAN:
mobType = Guardian.class;
break;
case GIANT:
mobType = Giant.class;
break;
case HORSE:
mobType = Horse.class;
if (!(subClass.isEmpty()) && version.gte(MCVersion.MC1_11)) {
for (String type : subTypes) {
try {
MCHorse.MCHorseVariant htype = MCHorse.MCHorseVariant.valueOf(type);
switch(htype) {
case DONKEY:
mobType = Donkey.class;
break;
case MULE:
mobType = Mule.class;
break;
case SKELETON:
mobType = SkeletonHorse.class;
break;
case ZOMBIE:
mobType = ZombieHorse.class;
break;
}
subClass = "";
break;
} catch (IllegalArgumentException notVar) {
// not variant
}
}
}
break;
case ILLUSIONER:
mobType = Illusioner.class;
break;
case IRONGOLEM:
mobType = IronGolem.class;
break;
case LLAMA:
mobType = Llama.class;
break;
case MAGMACUBE:
mobType = MagmaCube.class;
break;
case MOOSHROOM:
mobType = MushroomCow.class;
break;
case OCELOT:
mobType = Ocelot.class;
break;
case PARROT:
mobType = Parrot.class;
break;
case PIG:
mobType = Pig.class;
break;
case PIGZOMBIE:
mobType = PigZombie.class;
break;
case POLARBEAR:
mobType = PolarBear.class;
break;
case RABBIT:
mobType = Rabbit.class;
break;
case SHEEP:
mobType = Sheep.class;
break;
case SHULKER:
mobType = Shulker.class;
break;
case SILVERFISH:
mobType = Silverfish.class;
break;
case SKELETON:
mobType = Skeleton.class;
if (!(subClass.isEmpty()) && version.gte(MCVersion.MC1_11)) {
MCSkeletonType stype = MCSkeletonType.NORMAL;
for (String type : subTypes) {
try {
stype = MCSkeletonType.valueOf(type);
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a skeleton type", t);
}
}
if (stype == MCSkeletonType.WITHER) {
mobType = WitherSkeleton.class;
} else if (stype == MCSkeletonType.STRAY) {
mobType = Stray.class;
}
subClass = "";
}
break;
case SLIME:
mobType = Slime.class;
break;
case SNOWGOLEM:
mobType = Snowman.class;
break;
case SPIDER:
mobType = Spider.class;
break;
case SPIDERJOCKEY:
mobType = Spider.class;
break;
case SQUID:
mobType = Squid.class;
break;
case VEX:
mobType = Vex.class;
break;
case VILLAGER:
mobType = Villager.class;
break;
case VINDICATOR:
mobType = Vindicator.class;
break;
case WITCH:
mobType = Witch.class;
break;
case WITHER:
mobType = Wither.class;
break;
case WOLF:
mobType = Wolf.class;
break;
case ZOMBIE:
mobType = Zombie.class;
if (!subClass.isEmpty() && version.gte(MCVersion.MC1_11)) {
for (int i = 0; i < subTypes.length; i++) {
try {
MCZombieType ztype = MCZombieType.valueOf(subTypes[i]);
switch(ztype) {
case HUSK:
mobType = Husk.class;
case BABY:
continue;
case VILLAGER_BLACKSMITH:
subTypes[i] = "BLACKSMITH";
break;
case VILLAGER_BUTCHER:
subTypes[i] = "BUTCHER";
break;
case VILLAGER_LIBRARIAN:
subTypes[i] = "LIBRARIAN";
break;
case VILLAGER_PRIEST:
subTypes[i] = "PRIEST";
break;
case VILLAGER:
subTypes[i] = "FARMER";
break;
}
mobType = ZombieVillager.class;
} catch (IllegalArgumentException ex) {
// not a ZombieType
}
}
}
break;
}
} catch (NoClassDefFoundError e) {
throw new CREFormatException("No mob of type " + name + " exists", t);
}
for (int i = 0; i < qty; i++) {
Entity e = w.spawn(location, mobType);
if (name == MCMobs.SPIDERJOCKEY) {
e.setPassenger(w.spawn(location, Skeleton.class));
}
if (!subClass.isEmpty()) {
// if subClass is blank, none of this needs to run at all
if (e instanceof Sheep) {
Sheep s = (Sheep) e;
MCDyeColor color;
for (String type : subTypes) {
try {
color = MCDyeColor.valueOf(type);
s.setColor(BukkitMCDyeColor.getConvertor().getConcreteEnum(color));
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a valid color", t);
}
}
} else if (e instanceof Ocelot) {
Ocelot o = (Ocelot) e;
MCOcelotType otype;
for (String type : subTypes) {
try {
otype = MCOcelotType.valueOf(type);
o.setCatType(BukkitMCOcelotType.getConvertor().getConcreteEnum(otype));
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not an ocelot type", t);
}
}
} else if (e instanceof Creeper) {
Creeper c = (Creeper) e;
for (String type : subTypes) {
try {
MCCreeperType ctype = MCCreeperType.valueOf(type);
switch(ctype) {
case POWERED:
c.setPowered(true);
break;
default:
break;
}
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a creeper state", t);
}
}
} else if (e instanceof Wolf) {
Wolf w = (Wolf) e;
for (String type : subTypes) {
try {
MCWolfType wtype = MCWolfType.valueOf(type);
switch(wtype) {
case ANGRY:
w.setAngry(true);
break;
case TAMED:
w.setTamed(true);
break;
default:
break;
}
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a wolf state", t);
}
}
} else if (e instanceof Villager) {
Villager v = (Villager) e;
MCProfession job;
for (String type : subTypes) {
try {
job = MCProfession.valueOf(type);
v.setProfession(BukkitMCProfession.getConvertor().getConcreteEnum(job));
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a valid profession", t);
}
}
} else if (e instanceof Enderman) {
Enderman en = (Enderman) e;
for (String type : subTypes) {
try {
MaterialData held = new MaterialData(Material.valueOf(type));
en.setCarriedMaterial(held);
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a valid material", t);
}
}
} else if (e instanceof Slime) {
Slime sl = (Slime) e;
for (String type : subTypes) {
if (!"".equals(type)) {
try {
sl.setSize(Integer.parseInt(type));
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a valid size", t);
}
}
}
} else if (e instanceof Skeleton) {
Skeleton sk = (Skeleton) e;
for (String type : subTypes) {
try {
sk.setSkeletonType(Skeleton.SkeletonType.valueOf(type));
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a skeleton type", t);
}
}
} else if (e instanceof Zombie) {
if (e instanceof PigZombie) {
PigZombie pz = (PigZombie) e;
for (String value : subTypes) {
if (value.equals("BABY")) {
pz.setBaby(true);
continue;
}
try {
pz.setAnger(Integer.valueOf(value));
} catch (IllegalArgumentException iae) {
throw new CREFormatException(value + " is not a number.", t);
}
}
} else if (version.gte(MCVersion.MC1_11) && e instanceof ZombieVillager) {
ZombieVillager zv = (ZombieVillager) e;
for (String type : subTypes) {
if (type.equals("BABY")) {
zv.setBaby(true);
continue;
}
try {
MCProfession job = MCProfession.valueOf(type);
zv.setVillagerProfession(BukkitMCProfession.getConvertor().getConcreteEnum(job));
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a valid profession", t);
}
}
} else {
Zombie z = (Zombie) e;
for (String type : subTypes) {
try {
MCZombieType ztype = MCZombieType.valueOf(type);
switch(ztype) {
case BABY:
z.setBaby(true);
break;
case VILLAGER:
z.setVillager(true);
break;
case VILLAGER_BLACKSMITH:
case VILLAGER_BUTCHER:
case VILLAGER_LIBRARIAN:
case VILLAGER_PRIEST:
if (version.gte(MCVersion.MC1_9)) {
// < MC 1.11
z.setVillagerProfession(Villager.Profession.valueOf(type.substring(9).toUpperCase()));
} else {
z.setVillager(true);
}
break;
case HUSK:
if (version.gte(MCVersion.MC1_10) && version.lt(MCVersion.MC1_11)) {
z.setVillagerProfession(Villager.Profession.HUSK);
}
break;
}
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a zombie state", t);
}
}
}
} else if (e instanceof Pig) {
Pig p = (Pig) e;
for (String type : subTypes) {
try {
MCPigType ptype = MCPigType.valueOf(type);
switch(ptype) {
case SADDLED:
p.setSaddle(true);
break;
default:
break;
}
} catch (IllegalArgumentException ex) {
throw new CREFormatException(type + " is not a pig state", t);
}
}
} else if (e instanceof Horse) {
Horse h = (Horse) e;
for (String type : subTypes) {
if (version.lt(MCVersion.MC1_11)) {
try {
MCHorse.MCHorseVariant htype = MCHorse.MCHorseVariant.valueOf(type);
h.setVariant(BukkitMCHorse.BukkitMCHorseVariant.getConvertor().getConcreteEnum(htype));
// no other variants can have colors or patterns
break;
} catch (IllegalArgumentException ex) {
// not variant
}
}
try {
MCHorse.MCHorseColor hcolor = MCHorse.MCHorseColor.valueOf(type);
h.setColor(BukkitMCHorse.BukkitMCHorseColor.getConvertor().getConcreteEnum(hcolor));
continue;
} catch (IllegalArgumentException ex) {
// not color
}
try {
MCHorse.MCHorsePattern hpattern = MCHorse.MCHorsePattern.valueOf(type);
h.setStyle(BukkitMCHorse.BukkitMCHorsePattern.getConvertor().getConcreteEnum(hpattern));
} catch (IllegalArgumentException notAnything) {
throw new CREFormatException("Type " + type + " did not match any horse variants," + " colors, or patterns.", t);
}
}
}
}
ids.push(new CString(e.getUniqueId().toString(), t), t);
}
return ids;
}
use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.
the class InventoryManagement method GetInventory.
// @api
// public static class pinv_consolidate extends AbstractFunction {
//
// public String getName() {
// return "pinv_consolidate";
// }
//
// public Integer[] numArgs() {
// return new Integer[]{0, 1};
// }
//
// public String docs() {
// return "void {[player]} Consolidates a player's inventory as much as possible."
// + " There is no guarantee anything will happen after this function"
// + " is called, and there is no way to specify details about how"
// + " consolidation occurs, however, the following heuristics are followed:"
// + " The hotbar items will not be moved from the hotbar, unless there are"
// + " two+ slots that have the same item. Items in the main inventory area"
// + " will be moved closer to the bottom of the main inventory. No empty slots"
// + " will be filled in the hotbar.";
// }
//
// public Class<? extends CREThrowable>[] thrown() {
// return new Class[]{};
// }
//
// public boolean isRestricted() {
// return true;
// }
//
// public boolean preResolveVariables() {
// return true;
// }
//
// public Boolean runAsync() {
// return false;
// }
//
// public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
// MCPlayer p = environment.GetPlayer();
// if(args.length == 1){
// p = Static.GetPlayer(args[0], t);
// }
// //First, we need to address the hotbar
// for(int i = 0; i < 10; i++){
// //If the stack size is maxed out, we're done.
// }
//
// return CVoid.VOID;
// }
//
// public CHVersion since() {
// return CHVersion.V3_3_1;
// }
// }
private static MCInventory GetInventory(Construct specifier, MCWorld w, Target t) {
MCInventory inv;
if (specifier instanceof CArray) {
MCLocation l = ObjectGenerator.GetGenerator().location(specifier, w, t);
inv = StaticLayer.GetConvertor().GetLocationInventory(l);
} else {
MCEntity entity = Static.getEntity(specifier, t);
inv = StaticLayer.GetConvertor().GetEntityInventory(entity);
}
if (inv == null) {
throw new CREFormatException("The entity or location specified is not capable of having an inventory.", t);
} else {
return inv;
}
}
use of com.laytonsmith.core.exceptions.CRE.CREFormatException in project CommandHelper by EngineHub.
the class Regex method getPattern.
private static Pattern getPattern(Construct c, Target t) throws ConfigRuntimeException {
String regex = "";
int flags = 0;
String sflags = "";
if (c instanceof CArray) {
CArray ca = (CArray) c;
regex = ca.get(0, t).val();
sflags = ca.get(1, t).val();
for (int i = 0; i < sflags.length(); i++) {
if (sflags.toLowerCase().charAt(i) == 'i') {
flags |= java.util.regex.Pattern.CASE_INSENSITIVE;
} else if (sflags.toLowerCase().charAt(i) == 'm') {
flags |= java.util.regex.Pattern.MULTILINE;
} else if (sflags.toLowerCase().charAt(i) == 's') {
flags |= java.util.regex.Pattern.DOTALL;
} else {
throw new CREFormatException("Unrecognized flag: " + sflags.toLowerCase().charAt(i), t);
}
}
} else {
regex = c.val();
}
try {
return Pattern.compile(regex, flags);
} catch (PatternSyntaxException e) {
throw new CREFormatException(e.getMessage(), t);
}
}
Aggregations