-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zessirb
committed
Jul 21, 2018
1 parent
01981a7
commit 0b6086f
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/onaple/epicboundaries/service/IInstanceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/onaple/epicboundaries/service/InstanceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |