Initial commit

This commit is contained in:
Kristian Knarvik 2023-12-16 21:46:06 +01:00
commit d12a38d39e
6 changed files with 374 additions and 0 deletions

113
.gitignore vendored Normal file
View File

@ -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/

90
pom.xml Normal file
View File

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.knarcraft</groupId>
<artifactId>ClearOnWorldGuard</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ClearOnWorldGuard</name>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>sk89q-repo</id>
<url>https://maven.enginehub.org/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>20.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -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 <p>The logger used by this plugin</p>
*/
public static Logger logger() {
return instance.getLogger();
}
}

View File

@ -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<World, Set<ProtectedRegion>> 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<String> 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<ProtectedRegion> regions = new HashSet<>();
List<String> 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<ProtectedRegion> fromRegions = getOccupiedClearRegions(playerWorld, setFrom.getRegions());
Set<ProtectedRegion> 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 <p>The world the player is currently in</p>
* @param playerRegions <p>The regions the player is in or will be in</p>
* @return <p>All clear regions found in playerRegions</p>
*/
private Set<ProtectedRegion> getOccupiedClearRegions(World playerWorld, Set<ProtectedRegion> playerRegions) {
Set<ProtectedRegion> possibleRegions = protectedRegions.get(playerWorld);
Set<ProtectedRegion> result = new HashSet<>();
for (ProtectedRegion region : playerRegions) {
if (possibleRegions.contains(region)) {
result.add(region);
}
}
return result;
}
}

View File

@ -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: []

View File

@ -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 ]