mirror of
https://github.com/correl/mage.git
synced 2024-11-14 19:19:32 +00:00
made a util function more generic because why not
This commit is contained in:
parent
14838e670a
commit
b9550a7387
3 changed files with 7 additions and 8 deletions
|
@ -132,7 +132,7 @@ class MaddeningHexEffect extends OneShotEffect {
|
|||
opponents.remove(player.getId());
|
||||
}
|
||||
if (!opponents.isEmpty()) {
|
||||
permanent.attachTo(RandomUtil.randomFromSet(opponents), source, game);
|
||||
permanent.attachTo(RandomUtil.randomFromCollection(opponents), source, game);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -2716,7 +2716,7 @@ public abstract class PlayerImpl implements Player, Serializable {
|
|||
.stream()
|
||||
.filter(card -> filter.match(card, source.getSourceId(), getId(), game))
|
||||
.collect(Collectors.toSet());
|
||||
Card card = RandomUtil.randomFromSet(cards);
|
||||
Card card = RandomUtil.randomFromCollection(cards);
|
||||
if (card == null) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
package mage.util;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by IGOUDT on 5-9-2016.
|
||||
*/
|
||||
public final class RandomUtil {
|
||||
|
||||
private static Random random = new Random(); // thread safe with seed support
|
||||
private static final Random random = new Random(); // thread safe with seed support
|
||||
|
||||
private RandomUtil() {
|
||||
}
|
||||
|
@ -43,15 +42,15 @@ public final class RandomUtil {
|
|||
random.setSeed(newSeed);
|
||||
}
|
||||
|
||||
public static <T extends Serializable> T randomFromSet(Set<T> collection) {
|
||||
public static <T> T randomFromCollection(Collection<T> collection) {
|
||||
if (collection.size() < 2) {
|
||||
return collection.stream().findFirst().orElse(null);
|
||||
}
|
||||
int rand = nextInt(collection.size());
|
||||
int count = 0;
|
||||
for (T currentId : collection) {
|
||||
for (T current : collection) {
|
||||
if (count == rand) {
|
||||
return currentId;
|
||||
return current;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue