diff --git a/src/main/java/com/onaple/epicboundaries/EpicBoundaries.java b/src/main/java/com/onaple/epicboundaries/EpicBoundaries.java index 9b7dae5..b5816ee 100644 --- a/src/main/java/com/onaple/epicboundaries/EpicBoundaries.java +++ b/src/main/java/com/onaple/epicboundaries/EpicBoundaries.java @@ -5,12 +5,15 @@ import com.onaple.epicboundaries.commands.CreateInstanceCommand; import com.onaple.epicboundaries.data.access.InstanceDao; import com.onaple.epicboundaries.event.CopyWorldEvent; +import com.onaple.epicboundaries.service.IInstanceService; +import com.onaple.epicboundaries.service.InstanceService; import org.slf4j.Logger; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.args.GenericArguments; import org.spongepowered.api.command.spec.CommandSpec; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.game.state.GameInitializationEvent; +import org.spongepowered.api.event.game.state.GamePreInitializationEvent; import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.scheduler.Task; @@ -81,6 +84,11 @@ public void onServerStart(GameInitializationEvent event) { logger.info("EPICBOUNDARIES initialized."); } + @Listener + public void onGamePreInitialization(GamePreInitializationEvent event) { + Sponge.getServiceManager().setProvider(this, IInstanceService.class, new InstanceService()); + } + /** * Action occuring when a world has just been copied * @param event World copied event diff --git a/src/main/java/com/onaple/epicboundaries/service/IInstanceService.java b/src/main/java/com/onaple/epicboundaries/service/IInstanceService.java new file mode 100644 index 0000000..fc95377 --- /dev/null +++ b/src/main/java/com/onaple/epicboundaries/service/IInstanceService.java @@ -0,0 +1,10 @@ +package com.onaple.epicboundaries.service; + +import java.util.List; +import java.util.Optional; + +public interface IInstanceService { + boolean apparate(String worldName, String playerName); + int apparate(String worldName, List playerName); + Optional createInstance(String worldToCopy, String playerName); +} diff --git a/src/main/java/com/onaple/epicboundaries/service/InstanceService.java b/src/main/java/com/onaple/epicboundaries/service/InstanceService.java new file mode 100644 index 0000000..284a8c2 --- /dev/null +++ b/src/main/java/com/onaple/epicboundaries/service/InstanceService.java @@ -0,0 +1,71 @@ +package com.onaple.epicboundaries.service; + +import com.onaple.epicboundaries.WorldAction; +import org.spongepowered.api.Sponge; + +import java.util.List; +import java.util.Optional; + +public class InstanceService implements IInstanceService { + /** + * Try to transfer a player to a given world + * @param worldName Name of the world + * @param playerName Name of the player + * @return True if player has been transferred + */ + @Override + public boolean apparate(String worldName, String playerName) { + return Sponge.getServer().loadWorld(worldName).map(world -> + Sponge.getServer().getPlayer(playerName).map(player -> { + WorldAction worldAction = new WorldAction(); + worldAction.transferPlayerToWorld(player, world); + return true; + }).orElse(false) + ).orElse(false); + } + + /** + * Try to transfer a group of players to a given world + * @param worldName Name of the world + * @param playerNames Names of the players + * @return Successful transfer count + */ + @Override + public int apparate(String worldName, List playerNames) { + return Sponge.getServer().loadWorld(worldName).map(world -> { + int playerTransferCount = 0; + WorldAction worldAction = new WorldAction(); + for(String playerName : playerNames) { + playerTransferCount += Sponge.getServer().getPlayer(playerName).map(player -> { + worldAction.transferPlayerToWorld(player, world); + return 1; + }).orElse(0); + } + return playerTransferCount; + }).orElse(0); + } + + /** + * Try to create an instance from a world and transfer a player to it + * @param worldToCopy World to copy + * @param playerName Name of the player to transfer + * @return Optional of world name, if copy successfully initiated + */ + @Override + public Optional createInstance(String worldToCopy, String playerName) { + if (worldToCopy.equals(Sponge.getServer().getDefaultWorldName())) { + return Optional.empty(); + } + Sponge.getServer().getWorldProperties(worldToCopy).ifPresent(worldProperties -> { + String uuid, newWorldName; + do { + uuid = java.util.UUID.randomUUID().toString(); + newWorldName = uuid; + } while (Sponge.getServer().getWorldProperties(newWorldName).isPresent()); + WorldAction worldAction = new WorldAction(); + worldAction.copyWorld(worldProperties, newWorldName); + worldAction.addPlayerToTransferQueue(playerName, newWorldName); + }); + return Optional.empty(); + } +}