From d12a38d39ed3b9b930a8353901ce30cb0664c8b9 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 16 Dec 2023 21:46:06 +0100 Subject: [PATCH] Initial commit --- .gitignore | 113 ++++++++++++++++ pom.xml | 90 +++++++++++++ .../clearonworldguard/ClearOnWorldGuard.java | 34 +++++ .../clearonworldguard/WorldGuardListener.java | 124 ++++++++++++++++++ src/main/resources/config.yml | 7 + src/main/resources/plugin.yml | 6 + 6 files changed, 374 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/net/knarcraft/clearonworldguard/ClearOnWorldGuard.java create mode 100644 src/main/java/net/knarcraft/clearonworldguard/WorldGuardListener.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4788b4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1488b4c --- /dev/null +++ b/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + net.knarcraft + ClearOnWorldGuard + 1.0-SNAPSHOT + jar + + ClearOnWorldGuard + + + 1.8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + + spigotmc-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + sk89q-repo + https://maven.enginehub.org/repo/ + + + + + + org.spigotmc + spigot-api + 1.20.1-R0.1-SNAPSHOT + provided + + + com.sk89q.worldguard + worldguard-bukkit + 7.0.7 + provided + + + org.jetbrains + annotations + 20.1.0 + compile + + + diff --git a/src/main/java/net/knarcraft/clearonworldguard/ClearOnWorldGuard.java b/src/main/java/net/knarcraft/clearonworldguard/ClearOnWorldGuard.java new file mode 100644 index 0000000..27aad0e --- /dev/null +++ b/src/main/java/net/knarcraft/clearonworldguard/ClearOnWorldGuard.java @@ -0,0 +1,34 @@ +package net.knarcraft.clearonworldguard; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.logging.Logger; + +@SuppressWarnings("unused") +public final class ClearOnWorldGuard extends JavaPlugin { + + private static ClearOnWorldGuard instance; + + @Override + public void onEnable() { + instance = this; + this.saveDefaultConfig(); + this.reloadConfig(); + FileConfiguration configuration = this.getConfig(); + + // Register the WorldGuard listener + Bukkit.getPluginManager().registerEvents(new WorldGuardListener(configuration), this); + } + + /** + * Gets the logger used by this plugin + * + * @return

The logger used by this plugin

+ */ + public static Logger logger() { + return instance.getLogger(); + } + +} diff --git a/src/main/java/net/knarcraft/clearonworldguard/WorldGuardListener.java b/src/main/java/net/knarcraft/clearonworldguard/WorldGuardListener.java new file mode 100644 index 0000000..7916fb8 --- /dev/null +++ b/src/main/java/net/knarcraft/clearonworldguard/WorldGuardListener.java @@ -0,0 +1,124 @@ +package net.knarcraft.clearonworldguard; + +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.protection.regions.RegionContainer; +import com.sk89q.worldguard.protection.regions.RegionQuery; +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.logging.Level; + +/** + * A listener that listens to players entering and leaving WorldGuard regions + */ +public class WorldGuardListener implements Listener { + + private final RegionContainer regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer(); + private final RegionQuery query = regionContainer.createQuery(); + private final Map> protectedRegions = new HashMap<>(); + + /** + * Instantiates a new WorldGuard listener + */ + public WorldGuardListener(@NotNull FileConfiguration configuration) { + ConfigurationSection worldsSection = configuration.getConfigurationSection("clearRegions"); + if (worldsSection == null) { + ClearOnWorldGuard.logger().log(Level.WARNING, "Unable to find clearRegions sections in the " + + "configuration file!"); + return; + } + Set worlds = worldsSection.getKeys(false); + + for (String worldId : worlds) { + // Parse every world identifier + World world; + try { + UUID worldUUID = UUID.fromString(worldId); + world = Bukkit.getWorld(worldUUID); + } catch (IllegalArgumentException exception) { + world = Bukkit.getWorld(worldId); + } + if (world == null) { + ClearOnWorldGuard.logger().log(Level.WARNING, "The specified world: \"" + worldId + "\" is invalid."); + continue; + } + + // Get a region manager for the world + RegionManager regionManager = regionContainer.get(BukkitAdapter.adapt(world)); + if (regionManager == null) { + ClearOnWorldGuard.logger().log(Level.WARNING, "Unable to get region manager for world: " + + world.getName()); + continue; + } + + Set regions = new HashSet<>(); + List regionNames = worldsSection.getStringList(worldId); + for (String regionName : regionNames) { + // Register the region if it's valid + ProtectedRegion region = regionManager.getRegion(regionName); + if (region == null) { + ClearOnWorldGuard.logger().log(Level.WARNING, "Region name " + regionName + " is invalid"); + } else { + regions.add(region); + } + } + protectedRegions.put(world, regions); + } + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onMove(PlayerMoveEvent event) { + if (event.getTo() == null || event.getFrom().getWorld() == null) { + return; + } + World playerWorld = event.getFrom().getWorld(); + + ApplicableRegionSet setFrom = query.getApplicableRegions(BukkitAdapter.adapt(event.getFrom())); + ApplicableRegionSet setTo = query.getApplicableRegions(BukkitAdapter.adapt(event.getTo())); + + Set fromRegions = getOccupiedClearRegions(playerWorld, setFrom.getRegions()); + Set toRegions = getOccupiedClearRegions(playerWorld, setTo.getRegions()); + + // If the player is in one or more clear regions, clear unless the clear regions are the same + if ((!fromRegions.isEmpty() || !toRegions.isEmpty()) && !fromRegions.equals(toRegions)) { + event.getPlayer().getInventory().clear(); + } + } + + /** + * Gets all regions set as clear regions the player currently occupies + * + * @param playerWorld

The world the player is currently in

+ * @param playerRegions

The regions the player is in or will be in

+ * @return

All clear regions found in playerRegions

+ */ + private Set getOccupiedClearRegions(World playerWorld, Set playerRegions) { + Set possibleRegions = protectedRegions.get(playerWorld); + Set result = new HashSet<>(); + for (ProtectedRegion region : playerRegions) { + if (possibleRegions.contains(region)) { + result.add(region); + } + } + + return result; + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..81419e9 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,7 @@ +# The regions for each world that should +clearRegions: + # The name or UUID of a world. Add any regions that should force inventory clearing as a comma-separated list inside + # the square parentheses. + world: [] + world_nether: [] + world_the_end: [] \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..697f59a --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,6 @@ +name: ClearOnWorldGuard +version: '${project.version}' +main: net.knarcraft.clearonworldguard.ClearOnWorldGuard +api-version: '1.20' +description: A custom KnarCraft plugin +depend: [ WorldGuard ] \ No newline at end of file