Compare commits

...

6 Commits

Author SHA1 Message Date
langua 0399d737dc
Merge 1595b18a00 into 6023c21ba9 2024-05-07 15:04:07 +12:00
Intelli 6023c21ba9 Fixed hopper pulls performing container validation on source block 2024-05-06 19:19:48 -06:00
Intelli 3e3496ad12 Corrected version references in API v10 documentation 2024-05-06 18:18:56 -06:00
langua 1595b18a00
add static getHandlerList method as required by bukkit 2024-04-20 08:50:54 +02:00
langua feb36b03a2
add CoreProtectBlockBreakPreLogEvent
- called before logging block breaking
- can fetch the location is being logged
- can modify username as CoreProtectPreLogEvent
2024-04-19 22:02:28 +02:00
langua 8dd263e839
add CoreProtectBlockPlacePreLogEvent
- called before logging block placing
- can fetch the location and block state is being logged
- can modify username as CoreProtectPreLogEvent
2024-04-19 22:01:43 +02:00
6 changed files with 150 additions and 16 deletions

View File

@ -25,8 +25,8 @@ logRemoval(String user, BlockState blockState)
## Getting Started
Ensure you're using CoreProtect 21.0 or higher. Add it as an external jar to your plugin in your IDE.
Alternatively, if using Maven, you can add it via the repository [https://maven.playpro.com](https://maven.playpro.com) (net.coreprotect, 21.0).
Ensure you're using CoreProtect 23.0 or higher. Add it as an external jar to your plugin in your IDE.
Alternatively, if using Maven, you can add it via the repository [https://maven.playpro.com](https://maven.playpro.com) (net.coreprotect, 23.0).
The first thing you need to do is get access to CoreProtect. You can do this by using code similar to the following:
@ -49,7 +49,7 @@ private CoreProtectAPI getCoreProtect() {
}
// Check that a compatible version of the API is loaded
if (CoreProtect.APIVersion() < 9) {
if (CoreProtect.APIVersion() < 10) {
return null;
}

View File

@ -13,6 +13,7 @@ import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectBlockBreakPreLogEvent;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.utility.Util;
@ -53,20 +54,28 @@ public class BlockBreakLogger {
blockData = overrideData;
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
// call events and fetch changed username, all other properties are readonly for now
CoreProtectPreLogEvent coreProtectPreLogEvent = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(coreProtectPreLogEvent);
}
user = coreProtectPreLogEvent.getUser();
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
CoreProtectBlockBreakPreLogEvent coreProtectBlockBreakPreLogEvent = new CoreProtectBlockBreakPreLogEvent(user, location);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(coreProtectBlockBreakPreLogEvent);
}
user = coreProtectBlockBreakPreLogEvent.getUser();
int userId = UserStatement.getId(preparedStmt, user, 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, event.getUser(), type });
CacheHandler.breakCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[]{time, user, type});
if (event.isCancelled()) {
if (coreProtectPreLogEvent.isCancelled() || coreProtectBlockBreakPreLogEvent.isCancelled()) {
return;
}

View File

@ -13,6 +13,7 @@ import net.coreprotect.config.Config;
import net.coreprotect.config.ConfigHandler;
import net.coreprotect.database.statement.BlockStatement;
import net.coreprotect.database.statement.UserStatement;
import net.coreprotect.event.CoreProtectBlockPlacePreLogEvent;
import net.coreprotect.event.CoreProtectPreLogEvent;
import net.coreprotect.thread.CacheHandler;
import net.coreprotect.utility.Util;
@ -83,20 +84,28 @@ public class BlockPlaceLogger {
}
}
CoreProtectPreLogEvent event = new CoreProtectPreLogEvent(user);
// call events and fetch changed username, all other properties are readonly for now
CoreProtectPreLogEvent coreProtectPreLogEvent = new CoreProtectPreLogEvent(user);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(event);
CoreProtect.getInstance().getServer().getPluginManager().callEvent(coreProtectPreLogEvent);
}
user = coreProtectPreLogEvent.getUser();
int userId = UserStatement.getId(preparedStmt, event.getUser(), true);
CoreProtectBlockPlacePreLogEvent coreProtectBlockPlacePreLogEvent = new CoreProtectBlockPlacePreLogEvent(user, block.getLocation(), block);
if (Config.getGlobal().API_ENABLED) {
CoreProtect.getInstance().getServer().getPluginManager().callEvent(coreProtectBlockPlacePreLogEvent);
}
user = coreProtectBlockPlacePreLogEvent.getUser();
int userId = UserStatement.getId(preparedStmt, user, true);
int wid = Util.getWorldId(block.getWorld().getName());
int time = (int) (System.currentTimeMillis() / 1000L);
if (event.getUser().length() > 0) {
CacheHandler.lookupCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[] { time, event.getUser(), type });
if (user.length() > 0) {
CacheHandler.lookupCache.put("" + x + "." + y + "." + z + "." + wid + "", new Object[]{time, user, type});
}
if (event.isCancelled()) {
if (coreProtectPreLogEvent.isCancelled() || coreProtectBlockPlacePreLogEvent.isCancelled()) {
return;
}

View File

@ -0,0 +1,55 @@
package net.coreprotect.event;
import org.bukkit.Location;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class CoreProtectBlockBreakPreLogEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private String user;
private final Location location;
private boolean cancelled = false;
public CoreProtectBlockBreakPreLogEvent(String user, Location location) {
super(true); // async
this.user = user;
this.location = location;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
cancelled = b;
}
public String getUser() {
return user;
}
public void setUser(String newUser) {
if (newUser == null || newUser.isEmpty()) {
throw new IllegalArgumentException("Invalid user");
}
this.user = newUser;
}
public Location getLocation() {
return location;
}
}

View File

@ -0,0 +1,61 @@
package net.coreprotect.event;
import org.bukkit.Location;
import org.bukkit.block.BlockState;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class CoreProtectBlockPlacePreLogEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private String user;
private final Location location;
private final BlockState blockState;
private boolean cancelled = false;
public CoreProtectBlockPlacePreLogEvent(String user, Location location, BlockState blockState) {
super(true); // async
this.user = user;
this.location = location;
this.blockState = blockState;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean b) {
cancelled = b;
}
public String getUser() {
return user;
}
public void setUser(String newUser) {
if (newUser == null || newUser.isEmpty()) {
throw new IllegalArgumentException("Invalid user");
}
this.user = newUser;
}
public Location getLocation() {
return location;
}
public BlockState getBlockState() {
return blockState;
}
}

View File

@ -28,7 +28,7 @@ public final class HopperPullListener {
}
}
ItemStack[] sourceContainer = Util.getContainerState(sourceHolder.getInventory().getContents());
ItemStack[] destinationContainer = Util.getContainerState(destinationHolder.getInventory().getContents());
ItemStack movedItem = item.clone();
final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
@ -39,7 +39,7 @@ public final class HopperPullListener {
}
boolean abort = false;
boolean addedInventory = Util.canAddContainer(sourceContainer, movedItem, sourceHolder.getInventory().getMaxStackSize());
boolean addedInventory = Util.canAddContainer(destinationContainer, movedItem, destinationHolder.getInventory().getMaxStackSize());
if (!addedInventory) {
abort = true;
}