Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add containerTransactionsLookup API method (#91) #480

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
47 changes: 46 additions & 1 deletion src/main/java/net/coreprotect/CoreProtectAPI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.coreprotect;

import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
Expand All @@ -17,8 +18,12 @@
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;

import net.coreprotect.api.BlockAPI;
import net.coreprotect.api.ContainerTransactionsAPI;
import net.coreprotect.api.QueueLookup;
import net.coreprotect.api.SessionLookup;
import net.coreprotect.config.Config;
Expand Down Expand Up @@ -117,9 +122,21 @@
return null;
}

final ItemMeta itemMeta = getItemMeta();
if (itemMeta instanceof BlockStateMeta) {
return ((BlockStateMeta) itemMeta).getBlockState().getBlockData();
}

String blockData = parse[12];
if (blockData == null || blockData.length() == 0) {
return getType().createBlockData();
final Material type = getType();
if (type.isBlock()) {
return type.createBlockData();
}
else {
return null;

}
}
return Bukkit.getServer().createBlockData(blockData);
}
Expand Down Expand Up @@ -147,6 +164,27 @@
public String worldName() {
return Util.getWorldName(Integer.parseInt(parse.length < 13 ? parse[5] : parse[9]));
}

public int getAmount() {
if (parse.length < 13) {
return 0;
}

return Integer.parseInt(parse[10]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can throw NumberFormatException

}

public ItemMeta getItemMeta() {
if (parse.length < 13) {
return null;
}

if (parse[11] == null || parse[11].isEmpty()) {
return null;
}
final byte[] metadata = parse[11].getBytes(StandardCharsets.ISO_8859_1);
final ItemStack item = (ItemStack) Rollback.populateItemStack(new ItemStack(getType(), getAmount()), metadata)[2];
return item.getItemMeta();
}
}

private static Map<Object, Boolean> parseList(List<Object> list) {
Expand Down Expand Up @@ -178,6 +216,13 @@
return null;
}

public List<String[]> containerTransactionsLookup(Location location, int time) {
if (Config.getGlobal().API_ENABLED) {
return ContainerTransactionsAPI.performLookup(location, time);
}
return null;
}

public List<String[]> queueLookup(Block block) {
return QueueLookup.performLookup(block);
}
Expand Down Expand Up @@ -419,119 +464,119 @@
return null;
}

private List<String[]> processData(int time, int radius, Location location, Map<Object, Boolean> restrictBlocksMap, Map<Object, Boolean> excludeBlocks, List<String> restrictUsers, List<String> excludeUsers, List<Integer> actionList, int action, int lookup, int offset, int rowCount, boolean useLimit) {
// You need to either specify time/radius or time/user
List<String[]> result = new ArrayList<>();
List<String> uuids = new ArrayList<>();

if (restrictUsers == null) {
restrictUsers = new ArrayList<>();
}

if (excludeUsers == null) {
excludeUsers = new ArrayList<>();
}

if (actionList == null) {
actionList = new ArrayList<>();
}

List<Object> restrictBlocks = new ArrayList<>(restrictBlocksMap.keySet());
if (actionList.size() == 0 && restrictBlocks.size() > 0) {
boolean addedMaterial = false;
boolean addedEntity = false;

for (Object argBlock : restrictBlocks) {
if (argBlock instanceof Material && !addedMaterial) {
actionList.add(0);
actionList.add(1);
addedMaterial = true;
}
else if (argBlock instanceof EntityType && !addedEntity) {
actionList.add(3);
addedEntity = true;
}
}
}

if (actionList.size() == 0) {
actionList.add(0);
actionList.add(1);
}

actionList.removeIf(actionListItem -> actionListItem > 3);

if (restrictUsers.size() == 0) {
restrictUsers.add("#global");
}

long timestamp = System.currentTimeMillis() / 1000L;
long startTime = timestamp - time;
long endTime = 0;

if (radius < 1) {
radius = -1;
}

if (restrictUsers.contains("#global") && radius == -1) {
return null;
}

if (radius > -1 && location == null) {
return null;
}

try (Connection connection = Database.getConnection(false, 1000)) {
if (connection != null) {
Statement statement = connection.createStatement();
boolean restrictWorld = false;

if (radius > 0) {
restrictWorld = true;
}

if (location == null) {
restrictWorld = false;
}

Integer[] argRadius = null;
if (location != null && radius > 0) {
int xMin = location.getBlockX() - radius;
int xMax = location.getBlockX() + radius;
int zMin = location.getBlockZ() - radius;
int zMax = location.getBlockZ() + radius;
argRadius = new Integer[] { radius, xMin, xMax, null, null, zMin, zMax, 0 };
}

if (lookup == 1) {
if (location != null) {
restrictWorld = true;
}

if (useLimit) {
result = Lookup.performPartialLookup(statement, null, uuids, restrictUsers, restrictBlocks, excludeBlocks, excludeUsers, actionList, location, argRadius, null, startTime, endTime, offset, rowCount, restrictWorld, true);
}
else {
result = Lookup.performLookup(statement, null, uuids, restrictUsers, restrictBlocks, excludeBlocks, excludeUsers, actionList, location, argRadius, startTime, endTime, restrictWorld, true);
}
}
else {
if (!Bukkit.isPrimaryThread()) {
boolean verbose = false;
result = Rollback.performRollbackRestore(statement, null, uuids, restrictUsers, null, restrictBlocks, excludeBlocks, excludeUsers, actionList, location, argRadius, startTime, endTime, restrictWorld, false, verbose, action, 0);
}
}

statement.close();
}
}
catch (Exception e) {
e.printStackTrace();
}

return result;
}

Check notice on line 579 in src/main/java/net/coreprotect/CoreProtectAPI.java

View check run for this annotation

codefactor.io / CodeFactor

src/main/java/net/coreprotect/CoreProtectAPI.java#L467-L579

Complex Method
@Deprecated
private List<String[]> processData(String user, int time, int radius, Location location, Map<Object, Boolean> restrictBlocks, Map<Object, Boolean> excludeBlocks, int action, int lookup, int offset, int rowCount, boolean useLimit) {
ArrayList<String> restrictUsers = new ArrayList<>();
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package net.coreprotect.api;

import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import org.bukkit.Location;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.Database;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.utility.Util;

public final class ContainerTransactionsAPI {

private static final int CONNECTION_WAIT_TIME = 1000;

private ContainerTransactionsAPI() {
throw new AssertionError();
}

public static List<String[]> performLookup(Location location, int offset) {
List<String[]> result = new ArrayList<>();

if (location == null) {
return result;
}

try (Connection connection = Database.getConnection(false, CONNECTION_WAIT_TIME)) {
final int x = (int) location.getX();
final int y = (int) location.getY();
final int z = (int) location.getZ();
final int now = (int) (System.currentTimeMillis() / 1000L);
final int worldId = Util.getWorldId(location.getWorld().getName());
final int timeFrom = offset > 0 ? now - offset : 0;

if (connection == null) {
return result;
}

try (Statement statement = connection.createStatement()) {
String query = "SELECT time,user,action,type,data,rolled_back,amount,metadata FROM " +
ConfigHandler.prefix + "container " + Util.getWidIndex("container") +
"WHERE wid = '" + worldId + "' AND x = '" + x + "' AND z = '" + z + "' AND y = '" + y + "'" +
" AND time > '" + timeFrom + "' ORDER BY rowid DESC";
try (ResultSet resultSet = statement.executeQuery(query)) {

while (resultSet.next()) {
final String resultTime = resultSet.getString("time");
final int resultUserId = resultSet.getInt("user");
final String resultAction = resultSet.getString("action");
final int resultType = resultSet.getInt("type");
final String resultData = resultSet.getString("data");
final String resultRolledBack = resultSet.getString("rolled_back");
final int resultAmount = resultSet.getInt("amount");
final byte[] resultMetadata = resultSet.getBytes("metadata");
if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
UserStatement.loadName(connection, resultUserId);
}
String resultUser = ConfigHandler.playerIdCacheReversed.get(resultUserId);
final String metadata = resultMetadata != null ? new String(resultMetadata, StandardCharsets.ISO_8859_1) : "";

String[] resultElement = new String[]{ resultTime, resultUser,
String.valueOf(x), String.valueOf(y), String.valueOf(z), String.valueOf(resultType),
resultData, resultAction, resultRolledBack, String.valueOf(worldId),
String.valueOf(resultAmount), metadata, "" };
result.add(resultElement);
}
}
}
}
catch (SQLException e) {
CoreProtect.getInstance().getLogger().log(Level.WARNING, e.toString(), e);
}

return result;
}

}
Loading