Add feature to disable logging in WorldGuard regions

resolves #826
This commit is contained in:
Brokkonaut
2021-08-27 03:22:29 +02:00
parent 5dfee1f906
commit 142bcae2ca
5 changed files with 107 additions and 4 deletions

10
pom.xml
View File

@ -42,13 +42,19 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.17-R0.1-SNAPSHOT</version>
<version>1.17.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.2.0-SNAPSHOT</version>
<version>7.2.7-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.6-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -1,5 +1,6 @@
package de.diddiz.LogBlock;
import de.diddiz.LogBlock.addons.worldguard.WorldGuardLoggingFlagsAddon;
import de.diddiz.LogBlock.config.Config;
import de.diddiz.LogBlock.listeners.*;
import de.diddiz.LogBlock.questioner.Questioner;
@ -38,6 +39,7 @@ public class LogBlock extends JavaPlugin {
private PlayerInfoLogging playerInfoLogging;
private ScaffoldingLogging scaffoldingLogging;
private Questioner questioner;
private WorldGuardLoggingFlagsAddon worldGuardLoggingFlagsAddon;
private boolean isConfigLoaded;
private volatile boolean isCompletelyEnabled;
@ -67,6 +69,14 @@ public class LogBlock extends JavaPlugin {
} catch (final Exception ex) {
getLogger().log(Level.SEVERE, "Could not load LogBlock config! " + ex.getMessage(), ex);
}
if (Config.worldGuardLoggingFlags) {
if (getServer().getPluginManager().getPlugin("WorldGuard") == null) {
getLogger().log(Level.SEVERE, "Invalid config! addons.worldguardLoggingFlags is set to true, but WorldGuard is not loaded.");
} else {
worldGuardLoggingFlagsAddon = new WorldGuardLoggingFlagsAddon(this);
worldGuardLoggingFlagsAddon.onPluginLoad();
}
}
}
@Override
@ -132,6 +142,9 @@ public class LogBlock extends JavaPlugin {
}
}
questioner = new Questioner(this);
if (worldGuardLoggingFlagsAddon != null) {
worldGuardLoggingFlagsAddon.onPluginEnable();
}
isCompletelyEnabled = true;
getServer().getScheduler().runTaskAsynchronously(this, new Updater.PlayerCountChecker(this));
}

View File

@ -0,0 +1,80 @@
package de.diddiz.LogBlock.addons.worldguard;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.events.BlockChangePreLogEvent;
import de.diddiz.LogBlock.events.EntityChangePreLogEvent;
import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class WorldGuardLoggingFlagsAddon {
private final StateFlag LOGBLOCK_LOG_BLOCKS = new StateFlag("logblock-log-blocks", true);
private final StateFlag LOGBLOCK_LOG_ENTITIES = new StateFlag("logblock-log-entities", true);
private LogBlock plugin;
private WorldGuardPlugin worldGuard;
public WorldGuardLoggingFlagsAddon(LogBlock plugin) {
this.plugin = plugin;
}
public void onPluginLoad() {
registerFlags();
}
public void onPluginEnable() {
worldGuard = (WorldGuardPlugin) plugin.getServer().getPluginManager().getPlugin("WorldGuard");
plugin.getServer().getPluginManager().registerEvents(new LoggingListener(), plugin);
}
private void registerFlags() {
try {
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
registry.register(LOGBLOCK_LOG_BLOCKS);
registry.register(LOGBLOCK_LOG_ENTITIES);
} catch (FlagConflictException e) {
plugin.getLogger().log(Level.SEVERE, "Could not initialize Flags", e);
}
}
private class LoggingListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void onBlockChangePreLog(BlockChangePreLogEvent event) {
RegionAssociable regionAssociable = null;
Location location = event.getLocation();
Entity actorEntity = event.getOwnerActor().getEntity();
if (actorEntity instanceof Player) {
regionAssociable = worldGuard.wrapPlayer((Player) actorEntity);
}
ApplicableRegionSet set = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().getApplicableRegions(BukkitAdapter.adapt(location));
if (!set.testState(regionAssociable, LOGBLOCK_LOG_BLOCKS)) {
event.setCancelled(true);
}
}
@EventHandler(ignoreCancelled = true)
public void onEntityChangePreLog(EntityChangePreLogEvent event) {
RegionAssociable regionAssociable = null;
Location location = event.getLocation();
Entity actorEntity = event.getOwnerActor().getEntity();
if (actorEntity instanceof Player) {
regionAssociable = worldGuard.wrapPlayer((Player) actorEntity);
}
ApplicableRegionSet set = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().getApplicableRegions(BukkitAdapter.adapt(location));
if (!set.testState(regionAssociable, LOGBLOCK_LOG_ENTITIES)) {
event.setCancelled(true);
}
}
}
}

View File

@ -55,6 +55,8 @@ public class Config {
public static boolean safetyIdCheck;
public static boolean debug;
public static boolean logEnvironmentalKills;
// addons
public static boolean worldGuardLoggingFlags;
// Not loaded from config - checked at runtime
public static boolean mb4 = false;
@ -162,6 +164,7 @@ public class Config {
def.put("tools.toolblock.mode", "LOOKUP");
def.put("tools.toolblock.permissionDefault", "OP");
def.put("safety.id.check", true);
def.put("addons.worldguardLoggingFlags", false);
def.put("debug", false);
for (final Entry<String, Object> e : def.entrySet()) {
if (!config.contains(e.getKey())) {
@ -284,6 +287,7 @@ public class Config {
toolsByName.put(alias, tool);
}
}
worldGuardLoggingFlags = config.getBoolean("addons.worldguardLoggingFlags");
final List<String> loggedWorlds = config.getStringList("loggedWorlds");
worldConfigs = new HashMap<>();
if (loggedWorlds.isEmpty()) {

View File

@ -5,8 +5,8 @@ authors: [md_5, ammar2, frymaster]
website: http://dev.bukkit.org/server-mods/logblock/
main: de.diddiz.LogBlock.LogBlock
description: ${project.description}
softdepend: [WorldEdit]
api-version: 1.14
softdepend: [WorldEdit, WorldGuard]
api-version: 1.17
commands:
lb:
description: 'LogBlock plugin commands'