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));
}
}
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;
}
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;
}
Aggregations