Nulls to optional. Also there was bug with AetherChaser "you may pay" was not set to optional

This commit is contained in:
doncarton 2017-02-26 15:15:54 +03:00
parent a46848791c
commit 5eaaa67d1e
5 changed files with 43 additions and 38 deletions

View file

@ -27,14 +27,6 @@
*/
package mage.server;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import mage.MageException;
import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists;
@ -68,6 +60,15 @@ import mage.server.util.ThreadExecutor;
import mage.view.ChatMessage;
import org.apache.log4j.Logger;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* @author BetaSteward_at_googlemail.com
*/
@ -195,8 +196,9 @@ public class TableController {
return false;
}
Player player = createPlayer(name, seat.getPlayerType(), skill);
if (player != null) {
Optional<Player> playerOptional = createPlayer(name, seat.getPlayerType(), skill);
if (playerOptional.isPresent()) {
Player player = playerOptional.get();
if (!player.canJoinTable(table)) {
user.showUserMessage("Join Table", new StringBuilder("A ").append(seat.getPlayerType()).append(" player can't join this table.").toString());
return false;
@ -227,10 +229,11 @@ public class TableController {
}
public synchronized boolean replaceDraftPlayer(Player oldPlayer, String name, String playerType, int skill) {
Player newPlayer = createPlayer(name, playerType, skill);
if (newPlayer == null || table.getState() != TableState.DRAFTING) {
Optional<Player> newPlayerOpt = createPlayer(name, playerType, skill);
if (!newPlayerOpt.isPresent() || table.getState() != TableState.DRAFTING) {
return false;
}
Player newPlayer = newPlayerOpt.get();
TournamentPlayer oldTournamentPlayer = tournament.getPlayer(oldPlayer.getId());
tournament.removePlayer(oldPlayer.getId());
tournament.addPlayer(newPlayer, playerType);
@ -306,13 +309,14 @@ public class TableController {
}
}
Player player = createPlayer(name, seat.getPlayerType(), skill);
if (player == null) {
Optional<Player> playerOpt = createPlayer(name, seat.getPlayerType(), skill);
if (!playerOpt.isPresent()) {
String message = new StringBuilder("Could not create player ").append(name).append(" of type ").append(seat.getPlayerType()).toString();
logger.warn(new StringBuilder("User: ").append(user.getName()).append(" => ").append(message).toString());
user.showUserMessage("Join Table", message);
return false;
}
Player player = playerOpt.get();
logger.debug("DECK validated: " + table.getValidator().getName() + ' ' + player.getName() + ' ' + deck.getName());
if (!player.canJoinTable(table)) {
user.showUserMessage("Join Table", new StringBuilder("A ").append(seat.getPlayerType()).append(" player can't join this table.").toString());
@ -443,17 +447,18 @@ public class TableController {
// ReplayManager.getInstance().replayGame(table.getId(), userId);
// return true;
// }
private Player createPlayer(String name, String playerType, int skill) {
Player player;
private Optional<Player> createPlayer(String name, String playerType, int skill) {
Optional<Player> playerOpt;
if (options == null) {
player = PlayerFactory.getInstance().createPlayer(playerType, name, RangeOfInfluence.ALL, skill);
playerOpt = PlayerFactory.getInstance().createPlayer(playerType, name, RangeOfInfluence.ALL, skill);
} else {
player = PlayerFactory.getInstance().createPlayer(playerType, name, options.getRange(), skill);
playerOpt = PlayerFactory.getInstance().createPlayer(playerType, name, options.getRange(), skill);
}
if (player != null) {
if (playerOpt.isPresent()) {
Player player = playerOpt.get();
logger.trace("Player " + player.getName() + " created id: " + player.getId());
}
return player;
return playerOpt;
}
public void leaveTableAll() {

View file

@ -28,14 +28,16 @@
package mage.server.game;
import java.lang.reflect.Constructor;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import mage.constants.RangeOfInfluence;
import mage.players.Player;
import org.apache.log4j.Logger;
import java.lang.reflect.Constructor;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
/**
*
* @author BetaSteward_at_googlemail.com
@ -53,14 +55,14 @@ public class PlayerFactory {
private PlayerFactory() {}
public Player createPlayer(String playerType, String name, RangeOfInfluence range, int skill) {
public Optional<Player> createPlayer(String playerType, String name, RangeOfInfluence range, int skill) {
try {
Class playerTypeClass = playerTypes.get(playerType);
if (playerTypeClass != null) {
Constructor<?> con = playerTypeClass.getConstructor(String.class, RangeOfInfluence.class, int.class);
Player player = (Player) con.newInstance(name, range, skill);
logger.trace("Player created: " + name + " - " + player.getId());
return player;
return Optional.of(player);
}
else {
logger.fatal("Unknown player type: " + playerType);
@ -68,7 +70,7 @@ public class PlayerFactory {
} catch (Exception ex) {
logger.fatal("PlayerFactory error ", ex);
}
return null;
return Optional.empty();
}
public Set<String> getPlayerTypes() {

View file

@ -27,7 +27,6 @@
*/
package mage.cards.a;
import java.util.UUID;
import mage.MageInt;
import mage.abilities.common.AttacksTriggeredAbility;
import mage.abilities.common.EntersBattlefieldTriggeredAbility;
@ -41,6 +40,8 @@ import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.game.permanent.token.ServoToken;
import java.util.UUID;
/**
*
* @author fireshoes
@ -62,7 +63,7 @@ public class AetherChaser extends CardImpl {
this.addAbility(new EntersBattlefieldTriggeredAbility(new GetEnergyCountersControllerEffect(2)));
// Whenever Aether Chaser attacks, you may pay {E}{E}. If you do, create a 1/1 colorless Servo artifact creature token.
this.addAbility(new AttacksTriggeredAbility(new DoIfCostPaid(new CreateTokenEffect(new ServoToken()), new PayEnergyCost(2)), false,
this.addAbility(new AttacksTriggeredAbility(new DoIfCostPaid(new CreateTokenEffect(new ServoToken()), new PayEnergyCost(2)), true,
"Whenever {this} attacks you may pay {E}{E}. If you do, create a 1/1 colorless Servo artifact creature token."));
}

View file

@ -28,7 +28,6 @@ import org.mage.test.player.TestPlayer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -297,7 +296,8 @@ public abstract class MageTestBase {
}
protected Player createPlayer(String name, String playerType) {
return PlayerFactory.getInstance().createPlayer(playerType, name, RangeOfInfluence.ALL, 5);
Optional<Player> playerOptional = PlayerFactory.getInstance().createPlayer(playerType, name, RangeOfInfluence.ALL, 5);
return playerOptional.orElseThrow(() -> new NullPointerException("PlayerFactory error - player is not created"));
}
protected Player createRandomPlayer(String name) {

View file

@ -27,11 +27,6 @@
*/
package mage.abilities.costs.mana;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import mage.Mana;
import mage.abilities.Ability;
import mage.abilities.costs.Cost;
@ -45,13 +40,15 @@ import mage.players.Player;
import mage.target.Targets;
import mage.util.ManaUtil;
import java.util.*;
/**
* @author BetaSteward_at_googlemail.com
* @param <T>
*/
public class ManaCostsImpl<T extends ManaCost> extends ArrayList<T> implements ManaCosts<T> {
protected UUID id;
protected final UUID id;
protected String text = null;
private static Map<String, ManaCosts> costs = new HashMap<>();
@ -323,7 +320,7 @@ public class ManaCostsImpl<T extends ManaCost> extends ArrayList<T> implements M
if (mana == null || mana.isEmpty()) {
return;
}
String[] symbols = mana.split("^\\{|\\}\\{|\\}$");
String[] symbols = mana.split("^\\{|}\\{|}$");
int modifierForX = 0;
for (String symbol : symbols) {
if (!symbol.isEmpty()) {