Search in sources :

Example 26 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class OptimizationUtilities method optimize0.

private static String optimize0(ParseTree node) {
    if (node.getData() instanceof CFunction) {
        StringBuilder b = new StringBuilder();
        boolean first = true;
        b.append(((CFunction) node.getData()).val()).append("(");
        for (ParseTree child : node.getChildren()) {
            if (!first) {
                b.append(",");
            }
            first = false;
            b.append(optimize0(child));
        }
        b.append(")");
        return b.toString();
    } else if (node.getData() instanceof CString) {
        // strings
        return new StringBuilder().append("'").append(node.getData().val().replaceAll("\t", "\\t").replaceAll("\n", "\\n").replace("\\", "\\\\").replace("'", "\\'")).append("'").toString();
    } else if (node.getData() instanceof IVariable) {
        return ((IVariable) node.getData()).getVariableName();
    } else if (node.getData() instanceof Variable) {
        return ((Variable) node.getData()).getVariableName();
    } else if (node.getData() instanceof CSlice) {
        return node.getData().val();
    } else if (node.getData() instanceof CArray) {
        // It's a hardcoded array. This only happens in the course of optimization, if
        // the optimizer adds a new array. We still need to handle it appropriately though.
        // The values in the array will be constant, guaranteed.
        StringBuilder b = new StringBuilder();
        b.append("array(");
        boolean first = true;
        CArray n = (CArray) node.getData();
        for (String key : n.stringKeySet()) {
            if (!first) {
                b.append(",");
            }
            first = false;
            b.append(optimize0(new ParseTree(n.get(key, Target.UNKNOWN), node.getFileOptions())));
        }
        b.append(")");
        return b.toString();
    } else {
        // static
        return node.getData().toString();
    }
}
Also used : Variable(com.laytonsmith.core.constructs.Variable) IVariable(com.laytonsmith.core.constructs.IVariable) IVariable(com.laytonsmith.core.constructs.IVariable) CSlice(com.laytonsmith.core.constructs.CSlice) CArray(com.laytonsmith.core.constructs.CArray) CFunction(com.laytonsmith.core.constructs.CFunction) CString(com.laytonsmith.core.constructs.CString) ParseTree(com.laytonsmith.core.ParseTree) CString(com.laytonsmith.core.constructs.CString)

Example 27 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class EventUtils method DumpEvents.

public static Construct DumpEvents() {
    CArray ca = new CArray(Target.UNKNOWN);
    for (SortedSet<BoundEvent> set : event_handles.values()) {
        Iterator<BoundEvent> i = set.iterator();
        while (i.hasNext()) {
            BoundEvent b = i.next();
            ca.push(new CString(b.toString() + ":" + b.getFile() + ":" + b.getLineNum(), Target.UNKNOWN), Target.UNKNOWN);
        }
    }
    return ca;
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString)

Example 28 with CArray

use of com.laytonsmith.core.constructs.CArray 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;
}
Also used : BukkitMCLivingEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCLivingEntity) BukkitMCEntity(com.laytonsmith.abstraction.bukkit.entities.BukkitMCEntity) MCLivingEntity(com.laytonsmith.abstraction.MCLivingEntity) MCEntity(com.laytonsmith.abstraction.MCEntity) CArray(com.laytonsmith.core.constructs.CArray) BukkitMCProfession(com.laytonsmith.abstraction.enums.bukkit.BukkitMCProfession) CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString) BukkitMCDyeColor(com.laytonsmith.abstraction.enums.bukkit.BukkitMCDyeColor) BukkitMCHorse(com.laytonsmith.abstraction.bukkit.entities.BukkitMCHorse) MCHorse(com.laytonsmith.abstraction.entities.MCHorse) BukkitMCHorse(com.laytonsmith.abstraction.bukkit.entities.BukkitMCHorse) MCHorse(com.laytonsmith.abstraction.entities.MCHorse) BukkitMCOcelotType(com.laytonsmith.abstraction.enums.bukkit.BukkitMCOcelotType) MaterialData(org.bukkit.material.MaterialData) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) Location(org.bukkit.Location) MCLocation(com.laytonsmith.abstraction.MCLocation)

Example 29 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class RandomTests method testStaticGetLocation.

/*@Test*/
public void testStaticGetLocation() {
    MCWorld fakeWorld = mock(MCWorld.class);
    MCServer fakeServer = mock(MCServer.class);
    when(fakeServer.getWorld("world")).thenReturn(fakeWorld);
    CommandHelperPlugin.myServer = fakeServer;
    CArray ca1 = new CArray(Target.UNKNOWN, C.onstruct(1), C.onstruct(2), C.onstruct(3));
    CArray ca2 = new CArray(Target.UNKNOWN, C.onstruct(1), C.onstruct(2), C.onstruct(3), C.onstruct("world"));
    CArray ca3 = new CArray(Target.UNKNOWN, C.onstruct(1), C.onstruct(2), C.onstruct(3), C.onstruct(45), C.onstruct(50));
    CArray ca4 = new CArray(Target.UNKNOWN, C.onstruct(1), C.onstruct(2), C.onstruct(3), C.onstruct("world"), C.onstruct(45), C.onstruct(50));
    MCLocation l1 = ObjectGenerator.GetGenerator().location(ca1, fakeWorld, Target.UNKNOWN);
    MCLocation l2 = ObjectGenerator.GetGenerator().location(ca2, fakeWorld, Target.UNKNOWN);
    MCLocation l3 = ObjectGenerator.GetGenerator().location(ca3, fakeWorld, Target.UNKNOWN);
    MCLocation l4 = ObjectGenerator.GetGenerator().location(ca4, fakeWorld, Target.UNKNOWN);
    assertEquals(fakeWorld, l1.getWorld());
    assertEquals(fakeWorld, l2.getWorld());
    assertEquals(fakeWorld, l3.getWorld());
    assertEquals(fakeWorld, l4.getWorld());
    assertEquals(1, l1.getX(), 0.00000000000000001);
    assertEquals(1, l2.getX(), 0.00000000000000001);
    assertEquals(1, l4.getX(), 0.00000000000000001);
    assertEquals(1, l4.getX(), 0.00000000000000001);
    assertEquals(2, l1.getY(), 0.00000000000000001);
    assertEquals(2, l2.getY(), 0.00000000000000001);
    assertEquals(2, l3.getY(), 0.00000000000000001);
    assertEquals(2, l4.getY(), 0.00000000000000001);
    assertEquals(3, l1.getZ(), 0.00000000000000001);
    assertEquals(3, l2.getZ(), 0.00000000000000001);
    assertEquals(3, l3.getZ(), 0.00000000000000001);
    assertEquals(3, l4.getZ(), 0.00000000000000001);
    assertEquals(0, l1.getYaw(), 0.0000000000000000001);
    assertEquals(0, l2.getYaw(), 0.0000000000000000001);
    assertEquals(45, l3.getYaw(), 0.0000000000000000001);
    assertEquals(45, l4.getYaw(), 0.0000000000000000001);
    assertEquals(0, l1.getPitch(), 0.0000000000000000001);
    assertEquals(0, l2.getPitch(), 0.0000000000000000001);
    assertEquals(50, l3.getPitch(), 0.0000000000000000001);
    assertEquals(50, l4.getPitch(), 0.0000000000000000001);
    CommandHelperPlugin.myServer = null;
}
Also used : MCLocation(com.laytonsmith.abstraction.MCLocation) CArray(com.laytonsmith.core.constructs.CArray) MCServer(com.laytonsmith.abstraction.MCServer) MCWorld(com.laytonsmith.abstraction.MCWorld)

Example 30 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class ArrayHandlingTest method testArraySize.

@Test(timeout = 10000)
public void testArraySize() throws Exception, CancelCommandException {
    ArrayHandling.array_size a = new ArrayHandling.array_size();
    CArray arr = commonArray;
    Construct ret = a.exec(Target.UNKNOWN, env, arr);
    assertReturn(ret, C.Int);
    assertCEquals(C.onstruct(3), ret);
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) Test(org.junit.Test) StaticTest(com.laytonsmith.testing.StaticTest)

Aggregations

CArray (com.laytonsmith.core.constructs.CArray)50 CString (com.laytonsmith.core.constructs.CString)27 Construct (com.laytonsmith.core.constructs.Construct)23 CInt (com.laytonsmith.core.constructs.CInt)17 CNull (com.laytonsmith.core.constructs.CNull)12 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)12 HashMap (java.util.HashMap)9 CDouble (com.laytonsmith.core.constructs.CDouble)8 IVariable (com.laytonsmith.core.constructs.IVariable)8 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 CBoolean (com.laytonsmith.core.constructs.CBoolean)5 Target (com.laytonsmith.core.constructs.Target)5 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)5 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)4 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)4 MCItemStack (com.laytonsmith.abstraction.MCItemStack)4