Initial commit
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
7
src/main/resources/config.yml
Normal file
7
src/main/resources/config.yml
Normal 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: []
|
6
src/main/resources/plugin.yml
Normal file
6
src/main/resources/plugin.yml
Normal 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 ]
|
Reference in New Issue
Block a user