Search in sources :

Example 1 with IntPair

use of me.botsko.prism.utils.IntPair in project Prism-Bukkit by prism.

the class SqlSelectQueryBuilder method blockCondition.

private void blockCondition() {
    // Blocks
    final Set<Material> blockFilters = parameters.getBlockFilters();
    if (!blockFilters.isEmpty()) {
        final String[] blockArr = new String[blockFilters.size()];
        int i = 0;
        for (Material m : blockFilters) {
            Set<IntPair> allIds = Prism.getItems().materialToAllIds(m);
            StringBuilder blockIds = new StringBuilder("(");
            for (IntPair pair : allIds) {
                blockIds.append(pair.first).append(',');
            }
            String in = blockIds.append(')').toString().replace(",)", ")");
            blockArr[i++] = tableNameData + ".block_id IN " + in;
        }
        addCondition(buildGroupConditions(null, blockArr, "%s%s", "OR", null));
    }
    Set<MaterialState> blockDataFilters = parameters.getBlockDataFilters();
    if (!blockDataFilters.isEmpty()) {
        final ArrayList<String> blockArr = new ArrayList<>();
        for (MaterialState data : blockDataFilters) {
            Set<IntPair> pairs = Prism.getItems().partialBlockDataIds(data.material, data.state);
            for (IntPair pair : pairs) {
                blockArr.add(tableNameData + ".block_id = " + pair.first + " AND " + tableNameData + ".block_subid = " + pair.second);
            }
        }
        addCondition(buildGroupConditions(null, blockArr.toArray(new String[0]), "%s%s", "OR", null));
    }
}
Also used : ArrayList(java.util.ArrayList) Material(org.bukkit.Material) IntPair(me.botsko.prism.utils.IntPair) MaterialState(me.botsko.prism.api.objects.MaterialState)

Example 2 with IntPair

use of me.botsko.prism.utils.IntPair in project Prism-Bukkit by prism.

the class SqlInsertBuilder method addInsertionToBatch.

@SuppressWarnings("DuplicatedCode")
@Override
public boolean addInsertionToBatch(Handler a) throws SQLException {
    if (batchStatement == null) {
        return false;
    }
    int worldId = 0;
    String worldName = a.getLoc().getWorld().getName();
    if (Prism.prismWorlds.containsKey(worldName)) {
        worldId = Prism.prismWorlds.get(worldName);
    }
    int actionId = 0;
    if (Prism.prismActions.containsKey(a.getActionType().getName())) {
        actionId = Prism.prismActions.get(a.getActionType().getName());
    }
    PrismPlayer prismPlayer = PlayerIdentification.getPrismPlayerByNameFromCache(a.getSourceName());
    int playerId = prismPlayer.getId();
    IntPair newIds = Prism.getItems().materialToIds(a.getMaterial(), Utilities.dataString(a.getBlockData()));
    IntPair oldIds = Prism.getItems().materialToIds(a.getOldMaterial(), Utilities.dataString(a.getOldBlockData()));
    Location l = a.getLoc();
    applyToInsert(batchStatement, a, actionId, playerId, worldId, newIds, oldIds, l);
    batchStatement.addBatch();
    extraDataQueue.add(a);
    return true;
}
Also used : PrismPlayer(me.botsko.prism.players.PrismPlayer) IntPair(me.botsko.prism.utils.IntPair) Location(org.bukkit.Location)

Example 3 with IntPair

use of me.botsko.prism.utils.IntPair in project Prism-Bukkit by prism.

the class SqlInsertBuilder method insertActionIntoDatabase.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("DuplicatedCode")
@Override
public long insertActionIntoDatabase(Handler a) {
    int worldId = 0;
    long id = 0;
    String worldName = a.getLoc().getWorld().getName();
    if (Prism.prismWorlds.containsKey(worldName)) {
        worldId = Prism.prismWorlds.get(worldName);
    }
    int actionId = 0;
    if (Prism.prismActions.containsKey(a.getActionType().getName())) {
        actionId = Prism.prismActions.get(a.getActionType().getName());
    }
    PrismPlayer prismPlayer = PlayerIdentification.getPrismPlayerByNameFromCache(a.getSourceName());
    int playerId = prismPlayer.getId();
    if (worldId == 0 || actionId == 0 || playerId == 0) {
        Prism.debug("Sql data error: Handler:" + a.toString());
    }
    IntPair newIds = Prism.getItems().materialToIds(a.getMaterial(), Utilities.dataString(a.getBlockData()));
    IntPair oldIds = Prism.getItems().materialToIds(a.getOldMaterial(), Utilities.dataString(a.getOldBlockData()));
    Location l = a.getLoc();
    try (Connection con = dataSource.getConnection();
        PreparedStatement s = con.prepareStatement(getQuery(), Statement.RETURN_GENERATED_KEYS)) {
        applyToInsert(s, a, actionId, playerId, worldId, newIds, oldIds, l);
        s.executeUpdate();
        ResultSet generatedKeys = s.getGeneratedKeys();
        if (generatedKeys.next()) {
            id = generatedKeys.getLong(1);
        }
        if (a.hasExtraData()) {
            String serialData = a.serialize();
            if (serialData != null && !serialData.isEmpty()) {
                try (PreparedStatement s2 = con.prepareStatement("INSERT INTO `" + prefix + "data_extra` (data_id, data) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS)) {
                    s2.setLong(1, id);
                    s2.setString(2, serialData);
                    s2.executeUpdate();
                }
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return id;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PrismPlayer(me.botsko.prism.players.PrismPlayer) IntPair(me.botsko.prism.utils.IntPair) Location(org.bukkit.Location)

Aggregations

IntPair (me.botsko.prism.utils.IntPair)3 PrismPlayer (me.botsko.prism.players.PrismPlayer)2 Location (org.bukkit.Location)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 MaterialState (me.botsko.prism.api.objects.MaterialState)1 Material (org.bukkit.Material)1