Search in sources :

Example 6 with MCWorld

use of com.laytonsmith.abstraction.MCWorld in project CommandHelper by EngineHub.

the class ObjectGenerator method location.

/**
 * Given a Location Object, returns a MCLocation. If the optional world is not specified in the object, the world
 * provided is used instead. Location "objects" are MethodScript arrays that represent a location in game. There are
 * 4 usages: <ul> <li>(x, y, z)</li> <li>(x, y, z, world)</li> <li>(x, y, z, yaw, pitch)</li> <li>(x, y, z, world,
 * yaw, pitch)</li> </ul> In all cases, the pitch and yaw default to 0, and the world defaults to the specified
 * world. <em>More conveniently: ([world], x, y, z, [yaw, pitch])</em>
 */
public MCLocation location(Construct c, MCWorld w, Target t) {
    if (!(c instanceof CArray)) {
        throw new CREFormatException("Expecting an array, received " + c.getCType(), t);
    }
    CArray array = (CArray) c;
    MCWorld world = w;
    double x = 0;
    double y = 0;
    double z = 0;
    float yaw = 0;
    float pitch = 0;
    if (!array.inAssociativeMode()) {
        if (array.size() == 3) {
            // Just the xyz, with default yaw and pitch, and given world
            x = Static.getNumber(array.get(0, t), t);
            y = Static.getNumber(array.get(1, t), t);
            z = Static.getNumber(array.get(2, t), t);
        } else if (array.size() == 4) {
            // x, y, z, world
            x = Static.getNumber(array.get(0, t), t);
            y = Static.getNumber(array.get(1, t), t);
            z = Static.getNumber(array.get(2, t), t);
            world = Static.getServer().getWorld(array.get(3, t).val());
        } else if (array.size() == 5) {
            // x, y, z, yaw, pitch, with given world
            x = Static.getNumber(array.get(0, t), t);
            y = Static.getNumber(array.get(1, t), t);
            z = Static.getNumber(array.get(2, t), t);
            yaw = (float) Static.getNumber(array.get(3, t), t);
            pitch = (float) Static.getNumber(array.get(4, t), t);
        } else if (array.size() == 6) {
            // All have been given
            x = Static.getNumber(array.get(0, t), t);
            y = Static.getNumber(array.get(1, t), t);
            z = Static.getNumber(array.get(2, t), t);
            world = Static.getServer().getWorld(array.get(3, t).val());
            yaw = (float) Static.getNumber(array.get(4, t), t);
            pitch = (float) Static.getNumber(array.get(5, t), t);
        } else {
            throw new CREFormatException("Expecting a Location array, but the array did not meet the format specifications", t);
        }
    } else {
        if (array.containsKey("x")) {
            x = Static.getNumber(array.get("x", t), t);
        }
        if (array.containsKey("y")) {
            y = Static.getNumber(array.get("y", t), t);
        }
        if (array.containsKey("z")) {
            z = Static.getNumber(array.get("z", t), t);
        }
        if (array.containsKey("world")) {
            world = Static.getServer().getWorld(array.get("world", t).val());
        }
        if (array.containsKey("yaw")) {
            yaw = (float) Static.getDouble(array.get("yaw", t), t);
        }
        if (array.containsKey("pitch")) {
            pitch = (float) Static.getDouble(array.get("pitch", t), t);
        }
    }
    // If world is still null at this point, it's an error
    if (world == null) {
        throw new CREInvalidWorldException("The specified world doesn't exist, or no world was provided", t);
    }
    return StaticLayer.GetLocation(world, x, y, z, yaw, pitch);
}
Also used : CREInvalidWorldException(com.laytonsmith.core.exceptions.CRE.CREInvalidWorldException) CArray(com.laytonsmith.core.constructs.CArray) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) MCWorld(com.laytonsmith.abstraction.MCWorld)

Aggregations

MCWorld (com.laytonsmith.abstraction.MCWorld)6 MCLocation (com.laytonsmith.abstraction.MCLocation)4 BukkitMCWorld (com.laytonsmith.abstraction.bukkit.BukkitMCWorld)3 CArray (com.laytonsmith.core.constructs.CArray)2 MCItemStack (com.laytonsmith.abstraction.MCItemStack)1 MCPlayer (com.laytonsmith.abstraction.MCPlayer)1 MCServer (com.laytonsmith.abstraction.MCServer)1 MCBlock (com.laytonsmith.abstraction.blocks.MCBlock)1 BukkitMCLocation (com.laytonsmith.abstraction.bukkit.BukkitMCLocation)1 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)1 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)1 CREInvalidWorldException (com.laytonsmith.core.exceptions.CRE.CREInvalidWorldException)1 StaticTest (com.laytonsmith.testing.StaticTest)1 Before (org.junit.Before)1 Test (org.junit.Test)1