diff --git a/build.gradle.kts b/build.gradle.kts index 9ce10cc8..e8b828e0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,6 +28,9 @@ dependencies { compileOnly("com.fastasyncworldedit:FastAsyncWorldEdit-Bukkit") { isTransitive = false } compileOnly(libs.io.papermc.paper.paper.api) compileOnly(libs.bluemap.api) + compileOnly("net.essentialsx:EssentialsX:2.19.0") { + isTransitive = false + } } val versionDetails: groovy.lang.Closure by extra diff --git a/settings.gradle.kts b/settings.gradle.kts index c6b80f74..a52e0b92 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -56,8 +56,17 @@ dependencyResolutionManagement { maven { url = uri("https://mvn.wesjd.net/") } - maven { url = uri("https://jitpack.io") } - maven("https://repo.bluecolored.de/releases") + maven { + url = uri("https://jitpack.io") + } + + maven { + url = uri("https://repo.bluecolored.de/releases") + } + + maven { + url = uri("https://repo.essentialsx.net/releases/") + } } } \ No newline at end of file diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/WarpMigrator.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/WarpMigrator.java new file mode 100644 index 00000000..1973bb47 --- /dev/null +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/WarpMigrator.java @@ -0,0 +1,22 @@ +package net.buildtheearth.buildteamtools.modules.navigation.components.warps; + +import net.buildtheearth.buildteamtools.modules.navigation.components.warps.migrators.EssentialsWarpMigrator; +import net.buildtheearth.buildteamtools.modules.navigation.components.warps.migrators.IWarpMigrator; +import net.buildtheearth.buildteamtools.modules.navigation.components.warps.model.WarpMigrationSource; + +import java.util.concurrent.CompletableFuture; + +public class WarpMigrator { + + private final IWarpMigrator migrator; + + public WarpMigrator(WarpMigrationSource source) { + this.migrator = switch (source) { + case ESSENTIALS -> new EssentialsWarpMigrator(); + }; + } + + public CompletableFuture migrate() { + return migrator.migrate(); + } +} diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/WarpsComponent.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/WarpsComponent.java index b00a579d..4f554c26 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/WarpsComponent.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/WarpsComponent.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.List; +import java.util.Objects; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -138,14 +139,15 @@ public void warpPlayer(Player player, @NotNull Warp warp) { } public static WarpGroup getOtherWarpGroup(@NonNull List groups) { - return groups.stream().filter(warpGroup -> warpGroup.getName().equalsIgnoreCase("Other")).findFirst().orElse(null); - } - - public static void createWarp(Player creator) { - WarpGroup group = getOtherWarpGroup(NetworkModule.getInstance().getBuildTeam().getWarpGroups()); + WarpGroup group = groups.stream().filter(warpGroup -> warpGroup.getName().equalsIgnoreCase("Other")).findFirst().orElse(null); if (group == null) { group = NavUtils.createOtherWarpGroup(NetworkModule.getInstance().getBuildTeam()); } + return group; + } + + public static void createWarp(Player creator) { + WarpGroup group = getOtherWarpGroup(Objects.requireNonNull(NetworkModule.getInstance().getBuildTeam()).getWarpGroups()); createWarp(creator, group); } @@ -189,6 +191,35 @@ public static void createWarp(@NonNull Player creator, WarpGroup group) { }); } + /** Creates a warp at the given location. + */ + public static void createWarp(@NonNull Location location, String name, WarpGroup group) { + double[] coordinates = CoordinateConversion.convertToGeo(location.getX(), location.getZ()); + + //Get the country belonging to the coordinates + CompletableFuture future = OpenStreetMapAPI.getCountryFromLocationAsync(coordinates); + + future.thenAccept(result -> { + String regionName = result[0]; + String countryCodeCCA2 = result[1].toUpperCase(); + + //Check if the team owns this region/country + boolean ownsRegion = NetworkModule.getInstance().ownsRegion(regionName, countryCodeCCA2); + + if(!ownsRegion) { + return; + } + + // Create an instance of the warp POJO + Warp warp = new Warp(group, name, countryCodeCCA2, "cca2", null, null, null, location.getWorld().getName(), coordinates[0], coordinates[1], location.getY(), location.getYaw(), location.getPitch(), false); + + Objects.requireNonNull(NetworkModule.getInstance().getBuildTeam()).createWarp(null, warp); + }).exceptionally(e -> { + BuildTeamTools.getInstance().getComponentLogger().error("An error occurred while creating the warp!", e); + return null; + }); + } + public void createWarpGroup(@NotNull Player creator) { // Create a default name for the warp diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/commands/WarpCommand.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/commands/WarpCommand.java index 37938a69..1fea48e5 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/commands/WarpCommand.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/commands/WarpCommand.java @@ -2,8 +2,10 @@ import com.alpsbte.alpslib.utils.ChatHelper; import net.buildtheearth.buildteamtools.modules.navigation.NavigationModule; +import net.buildtheearth.buildteamtools.modules.navigation.components.warps.WarpMigrator; import net.buildtheearth.buildteamtools.modules.navigation.components.warps.WarpsComponent; import net.buildtheearth.buildteamtools.modules.navigation.components.warps.model.Warp; +import net.buildtheearth.buildteamtools.modules.navigation.components.warps.model.WarpMigrationSource; import net.buildtheearth.buildteamtools.modules.network.NetworkModule; import net.buildtheearth.buildteamtools.modules.network.model.Permissions; import org.bukkit.command.Command; @@ -14,6 +16,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -58,6 +61,41 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command return true; } + // WARP MIGRATE + if(args[0].equalsIgnoreCase("migrate")) { + // Check if the player has the required permissions + if (!player.hasPermission(Permissions.WARP_MIGRATE)) { + player.sendMessage(ChatHelper.getErrorString("You don't have the required %s to %s warps.", "permission", "migrate")); + return true; + } + + // check if the command has the correct amount of arguments + if (args.length != 2) { + player.sendMessage(ChatHelper.getErrorString("Usage: /warp migrate ")); + player.sendMessage(ChatHelper.getErrorString("Valid sources are: %s", Arrays.toString(WarpMigrationSource.values()))); + return true; + } + + // check if the given source is valid + WarpMigrationSource source = WarpMigrationSource.fromString(args[1]); + if (source == null) { + player.sendMessage(ChatHelper.getErrorString("Invalid source: %s", args[1])); + player.sendMessage(ChatHelper.getErrorString("Valid sources are: %s", Arrays.toString(WarpMigrationSource.values()))); + return true; + } + + WarpMigrator migrator = new WarpMigrator(source); + player.sendMessage(ChatHelper.getStandardString(false, "Migrating the warps...")); + migrator.migrate().whenComplete((result, throwable) -> { + if (throwable != null) { + player.sendMessage(ChatHelper.getErrorString("Something went wrong while migrating the warps: %s", throwable.getMessage())); + return; + } + player.sendMessage(ChatHelper.getSuccessComponent("Successfully migrated the warps!")); + }); + return true; + } + // Combine the args to one warp name String key = String.join(" ", args); diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/migrators/EssentialsWarpMigrator.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/migrators/EssentialsWarpMigrator.java new file mode 100644 index 00000000..d3635905 --- /dev/null +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/migrators/EssentialsWarpMigrator.java @@ -0,0 +1,29 @@ +package net.buildtheearth.buildteamtools.modules.navigation.components.warps.migrators; + +import com.alpsbte.alpslib.utils.ChatHelper; +import com.earth2me.essentials.Essentials; +import com.earth2me.essentials.Warps; +import net.buildtheearth.buildteamtools.modules.navigation.components.warps.WarpsComponent; +import net.buildtheearth.buildteamtools.modules.navigation.components.warps.model.WarpGroup; +import net.buildtheearth.buildteamtools.modules.network.NetworkModule; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; + +public class EssentialsWarpMigrator implements IWarpMigrator { + @Override + public CompletableFuture migrate() { + return CompletableFuture.runAsync(() -> { + try { + Essentials essentials = (Essentials) Essentials.getProvidingPlugin(Essentials.class); + Warps warps = essentials.getWarps(); + for (String warp : warps.getList()) { + WarpGroup group = WarpsComponent.getOtherWarpGroup(Objects.requireNonNull(NetworkModule.getInstance().getBuildTeam()).getWarpGroups()); + WarpsComponent.createWarp(warps.getWarp(warp), warp, group); + } + } catch (Exception e) { + ChatHelper.logError("An error occurred while migrating the essentials warps!\n(this probably means essentials isn't installed)\n %s", e); + } + + }); + } +} diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/migrators/IWarpMigrator.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/migrators/IWarpMigrator.java new file mode 100644 index 00000000..43b9b358 --- /dev/null +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/migrators/IWarpMigrator.java @@ -0,0 +1,7 @@ +package net.buildtheearth.buildteamtools.modules.navigation.components.warps.migrators; + +import java.util.concurrent.CompletableFuture; + +public interface IWarpMigrator { + CompletableFuture migrate(); +} diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/model/WarpMigrationSource.java b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/model/WarpMigrationSource.java new file mode 100644 index 00000000..300f7817 --- /dev/null +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/navigation/components/warps/model/WarpMigrationSource.java @@ -0,0 +1,25 @@ +package net.buildtheearth.buildteamtools.modules.navigation.components.warps.model; + +import lombok.Getter; + +import java.util.Arrays; + +@Getter +public enum WarpMigrationSource { + ESSENTIALS("essentials"); + + private final String[] validNames; + + WarpMigrationSource(String... validNames) { + this.validNames = validNames; + } + + public static WarpMigrationSource fromString(String name) { + for (WarpMigrationSource warpMigrationSource : WarpMigrationSource.values()) { + if (Arrays.asList(warpMigrationSource.validNames).contains(name)) { + return warpMigrationSource; + } + } + return null; + } +} diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/BuildTeam.java b/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/BuildTeam.java index 82bbb0df..0cdc05bb 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/BuildTeam.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/BuildTeam.java @@ -63,7 +63,9 @@ public BuildTeam(String ID, String serverIP, String name, String blankName, Stri public void createWarp(Player creator, Warp warp){ // Check if the team owns that warp if(!warp.getWarpGroup().getBuildTeam().getID().equals(this.getID())){ + if (creator != null) { creator.sendMessage(ChatHelper.getErrorString("You can only create warps for your own team!")); + } return; } @@ -72,7 +74,9 @@ public void createWarp(Player creator, Warp warp){ public void onResponse(String response) { // Update the cache NetworkModule.getInstance().updateCache().thenRun(() -> { - ChatHelper.sendSuccessfulMessage(creator, "Successfully created the warp %s!", warp.getName()); + if (creator != null) { + ChatHelper.sendSuccessfulMessage(creator, "Successfully created the warp %s!", warp.getName()); + } // Refresh BlueMap markers refreshBluemapMarkers(); }); @@ -80,7 +84,10 @@ public void onResponse(String response) { @Override public void onFailure(IOException e) { - creator.sendMessage(ChatHelper.getErrorString("Something went wrong while creating the warp %s! Please take a look at the console.", warp.getName())); + if (creator != null) { + creator.sendMessage(ChatHelper.getErrorString("Something went wrong while creating the warp %s! Please take a look at the console.", warp.getName())); + + } e.printStackTrace(); } }); diff --git a/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/Permissions.java b/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/Permissions.java index d29ee740..92c5f571 100644 --- a/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/Permissions.java +++ b/src/main/java/net/buildtheearth/buildteamtools/modules/network/model/Permissions.java @@ -20,6 +20,7 @@ public abstract class Permissions { public static final String WARP_CREATE = "btt.warp.create"; public static final String WARP_EDIT = "btt.warp.edit"; public static final String WARP_DELETE = "btt.warp.delete"; + public static final String WARP_MIGRATE = "btt.warp.migrate"; public static final String WARP_GROUP_CREATE = "btt.warp.group.create"; public static final String WARP_GROUP_EDIT = "btt.warp.group.edit";