Skip to content

Commit

Permalink
Made PreLogEvent cancellable (implements #325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelli committed Jul 13, 2023
1 parent e7a6f21 commit 1a06e6a
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 11 deletions.
3 changes: 2 additions & 1 deletion docs/api/version/v9.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ void performPurge(int time)

#### CoreProtectPreLogEvent

Fired when a CoreProtect logger is about to log an action. Not cancellable.
Fired when a CoreProtect logger is about to log an action. Cancellable.

| Property | Description | Mutable |
| --- | --- | --- |
| User | The name of the user under which this action will be logged. | Yes |
| Cancelled | If cancelled, the action won't be logged to the database. | Yes |

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
Expand Down Expand Up @@ -48,7 +49,9 @@ else if (checkType == Material.PAINTING || BukkitAdapter.ADAPTER.isItemFrame(che
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
Expand All @@ -57,6 +60,11 @@ else if (checkType == Material.PAINTING || BukkitAdapter.ADAPTER.isItemFrame(che
int y = location.getBlockY();
int z = location.getBlockZ();
CacheHandler.breakCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, event.getUser(), type });

if (event.isCancelled()) {
return;
}

BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, type, data, meta, blockData, 0, 0);
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
Expand Down Expand Up @@ -79,7 +80,9 @@ else if (type == Material.WATER || type == Material.LAVA) {
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName());
Expand All @@ -89,8 +92,11 @@ else if (type == Material.WATER || type == Material.LAVA) {
CacheHandler.lookupCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, event.getUser(), type });
}

int internalType = Util.getBlockId(type.name(), true);
if (event.isCancelled()) {
return;
}

int internalType = Util.getBlockId(type.name(), true);
if (replacedType > 0 && Util.getType(replacedType) != Material.AIR && Util.getType(replacedType) != Material.CAVE_AIR) {
BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, replacedType, replacedData, null, replaceBlockData, 0, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Location;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.CommandStatement;
import net.coreprotect.database.statement.UserStatement;
Expand All @@ -28,7 +29,13 @@ public static void log(PreparedStatement preparedStmt, int batchCount, long time
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

if (event.isCancelled()) {
return;
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.inventory.ItemStack;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Queue;
import net.coreprotect.database.statement.ContainerStatement;
Expand Down Expand Up @@ -168,7 +169,13 @@ protected static void logTransaction(PreparedStatement preparedStmt, int batchCo
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

if (event.isCancelled()) {
return;
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.block.BlockState;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.EntityStatement;
Expand All @@ -28,7 +29,13 @@ public static void log(PreparedStatement preparedStmt, PreparedStatement prepare
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

if (event.isCancelled()) {
return;
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.inventory.ItemStack;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.ItemStatement;
import net.coreprotect.database.statement.UserStatement;
Expand Down Expand Up @@ -125,7 +126,13 @@ protected static void logTransaction(PreparedStatement preparedStmt, int batchCo
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

if (event.isCancelled()) {
return;
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.block.BlockState;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
Expand All @@ -27,7 +28,13 @@ public static void log(PreparedStatement preparedStmt, int batchCount, String us
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

if (event.isCancelled()) {
return;
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.block.BlockState;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
Expand All @@ -29,7 +30,13 @@ public static void log(PreparedStatement preparedStmt, int batchCount, String us
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

if (event.isCancelled()) {
return;
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int playerId = ConfigHandler.playerIdCache.get(player.toLowerCase(Locale.ROOT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Location;

import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.SignStatement;
import net.coreprotect.database.statement.UserStatement;
Expand All @@ -25,7 +26,13 @@ public static void log(PreparedStatement preparedStmt, int batchCount, String us
}

CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}

if (event.isCancelled()) {
return;
}

int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class CoreProtectPreLogEvent extends Event {

private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
private String user;

public CoreProtectPreLogEvent(String user) {
Expand All @@ -17,6 +18,14 @@ public String getUser() {
return user;
}

public boolean isCancelled() {
return cancelled;
}

public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}

public void setUser(String newUser) {
if (newUser == null || newUser.isEmpty()) {
throw new IllegalArgumentException("Invalid user");
Expand Down

0 comments on commit 1a06e6a

Please sign in to comment.