Ejemplo n.º 1
0
/**
 * Cyber-conversion into Cybermen involves the replacement of body parts (including limbs, organs,
 * and vital systems) with artificial components. Partial conversion, with the victim retaining
 * autonomy and a human identity and body parts, is possible.
 *
 * @author eccentric_nz
 */
public class TARDISControlsConverter {

  private final TARDIS plugin;
  private final TARDISDatabaseConnection service = TARDISDatabaseConnection.getInstance();
  private final Connection connection = service.getConnection();

  public TARDISControlsConverter(TARDIS plugin) {
    this.plugin = plugin;
  }

  /** Convert pre-TARDIS v2.3 controls to the new system. */
  public void convertControls() {
    ResultSetTardis rs = new ResultSetTardis(plugin, null, "", true);
    if (rs.resultSet()) {
      int i = 0;
      ArrayList<HashMap<String, String>> data = rs.getData();
      Statement del = null;
      PreparedStatement ps = null;
      try {
        service.testConnection(connection);
        // clear the controls table first - just incase they have reset `conversion_done` in the
        // config
        del = connection.createStatement();
        del.executeUpdate("DELETE FROM controls");
        // insert values from tardis table
        ps =
            connection.prepareStatement(
                "INSERT INTO controls (tardis_id, type, location) VALUES (?,?,?)");
        for (HashMap<String, String> map : data) {
          int id = plugin.getUtils().parseInt(map.get("tardis_id"));
          String tmph;
          if (map.get("handbrake") == null || map.get("handbrake").isEmpty()) {
            tmph = estimateHandbrake(map.get("size"), map.get("chameleon"));
            plugin
                .getConsole()
                .sendMessage(
                    plugin.getPluginName()
                        + ChatColor.RED
                        + "Handbrake location not found, making an educated guess...");
          } else {
            tmph = map.get("handbrake");
          }
          String tmpb;
          if (map.get("button") == null || map.get("button").isEmpty()) {
            tmpb = estimateButton(map.get("size"), map.get("chameleon"));
            plugin
                .getConsole()
                .sendMessage(
                    plugin.getPluginName()
                        + ChatColor.RED
                        + "Button location not found, making an educated guess...");
          } else {
            tmpb = map.get("button");
          }
          String tmpa;
          if (map.get("artron_button") == null || map.get("artron_button").isEmpty()) {
            tmpa = estimateArtron(map.get("size"), map.get("chameleon"));
            plugin
                .getConsole()
                .sendMessage(
                    plugin.getPluginName()
                        + ChatColor.RED
                        + "Artron Button location not found, making an educated guess...");
          } else {
            tmpa = map.get("artron_button");
          }
          String[] tmpr = new String[4];
          if (map.get("repeater0") == null || map.get("repeater0").isEmpty()) {
            tmpr = estimateRepeaters(map.get("size"), map.get("chameleon"));
            plugin
                .getConsole()
                .sendMessage(
                    plugin.getPluginName()
                        + ChatColor.RED
                        + "Repeater locations not found, making an educated guess...");
          } else {
            tmpr[0] = map.get("repeater0");
            tmpr[1] = map.get("repeater1");
            tmpr[2] = map.get("repeater2");
            tmpr[3] = map.get("repeater3");
          }
          String hb = plugin.getUtils().makeLocationStr(tmph);
          String bn = plugin.getUtils().makeLocationStr(tmpb);
          String ab = plugin.getUtils().makeLocationStr(tmpa);
          ps.setInt(1, id);
          ps.setInt(2, 0);
          ps.setString(3, hb);
          ps.addBatch();
          ps.setInt(1, id);
          ps.setInt(2, 1);
          ps.setString(3, bn);
          ps.addBatch();
          ps.setInt(1, id);
          ps.setInt(2, 2);
          ps.setString(3, tmpr[0]);
          ps.addBatch();
          ps.setInt(1, id);
          ps.setInt(2, 3);
          ps.setString(3, tmpr[1]);
          ps.addBatch();
          ps.setInt(1, id);
          ps.setInt(2, 4);
          ps.setString(3, tmpr[2]);
          ps.addBatch();
          ps.setInt(1, id);
          ps.setInt(2, 5);
          ps.setString(3, tmpr[3]);
          ps.addBatch();
          ps.setInt(1, id);
          ps.setInt(2, 6);
          ps.setString(3, ab);
          ps.addBatch();
          connection.setAutoCommit(false);
          ps.executeBatch();
          connection.setAutoCommit(true);
          i++;
        }
      } catch (SQLException e) {
        plugin.debug("Control conversion error: " + e.getMessage());
      } finally {
        if (del != null) {
          try {
            del.close();
          } catch (SQLException e) {
            plugin.debug("Control delete statement close error: " + e.getMessage());
          }
        }
        if (ps != null) {
          try {
            ps.close();
          } catch (SQLException e) {
            plugin.debug("Control prepared statement close error: " + e.getMessage());
          }
        }
      }
      if (i > 0) {
        plugin
            .getConsole()
            .sendMessage(plugin.getPluginName() + "Converted " + i + " control consoles");
        plugin.getConfig().set("conversions.conversion_done", true);
        plugin.saveConfig();
      }
    }
  }

  private String estimateHandbrake(String size, String cham) {
    SCHEMATIC s = SCHEMATIC.valueOf(size);
    String[] data = cham.split(":");
    int x = plugin.getUtils().parseInt(data[1]);
    int y = plugin.getUtils().parseInt(data[2]);
    int z = plugin.getUtils().parseInt(data[3]);
    switch (s) {
      case DELUXE:
        return data[0] + ":" + (x + 1) + ":" + (y + 1) + ":" + (z - 2);
      default:
        return data[0] + ":" + (x - 2) + ":" + y + ":" + z;
    }
  }

  private String estimateButton(String size, String cham) {
    SCHEMATIC s = SCHEMATIC.valueOf(size);
    String[] data = cham.split(":");
    int x = plugin.getUtils().parseInt(data[1]);
    int y = plugin.getUtils().parseInt(data[2]);
    int z = plugin.getUtils().parseInt(data[3]);
    switch (s) {
      case DELUXE:
        return data[0] + ":" + (x - 1) + ":" + y + ":" + (z - 1);
      default:
        return data[0] + ":" + x + ":" + y + ":" + (z + 2);
    }
  }

  private String estimateArtron(String size, String cham) {
    SCHEMATIC s = SCHEMATIC.valueOf(size);
    String[] data = cham.split(":");
    int x = plugin.getUtils().parseInt(data[1]);
    int y = plugin.getUtils().parseInt(data[2]);
    int z = plugin.getUtils().parseInt(data[3]);
    switch (s) {
      case DELUXE:
        return data[0] + ":" + (x + 5) + ":" + y + ":" + (z - 1);
      default:
        return data[0] + ":" + (x - 2) + ":" + y + ":" + (z + 2);
    }
  }

  private String[] estimateRepeaters(String size, String cham) {
    String[] r = new String[4];
    SCHEMATIC s = SCHEMATIC.valueOf(size);
    String[] data = cham.split(":");
    int x = plugin.getUtils().parseInt(data[1]);
    int y = plugin.getUtils().parseInt(data[2]);
    int z = plugin.getUtils().parseInt(data[3]);
    switch (s) {
      case DELUXE:
        r[0] = data[0] + ":" + (x + 2) + ":" + (y + 1) + ":" + (z - 3); // environment
        r[1] = data[0] + ":" + x + ":" + (y + 1) + ":" + (z - 1); // x
        r[2] = data[0] + ":" + (x + 4) + ":" + (y + 1) + ":" + (z - 1); // z
        r[3] = data[0] + ":" + (x + 2) + ":" + (y + 1) + ":" + (z + 1); // y
        break;
      default:
        r[0] = data[0] + ":" + (x - 1) + ":" + y + ":" + (z - 1);
        r[1] = data[0] + ":" + (x - 3) + ":" + y + ":" + (z + 1);
        r[2] = data[0] + ":" + (x + 1) + ":" + y + ":" + (z + 1);
        r[3] = data[0] + ":" + (x - 1) + ":" + y + ":" + (z + 3);
        break;
    }
    return r;
  }
}
Ejemplo n.º 2
0
/** @author eccentric_nz */
public class TARDISSQLInsertControl implements Runnable {

  private final TARDIS plugin;
  private final TARDISDatabaseConnection service = TARDISDatabaseConnection.getInstance();
  private final Connection connection = service.getConnection();
  private final int id;
  private final int type;
  private final String l;
  private final int s;

  /**
   * Updates data in an SQLite database table. This method builds an SQL query string from the
   * parameters supplied and then executes the update.
   *
   * @param plugin an instance of the main plugin class
   * @param id the unique TARDIS identifier
   * @param type the type of control to insert
   * @param l the location of the control
   * @param s whether the control is a secondary control
   */
  public TARDISSQLInsertControl(TARDIS plugin, int id, int type, String l, int s) {
    this.plugin = plugin;
    this.id = id;
    this.type = type;
    this.l = l;
    this.s = s;
  }

  @Override
  public void run() {
    Statement statement = null;
    try {
      service.testConnection(connection);
      statement = connection.createStatement();
      String select =
          "SELECT c_id FROM controls WHERE tardis_id = "
              + id
              + " AND type = "
              + type
              + " AND secondary = "
              + s;
      ResultSet rs = statement.executeQuery(select);
      if (rs.isBeforeFirst()) {
        // update
        String update =
            "UPDATE controls SET location = '" + l + "' WHERE c_id = " + rs.getInt("c_id");
        statement.executeUpdate(update);
      } else {
        // insert
        String insert =
            "INSERT INTO controls (tardis_id, type, location, secondary) VALUES ("
                + id
                + ", "
                + type
                + ", '"
                + l
                + "', "
                + s
                + ")";
        statement.executeUpdate(insert);
      }
    } catch (SQLException e) {
      plugin.debug("Insert control error! " + e.getMessage());
    } finally {
      try {
        if (statement != null) {
          statement.close();
        }
      } catch (SQLException e) {
        plugin.debug("Error closing insert control statement! " + e.getMessage());
      }
    }
  }
}
Ejemplo n.º 3
0
/**
 * Many facts, figures, and formulas are contained within the Matrix, including... a list of
 * locations the TARDIS can travel to.
 *
 * @author eccentric_nz
 */
public class ResultSetNextLocation {

  private final TARDISDatabaseConnection service = TARDISDatabaseConnection.getInstance();
  private final Connection connection = service.getConnection();
  private final TARDIS plugin;
  private final HashMap<String, Object> where;
  private int next_id;
  private int tardis_id;
  private World world;
  private int x;
  private int y;
  private int z;
  private COMPASS direction;
  private boolean submarine;

  /**
   * Creates a class instance that can be used to retrieve an SQL ResultSet from the next locations
   * table.
   *
   * @param plugin an instance of the main class.
   * @param where a HashMap<String, Object> of table fields and values to refine the search.
   */
  public ResultSetNextLocation(TARDIS plugin, HashMap<String, Object> where) {
    this.plugin = plugin;
    this.where = where;
  }

  /**
   * Retrieves an SQL ResultSet from the destinations table. This method builds an SQL query string
   * from the parameters supplied and then executes the query. Use the getters to retrieve the
   * results.
   *
   * @return true or false depending on whether any data matches the query
   */
  public boolean resultSet() {
    PreparedStatement statement = null;
    ResultSet rs = null;
    String wheres = "";
    if (where != null) {
      StringBuilder sbw = new StringBuilder();
      for (Map.Entry<String, Object> entry : where.entrySet()) {
        sbw.append(entry.getKey()).append(" = ? AND ");
      }
      wheres = " WHERE " + sbw.toString().substring(0, sbw.length() - 5);
    }
    String query = "SELECT * FROM next" + wheres;
    try {
      service.testConnection(connection);
      statement = connection.prepareStatement(query);
      if (where != null) {
        int s = 1;
        for (Map.Entry<String, Object> entry : where.entrySet()) {
          if (entry.getValue().getClass().equals(String.class)) {
            statement.setString(s, entry.getValue().toString());
          } else {
            statement.setInt(s, plugin.getUtils().parseInt(entry.getValue().toString()));
          }
          s++;
        }
        where.clear();
      }
      rs = statement.executeQuery();
      if (rs.isBeforeFirst()) {
        while (rs.next()) {
          this.next_id = rs.getInt("next_id");
          this.tardis_id = rs.getInt("tardis_id");
          this.world = plugin.getServer().getWorld(rs.getString("world"));
          this.x = rs.getInt("x");
          this.y = rs.getInt("y");
          this.z = rs.getInt("z");
          this.direction = COMPASS.valueOf(rs.getString("direction"));
          this.submarine = rs.getBoolean("submarine");
        }
      } else {
        return false;
      }
    } catch (SQLException e) {
      plugin.debug("ResultSet error for destinations table! " + e.getMessage());
      return false;
    } finally {
      try {
        if (rs != null) {
          rs.close();
        }
        if (statement != null) {
          statement.close();
        }
      } catch (SQLException e) {
        plugin.debug("Error closing destinations table! " + e.getMessage());
      }
    }
    return this.world != null;
  }

  public int getNext_id() {
    return next_id;
  }

  public int getTardis_id() {
    return tardis_id;
  }

  public World getWorld() {
    return world;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }

  public int getZ() {
    return z;
  }

  public COMPASS getDirection() {
    return direction;
  }

  public boolean isSubmarine() {
    return submarine;
  }
}
Ejemplo n.º 4
0
/** @author eccentric_nz */
public class TARDISSQLUpdate implements Runnable {

  private final TARDIS plugin;
  TARDISDatabaseConnection service = TARDISDatabaseConnection.getInstance();
  Connection connection = service.getConnection();
  private final String table;
  private final HashMap<String, Object> data;
  private final HashMap<String, Object> where;

  /**
   * Updates data in an SQLite database table. This method builds an SQL query string from the
   * parameters supplied and then executes the update.
   *
   * @param plugin an instance of the main plugin class
   * @param table the database table name to update.
   * @param data a HashMap<String, Object> of table fields and values update.
   * @param where a HashMap<String, Object> of table fields and values to select the records to
   *     update.
   */
  public TARDISSQLUpdate(
      TARDIS plugin, String table, HashMap<String, Object> data, HashMap<String, Object> where) {
    this.plugin = plugin;
    this.table = table;
    this.data = data;
    this.where = where;
  }

  @Override
  public void run() {
    PreparedStatement ps = null;
    String updates;
    String wheres;
    StringBuilder sbu = new StringBuilder();
    StringBuilder sbw = new StringBuilder();
    for (Map.Entry<String, Object> entry : data.entrySet()) {
      sbu.append(entry.getKey()).append(" = ?,");
    }
    for (Map.Entry<String, Object> entry : where.entrySet()) {
      sbw.append(entry.getKey()).append(" = ");
      if (entry.getValue().getClass().equals(String.class)
          || entry.getValue().getClass().equals(UUID.class)) {
        sbw.append("'").append(entry.getValue()).append("' AND ");
      } else {
        sbw.append(entry.getValue()).append(" AND ");
      }
    }
    where.clear();
    updates = sbu.toString().substring(0, sbu.length() - 1);
    wheres = sbw.toString().substring(0, sbw.length() - 5);
    String query = "UPDATE " + table + " SET " + updates + " WHERE " + wheres;
    try {
      service.testConnection(connection);
      ps = connection.prepareStatement(query);
      int s = 1;
      for (Map.Entry<String, Object> entry : data.entrySet()) {
        if (entry.getValue().getClass().equals(String.class)
            || entry.getValue().getClass().equals(UUID.class)) {
          ps.setString(s, entry.getValue().toString());
        }
        if (entry.getValue() instanceof Integer) {
          ps.setInt(s, (Integer) entry.getValue());
        }
        if (entry.getValue() instanceof Long) {
          ps.setLong(s, (Long) entry.getValue());
        }
        s++;
      }
      data.clear();
      ps.executeUpdate();
    } catch (SQLException e) {
      plugin.debug("Update error for " + table + "! " + e.getMessage());
    } finally {
      try {
        if (ps != null) {
          ps.close();
        }
      } catch (SQLException e) {
        plugin.debug("Error closing " + table + "! " + e.getMessage());
      }
    }
  }
}
Ejemplo n.º 5
0
/**
 * Many facts, figures, and formulas are contained within the Matrix, including... a list of
 * locations the TARDIS can travel to.
 *
 * @author eccentric_nz
 */
public class ResultSetDestinations {

  private final TARDISDatabaseConnection service = TARDISDatabaseConnection.getInstance();
  private final Connection connection = service.getConnection();
  private final TARDIS plugin;
  private final HashMap<String, Object> where;
  private final boolean multiple;
  private int dest_id;
  private int tardis_id;
  private String dest_name;
  private String world;
  private int x;
  private int y;
  private int z;
  private String direction;
  private String bind;
  private int type;
  private boolean submarine;
  private final ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();

  /**
   * Creates a class instance that can be used to retrieve an SQL ResultSet from the destinations
   * table.
   *
   * @param plugin an instance of the main class.
   * @param where a HashMap<String, Object> of table fields and values to refine the search.
   * @param multiple a boolean indicating whether multiple rows should be fetched
   */
  public ResultSetDestinations(TARDIS plugin, HashMap<String, Object> where, boolean multiple) {
    this.plugin = plugin;
    this.where = where;
    this.multiple = multiple;
  }

  /**
   * Retrieves an SQL ResultSet from the destinations table. This method builds an SQL query string
   * from the parameters supplied and then executes the query. Use the getters to retrieve the
   * results.
   *
   * @return true or false depending on whether any data matches the query
   */
  public boolean resultSet() {
    PreparedStatement statement = null;
    ResultSet rs = null;
    String wheres = "";
    if (where != null) {
      StringBuilder sbw = new StringBuilder();
      for (Map.Entry<String, Object> entry : where.entrySet()) {
        sbw.append(entry.getKey()).append(" = ? AND ");
      }
      wheres = " WHERE " + sbw.toString().substring(0, sbw.length() - 5);
    }
    String query = "SELECT * FROM destinations" + wheres;
    try {
      service.testConnection(connection);
      statement = connection.prepareStatement(query);
      if (where != null) {
        int s = 1;
        for (Map.Entry<String, Object> entry : where.entrySet()) {
          if (entry.getValue().getClass().equals(String.class)) {
            statement.setString(s, entry.getValue().toString());
          } else {
            statement.setInt(s, plugin.utils.parseInt(entry.getValue().toString()));
          }
          s++;
        }
        where.clear();
      }
      rs = statement.executeQuery();
      if (rs.isBeforeFirst()) {
        while (rs.next()) {
          if (multiple) {
            HashMap<String, String> row = new HashMap<String, String>();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columns = rsmd.getColumnCount();
            for (int i = 1; i < columns + 1; i++) {
              row.put(rsmd.getColumnName(i).toLowerCase(Locale.ENGLISH), rs.getString(i));
            }
            data.add(row);
          }
          this.dest_id = rs.getInt("dest_id");
          this.tardis_id = rs.getInt("tardis_id");
          this.dest_name = rs.getString("dest_name");
          this.world = rs.getString("world");
          this.x = rs.getInt("x");
          this.y = rs.getInt("y");
          this.z = rs.getInt("z");
          this.direction = rs.getString("direction");
          this.submarine = rs.getBoolean("submarine");
          this.bind = rs.getString("bind");
          this.type = rs.getInt("type");
        }
      } else {
        return false;
      }
    } catch (SQLException e) {
      plugin.debug("ResultSet error for destinations table! " + e.getMessage());
      return false;
    } finally {
      try {
        if (rs != null) {
          rs.close();
        }
        if (statement != null) {
          statement.close();
        }
      } catch (SQLException e) {
        plugin.debug("Error closing destinations table! " + e.getMessage());
      }
    }
    return true;
  }

  public int getDest_id() {
    return dest_id;
  }

  public int getTardis_id() {
    return tardis_id;
  }

  public String getDest_name() {
    return dest_name;
  }

  public String getWorld() {
    return world;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }

  public int getZ() {
    return z;
  }

  public String getDirection() {
    return direction;
  }

  public boolean isSubmarine() {
    return submarine;
  }

  public String getBind() {
    return bind;
  }

  public int getType() {
    return type;
  }

  public ArrayList<HashMap<String, String>> getData() {
    return data;
  }
}