public Construct exec(Target t, Env env, Construct... args) throws CancelCommandException, ConfigRuntimeException { double x = 0; double y = 0; double z = 0; MCWorld w = null; String world = null; if (env.GetPlayer() instanceof MCPlayer) { w = env.GetPlayer().getWorld(); } if (args.length == 1 || args.length == 2) { if (args[0] instanceof CArray) { MCLocation loc = ObjectGenerator.GetGenerator().location(args[0], w, t); x = loc.getX(); y = loc.getY(); z = loc.getZ(); world = loc.getWorld().getName(); } else { throw new ConfigRuntimeException( "get_block_at expects param 1 to be an array", ExceptionType.CastException, t); } if (args.length == 2) { world = args[1].val(); } } else if (args.length == 3 || args.length == 4) { x = Static.getDouble(args[0]); y = Static.getDouble(args[1]); z = Static.getDouble(args[2]); if (args.length == 4) { world = args[3].val(); } } if (world != null) { w = Static.getServer().getWorld(world); } if (w == null) { throw new ConfigRuntimeException( "The specified world " + world + " doesn't exist", ExceptionType.InvalidWorldException, t); } x = java.lang.Math.floor(x); y = java.lang.Math.floor(y); z = java.lang.Math.floor(z); MCBlock b = w.getBlockAt((int) x, (int) y, (int) z); return new CString(b.getTypeId() + ":" + b.getData(), t); }
public Construct exec(Target t, Env env, Construct... args) throws CancelCommandException, ConfigRuntimeException { double x = 0; double y = 0; double z = 0; MCWorld w = null; String world = null; if (env.GetPlayer() instanceof MCPlayer) { w = env.GetPlayer().getWorld(); } if (args[0] instanceof CArray && !(args.length == 3)) { MCLocation loc = ObjectGenerator.GetGenerator().location(args[0], w, t); x = loc.getX(); z = loc.getZ(); world = loc.getWorld().getName(); if (args.length == 2) { world = args[1].val(); } } else if (args.length == 2 || args.length == 3) { x = Static.getDouble(args[0]); z = Static.getDouble(args[1]); if (args.length == 3) { world = args[2].val(); } } if (world != null) { w = Static.getServer().getWorld(world); } if (w == null) { throw new ConfigRuntimeException( "The specified world " + world + " doesn't exist", ExceptionType.InvalidWorldException, t); } x = java.lang.Math.floor(x); y = java.lang.Math.floor(y) - 1; z = java.lang.Math.floor(z); MCBlock b = w.getHighestBlockAt((int) x, (int) z); return new CArray(t, new CInt(b.getX(), t), new CInt(b.getY(), t), new CInt(b.getZ(), t)); }
@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); } }