Example #1
0
 public Construct exec(Target t, Env environment, Construct... args)
     throws ConfigRuntimeException {
   int x;
   int z;
   MCWorld w;
   if (args.length == 2) {
     MCWorld defaultWorld =
         environment.GetPlayer() == null ? null : environment.GetPlayer().getWorld();
     MCLocation l = ObjectGenerator.GetGenerator().location(args[0], defaultWorld, t);
     x = l.getBlockX();
     z = l.getBlockZ();
     w = l.getWorld();
   } else {
     x = (int) Static.getInt(args[0]);
     z = (int) Static.getInt(args[1]);
     if (args.length == 3) {
       w = environment.GetPlayer().getWorld();
     } else {
       w = Static.getServer().getWorld(args[2].val());
     }
   }
   MCBiomeType bt;
   try {
     bt = MCBiomeType.valueOf(args[args.length - 1].val());
   } catch (IllegalArgumentException e) {
     throw new ConfigRuntimeException(
         "The biome type \"" + args[1].val() + "\" does not exist.",
         ExceptionType.FormatException,
         t);
   }
   w.setBiome(x, z, bt);
   return new CVoid(t);
 }
Example #2
0
 @Test(timeout = 10000)
 public void testRand() {
   Math.rand a = new Math.rand();
   for (int i = 0; i < 1000; i++) {
     long j = Static.getInt(a.exec(Target.UNKNOWN, env, C.onstruct(10)));
     if (!(j < 10 && j >= 0)) {
       fail("Expected a number between 0 and 10, but got " + j);
     }
     j = Static.getInt(a.exec(Target.UNKNOWN, env, C.onstruct(10), C.onstruct(20)));
     if (!(j < 20 && j >= 10)) {
       fail("Expected a number between 10 and 20, but got " + j);
     }
   }
   try {
     a.exec(Target.UNKNOWN, env, C.onstruct(20), C.onstruct(10));
     fail("Didn't expect this test to pass");
   } catch (ConfigRuntimeException e) {
   }
   try {
     a.exec(Target.UNKNOWN, env, C.onstruct(-1));
     fail("Didn't expect this test to pass");
   } catch (ConfigRuntimeException e) {
   }
   try {
     a.exec(Target.UNKNOWN, env, C.onstruct(87357983597853791L));
     fail("Didn't expect this test to pass");
   } catch (ConfigRuntimeException e) {
   }
 }
Example #3
0
 public Construct exec(Target t, Env environment, Construct... args)
     throws ConfigRuntimeException {
   int x;
   int z;
   MCWorld w;
   if (args.length == 1) {
     MCWorld defaultWorld =
         environment.GetPlayer() == null ? null : environment.GetPlayer().getWorld();
     MCLocation l = ObjectGenerator.GetGenerator().location(args[0], defaultWorld, t);
     x = l.getBlockX();
     z = l.getBlockZ();
     w = l.getWorld();
   } else {
     x = (int) Static.getInt(args[0]);
     z = (int) Static.getInt(args[1]);
     if (args.length == 2) {
       w = environment.GetPlayer().getWorld();
     } else {
       w = Static.getServer().getWorld(args[2].val());
     }
   }
   MCBiomeType bt = w.getBiome(x, z);
   return new CString(bt.name(), t);
 }
Example #4
0
 @Override
 public Construct exec(Target t, Environment environment, Construct... args)
     throws ConfigRuntimeException {
   try {
     Profiles.Profile profile;
     if (args[0] instanceof CArray) {
       Map<String, String> data = new HashMap<String, String>();
       for (String key : ((CArray) args[0]).keySet()) {
         data.put(key, ((CArray) args[0]).get(key).val());
       }
       profile = Profiles.getProfile(data);
     } else {
       Profiles profiles = environment.getEnv(GlobalEnv.class).getSQLProfiles();
       profile = profiles.getProfileById(args[0].val());
     }
     String query = args[1].val();
     Construct[] params = new Construct[args.length - 2];
     for (int i = 2; i < args.length; i++) {
       int index = i - 2;
       params[index] = args[i];
     }
     // Parameters are now all parsed into java objects.
     Connection conn = DriverManager.getConnection(profile.getConnectionString());
     PreparedStatement ps = null;
     try {
       ps = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
       for (int i = 0; i < params.length; i++) {
         int type = ps.getParameterMetaData().getParameterType(i + 1);
         if (params[i] == null) {
           if (ps.getParameterMetaData().isNullable(i + 1)
               == ParameterMetaData.parameterNoNulls) {
             throw new ConfigRuntimeException(
                 "Parameter "
                     + (i + 1)
                     + " cannot be set to null. Check your parameters and try again.",
                 ExceptionType.SQLException,
                 t);
           } else {
             ps.setNull(i + 1, type);
             continue;
           }
         }
         try {
           if (params[i] instanceof CInt) {
             ps.setLong(i + 1, Static.getInt(params[i], t));
           } else if (params[i] instanceof CDouble) {
             ps.setDouble(i + 1, (Double) Static.getDouble(params[i], t));
           } else if (params[i] instanceof CString) {
             ps.setString(i + 1, (String) params[i].val());
           } else if (params[i] instanceof CByteArray) {
             ps.setBytes(i + 1, ((CByteArray) params[i]).asByteArrayCopy());
           } else if (params[i] instanceof CBoolean) {
             ps.setBoolean(i + 1, Static.getBoolean(params[i]));
           } else {
             throw new ConfigRuntimeException(
                 "The type "
                     + params[i].getClass().getSimpleName()
                     + " of parameter "
                     + (i + 1)
                     + " is not supported.",
                 ExceptionType.CastException,
                 t);
           }
         } catch (ClassCastException ex) {
           throw new ConfigRuntimeException(
               "Could not cast parameter "
                   + (i + 1)
                   + " to "
                   + ps.getParameterMetaData().getParameterTypeName(i + 1)
                   + " from "
                   + params[i].getClass().getSimpleName()
                   + ".",
               ExceptionType.CastException,
               t,
               ex);
         }
       }
       boolean isResultSet = ps.execute();
       if (isResultSet) {
         // Result set
         CArray ret = new CArray(t);
         ResultSetMetaData md = ps.getMetaData();
         ResultSet rs = ps.getResultSet();
         while (rs.next()) {
           CArray row = new CArray(t);
           for (int i = 1; i <= md.getColumnCount(); i++) {
             Construct value;
             int columnType = md.getColumnType(i);
             if (columnType == Types.INTEGER
                 || columnType == Types.TINYINT
                 || columnType == Types.SMALLINT
                 || columnType == Types.BIGINT) {
               value = new CInt(rs.getLong(i), t);
             } else if (columnType == Types.FLOAT
                 || columnType == Types.DOUBLE
                 || columnType == Types.REAL
                 || columnType == Types.DECIMAL
                 || columnType == Types.NUMERIC) {
               value = new CDouble(rs.getDouble(i), t);
             } else if (columnType == Types.VARCHAR
                 || columnType == Types.CHAR
                 || columnType == Types.LONGVARCHAR) {
               value = new CString(rs.getString(i), t);
             } else if (columnType == Types.BLOB
                 || columnType == Types.BINARY
                 || columnType == Types.VARBINARY
                 || columnType == Types.LONGVARBINARY) {
               value = CByteArray.wrap(rs.getBytes(i), t);
             } else if (columnType == Types.DATE
                 || columnType == Types.TIME
                 || columnType == Types.TIMESTAMP) {
               if (md.getColumnTypeName(i).equals("YEAR")) {
                 value = new CInt(rs.getLong(i), t);
               } else {
                 value = new CInt(rs.getTimestamp(i).getTime(), t);
               }
             } else if (columnType == Types.BOOLEAN || columnType == Types.BIT) {
               value = new CBoolean(rs.getBoolean(i), t);
             } else {
               throw new ConfigRuntimeException(
                   "SQL returned a unhandled column type "
                       + md.getColumnTypeName(i)
                       + " for column "
                       + md.getColumnName(i)
                       + ".",
                   ExceptionType.CastException,
                   t);
             }
             row.set(md.getColumnName(i), value, t);
           }
           ret.push(row);
         }
         return ret;
       } else {
         ResultSet rs = ps.getGeneratedKeys();
         if (rs.next()) {
           // This was an insert or something that returned generated keys. So we return
           // that here.
           return new CInt(rs.getInt(1), t);
         }
         // Update count. Just return null.
         return new CNull(t);
       }
     } finally {
       if (ps != null) {
         ps.close();
       }
       if (conn != null) {
         conn.close();
       }
     }
   } catch (Profiles.InvalidProfileException ex) {
     throw new ConfigRuntimeException(ex.getMessage(), ExceptionType.SQLException, t, ex);
   } catch (SQLException ex) {
     throw new ConfigRuntimeException(ex.getMessage(), ExceptionType.SQLException, t, ex);
   }
 }