Exemplo n.º 1
0
    public Construct exec(Target t, Env environment, Construct... args)
        throws ConfigRuntimeException {
      MCLocation l =
          ObjectGenerator.GetGenerator()
              .location(
                  args[0],
                  environment.GetPlayer() == null ? null : environment.GetPlayer().getWorld(),
                  t);
      if (l.getBlock().isSign()) {
        String line1 = "";
        String line2 = "";
        String line3 = "";
        String line4 = "";
        if (args.length == 2 && args[1] instanceof CArray) {
          CArray ca = (CArray) args[1];
          if (ca.size() >= 1) {
            line1 = ca.get(0, t).val();
          }
          if (ca.size() >= 2) {
            line2 = ca.get(1, t).val();
          }
          if (ca.size() >= 3) {
            line3 = ca.get(2, t).val();
          }
          if (ca.size() >= 4) {
            line4 = ca.get(3, t).val();
          }

        } else {
          if (args.length >= 2) {
            line1 = args[1].val();
          }
          if (args.length >= 3) {
            line2 = args[2].val();
          }
          if (args.length >= 4) {
            line3 = args[3].val();
          }
          if (args.length >= 5) {
            line4 = args[4].val();
          }
        }
        MCSign s = l.getBlock().getSign();
        s.setLine(0, line1);
        s.setLine(1, line2);
        s.setLine(2, line3);
        s.setLine(3, line4);
        return new CVoid(t);
      } else {
        throw new ConfigRuntimeException(
            "The block at the specified location is not a sign", ExceptionType.RangeException, t);
      }
    }
Exemplo n.º 2
0
  private static CArray deepClone(CArray array, Target t, ArrayList<CArray[]> cloneRefs) {

    // Return the clone reference if this array has been cloned before (both clones will have the
    // same reference).
    for (CArray[] refCouple : cloneRefs) {
      if (refCouple[0] == array) {
        return refCouple[1];
      }
    }

    // Create the clone to put array in and add it to the cloneRefs list.
    CArray clone = new CArray(t, (int) array.size());
    clone.associative_mode = array.associative_mode;
    cloneRefs.add(new CArray[] {array, clone});

    // Iterate over the array, recursively calling this method to perform a deep clone.
    for (Construct key : array.keySet()) {
      Construct value = array.get(key, t);
      if (value instanceof CArray) {
        value = deepClone((CArray) value, t, cloneRefs);
      }
      clone.set(key, value, t);
    }
    return clone;
  }
Exemplo n.º 3
0
 public MCEntity.Velocity velocity(Construct c, Target t) {
   CArray va;
   double x, y, z, mag;
   x = y = z = mag = 0;
   if (c instanceof CArray) {
     va = (CArray) c;
     if (va.containsKey("x")) {
       x = Static.getDouble(va.get("x"), t);
     }
     if (va.containsKey("y")) {
       y = Static.getDouble(va.get("y"), t);
     }
     if (va.containsKey("z")) {
       z = Static.getDouble(va.get("z"), t);
     }
     if (!va.containsKey("x") && !va.containsKey("y") && !va.containsKey("z")) {
       switch ((int) va.size()) {
         case 4:
           z = Static.getDouble(va.get(3), t);
           y = Static.getDouble(va.get(2), t);
           x = Static.getDouble(va.get(1), t);
           break;
         case 3:
           z = Static.getDouble(va.get(2), t);
         case 2:
           y = Static.getDouble(va.get(1), t);
         case 1:
           x = Static.getDouble(va.get(0), t);
       }
     }
     return new MCEntity.Velocity(mag, x, y, z);
   } else {
     throw new Exceptions.FormatException("Expected an array but recieved " + c, t);
   }
 }
Exemplo n.º 4
0
    @Override
    public Construct exec(Target t, Environment environment, Construct... args)
        throws ConfigRuntimeException {
      if (environment.getEnv(GlobalEnv.class).GetEvent() == null) {
        throw new ConfigRuntimeException(
            "lock must be called from within an event handler", ExceptionType.BindException, t);
      }

      BoundEvent.ActiveEvent e = environment.getEnv(GlobalEnv.class).GetEvent();
      Priority p = e.getBoundEvent().getPriority();
      List<String> params = new ArrayList<String>();
      if (args.length == 0) {
        e.lock(null);
      } else {
        if (args[0] instanceof CArray) {
          CArray ca = (CArray) args[1];
          for (int i = 0; i < ca.size(); i++) {
            params.add(ca.get(i, t).val());
          }
        } else {
          for (int i = 0; i < args.length; i++) {
            params.add(args[i].val());
          }
        }
      }
      for (String param : params) {
        e.lock(param);
      }
      return CVoid.VOID;
    }
Exemplo n.º 5
0
 /**
  * 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>(x, y, z, world)
  *   <li>(x, y, z, yaw, pitch)
  *   <li>(x, y, z, world, yaw, pitch)
  * </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 ConfigRuntimeException(
         "Expecting an array, received " + c.getCType(), ExceptionType.FormatException, 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 ConfigRuntimeException(
           "Expecting a Location array, but the array did not meet the format specifications",
           ExceptionType.FormatException,
           t);
     }
   }
   if (array.containsKey("x")) {
     x = Static.getNumber(array.get("x"), t);
   }
   if (array.containsKey("y")) {
     y = Static.getNumber(array.get("y"), t);
   }
   if (array.containsKey("z")) {
     z = Static.getNumber(array.get("z"), t);
   }
   if (array.containsKey("world")) {
     world = Static.getServer().getWorld(array.get("world").val());
   }
   if (array.containsKey("yaw")) {
     yaw = (float) Static.getDouble(array.get("yaw"), t);
   }
   if (array.containsKey("pitch")) {
     pitch = (float) Static.getDouble(array.get("pitch"), t);
   }
   // If world is still null at this point, it's an error
   if (world == null) {
     throw new ConfigRuntimeException(
         "The specified world doesn't exist, or no world was provided",
         ExceptionType.InvalidWorldException,
         t);
   }
   return StaticLayer.GetLocation(world, x, y, z, yaw, pitch);
 }
Exemplo n.º 6
0
 public MCItemMeta itemMeta(Construct c, int i, Target t) {
   MCItemMeta meta =
       Static.getServer().getItemFactory().getItemMeta(StaticLayer.GetConvertor().getMaterial(i));
   if (c instanceof CNull) {
     return meta;
   }
   CArray ma = null;
   if (c instanceof CArray) {
     ma = (CArray) c;
     try {
       if (ma.containsKey("display")) {
         Construct dni = ma.get("display");
         if (!(dni instanceof CNull)) {
           meta.setDisplayName(dni.val());
         }
       }
       if (ma.containsKey("lore")) {
         Construct li = ma.get("lore");
         if (li instanceof CNull) {
           // do nothing
         } else if (li instanceof CArray) {
           CArray la = (CArray) li;
           List<String> ll = new ArrayList<String>();
           for (int j = 0; j < la.size(); j++) {
             ll.add(la.get(j).val());
           }
           meta.setLore(ll);
         } else {
           throw new Exceptions.FormatException("Lore was expected to be an array.", t);
         }
       }
       if (meta instanceof MCLeatherArmorMeta) {
         if (ma.containsKey("color")) {
           Construct ci = ma.get("color");
           if (ci instanceof CNull) {
             // nothing
           } else if (ci instanceof CArray) {
             ((MCLeatherArmorMeta) meta).setColor(color((CArray) ci, t));
           } else {
             throw new Exceptions.FormatException("Color was expected to be an array.", t);
           }
         }
       }
       if (meta instanceof MCBookMeta) {
         if (ma.containsKey("title")) {
           Construct title = ma.get("title");
           if (!(title instanceof CNull)) {
             ((MCBookMeta) meta).setTitle(title.val());
           }
         }
         if (ma.containsKey("author")) {
           Construct author = ma.get("author");
           if (!(author instanceof CNull)) {
             ((MCBookMeta) meta).setAuthor(author.val());
           }
         }
         if (ma.containsKey("pages")) {
           Construct pages = ma.get("pages");
           if (pages instanceof CNull) {
             // nothing
           } else if (pages instanceof CArray) {
             CArray pa = (CArray) pages;
             List<String> pl = new ArrayList<String>();
             for (int j = 0; j < pa.size(); j++) {
               pl.add(pa.get(j).val());
             }
             ((MCBookMeta) meta).setPages(pl);
           } else {
             throw new Exceptions.FormatException("Pages field was expected to be an array.", t);
           }
         }
       }
       if (meta instanceof MCSkullMeta) {
         if (ma.containsKey("owner")) {
           Construct owner = ma.get("owner");
           if (!(owner instanceof CNull)) {
             ((MCSkullMeta) meta).setOwner(owner.val());
           }
         }
       }
       if (meta instanceof MCEnchantmentStorageMeta) {
         if (ma.containsKey("stored")) {
           Construct stored = ma.get("stored");
           if (stored instanceof CNull) {
             // Still doing nothing
           } else if (stored instanceof CArray) {
             for (String index : ((CArray) stored).keySet()) {
               try {
                 CArray earray = (CArray) ((CArray) stored).get(index);
                 MCEnchantment etype =
                     StaticLayer.GetConvertor().GetEnchantmentByName(earray.get("etype").val());
                 int elevel = Static.getInt32(earray.get("elevel"), t);
                 ((MCEnchantmentStorageMeta) meta).addStoredEnchant(etype, elevel, true);
               } catch (Exception bade) {
                 throw new Exceptions.FormatException(
                     "Could not get enchantment data from index " + index, t);
               }
             }
           } else {
             throw new Exceptions.FormatException(
                 "Stored field was expected to be an array of Enchantment arrays", t);
           }
         }
       }
     } catch (Exception ex) {
       throw new Exceptions.FormatException(
           "Could not get ItemMeta from the given information.", t);
     }
   } else {
     throw new Exceptions.FormatException(
         "An array was expected but recieved " + c + " instead.", t);
   }
   return meta;
 }