Make user mutable via CoreProtectPreLogEvent (#66)

* Make user mutable via CoreProtectPreLogEvent

* Cleanup & added missing toLowerCase in getId

* Docs: add Event category; add CoreProtectPreLogEvent

* Merged event documentation into API documentation

* Removed event.getUser() equality check

* Added IllegalArgumentException check within PreLogEvent

* Reverted docs/index.md

Co-authored-by: Intelli <contact@intelli.software>
This commit is contained in:
bermudalocket 2021-08-06 20:40:21 -04:00 committed by GitHub
parent 39cd888440
commit da718de252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 158 additions and 38 deletions

View File

@ -122,21 +122,39 @@ boolean hasRemoved(String user, Block block, int time, int offset)
void performPurge(int time)
```
### Usage
---
`isEnabled()`
### Available Events
*The following events are emitted by CoreProtect.*
#### CoreProtectPreLogEvent
Fired when a CoreProtect logger is about to log an action. Not cancellable.
| Property | Description | Mutable |
| --- | --- | --- |
| User | The name of the user under which this action will be logged. | Yes |
---
### Method Usage
*Detailed method information is listed below.*
#### `isEnabled()`
Calling this will return true if the server has the CoreProtect API enabled, and false if it does not.
---
`testAPI()`
#### `testAPI()`
Running this will print out "[CoreProtect] API Test Successful." in the server console.
---
`performLookup(int time, List<String> restrict_users, List<String> exclude_users, List<Object> restrict_blocks, List<Object> exclude_blocks, List<Integer> action_list, int radius, Location radius_location)`
#### `performLookup(int time, List<String> restrict_users, List<String> exclude_users, List<Object> restrict_blocks, List<Object> exclude_blocks, List<Integer> action_list, int radius, Location radius_location)`
This will perform a lookup.
@ -151,7 +169,7 @@ This will perform a lookup.
---
`performRollback(int time, List<String> restrict_users, List<String> exclude_users, List<Object> restrict_blocks, List<Object> exclude_blocks, List<Integer> action_list, int radius, Location radius_location)`
#### `performRollback(int time, List<String> restrict_users, List<String> exclude_users, List<Object> restrict_blocks, List<Object> exclude_blocks, List<Integer> action_list, int radius, Location radius_location)`
This will perform a rollback. Method must be called async.
@ -166,7 +184,7 @@ This will perform a rollback. Method must be called async.
---
`performRestore(int time, List<String> restrict_users, List<String> exclude_users, List<Object> restrict_blocks, List<Object> exclude_blocks, List<Integer> action_list, int radius, Location radius_location)`
#### `performRestore(int time, List<String> restrict_users, List<String> exclude_users, List<Object> restrict_blocks, List<Object> exclude_blocks, List<Integer> action_list, int radius, Location radius_location)`
This will perform a restore.
@ -181,7 +199,7 @@ This will perform a restore.
---
`blockLookup(Block block, int time)`
#### `blockLookup(Block block, int time)`
This will perform a full lookup on a single block.
@ -190,7 +208,7 @@ This will perform a full lookup on a single block.
---
`ParseResult parseResult(String[] result)`
#### `ParseResult parseResult(String[] result)`
This will parse results from a lookup. You'll then be able to view the following:
@ -208,7 +226,7 @@ This will parse results from a lookup. You'll then be able to view the following
---
`logPlacement(String user, Location location, Material type, BlockData blockData)`
#### `logPlacement(String user, Location location, Material type, BlockData blockData)`
This will log a block as being placed.
@ -219,7 +237,7 @@ This will log a block as being placed.
---
`logRemoval(String user, Location location, Material type, BlockData blockData)`
#### `logRemoval(String user, Location location, Material type, BlockData blockData)`
This will log a block as being removed/broken, and will log the block's inventory (if applicable).
@ -230,7 +248,7 @@ This will log a block as being removed/broken, and will log the block's inventor
---
`logContainerTransaction(String user, Location location)`
#### `logContainerTransaction(String user, Location location)`
This will log any transactions made to a block's inventory immediately after calling the method.
@ -239,7 +257,7 @@ This will log any transactions made to a block's inventory immediately after cal
---
`logInteraction(String user, Location location)`
#### `logInteraction(String user, Location location)`
This will log a block as having been interacted with.
@ -248,7 +266,7 @@ This will log a block as having been interacted with.
---
`hasPlaced(String user, Block block, int time, int offset)`
#### `hasPlaced(String user, Block block, int time, int offset)`
This will return true if a user has already placed a block at the location within the specified time limit.
@ -259,7 +277,7 @@ This will return true if a user has already placed a block at the location withi
---
`hasRemoved(String user, Block block, int time, int offset)`
#### `hasRemoved(String user, Block block, int time, int offset)`
This will return true if a user has already removed a block at the location within the specified time limit.
@ -270,7 +288,7 @@ This will return true if a user has already removed a block at the location with
---
`performPurge(int time)`
#### `performPurge(int time)`
This will perform a purge on the CoreProtect database.

View File

@ -7,8 +7,11 @@ import java.util.Locale;
import org.bukkit.Location;
import org.bukkit.Material;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.utility.Util;
@ -40,13 +43,16 @@ public class BlockBreakLogger {
blockData = blockData.replaceFirst("has_book=true", "has_book=false");
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
int time = (int) (System.currentTimeMillis() / 1000L);
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
CacheHandler.breakCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, user, type });
int userId = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
CacheHandler.breakCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, event.getUser(), type });
BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, type, data, meta, blockData, 0, 0);
}
catch (Exception e) {

View File

@ -12,9 +12,12 @@ import org.bukkit.block.data.type.Bed;
import org.bukkit.block.data.type.Door;
import org.bukkit.block.data.type.Door.Hinge;
import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.model.BlockGroup;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.utility.Util;
@ -55,7 +58,11 @@ public class BlockPlaceLogger {
if (type.equals(Material.AIR) || type.equals(Material.CAVE_AIR)) {
return;
}
int userId = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName());
int time = (int) (System.currentTimeMillis() / 1000L);
int x = block.getX();
@ -68,8 +75,8 @@ public class BlockPlaceLogger {
int doubledata = data;
int logdouble = 0;
if (user.length() > 0) {
CacheHandler.lookupCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, user, type });
if (event.getUser().length() > 0) {
CacheHandler.lookupCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, event.getUser(), type });
}
String doubleBlockData = null;

View File

@ -5,8 +5,11 @@ import java.util.Locale;
import org.bukkit.Location;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.CommandStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.utility.Util;
public class CommandLogger {
@ -23,11 +26,15 @@ public class CommandLogger {
if (ConfigHandler.blacklist.get(((message + " ").split(" "))[0].toLowerCase(Locale.ROOT)) != null) {
return;
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
int wid = Util.getWorldId(location.getWorld().getName());
int userId = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
CommandStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, message);
}
catch (Exception e) {

View File

@ -13,9 +13,12 @@ import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.statement.ContainerStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.utility.Util;
import net.coreprotect.utility.serialize.ItemMetaHandler;
@ -161,8 +164,11 @@ public class ContainerLogger extends Queue {
metadata = null;
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
int userid = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
int time = (int) (System.currentTimeMillis() / 1000L);
int x = location.getBlockX();
int y = location.getBlockY();
@ -170,7 +176,7 @@ public class ContainerLogger extends Queue {
int typeId = Util.getBlockId(item.getType().name(), true);
int data = item.getDurability();
int amount = item.getAmount();
ContainerStatement.insert(preparedStmt, batchCount, time, userid, wid, x, y, z, typeId, data, amount, metadata, action, 0);
ContainerStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, typeId, data, amount, metadata, action, 0);
}
}
slot++;

View File

@ -7,9 +7,12 @@ import java.util.Locale;
import org.bukkit.block.BlockState;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.EntityStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.utility.Util;
public class EntityKillLogger {
@ -23,18 +26,22 @@ public class EntityKillLogger {
if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null) {
return;
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName());
int time = (int) (System.currentTimeMillis() / 1000L);
int x = block.getX();
int y = block.getY();
int z = block.getZ();
int userid = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
EntityStatement.insert(preparedStmt2, time, data);
ResultSet keys = preparedStmt2.getGeneratedKeys();
keys.next();
int entity_key = keys.getInt(1);
keys.close();
BlockStatement.insert(preparedStmt, batchCount, time, userid, wid, x, y, z, type, entity_key, null, null, 3, 0);
BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, type, entity_key, null, null, 3, 0);
}
catch (Exception e) {
e.printStackTrace();

View File

@ -9,8 +9,11 @@ import java.util.Map;
import org.bukkit.Location;
import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.ItemStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.utility.Util;
import net.coreprotect.utility.serialize.ItemMetaHandler;
@ -65,15 +68,18 @@ public class ItemLogger {
data = null;
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
int userid = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
int time = (int) (System.currentTimeMillis() / 1000L);
int x = location.getBlockX();
int y = location.getBlockY();
int z = location.getBlockZ();
int typeId = Util.getBlockId(item.getType().name(), true);
int amount = item.getAmount();
ItemStatement.insert(preparedStmt, batchCount, time, userid, wid, x, y, z, typeId, data, amount, action);
ItemStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, typeId, data, amount, action);
}
}
}

View File

@ -6,8 +6,11 @@ import java.util.Locale;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.utility.Util;
public class PlayerInteractLogger {
@ -22,8 +25,12 @@ public class PlayerInteractLogger {
if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null || Util.getType(type).equals(Material.AIR) || Util.getType(type).equals(Material.CAVE_AIR)) {
return;
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName());
int userId = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
int time = (int) (System.currentTimeMillis() / 1000L);
int x = block.getX();
int y = block.getY();

View File

@ -5,9 +5,11 @@ import java.util.Locale;
import org.bukkit.block.BlockState;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.utility.Util;
public class PlayerKillLogger {
@ -21,16 +23,17 @@ public class PlayerKillLogger {
if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null) {
return;
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int playerId = ConfigHandler.playerIdCache.get(player.toLowerCase(Locale.ROOT));
int wid = Util.getWorldId(block.getWorld().getName());
int time = (int) (System.currentTimeMillis() / 1000L);
int x = block.getX();
int y = block.getY();
int z = block.getZ();
int userId = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
if (ConfigHandler.playerIdCache.get(player.toLowerCase(Locale.ROOT)) == null) {
UserStatement.loadId(preparedStmt.getConnection(), player, null);
}
int playerId = ConfigHandler.playerIdCache.get(player.toLowerCase(Locale.ROOT));
BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, 0, playerId, null, null, 3, 0);
}
catch (Exception e) {

View File

@ -5,8 +5,11 @@ import java.util.Locale;
import org.bukkit.Location;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.SignStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.utility.Util;
public class SignTextLogger {
@ -20,7 +23,11 @@ public class SignTextLogger {
if (ConfigHandler.blacklist.get(user.toLowerCase(Locale.ROOT)) != null) {
return;
}
int userId = ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
int time = (int) (System.currentTimeMillis() / 1000L) - timeOffset;
int x = location.getBlockX();

View File

@ -3,6 +3,7 @@ package net.coreprotect.database.statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;
@ -36,6 +37,14 @@ public class UserStatement {
return id;
}
public static int getId(PreparedStatement preparedStatement, String user, boolean load) throws SQLException {
if (load && !ConfigHandler.playerIdCache.containsKey(user.toLowerCase(Locale.ROOT))) {
UserStatement.loadId(preparedStatement.getConnection(), user, null);
}
return ConfigHandler.playerIdCache.get(user.toLowerCase(Locale.ROOT));
}
public static int loadId(Connection connection, String user, String uuid) {
// generate if doesn't exist
int id = -1;

View File

@ -0,0 +1,37 @@
package net.coreprotect.event;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class CoreProtectPreLogEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private String user;
public CoreProtectPreLogEvent(String user) {
super(true); // async
this.user = user;
}
public String getUser() {
return user;
}
public void setUser(String newUser) {
if (newUser == null || newUser.isEmpty()) {
throw new IllegalArgumentException("Invalid user");
}
this.user = newUser;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}