59 lines
2.0 KiB
Java
59 lines
2.0 KiB
Java
package games.dmg.spigottyrant;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.UUID;
|
|
|
|
public final class CapturedMobService {
|
|
public PlayerState capture(
|
|
PlayerState player,
|
|
UUID captureId,
|
|
String entityType,
|
|
String snapshot
|
|
) {
|
|
if (player.tyrantClass() != TyrantClass.TAMER) {
|
|
throw new IllegalStateException("only a Tamer can capture mobs");
|
|
}
|
|
if (!player.capturedMobs().isEmpty()) {
|
|
throw new IllegalStateException(
|
|
"release the currently captured mob before capturing another"
|
|
);
|
|
}
|
|
if ("ENDER_DRAGON".equalsIgnoreCase(entityType)) {
|
|
throw new IllegalArgumentException("Ender Dragons cannot be captured");
|
|
}
|
|
List<CapturedMob> mobs = new ArrayList<>(player.capturedMobs());
|
|
mobs.add(new CapturedMob(entityType, Map.of(
|
|
"capture-id", captureId.toString(),
|
|
"snapshot", snapshot
|
|
)));
|
|
return copy(player, mobs);
|
|
}
|
|
|
|
public Optional<CapturedMob> find(PlayerState player, UUID captureId) {
|
|
return player.capturedMobs().stream()
|
|
.filter(mob -> captureId.toString().equals(mob.data().get("capture-id")))
|
|
.findFirst();
|
|
}
|
|
|
|
public PlayerState release(PlayerState player, UUID captureId) {
|
|
if (find(player, captureId).isEmpty()) {
|
|
return player;
|
|
}
|
|
List<CapturedMob> mobs = player.capturedMobs().stream()
|
|
.filter(mob -> !captureId.toString().equals(mob.data().get("capture-id")))
|
|
.toList();
|
|
return copy(player, mobs);
|
|
}
|
|
|
|
private static PlayerState copy(PlayerState player, List<CapturedMob> mobs) {
|
|
return new PlayerState(
|
|
player.playerId(), player.latestName(), player.lastLogin(), player.optedOutUntil(),
|
|
player.tyrantClass(), player.followerOf(), player.cooldownEnds(),
|
|
player.readyAbilityItems(), mobs
|
|
);
|
|
}
|
|
}
|