diff --git a/pom.xml b/pom.xml index 3194a15..589f662 100644 --- a/pom.xml +++ b/pom.xml @@ -42,13 +42,19 @@ org.spigotmc spigot-api - 1.17-R0.1-SNAPSHOT + 1.17.1-R0.1-SNAPSHOT provided com.sk89q.worldedit worldedit-bukkit - 7.2.0-SNAPSHOT + 7.2.7-SNAPSHOT + provided + + + com.sk89q.worldguard + worldguard-bukkit + 7.0.6-SNAPSHOT provided diff --git a/src/main/java/de/diddiz/LogBlock/LogBlock.java b/src/main/java/de/diddiz/LogBlock/LogBlock.java index 0da9034..57cd09e 100644 --- a/src/main/java/de/diddiz/LogBlock/LogBlock.java +++ b/src/main/java/de/diddiz/LogBlock/LogBlock.java @@ -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)); } diff --git a/src/main/java/de/diddiz/LogBlock/addons/worldguard/WorldGuardLoggingFlagsAddon.java b/src/main/java/de/diddiz/LogBlock/addons/worldguard/WorldGuardLoggingFlagsAddon.java new file mode 100644 index 0000000..92f726c --- /dev/null +++ b/src/main/java/de/diddiz/LogBlock/addons/worldguard/WorldGuardLoggingFlagsAddon.java @@ -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); + } + } + } +} diff --git a/src/main/java/de/diddiz/LogBlock/config/Config.java b/src/main/java/de/diddiz/LogBlock/config/Config.java index f0ec2ad..59dec80 100644 --- a/src/main/java/de/diddiz/LogBlock/config/Config.java +++ b/src/main/java/de/diddiz/LogBlock/config/Config.java @@ -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 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 loggedWorlds = config.getStringList("loggedWorlds"); worldConfigs = new HashMap<>(); if (loggedWorlds.isEmpty()) { diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index f798208..43cee15 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -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'