Skip to content

Commit

Permalink
Added interface for other plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Zessirb committed Jul 21, 2018
1 parent 01981a7 commit 0b6086f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/onaple/epicboundaries/EpicBoundaries.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> playerName);
Optional<String> createInstance(String worldToCopy, String playerName);
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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();
}
}

0 comments on commit 0b6086f

Please sign in to comment.