use of com.laytonsmith.core.constructs.CNull in project CommandHelper by EngineHub.
the class ObjectGenerator method itemMeta.
public Construct itemMeta(MCItemStack is, Target t) {
if (!is.hasItemMeta()) {
return CNull.NULL;
} else {
Construct display, lore;
CArray ma = CArray.GetAssociativeArray(t);
MCItemMeta meta = is.getItemMeta();
if (meta.hasDisplayName()) {
display = new CString(meta.getDisplayName(), t);
} else {
display = CNull.NULL;
}
if (meta.hasLore()) {
lore = new CArray(t);
for (String l : meta.getLore()) {
((CArray) lore).push(new CString(l, t), t);
}
} else {
lore = CNull.NULL;
}
ma.set("display", display, t);
ma.set("lore", lore, t);
ma.set("enchants", enchants(meta.getEnchants(), t), t);
ma.set("repair", new CInt(meta.getRepairCost(), t), t);
// Version specific ItemMeta
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8)) {
Set<MCItemFlag> itemFlags = meta.getItemFlags();
CArray flagArray = new CArray(t);
if (itemFlags.size() > 0) {
for (MCItemFlag flag : itemFlags) {
flagArray.push(new CString(flag.name(), t), t);
}
}
ma.set("flags", flagArray, t);
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_11)) {
ma.set("unbreakable", CBoolean.get(meta.isUnbreakable()), t);
}
}
// Specific ItemMeta
if (meta instanceof MCBlockStateMeta) {
MCBlockState bs = ((MCBlockStateMeta) meta).getBlockState();
if (bs instanceof MCShulkerBox) {
MCShulkerBox mcsb = (MCShulkerBox) bs;
MCInventory inv = mcsb.getInventory();
CArray box = CArray.GetAssociativeArray(t);
for (int i = 0; i < inv.getSize(); i++) {
Construct item = ObjectGenerator.GetGenerator().item(inv.getItem(i), t);
if (!(item instanceof CNull)) {
box.set(i, item, t);
}
}
ma.set("inventory", box, t);
} else if (bs instanceof MCBanner) {
MCBanner banner = (MCBanner) bs;
CArray patterns = new CArray(t, banner.numberOfPatterns());
for (MCPattern p : banner.getPatterns()) {
CArray pattern = CArray.GetAssociativeArray(t);
pattern.set("shape", new CString(p.getShape().toString(), t), t);
pattern.set("color", new CString(p.getColor().toString(), t), t);
patterns.push(pattern, t);
}
ma.set("patterns", patterns, t);
MCDyeColor dyeColor = banner.getBaseColor();
if (dyeColor != null) {
ma.set("basecolor", new CString(dyeColor.toString(), t), t);
}
} else if (bs instanceof MCCreatureSpawner) {
MCCreatureSpawner mccs = (MCCreatureSpawner) bs;
ma.set("spawntype", mccs.getSpawnedType().name());
}
} else if (meta instanceof MCFireworkEffectMeta) {
MCFireworkEffectMeta mcfem = (MCFireworkEffectMeta) meta;
MCFireworkEffect effect = mcfem.getEffect();
if (effect == null) {
ma.set("effect", CNull.NULL, t);
} else {
ma.set("effect", fireworkEffect(effect, t), t);
}
} else if (meta instanceof MCFireworkMeta) {
MCFireworkMeta mcfm = (MCFireworkMeta) meta;
CArray firework = CArray.GetAssociativeArray(t);
firework.set("strength", new CInt(mcfm.getStrength(), t), t);
CArray fe = new CArray(t);
for (MCFireworkEffect effect : mcfm.getEffects()) {
fe.push(fireworkEffect(effect, t), t);
}
firework.set("effects", fe, t);
ma.set("firework", firework, t);
} else if (meta instanceof MCLeatherArmorMeta) {
CArray color = color(((MCLeatherArmorMeta) meta).getColor(), t);
ma.set("color", color, t);
} else if (meta instanceof MCBookMeta) {
Construct title, author, pages;
if (((MCBookMeta) meta).hasTitle()) {
title = new CString(((MCBookMeta) meta).getTitle(), t);
} else {
title = CNull.NULL;
}
if (((MCBookMeta) meta).hasAuthor()) {
author = new CString(((MCBookMeta) meta).getAuthor(), t);
} else {
author = CNull.NULL;
}
if (((MCBookMeta) meta).hasPages()) {
pages = new CArray(t);
for (String p : ((MCBookMeta) meta).getPages()) {
((CArray) pages).push(new CString(p, t), t);
}
} else {
pages = CNull.NULL;
}
ma.set("title", title, t);
ma.set("author", author, t);
ma.set("pages", pages, t);
} else if (meta instanceof MCSkullMeta) {
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_12_X)) {
if (((MCSkullMeta) meta).hasOwner()) {
MCOfflinePlayer player = ((MCSkullMeta) meta).getOwningPlayer();
ma.set("owner", new CString(player.getName(), t), t);
ma.set("owneruuid", new CString(player.getUniqueID().toString(), t), t);
} else {
ma.set("owner", CNull.NULL, t);
ma.set("owneruuid", CNull.NULL, t);
}
} else {
if (((MCSkullMeta) meta).hasOwner()) {
ma.set("owner", new CString(((MCSkullMeta) meta).getOwner(), t), t);
} else {
ma.set("owner", CNull.NULL, t);
}
}
} else if (meta instanceof MCEnchantmentStorageMeta) {
Construct stored;
if (((MCEnchantmentStorageMeta) meta).hasStoredEnchants()) {
stored = enchants(((MCEnchantmentStorageMeta) meta).getStoredEnchants(), t);
} else {
stored = CNull.NULL;
}
ma.set("stored", stored, t);
} else if (meta instanceof MCPotionMeta) {
MCPotionMeta potionmeta = (MCPotionMeta) meta;
CArray effects = potions(potionmeta.getCustomEffects(), t);
ma.set("potions", effects, t);
if (Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_9)) {
MCPotionData potiondata = potionmeta.getBasePotionData();
if (potiondata != null) {
ma.set("base", potionData(potiondata, t), t);
}
} else if (effects.size() > 0) {
ma.set("main", ((CArray) effects.get(0, t)).get("id", t), t);
}
} else if (meta instanceof MCBannerMeta) {
MCBannerMeta bannermeta = (MCBannerMeta) meta;
CArray patterns = new CArray(t, bannermeta.numberOfPatterns());
for (MCPattern p : bannermeta.getPatterns()) {
CArray pattern = CArray.GetAssociativeArray(t);
pattern.set("shape", new CString(p.getShape().toString(), t), t);
pattern.set("color", new CString(p.getColor().toString(), t), t);
patterns.push(pattern, t);
}
ma.set("patterns", patterns, t);
MCDyeColor dyeColor = bannermeta.getBaseColor();
if (dyeColor != null) {
ma.set("basecolor", new CString(dyeColor.toString(), t), t);
}
} else if (meta instanceof MCSpawnEggMeta) {
MCEntityType spawntype = ((MCSpawnEggMeta) meta).getSpawnedType();
if (spawntype == null) {
ma.set("spawntype", CNull.NULL, t);
} else {
ma.set("spawntype", new CString(spawntype.name(), t), t);
}
} else if (meta instanceof MCMapMeta && Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_11)) {
MCColor mapcolor = ((MCMapMeta) meta).getColor();
Construct color;
if (mapcolor == null) {
color = CNull.NULL;
} else {
color = color(mapcolor, t);
}
ma.set("color", color, t);
}
return ma;
}
}
use of com.laytonsmith.core.constructs.CNull in project CommandHelper by EngineHub.
the class MethodScriptCompiler method execute.
/**
* Executes a pre-compiled MethodScript, given the specified Script environment, but also provides a method to set
* the constants in the script.
*
* @param root
* @param env
* @param done
* @param script
* @param vars
* @return
*/
public static Construct execute(ParseTree root, Environment env, MethodScriptComplete done, Script script, List<Variable> vars) {
if (root == null) {
return CVoid.VOID;
}
if (script == null) {
script = new Script(null, null, env.getEnv(GlobalEnv.class).GetLabel(), new FileOptions(new HashMap<>()));
}
if (vars != null) {
Map<String, Variable> varMap = new HashMap<>();
for (Variable v : vars) {
varMap.put(v.getVariableName(), v);
}
for (Construct tempNode : root.getAllData()) {
if (tempNode instanceof Variable) {
Variable vv = varMap.get(((Variable) tempNode).getVariableName());
if (vv != null) {
((Variable) tempNode).setVal(vv.getDefault());
} else {
// The variable is unset. I'm not quite sure what cases would cause this
((Variable) tempNode).setVal("");
}
}
}
}
StringBuilder b = new StringBuilder();
Construct returnable = null;
for (ParseTree gg : root.getChildren()) {
Construct retc = script.eval(gg, env);
if (root.numberOfChildren() == 1) {
returnable = retc;
}
String ret = retc instanceof CNull ? "null" : retc.val();
if (ret != null && !ret.trim().isEmpty()) {
b.append(ret).append(" ");
}
}
if (done != null) {
done.done(b.toString().trim());
}
if (returnable != null) {
return returnable;
}
return Static.resolveConstruct(b.toString().trim(), Target.UNKNOWN);
}
use of com.laytonsmith.core.constructs.CNull in project CommandHelper by EngineHub.
the class Static method getJavaObject.
/**
* Given a MethodScript object, returns a java object.
*
* @param construct
* @return
*/
public static Object getJavaObject(Construct construct) {
if ((construct == null) || (construct instanceof CNull)) {
return null;
} else if (construct instanceof CVoid) {
return "";
} else if (construct instanceof CBoolean) {
return ((CBoolean) construct).getBoolean();
} else if (construct instanceof CInt) {
return ((CInt) construct).getInt();
} else if (construct instanceof CDouble) {
return ((CDouble) construct).getDouble();
} else if (construct instanceof CString) {
return construct.val();
} else if (construct instanceof CByteArray) {
return ((CByteArray) construct).asByteArrayCopy();
} else if (construct instanceof CResource) {
return ((CResource) construct).getResource();
} else if (construct instanceof CArray) {
CArray array = (CArray) construct;
if (array.isAssociative()) {
HashMap<String, Object> map = new HashMap<>();
for (Construct key : array.keySet()) {
Construct c = array.get(key.val(), Target.UNKNOWN);
map.put(key.val(), (c == array) ? map : getJavaObject(c));
}
return map;
} else {
Object[] a = new Object[(int) array.size()];
boolean nullable = false;
Class<?> clazz = null;
for (int i = 0; i < array.size(); i++) {
Construct c = array.get(i, Target.UNKNOWN);
if (c == array) {
a[i] = a;
} else {
a[i] = getJavaObject(array.get(i, Target.UNKNOWN));
}
if (a[i] != null) {
if (clazz == null) {
clazz = a[i].getClass();
} else if (!clazz.equals(Object.class)) {
// to test if it is possible to return something more specific than Object[]
Class<?> cl = a[i].getClass();
while (!clazz.isAssignableFrom(cl)) {
clazz = clazz.getSuperclass();
}
}
} else {
nullable = true;
}
}
if ((clazz != null) && (!clazz.equals(Object.class))) {
if (clazz.equals(Boolean.class) && !nullable) {
boolean[] r = new boolean[a.length];
for (int i = 0; i < a.length; i++) {
r[i] = (boolean) a[i];
}
return r;
}
if (clazz.equals(Long.class) && !nullable) {
long[] r = new long[a.length];
for (int i = 0; i < a.length; i++) {
r[i] = (long) a[i];
}
return r;
} else if (clazz.equals(Double.class) && !nullable) {
double[] r = new double[a.length];
for (int i = 0; i < a.length; i++) {
r[i] = (double) a[i];
}
return r;
} else {
Object[] r = (Object[]) Array.newInstance(clazz, a.length);
System.arraycopy(a, 0, r, 0, a.length);
return r;
}
} else {
return a;
}
}
} else {
return construct;
}
}
Aggregations