Skip to content

Commit

Permalink
Initial work on WorldGuard importer
Browse files Browse the repository at this point in the history
  • Loading branch information
T14D3 committed Jan 11, 2025
1 parent 3062f2a commit 75bd443
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 30 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
compileOnly 'io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT'
compileOnly 'me.clip:placeholderapi:2.11.6'
compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.3.0'
compileOnly 'com.sk89q.worldguard:worldguard-bukkit:7.0.12'
implementation(platform("com.intellectualsites.bom:bom-newest:1.52"))
compileOnly("com.fastasyncworldedit:FastAsyncWorldEdit-Core")
compileOnly("com.fastasyncworldedit:FastAsyncWorldEdit-Bukkit")
Expand Down
88 changes: 60 additions & 28 deletions src/main/java/de/t14d3/zones/RegionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RegionManager {
private final FileConfiguration regionsConfig;
final PermissionManager permissionManager;
private final Zones plugin;
public Map<String, Region> loadedRegions = new HashMap<>();
Map<String, Region> loadedRegions = new HashMap<>();
public Map<Location, List<String>> regionCache = new HashMap<>();

public RegionManager(Zones plugin, PermissionManager permissionManager) {
Expand Down Expand Up @@ -194,9 +194,55 @@ public String createNewRegion(String name, Location min, Location max, UUID play

permissionManager.invalidateAllCaches();
saveRegion(regionKey, newRegion);
loadedRegions.put(regionKey, newRegion);
return regionKey;
}

/**
* Creates a new region with the given name and minimum and maximum locations
* The region will be owned by the given player
*
* @param name The name of the new region.
* @param min The minimum location of the new region.
* @param max The maximum location of the new region.
* @param playerUUID The UUID of the player who will own the new region.
*/
public void createNewRegion(String name, Location min, Location max, UUID playerUUID) {
Map<String, String> permissions = new HashMap<>();
permissions.put("role", "owner");
createNewRegion(name, min, max, playerUUID, permissions);
}

/**
* Creates a new region with the specified key, name, minimum and maximum locations, members and their permissions.
*
* @param key The key of the new region.
* @param name The name of the new region.
* @param min The minimum location of the new region.
* @param max The maximum location of the new region.
* @param members The members of the new region.
* @return The newly created region.
*/
public Region createNewRegion(String key, String name, Location min, Location max, Map<UUID, Map<String, String>> members) {
Region region = new Region(name, min, max, members, key);
saveRegion(key, region);
return region;
}

public void create2DRegion(String name, Location min, Location max, UUID playerUUID) {
min.setY(-63);
max.setY(319);
createNewRegion(name, min, max, playerUUID);
}

public String create2DRegion(String name, Location min, Location max, UUID playerUUID, Map<String, String> ownerPermissions) {
min.setY(-63);
max.setY(319);
return createNewRegion(name, min, max, playerUUID, ownerPermissions);
}



/**
* Creates a new region as a sub-region of the given parent region.
* The region will be owned by the given player with the given permissions.
Expand Down Expand Up @@ -224,33 +270,7 @@ public void createSubRegion(String name, Location min, Location max, UUID player
permissionManager.invalidateAllCaches();
regionCache.clear();
saveRegion(regionKey, newRegion);
}

/**
* Creates a new region with the given name and minimum and maximum locations
* The region will be owned by the given player
*
* @param name The name of the new region.
* @param min The minimum location of the new region.
* @param max The maximum location of the new region.
* @param playerUUID The UUID of the player who will own the new region.
*/
public void createNewRegion(String name, Location min, Location max, UUID playerUUID) {
Map<String, String> permissions = new HashMap<>();
permissions.put("role", "owner");
createNewRegion(name, min, max, playerUUID, permissions);
}

public void create2DRegion(String name, Location min, Location max, UUID playerUUID) {
min.setY(-63);
max.setY(319);
createNewRegion(name, min, max, playerUUID);
}

public String create2DRegion(String name, Location min, Location max, UUID playerUUID, Map<String, String> ownerPermissions) {
min.setY(-63);
max.setY(319);
return createNewRegion(name, min, max, playerUUID, ownerPermissions);
loadedRegions.put(regionKey, newRegion);
}

/**
Expand Down Expand Up @@ -449,4 +469,16 @@ public Map<String, String> getMemberPermissions(Player player, Region region) {
public Map<String, String> getMemberPermissions(UUID uuid, Region region) {
return region.getMemberPermissions(uuid);
}

/**
* Adds a region to the loaded regions map.
* Requires an existing region object, to create a new region use {@link #createNewRegion(String, String, Location, Location, Map)}.
*
* @param region The region to add.
*
* @see #createNewRegion
*/
public void addRegion(Region region) {
loadedRegions.put(region.getKey(), region);
}
}
5 changes: 5 additions & 0 deletions src/main/java/de/t14d3/zones/Zones.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.t14d3.zones.integrations.FAWEIntegration;
import de.t14d3.zones.integrations.PlaceholderAPI;
import de.t14d3.zones.integrations.WorldEditSession;
import de.t14d3.zones.integrations.WorldGuardImporter;
import de.t14d3.zones.listeners.CommandListener;
import de.t14d3.zones.listeners.PlayerInteractListener;
import de.t14d3.zones.listeners.PlayerQuitListener;
Expand Down Expand Up @@ -112,6 +113,10 @@ public void onEnable() {
getLogger().info("WorldEdit Integration enabled.");
}

if (getServer().getPluginManager().getPlugin("WorldGuard") != null) {
new WorldGuardImporter(this);
}

getLogger().info("Zones plugin has been enabled! Loaded " + regionManager.loadedRegions.size() + " regions.");
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/de/t14d3/zones/integrations/WorldGuardImporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package de.t14d3.zones.integrations;

import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionType;
import de.t14d3.zones.Region;
import de.t14d3.zones.Zones;
import org.bukkit.Location;
import org.bukkit.World;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class WorldGuardImporter {

private final Zones plugin;

public WorldGuardImporter(Zones plugin) {
this.plugin = plugin;
}

public void importRegions() {
final int[] count = {0};
new Thread(() -> {
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();

for (World world : plugin.getServer().getWorlds()) {
for (Map.Entry<String, ProtectedRegion> entry : container.get(BukkitAdapter.adapt(world)).getRegions().entrySet()) {
ProtectedRegion region = entry.getValue();
if (!region.getType().equals(RegionType.CUBOID)) {
continue;
}
String key;
do {
key = UUID.randomUUID().toString().substring(0, 8);
} while (plugin.getRegionManager().regions().containsKey(key));
String name = entry.getKey();
Location min = BukkitAdapter.adapt(world, region.getMinimumPoint());
Location max = BukkitAdapter.adapt(world, region.getMaximumPoint());
Map<UUID, Map<String, String>> members = new HashMap<>();
Map<String, String> tempPerms = Map.of("role", "member");

region.getMembers().getUniqueIds().forEach(uuid -> members.put(uuid, tempPerms));

Region newRegion = plugin.getRegionManager().createNewRegion(key, name, min, max, members);
plugin.getRegionManager().addRegion(newRegion);
count[0]++;
}

}

plugin.getLogger().info("Imported " + count[0] + " regions from WorldGuard.");
}).start();
}
}
37 changes: 35 additions & 2 deletions src/main/java/de/t14d3/zones/listeners/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.t14d3.zones.Region;
import de.t14d3.zones.RegionManager;
import de.t14d3.zones.Zones;
import de.t14d3.zones.integrations.WorldGuardImporter;
import de.t14d3.zones.utils.Actions;
import de.t14d3.zones.utils.Direction;
import de.t14d3.zones.utils.Messages;
Expand Down Expand Up @@ -139,6 +140,13 @@ public void execute(CommandSourceStack stack, String[] args) {
sender.sendMessage(miniMessage.deserialize(messages.get("commands.no-permission")));
}
break;
case "import":
if (sender.hasPermission("zones.import")) {
handleImportCommand(sender, args);
} else {
sender.sendMessage(miniMessage.deserialize(messages.get("commands.no-permission")));
}
break;
default:
stack.getSender().sendMessage(miniMessage.deserialize(messages.get("commands.invalid")));
break;
Expand All @@ -149,7 +157,7 @@ public void execute(CommandSourceStack stack, String[] args) {
public @NotNull Collection<String> suggest(@NotNull CommandSourceStack stack, String[] args) {
if (args.length <= 1) {
List<String> suggestions = new ArrayList<>();
for (String command : List.of("info", "delete", "create", "subcreate", "cancel", "list", "set", "load", "save", "expand", "select")) {
for (String command : List.of("info", "delete", "create", "subcreate", "cancel", "list", "set", "load", "save", "expand", "select", "import")) {
if (stack.getSender().hasPermission("zones." + command)
&& (args.length == 0 || command.startsWith(args[0].toLowerCase()))) {
suggestions.add(command);
Expand Down Expand Up @@ -223,6 +231,15 @@ public void execute(CommandSourceStack stack, String[] args) {
return List.of("overlap");
}
}
if (args[0].equalsIgnoreCase("import")) {
if (args.length == 2) {
List<String> suggestions = new ArrayList<>();
if (plugin.getServer().getPluginManager().getPlugin("WorldGuard") != null) {
suggestions.add("worldguard");
}
return suggestions;
}
}
return List.of();
}

Expand Down Expand Up @@ -500,7 +517,7 @@ private void handleSaveCommand(CommandSender sender, String[] args) {

private void handleLoadCommand(CommandSender sender, String[] args) {
regionManager.loadRegions();
int count = regionManager.loadedRegions.size();
int count = regionManager.regions().size();
sender.sendMessage(miniMessage.deserialize(messages.get("commands.load"), parsed("count", String.valueOf(count))));
}

Expand Down Expand Up @@ -528,6 +545,22 @@ private void handleSelectCommand(CommandSender sender, String[] args) {
}
}

private void handleImportCommand(CommandSender sender, String[] args) {
if (args.length < 2) {
sender.sendMessage(miniMessage.deserialize(messages.get("commands.import.no-plugin")));
return;
}
if (args[1].equalsIgnoreCase("worldguard")) {
if (plugin.getServer().getPluginManager().getPlugin("WorldGuard") == null) {
sender.sendMessage(miniMessage.deserialize(messages.get("commands.import.not-loaded"), parsed("plugin", "WorldGuard")));
return;
}
WorldGuardImporter worldGuardImporter = new WorldGuardImporter(plugin);
worldGuardImporter.importRegions();
sender.sendMessage(miniMessage.deserialize(messages.get("commands.import.success")));
}
}

private CompletableFuture<Component> regionInfo(Map.Entry<String, Region> entry, boolean showMembers) {
var mm = MiniMessage.miniMessage();
Component comp = Component.text("");
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ commands.subcreate.success=<green>Sub-region created successfully!
commands.load=<green>Loaded <count> regions!
commands.rename.provide-name=<red>Provide a new name for the region!
commands.rename.success=<green>Region <gold>'<region>'<green> renamed to <gold>'<name>'<green>!
commands.import.not-loaded=<red>Error: Plugin <plugin> must be loaded for this command!
commands.import.no-plugin=<red>Provide a plugin name to import regions from!
commands.import.success=<green>Imported regions!
messages.default=<red>Message <key> not found! Contact a Server Administrator
region.none-found=<red>No regions found!
region.no-interact-permission=<red>You are lacking the permission <aqua><actions> <red>for type <yellow><type> <red>in region(s) <gold><region>
Expand Down

0 comments on commit 75bd443

Please sign in to comment.