コード例 #1
0
  @Override
  public void run() {
    HttpURLConnection connection = null;
    String response = "";
    try {
      URL url = new URL(URL);
      connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection.setRequestProperty(
          "Content-Length", Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");
      connection.setUseCaches(false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      DataOutputStream e = new DataOutputStream(connection.getOutputStream());
      e.writeBytes(urlParameters);
      e.flush();
      e.close();
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      StringBuffer responseBuffer = new StringBuffer();
      String line;
      while ((line = rd.readLine()) != null) {
        responseBuffer.append(line);
        responseBuffer.append('\r');
      }
      rd.close();
      response = responseBuffer.toString();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (connection != null) {
        connection.disconnect();
      }
    }

    if (VanillaPlugin.getInstance().getEngine().debugMode()) {
      Spout.getLogger().info("Authentification: " + response);
    }
    if (response.contains(":")) {
      String[] infos = response.split(":");
      VanillaPlugin.getInstance().setClientAuthInfos(infos[2], infos[3]);
    } else {
      Spout.getLogger().info("Authentification failed: " + response);
    }
  }
コード例 #2
0
 @Override
 public void onTick(float dt) {
   final Random random = GenericMath.getRandom();
   float secondsUntilWeatherChange = sky.getData().get(VanillaData.WEATHER_CHANGE_TIME);
   secondsUntilWeatherChange -= dt;
   if (forceWeatherUpdate.compareAndSet(true, false) || secondsUntilWeatherChange <= 0) {
     this.sky.updateWeather(getCurrent(), getForecast());
     sky.getData().put(VanillaData.WORLD_WEATHER, getForecast());
     final Weather current = getCurrent();
     Weather forecast = current;
     while (forecast == current) {
       // When Rain/Snow or Thunderstorms occur, always go to Clear after.
       if (current == Weather.RAIN || current == Weather.THUNDERSTORM) {
         forecast = Weather.CLEAR;
       } else {
         forecast = Weather.get(random.nextInt(3));
       }
       setForecast(forecast);
     }
     setForecast(forecast);
     secondsUntilWeatherChange =
         current.getBaseWeatherTime() + random.nextInt(current.getRandomWeatherTime());
     if (VanillaPlugin.getInstance().getEngine().debugMode()) {
       Spout.getLogger()
           .info(
               "Weather changed to: "
                   + current
                   + ", next change in "
                   + secondsUntilWeatherChange / 1000F
                   + "s");
     }
   }
   float currentRainStrength = sky.getData().get(VanillaData.CURRENT_RAIN_STRENGTH);
   sky.getData().put(VanillaData.PREVIOUS_RAIN_STRENGTH, currentRainStrength);
   if (this.isRaining()) {
     currentRainStrength = Math.min(1.0f, currentRainStrength + 0.01f);
   } else {
     currentRainStrength = Math.max(0.0f, currentRainStrength - 0.01f);
   }
   sky.getData().put(VanillaData.CURRENT_RAIN_STRENGTH, currentRainStrength);
   if (hasLightning()) {
     lightning.onTick(dt);
   }
   if (getCurrent().isRaining()) {
     snowfall.onTick(dt);
   }
   sky.getData().put(VanillaData.WEATHER_CHANGE_TIME, secondsUntilWeatherChange);
 }
コード例 #3
0
 public final Engine getEngine() {
   return VanillaPlugin.getInstance().getEngine();
 }
コード例 #4
0
ファイル: LoginAuth.java プロジェクト: Grafe/Vanilla
  public void run() {
    long start = System.currentTimeMillis();
    String sessionId = session.getDataMap().get(VanillaProtocol.SESSION_ID);
    String encodedUser;
    String encodedId;
    try {
      encodedUser = URLEncoder.encode(name, "UTF-8");
      encodedId = URLEncoder.encode(sessionId, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      failed("Unable to uncode username or sessionid");
      return;
    }
    String fullURL = URLBase + userPrefix + encodedUser + idPrefix + encodedId;
    URL authURL;
    try {
      authURL = new URL(fullURL);
    } catch (MalformedURLException e2) {
      failed("Unable to parse URL");
      return;
    }

    URLConnection connection;
    try {
      connection = authURL.openConnection();
    } catch (IOException e2) {
      failed("Unable to open connection");
      return;
    }

    if (!(connection instanceof HttpURLConnection)) {
      failed("Unable to open http connection");
      return;
    }

    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    httpConnection.setConnectTimeout(30000);
    httpConnection.setReadTimeout(30000);

    try {
      httpConnection.connect();
    } catch (IOException e) {
      e.printStackTrace();
      failed("Unable to connect to auth server");
      return;
    }

    BufferedReader in = null;
    try {
      in = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
      String reply = in.readLine();
      if (authString.equals(reply)) {
        if (runnable != null) {
          Spout.getEngine()
              .getScheduler()
              .scheduleSyncDelayedTask(
                  VanillaPlugin.getInstance(), runnable, TaskPriority.CRITICAL);
        }
      } else {
        failed("Auth server refused authentication");
      }
    } catch (IOException e) {
      e.printStackTrace();
      failed("Unable to read reply from auth server");
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
        } finally {
          httpConnection.disconnect();
        }
      } else {
        httpConnection.disconnect();
      }
    }
    if (Spout.getEngine().debugMode()) {
      Spout.getLogger().info("Authing took " + (System.currentTimeMillis() - start) + "ms");
    }
  }