-
-
Notifications
You must be signed in to change notification settings - Fork 345
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
takejohn
wants to merge
8
commits into
PlayPro:master
Choose a base branch
from
takejohn:feature/#91-container-transactions-lookup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b1c20a
Set projectBranch to 'development'
takejohn 59166ad
Update Folia API dependency
takejohn ddce775
Merge branch 'patch/dependency-folia-api-1.20.2' into feature/#91-con…
takejohn 7c00d4c
Add containerTransactionsLookup API method
takejohn a58d764
Revert "Set projectBranch to 'development'"
takejohn 1540fec
Change first argument of containerTransactionsLookup to Location
takejohn b905832
Merge branch 'master' into feature/#91-container-transactions-lookup
takejohn 8c85baf
Merge branch 'master' into feature/#91-container-transactions-lookup
takejohn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/net/coreprotect/api/ContainerTransactionsAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can throw NumberFormatException