Made PreLogEvent cancellable (implements #325)

This commit is contained in:
Intelli 2023-07-13 17:55:00 -06:00
parent e7a6f21d88
commit 1a06e6a6bf
11 changed files with 84 additions and 11 deletions

View File

@ -120,11 +120,12 @@ void performPurge(int time)
#### CoreProtectPreLogEvent #### 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 | | Property | Description | Mutable |
| --- | --- | --- | | --- | --- | --- |
| User | The name of the user under which this action will be logged. | Yes | | 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 |
--- ---

View File

@ -9,6 +9,7 @@ import org.bukkit.Material;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter; import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement; import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement; import net.coreprotect.database.statement.UserStatement;
@ -48,7 +49,9 @@ public class BlockBreakLogger {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName()); int wid = Util.getWorldId(location.getWorld().getName());
@ -57,6 +60,11 @@ public class BlockBreakLogger {
int y = location.getBlockY(); int y = location.getBlockY();
int z = location.getBlockZ(); int z = location.getBlockZ();
CacheHandler.breakCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, event.getUser(), type }); 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); BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, type, data, meta, blockData, 0, 0);
} }
catch (Exception e) { catch (Exception e) {

View File

@ -9,6 +9,7 @@ import org.bukkit.block.BlockState;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter; import net.coreprotect.bukkit.BukkitAdapter;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement; import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement; import net.coreprotect.database.statement.UserStatement;
@ -79,7 +80,9 @@ public class BlockPlaceLogger {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName()); int wid = Util.getWorldId(block.getWorld().getName());
@ -89,8 +92,11 @@ public class BlockPlaceLogger {
CacheHandler.lookupCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, event.getUser(), type }); 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) { 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); BlockStatement.insert(preparedStmt, batchCount, time, userId, wid, x, y, z, replacedType, replacedData, null, replaceBlockData, 0, 0);
} }

View File

@ -6,6 +6,7 @@ import java.util.Locale;
import org.bukkit.Location; import org.bukkit.Location;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.CommandStatement; import net.coreprotect.database.statement.CommandStatement;
import net.coreprotect.database.statement.UserStatement; import net.coreprotect.database.statement.UserStatement;
@ -28,7 +29,13 @@ public class CommandLogger {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
if (event.isCancelled()) {
return;
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName()); int wid = Util.getWorldId(location.getWorld().getName());

View File

@ -14,6 +14,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.consumer.Queue; import net.coreprotect.consumer.Queue;
import net.coreprotect.database.statement.ContainerStatement; import net.coreprotect.database.statement.ContainerStatement;
@ -168,7 +169,13 @@ public class ContainerLogger extends Queue {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
if (event.isCancelled()) {
return;
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName()); int wid = Util.getWorldId(location.getWorld().getName());

View File

@ -8,6 +8,7 @@ import java.util.Locale;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement; import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.EntityStatement; import net.coreprotect.database.statement.EntityStatement;
@ -28,7 +29,13 @@ public class EntityKillLogger {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
if (event.isCancelled()) {
return;
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName()); int wid = Util.getWorldId(block.getWorld().getName());

View File

@ -10,6 +10,7 @@ import org.bukkit.Location;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.ItemStatement; import net.coreprotect.database.statement.ItemStatement;
import net.coreprotect.database.statement.UserStatement; import net.coreprotect.database.statement.UserStatement;
@ -125,7 +126,13 @@ public class ItemLogger {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
if (event.isCancelled()) {
return;
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName()); int wid = Util.getWorldId(location.getWorld().getName());

View File

@ -7,6 +7,7 @@ import org.bukkit.Material;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement; import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement; import net.coreprotect.database.statement.UserStatement;
@ -27,7 +28,13 @@ public class PlayerInteractLogger {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
if (event.isCancelled()) {
return;
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(block.getWorld().getName()); int wid = Util.getWorldId(block.getWorld().getName());

View File

@ -6,6 +6,7 @@ import java.util.Locale;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement; import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement; import net.coreprotect.database.statement.UserStatement;
@ -29,7 +30,13 @@ public class PlayerKillLogger {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
if (event.isCancelled()) {
return;
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int playerId = ConfigHandler.playerIdCache.get(player.toLowerCase(Locale.ROOT)); int playerId = ConfigHandler.playerIdCache.get(player.toLowerCase(Locale.ROOT));

View File

@ -6,6 +6,7 @@ import java.util.Locale;
import org.bukkit.Location; import org.bukkit.Location;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler; import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.SignStatement; import net.coreprotect.database.statement.SignStatement;
import net.coreprotect.database.statement.UserStatement; import net.coreprotect.database.statement.UserStatement;
@ -25,7 +26,13 @@ public class SignTextLogger {
} }
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user); CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event); CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
}
if (event.isCancelled()) {
return;
}
int userId = UserStatement.getId(preparedStmt, event.getUser(), true); int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
int wid = Util.getWorldId(location.getWorld().getName()); int wid = Util.getWorldId(location.getWorld().getName());

View File

@ -6,6 +6,7 @@ import org.bukkit.event.HandlerList;
public class CoreProtectPreLogEvent extends Event { public class CoreProtectPreLogEvent extends Event {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
private String user; private String user;
public CoreProtectPreLogEvent(String user) { public CoreProtectPreLogEvent(String user) {
@ -17,6 +18,14 @@ public class CoreProtectPreLogEvent extends Event {
return user; return user;
} }
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
public void setUser(String newUser) { public void setUser(String newUser) {
if (newUser == null || newUser.isEmpty()) { if (newUser == null || newUser.isEmpty()) {
throw new IllegalArgumentException("Invalid user"); throw new IllegalArgumentException("Invalid user");