Integrate CoreProtect logging for bounty head metadata #11

Merged
NotAlexNoyle merged 1 commits from codex/use-coreprotect-api-for-head-metadata into main 2026-03-09 09:13:46 -04:00
4 changed files with 154 additions and 2 deletions

No files matched your search

+2
View File
@@ -66,6 +66,7 @@ repositories {
maven { url = uri("https://repo.glaremasters.me/repository/towny/") }
maven { url = uri("https://repo.extendedclip.com/releases/") }
maven { url = uri("https://repo.tcoded.com/public/") }
maven { url = uri("https://maven.playpro.com") }
}
/* ---------------------- Java project deps ---------------------------- */
@@ -73,6 +74,7 @@ dependencies {
compileOnly("org.purpurmc.purpur:purpur-api:1.19.4-R0.1-SNAPSHOT") // Declare Purpur API version to be packaged.
compileOnly("net.sacredlabyrinth.phaed.simpleclans:SimpleClans:2.15.2")
compileOnly("net.luckperms:api:5.5") // Import LuckPerms API.
compileOnly("net.coreprotect:coreprotect:23.1")
compileOnlyApi(project(":libs:Utilities-OG")) // Import TrueOG Network Utilities-OG Java API (from source).
compileOnlyApi(project(":libs:GxUI-OG")) // Import TrueOG Network GxUI-OG Java API (from source).
compileOnlyApi(project(":libs:DiamondBank-OG")) {
@@ -16,6 +16,7 @@ import com.google.common.collect.ImmutableList;
import com.tcoded.playerbountiesplus.command.BountyCommand;
import com.tcoded.playerbountiesplus.command.PlayerBountiesPlusAdminCmd;
import com.tcoded.playerbountiesplus.hook.currency.DiamondBankOGHook;
import com.tcoded.playerbountiesplus.hook.logging.CoreProtectHook;
import com.tcoded.playerbountiesplus.hook.currency.EconomyHook;
import com.tcoded.playerbountiesplus.hook.team.TeamHook;
import com.tcoded.playerbountiesplus.listener.BountyHeadListener;
@@ -61,6 +62,9 @@ public final class PlayerBountiesOG extends JavaPlugin {
// LuckPerms API hook.
private LuckPerms luckPerms;
// CoreProtect API hook.
private CoreProtectHook coreProtectHook;
public PlayerBountiesOG() {
instance = this;
@@ -88,6 +92,14 @@ public final class PlayerBountiesOG extends JavaPlugin {
}
this.coreProtectHook = new CoreProtectHook(this);
if (!this.coreProtectHook.isEnabled()) {
getLogger().warning(
"CoreProtect API v11+ not found - custom bounty head metadata may not rollback correctly.");
}
// Initialize the DiamondBank-OG API.
final RegisteredServiceProvider<DiamondBankAPIJava> provider = getServer().getServicesManager()
.getRegistration(DiamondBankAPIJava.class);
@@ -146,7 +158,9 @@ public final class PlayerBountiesOG extends JavaPlugin {
this.getServer().getPluginManager().registerEvents(new DeathListener(this, this.diamondBankAPI, this.luckPerms),
this);
this.getServer().getPluginManager()
.registerEvents(new BountyHeadListener(this, this.diamondBankAPI, this.luckPerms), this);
.registerEvents(
new BountyHeadListener(this, this.diamondBankAPI, this.luckPerms, this.coreProtectHook),
this);
this.getServer().getPluginManager().registerEvents(new GuiProtectionListener(this), this);
final List<Plugin> plugins = ImmutableList.copyOf(this.getServer().getPluginManager().getPlugins());
@@ -212,6 +226,12 @@ public final class PlayerBountiesOG extends JavaPlugin {
}
public CoreProtectHook getCoreProtectHook() {
return this.coreProtectHook;
}
// Utilities.
private static void findPluginWithQuery(List<Plugin> plugins, String pluginNameQuery, String... excludeStrings) {
@@ -0,0 +1,113 @@
package com.tcoded.playerbountiesplus.hook.logging;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.plugin.Plugin;
import net.coreprotect.CoreProtect;
import net.coreprotect.CoreProtectAPI;
public class CoreProtectHook {
private final CoreProtectAPI coreProtectAPI;
public CoreProtectHook(Plugin plugin) {
this.coreProtectAPI = findCoreProtect(plugin);
}
public boolean isEnabled() {
return this.coreProtectAPI != null;
}
public boolean logPlacement(String user, BlockState blockState) {
if (this.coreProtectAPI == null || user == null || blockState == null) {
return false;
}
return this.coreProtectAPI.logPlacement(user, blockState);
}
public boolean logRemoval(String user, BlockState blockState) {
if (this.coreProtectAPI == null || user == null || blockState == null) {
return false;
}
return this.coreProtectAPI.logRemoval(user, blockState);
}
public boolean logContainerTransaction(String user, Location location) {
if (this.coreProtectAPI == null || user == null || location == null) {
return false;
}
return this.coreProtectAPI.logContainerTransaction(user, location);
}
public boolean logInteraction(String user, Location location) {
if (this.coreProtectAPI == null || user == null || location == null) {
return false;
}
return this.coreProtectAPI.logInteraction(user, location);
}
public boolean hasPlaced(String user, Block block, int time, int offset) {
if (this.coreProtectAPI == null || user == null || block == null) {
return false;
}
return this.coreProtectAPI.hasPlaced(user, block, time, offset);
}
private static CoreProtectAPI findCoreProtect(Plugin plugin) {
if (plugin == null) {
return null;
}
final Plugin coreProtectPlugin = plugin.getServer().getPluginManager().getPlugin("CoreProtect");
if (!(coreProtectPlugin instanceof CoreProtect coreProtect)) {
return null;
}
final CoreProtectAPI api = coreProtect.getAPI();
if (api == null || !api.isEnabled() || api.APIVersion() < 11) {
return null;
}
return api;
}
}
@@ -24,6 +24,7 @@ import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import com.tcoded.playerbountiesplus.PlayerBountiesOG;
import com.tcoded.playerbountiesplus.hook.logging.CoreProtectHook;
import net.kyori.adventure.text.TextComponent;
import net.luckperms.api.LuckPerms;
@@ -39,14 +40,17 @@ public class BountyHeadListener implements Listener {
private final NamespacedKey targetUuidKey;
private final NamespacedKey amountKey;
private final LuckPerms luckPerms;
private final CoreProtectHook coreProtectHook;
public BountyHeadListener(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI, LuckPerms luckPerms) {
public BountyHeadListener(PlayerBountiesOG plugin, DiamondBankAPIJava diamondBankAPI, LuckPerms luckPerms,
CoreProtectHook coreProtectHook) {
this.diamondBankAPI = diamondBankAPI;
this.targetKey = new NamespacedKey(plugin, DeathListener.BOUNTY_HEAD_NAME_KEY);
this.targetUuidKey = new NamespacedKey(plugin, DeathListener.BOUNTY_HEAD_UUID_KEY);
this.amountKey = new NamespacedKey(plugin, DeathListener.BOUNTY_HEAD_AMOUNT_KEY);
this.luckPerms = luckPerms;
this.coreProtectHook = coreProtectHook;
}
@@ -89,6 +93,12 @@ public class BountyHeadListener implements Listener {
blockData.set(amountKey, PersistentDataType.DOUBLE, bountyAmount);
skull.update(true, false);
if (this.coreProtectHook != null) {
this.coreProtectHook.logPlacement(event.getPlayer().getName(), skull);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@@ -101,6 +111,7 @@ public class BountyHeadListener implements Listener {
}
final BlockState state = event.getBlock().getState();
final BlockState snapshot = state;
if (!(state instanceof Skull skull)) {
return;
@@ -126,6 +137,12 @@ public class BountyHeadListener implements Listener {
drop.setItemMeta(meta);
if (this.coreProtectHook != null) {
this.coreProtectHook.logRemoval(event.getPlayer().getName(), snapshot);
}
event.setDropItems(false);
event.getBlock().setType(Material.AIR, false);
event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), drop);