Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<com.palantir.gradle.gitversion.VersionDetails> by extra
Expand Down
13 changes: 11 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Void> migrate() {
return migrator.migrate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -138,14 +139,15 @@ public void warpPlayer(Player player, @NotNull Warp warp) {
}

public static WarpGroup getOtherWarpGroup(@NonNull List<WarpGroup> 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);
}

Expand Down Expand Up @@ -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<String[]> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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 <source>"));
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Void> 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);
}

});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.buildtheearth.buildteamtools.modules.navigation.components.warps.migrators;

import java.util.concurrent.CompletableFuture;

public interface IWarpMigrator {
CompletableFuture<Void> migrate();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -72,15 +74,20 @@ 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();
});
}

@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();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading